-
Notifications
You must be signed in to change notification settings - Fork 1
/
bnfc.el
104 lines (86 loc) · 2.84 KB
/
bnfc.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;;; bnfc.el --- Define context-free grammars for the BNFC tool
;; Copyright (C) 2016 Jacob Mitchell <[email protected]>
;; Author: Jacob Mitchell <[email protected]>
;; URL: https://github.com/jmitchell/bnfc-mode
;; Keywords: languages, tools
;; Version: 0.4
;; Package-Requires: ((emacs "24.3"))
;; This file is not part of GNU Emacs.
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; bnfc-mode simplifies editing BNFC input files in Emacs. BNFC is a
;; handy tool for converting context-free grammars into parsers,
;; syntax highlighters, and documentation.
;;; Code:
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cf\\'" . bnfc-mode))
(defconst bnfc-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\{ "(}1c" table)
(modify-syntax-entry ?\} "){4c" table)
(modify-syntax-entry ?- "_ 123" table)
(modify-syntax-entry ?\n ">" table)
table))
(defconst bnfc-keywords
'("char"
"coercions"
"comment"
"digit"
"entrypoints"
"eps"
"internal"
"layout"
"letter"
"lower"
"nonempty"
"position"
"rules"
"separator"
"stop"
"terminator"
"token"
"toplevel"
"upper"))
(defconst bnfc-builtins
'("Char"
"Double"
"Ident"
"Integer"
"String"))
(defconst bnfc-label
(rx symbol-start
(group upper (0+ (any letter digit "_")))
symbol-end
(0+ space)
"."))
(defconst bnfc-production-variable
;; Regexp overlaps with BNFC-LABEL, but ordering of the
;; FONT-LOCK-DEFAULTS resolves the ambiguity. Strings matching both
;; regexps are treated as labels.
(rx symbol-start
(group upper (0+ (any letter digit "_")))
symbol-end))
(defvar bnfc-font-lock-keywords
(append
`((,(regexp-opt bnfc-keywords 'symbols) . font-lock-keyword-face)
(,(regexp-opt bnfc-builtins 'symbols) . font-lock-builtin-face)
(,bnfc-label 1 font-lock-variable-name-face)
(,bnfc-production-variable 1 font-lock-type-face))))
;;;###autoload
(define-derived-mode bnfc-mode prog-mode "BNFC"
:syntax-table bnfc-mode-syntax-table
(setq-local font-lock-defaults '(bnfc-font-lock-keywords))
(setq-local comment-start "--")
(setq-local comment-end ""))
(provide 'bnfc)
;;; bnfc.el ends here