diff --git a/vm-ubuntu-24.04/p4_16-mode.el b/vm-ubuntu-24.04/p4_16-mode.el index 3479fd6e..b9db28bf 100644 --- a/vm-ubuntu-24.04/p4_16-mode.el +++ b/vm-ubuntu-24.04/p4_16-mode.el @@ -14,16 +14,27 @@ ;; This file is free software… ;; This mode has preliminary support for P4_16. It covers the core language, -;; but it is not clear yet, how we can highlight the indentifiers, defined +;; but it is not clear yet, how we can highlight the identifiers, defined ;; for a particular architecture. Core library definitions are included ;; Placeholder for user customization code (defvar p4_16-mode-hook nil) +(defun p4_16-electric-brace (arg) + "Insert a brace." + (interactive "*P") + (self-insert-command (prefix-numeric-value arg)) + (save-excursion + (move-beginning-of-line nil) + (indent-for-tab-command))) + ;; Define the keymap (for now it is pretty much default) (defvar p4_16-mode-map (let ((map (make-keymap))) (define-key map "\C-j" 'newline-and-indent) + (define-key map "{" 'p4_16-electric-brace) + (define-key map "}" 'p4_16-electric-brace) + (define-key map "\C-c\C-c" 'comment-region) map) "Keymap for P4_16 major mode") @@ -41,7 +52,7 @@ "package" "parser" "return" "select" "state" "struct" "switch" - "table" "transition" "tuple" "typedef" + "table" "transition" "tuple" "typedef" "type" "verify" )) @@ -186,38 +197,93 @@ ;;; Imenu support (require 'imenu) + +;; regex to optionally match a name annotation, e.g. like this example: +;; @name(".process_replication.nop") +(setq opt-name-annotation-regex + "\\(@name[(] *\"[A-Za-z0-9_. ]+\" *[)]\\)\?") + (setq p4_16-imenu-generic-expression - '( - ("Controls" "^ *control +\\([A-Za-z0-9_]*\\)" 1) - ("Externs" "^ *extern +\\([A-Za-z0-9_]*\\) *\\([A-Za-z0-9_]*\\)" 2) - ("Tables" "^ *table +\\([A-Za-z0-9_]*\\)" 1) - ("Actions" "^ *action +\\([A-Za-z0-9_]*\\)" 1) - ("Parsers" "^ *parser +\\([A-Za-z0-9_]*\\)" 1) - ("Parser States" "^ *state +\\([A-Za-z0-9_]*\\)" 1) - ("Headers" "^ *header +\\([A-Za-z0-9_]*\\)" 1) - ("Header Unions" "^ *header_union +\\([A-Za-z0-9_]*\\)" 1) - ("Structs" "^ *struct +\\([A-Za-z0-9_]*\\)" 1) + (list + (list "Controls" + (concat "^ *" + opt-name-annotation-regex + " *control +\\([A-Za-z0-9_]*\\)") + 2) + (list "Externs" + (concat "^ *" + opt-name-annotation-regex + " *extern +\\([A-Za-z0-9_]*\\) *\\([A-Za-z0-9_]*\\)") + 3) + (list "Tables" + (concat "^ *" + opt-name-annotation-regex + " *table +\\([A-Za-z0-9_]*\\)") + 2) + (list "Actions" + (concat "^ *" + opt-name-annotation-regex + " *action +\\([A-Za-z0-9_]*\\)") + 2) + (list "Parsers" + (concat "^ *" + opt-name-annotation-regex + " *parser +\\([A-Za-z0-9_]*\\)") + 2) + (list "Parser States" + (concat "^ *" + opt-name-annotation-regex + " *state +\\([A-Za-z0-9_]*\\)") + 2) + (list "Headers" + (concat "^ *" + opt-name-annotation-regex + " *header +\\([A-Za-z0-9_]*\\)") + 2) + (list "Header Unions" + (concat "^ *" + opt-name-annotation-regex + " *header_union +\\([A-Za-z0-9_]*\\)") + 2) + (list "Structs" + (concat "^ *" + opt-name-annotation-regex + " *struct +\\([A-Za-z0-9_]*\\)") + 2) )) ;;; Cscope Support (require 'xcscope) +;; The following version was generated by Google AI assistant with the +;; original version p4_16-mode as input, with the request that it be +;; updated to work with Emacs version 30. Further questioning about +;; how far back in Emacs versions this ought to work claimed it should +;; work back to Emacs 25 and Emacs 24.x for some x > 0. ;; Put everything together -(defun p4_16-mode () - "Major mode for editing P4_16 programs" - (interactive) - (kill-all-local-variables) - (set-syntax-table p4_16-mode-syntax-table) - (use-local-map p4_16-mode-map) - (set (make-local-variable 'font-lock-defaults) '(p4_16-font-lock-keywords)) - (set (make-local-variable 'indent-line-function) 'p4_16-indent-line) - (setq major-mode 'p4_16-mode) - (setq mode-name "P4_16") +;;;###autoload +(define-derived-mode p4_16-mode prog-mode "P4_16" + "Major mode for editing P4_16 programs." + :syntax-table p4_16-mode-syntax-table + + ;; Comments + (setq-local comment-start "// ") + (setq-local comment-end "") + + ;; Font-lock (Syntax Highlighting) + (setq font-lock-defaults '(p4_16-font-lock-keywords)) + + ;; Indentation + (setq-local indent-line-function #'p4_16-indent-line) + (setq-local indent-tabs-mode nil) + + ;; Imenu (setq imenu-generic-expression p4_16-imenu-generic-expression) (imenu-add-to-menubar "P4_16") - (cscope-minor-mode) - (run-hooks 'p4_16-mode-hook) -) + + ;; Cscope minor mode + (when (fboundp 'cscope-minor-mode) + (cscope-minor-mode))) ;; The most important line (provide 'p4_16-mode)