-
Notifications
You must be signed in to change notification settings - Fork 8
/
bnf-mode.el
202 lines (157 loc) · 6.13 KB
/
bnf-mode.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;; bnf-mode.el --- Major mode for editing BNF grammars. -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2022 Free Software Foundation, Inc
;; Author: Serghei Iakovlev <egrep@protonmail.ch>
;; Maintainer: Serghei Iakovlev <egrep@protonmail.ch>
;; Version: 0.4.5
;; URL: https://github.com/sergeyklay/bnf-mode
;; Keywords: languages
;; Package-Requires: ((cl-lib "0.5") (emacs "25.1"))
;; Revision: $Format:%h (%cD %d)$
;;;; License
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; BNF Mode is a GNU Emacs major mode for editing BNF grammars. Presently it
;; provides basic syntax and font-locking for BNF files. BNF notation is
;; supported exactly form as it was first announced in the ALGOL 60 report.
;;; Code:
;;;; Requirements
(eval-when-compile
(require 'rx)) ; `rx'
;;;; Customization
;;;###autoload
(defgroup bnf nil
"Major mode for editing BNF grammars."
:tag "BNF"
:prefix "bnf-"
:group 'languages
:link '(url-link :tag "GitHub Page" "https://github.com/sergeyklay/bnf-mode")
:link '(emacs-commentary-link :tag "Commentary" "bnf-mode"))
;;;; Specialized rx
(eval-when-compile
(defconst bnf-rx-constituents
`((bnf-rule-name . ,(rx (and
(1+ (or alnum digit))
(0+ (or alnum digit
(in "!\"#$%&'()*+,-./:;=?@[]^_`{|}~")
(in " \t"))))))
"Additional special sexps for `bnf-rx'."))
(defmacro bnf-rx (&rest sexps)
"BNF-specific replacement for `rx'.
In addition to the standard forms of `rx', the following forms
are available:
`bnf-rule-name'
Any valid BNF rule name. This rule was obtained by studying
ALGOL 60 report, where the BNF was officially announced.
Please note: This rule is not suitable for ABNF or EBNF
(see URL `https://www.masswerk.at/algol60/report.htm').
See `rx' documentation for more information about REGEXPS param."
(let ((rx-constituents (append bnf-rx-constituents rx-constituents)))
(rx-to-string (cond ((null sexps) (error "No regexp is provided"))
((cdr sexps) `(and ,@sexps))
(t (car sexps)))
t))))
;;;; Font Locking
(defvar bnf-font-lock-keywords
`(
;; LHS nonterminals may be preceded
;; by an unlimited number of spaces
(,(bnf-rx (and line-start
(0+ space)
"<"
(group bnf-rule-name)
">"
(0+ space)
"::="))
1 font-lock-function-name-face)
;; Other nonterminals
(,(bnf-rx (and (0+ space)
"<"
(group bnf-rule-name)
">"
(0+ space)))
1 font-lock-builtin-face)
;; “may expand into” symbol
(,(bnf-rx (and symbol-start
(group "::=")
symbol-end))
1 font-lock-constant-face)
;; Alternatives
(,(bnf-rx (and (0+ space)
symbol-start
(group "|")
symbol-end
(0+ space)))
1 font-lock-warning-face))
"Font lock BNF keywords for BNF Mode.")
;;;; Syntax
(defvar bnf-mode-syntax-table
(let ((table (make-syntax-table)))
;; FIXME: "_" doesn't mean "symbol" but "symbol constituent".
;; I.e. the settings below mean that Emacs will consider "a:b=(c" as one
;; symbol (aka "identifier") which can be seen if you try to C-M-f and
;; C-M-b to move by sexps.
;; Treat ::= as sequence of symbols
(modify-syntax-entry ?\: "_" table)
(modify-syntax-entry ?\= "_" table)
;; Treat | as a symbol
(modify-syntax-entry ?\| "_" table)
;; In BNF there are no strings
;; so treat ' and " as a symbols
(modify-syntax-entry ?\" "_" table)
(modify-syntax-entry ?\' "_" table)
;; In BNF there are no grouping
;; brackets except angle ones
(modify-syntax-entry ?\( "_" table)
(modify-syntax-entry ?\) "_" table)
(modify-syntax-entry ?\{ "_" table)
(modify-syntax-entry ?\} "_" table)
(modify-syntax-entry ?\[ "_" table)
(modify-syntax-entry ?\] "_" table)
;; Group angle brackets
(modify-syntax-entry ?\< "(>" table)
(modify-syntax-entry ?\> ")<" table)
;; Comments are begins with “;” and ends with “\n”
(modify-syntax-entry ?\; "<" table)
(modify-syntax-entry ?\n ">" table)
table)
"Syntax table in use in `bnf-mode' buffers.")
;;;; Initialization
;;;###autoload
(define-derived-mode bnf-mode prog-mode "BNF"
"A major mode for editing BNF grammars.
\\{bnf-mode-map}
Turning on BNF Mode calls the value of `prog-mode-hook' and then of
`bnf-mode-hook', if they are non-nil."
:syntax-table bnf-mode-syntax-table
;; Comments setup
(setq-local comment-use-syntax nil)
(setq-local comment-start "; ")
(setq-local comment-end "")
(setq-local comment-start-skip "\\(?:\\(\\W\\|^\\);+\\)\\s-+")
;; Font locking
(setq font-lock-defaults
'(
;; Keywords
bnf-font-lock-keywords
;; keywords-only
nil
;; According to RFC5234 rule names are case insensitive.
;; The names <rulename>, <Rulename>, <RULENAME>, and <rUlENamE>
;; all refer to the same rule. As far as is known, this doesn't
;; conflict with original BNF version
;; (see URL `https://tools.ietf.org/html/rfc5234')
t)))
;; Invoke bnf-mode when appropriate
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.bnf\\'" . bnf-mode))
(provide 'bnf-mode)
;;; bnf-mode.el ends here