1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Maintainer:  The LLVM team, http://llvm.org/
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Description: Major mode for TableGen description files (part of LLVM project)
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Updated:     2007-12-18
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(require 'comint)
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(require 'custom)
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(require 'ansi-color)
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Create mode-specific tables.
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar td-decorators-face 'td-decorators-face
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  "Face method decorators.")
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(make-face 'td-decorators-face)
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar tablegen-font-lock-keywords
1519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  (let ((kw (regexp-opt '("class" "defm" "def" "field" "include" "in"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         "let" "multiclass")
1719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        'words))
1819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (type-kw (regexp-opt '("bit" "bits" "code" "dag" "int" "list" "string")
1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             'words))
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        )
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (list
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Comments
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;;     '("\/\/" . font-lock-comment-face)
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Strings
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("\"[^\"]+\"" . font-lock-string-face)
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Hex constants
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("\\<0x[0-9A-Fa-f]+\\>" . font-lock-preprocessor-face)
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Binary constants
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("\\<0b[01]+\\>" . font-lock-preprocessor-face)
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Integer literals
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("\\<[-]?[0-9]+\\>" . font-lock-preprocessor-face)
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Floating point constants
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("\\<[-+]?[0-9]+\.[0-9]*\([eE][-+]?[0-9]+\)?\\>" . font-lock-preprocessor-face)
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     '("^[ \t]*\\(@.+\\)" 1 'td-decorators-face)
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Keywords
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman     (cons (concat kw "[ \n\t(]") 1)
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ;; Type keywords
4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman     (cons (concat type-kw "[ \n\t(]") 1)
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     ))
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  "Additional expressions to highlight in TableGen mode.")
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(put 'tablegen-mode 'font-lock-defaults '(tablegen-font-lock-keywords))
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; ---------------------- Syntax table ---------------------------
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Shamelessly ripped from jasmin.el
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; URL: http://www.neilvandyke.org/jasmin-emacs/jasmin.el
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar tablegen-mode-syntax-table nil
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  "Syntax table used in `tablegen-mode' buffers.")
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(when (not tablegen-mode-syntax-table)
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (setq tablegen-mode-syntax-table (make-syntax-table))
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; whitespace (` ')
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\   " "      tablegen-mode-syntax-table)
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\t  " "      tablegen-mode-syntax-table)
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\r  " "      tablegen-mode-syntax-table)
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\n  " "      tablegen-mode-syntax-table)
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\f  " "      tablegen-mode-syntax-table)
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; word constituents (`w')
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\%  "w"      tablegen-mode-syntax-table)
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\_  "w"      tablegen-mode-syntax-table)
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; comments
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?/   ". 124b" tablegen-mode-syntax-table)
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?*   ". 23"   tablegen-mode-syntax-table)
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\n  "> b"    tablegen-mode-syntax-table)
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; open paren (`(')
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\(  "("      tablegen-mode-syntax-table)
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\[  "("      tablegen-mode-syntax-table)
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\{  "("      tablegen-mode-syntax-table)
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\<  "("      tablegen-mode-syntax-table)
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; close paren (`)')
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\)  ")"      tablegen-mode-syntax-table)
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\]  ")"      tablegen-mode-syntax-table)
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\}  ")"      tablegen-mode-syntax-table)
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\>  ")"      tablegen-mode-syntax-table)
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ;; string quote ('"')
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (modify-syntax-entry ?\"  "\""     tablegen-mode-syntax-table)
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  )
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; --------------------- Abbrev table -----------------------------
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar tablegen-mode-abbrev-table nil
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  "Abbrev table used while in TableGen mode.")
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(define-abbrev-table 'tablegen-mode-abbrev-table ())
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar tablegen-mode-hook nil)
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defvar tablegen-mode-map nil)   ; Create a mode-specific keymap.
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(if (not tablegen-mode-map)
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ()  ; Do not change the keymap if it is already set up.
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (setq tablegen-mode-map (make-sparse-keymap))
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (define-key tablegen-mode-map "\t"  'tab-to-tab-stop)
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (define-key tablegen-mode-map "\es" 'center-line)
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (define-key tablegen-mode-map "\eS" 'center-paragraph))
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(defun tablegen-mode ()
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  "Major mode for editing TableGen description files.
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  \\{tablegen-mode-map}
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Runs tablegen-mode-hook on startup."
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (interactive)
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (kill-all-local-variables)
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (use-local-map tablegen-mode-map)      ; Provides the local keymap.
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (make-local-variable 'font-lock-defaults)
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (setq major-mode 'tablegen-mode        ; This is how describe-mode
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         ;   finds the doc string to print.
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	mode-name             "TableGen" ; This name goes into the modeline.
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        local-abbrev-table    tablegen-mode-abbrev-table
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	font-lock-defaults    `(tablegen-font-lock-keywords)
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman	require-final-newline t
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        )
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (set-syntax-table tablegen-mode-syntax-table)
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (make-local-variable 'comment-start)
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (setq comment-start "//")
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (run-hooks 'tablegen-mode-hook))       ; Finally, this permits the user to
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         ;   customize the mode with a hook.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; Associate .td files with tablegen-mode
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(setq auto-mode-alist (append '(("\\.td$" . tablegen-mode)) auto-mode-alist))
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman(provide 'tablegen-mode)
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman;; end of tablegen-mode.el
123