Mercurial > emacs
annotate lisp/progmodes/ebnf-bnf.el @ 69700:a00170090600
updated GNU Emacs for VMS link
author | Ramprasad B <ramprasad_i82@yahoo.com> |
---|---|
date | Tue, 28 Mar 2006 07:25:39 +0000 |
parents | dc49655f57ae |
children | 8c9e156de392 4b3d39451150 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
1 ;;; ebnf-bnf.el --- parser for EBNF |
27451 | 2 |
68773
dc49655f57ae
Update copyright for 2006.
Nick Roberts <nickrob@snap.net.nz>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
54140
766aaa5bded5
ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
Vinicius Jose Latorre <viniciusjl@ig.com.br>
parents:
52401
diff
changeset
|
4 ;; Free Sofware Foundation, Inc. |
27451 | 5 |
54140
766aaa5bded5
ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
Vinicius Jose Latorre <viniciusjl@ig.com.br>
parents:
52401
diff
changeset
|
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br> |
766aaa5bded5
ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
Vinicius Jose Latorre <viniciusjl@ig.com.br>
parents:
52401
diff
changeset
|
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br> |
54714 | 8 ;; Time-stamp: <2004/04/03 16:42:18 vinicius> |
39343 | 9 ;; Keywords: wp, ebnf, PostScript |
54714 | 10 ;; Version: 1.9 |
27451 | 11 |
27539 | 12 ;; This file is part of GNU Emacs. |
27451 | 13 |
27539 | 14 ;; GNU Emacs is free software; you can redistribute it and/or modify |
27451 | 15 ;; it under the terms of the GNU General Public License as published by |
16 ;; the Free Software Foundation; either version 2, or (at your option) | |
17 ;; any later version. | |
18 | |
27539 | 19 ;; GNU Emacs is distributed in the hope that it will be useful, |
27451 | 20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 ;; GNU General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
27 ;; Boston, MA 02110-1301, USA. | |
27451 | 28 |
29 ;;; Commentary: | |
30 | |
31 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
32 ;; | |
33 ;; | |
34 ;; This is part of ebnf2ps package. | |
35 ;; | |
36 ;; This package defines a parser for EBNF. | |
37 ;; | |
38 ;; See ebnf2ps.el for documentation. | |
39 ;; | |
40 ;; | |
41 ;; EBNF Syntax | |
42 ;; ----------- | |
43 ;; | |
44 ;; The current EBNF that ebnf2ps accepts has the following constructions: | |
45 ;; | |
46 ;; ; comment (until end of line) | |
47 ;; A non-terminal | |
48 ;; "C" terminal | |
49 ;; ?C? special | |
50 ;; $A default non-terminal | |
51 ;; $"C" default terminal | |
52 ;; $?C? default special | |
53 ;; A = B. production (A is the header and B the body) | |
54 ;; C D sequence (C occurs before D) | |
55 ;; C | D alternative (C or D occurs) | |
56 ;; A - B exception (A excluding B, B without any non-terminal) | |
54192 | 57 ;; n * A repetition (A repeats at least n (integer) times) |
58 ;; n * n A repetition (A repeats exactly n (integer) times) | |
59 ;; n * m A repetition (A repeats at least n (integer) and at most | |
60 ;; m (integer) times) | |
27451 | 61 ;; (C) group (expression C is grouped together) |
62 ;; [C] optional (C may or not occurs) | |
63 ;; C+ one or more occurrences of C | |
64 ;; {C}+ one or more occurrences of C | |
65 ;; {C}* zero or more occurrences of C | |
66 ;; {C} zero or more occurrences of C | |
67 ;; C / D equivalent to: C {D C}* | |
68 ;; {C || D}+ equivalent to: C {D C}* | |
69 ;; {C || D}* equivalent to: [C {D C}*] | |
70 ;; {C || D} equivalent to: [C {D C}*] | |
71 ;; | |
72 ;; The EBNF syntax written using the notation above is: | |
73 ;; | |
74 ;; EBNF = {production}+. | |
75 ;; | |
76 ;; production = non_terminal "=" body ".". ;; production | |
77 ;; | |
78 ;; body = {sequence || "|"}*. ;; alternative | |
79 ;; | |
80 ;; sequence = {exception}*. ;; sequence | |
81 ;; | |
82 ;; exception = repeat [ "-" repeat]. ;; exception | |
83 ;; | |
54192 | 84 ;; repeat = [ integer "*" [ integer ]] term. ;; repetition |
27451 | 85 ;; |
86 ;; term = factor | |
87 ;; | [factor] "+" ;; one-or-more | |
88 ;; | [factor] "/" [factor] ;; one-or-more | |
89 ;; . | |
90 ;; | |
91 ;; factor = [ "$" ] "\"" terminal "\"" ;; terminal | |
92 ;; | [ "$" ] non_terminal ;; non-terminal | |
93 ;; | [ "$" ] "?" special "?" ;; special | |
94 ;; | "(" body ")" ;; group | |
95 ;; | "[" body "]" ;; zero-or-one | |
96 ;; | "{" body [ "||" body ] "}+" ;; one-or-more | |
97 ;; | "{" body [ "||" body ] "}*" ;; zero-or-more | |
98 ;; | "{" body [ "||" body ] "}" ;; zero-or-more | |
99 ;; . | |
100 ;; | |
39343 | 101 ;; non_terminal = "[!#%&'*-,0-:<>@-Z\\\\^-z~\\240-\\377]+". |
54192 | 102 ;; ;; that is, a valid non_terminal accepts decimal digits, letters (upper |
103 ;; ;; and lower), 8-bit accentuated characters, | |
104 ;; ;; "!", "#", "%", "&", "'", "*", "+", ",", ":", | |
105 ;; ;; "<", ">", "@", "\", "^", "_", "`" and "~". | |
27451 | 106 ;; |
107 ;; terminal = "\\([^\"\\]\\|\\\\[ -~\\240-\\377]\\)+". | |
54192 | 108 ;; ;; that is, a valid terminal accepts any printable character (including |
109 ;; ;; 8-bit accentuated characters) except `"', as `"' is used to delimit a | |
110 ;; ;; terminal. Also, accepts escaped characters, that is, a character | |
111 ;; ;; pair starting with `\' followed by a printable character, for | |
63629
b42bf212f030
(ebnf-bnf-lex): Fix spellings.
Juanma Barranquero <lekktu@gmail.com>
parents:
60917
diff
changeset
|
112 ;; ;; example: \", \\. |
27451 | 113 ;; |
54192 | 114 ;; special = "[^?\\000-\\010\\012-\\037\\177-\\237]*". |
115 ;; ;; that is, a valid special accepts any printable character (including | |
116 ;; ;; 8-bit accentuated characters) and tabs except `?', as `?' is used to | |
117 ;; ;; delimit a special. | |
27451 | 118 ;; |
119 ;; integer = "[0-9]+". | |
54192 | 120 ;; ;; that is, an integer is a sequence of one or more decimal digits. |
27451 | 121 ;; |
122 ;; comment = ";" "[^\\n\\000-\\010\\016-\\037\\177-\\237]*" "\\n". | |
54192 | 123 ;; ;; that is, a comment starts with the character `;' and terminates at end |
124 ;; ;; of line. Also, it only accepts printable characters (including 8-bit | |
125 ;; ;; accentuated characters) and tabs. | |
27451 | 126 ;; |
127 ;; | |
128 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
129 | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
130 ;;; Code: |
27451 | 131 |
132 | |
133 (require 'ebnf-otz) | |
134 | |
135 | |
136 (defvar ebnf-bnf-lex nil | |
137 "Value returned by `ebnf-bnf-lex' function.") | |
138 | |
139 | |
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
49670
cc820064216c
Fix typo in comment.
Juanma Barranquero <lekktu@gmail.com>
parents:
39422
diff
changeset
|
141 ;; Syntactic analyzer |
27451 | 142 |
143 | |
144 ;;; EBNF = {production}+. | |
145 | |
146 (defun ebnf-bnf-parser (start) | |
147 "EBNF parser." | |
148 (let ((total (+ (- ebnf-limit start) 1)) | |
149 (bias (1- start)) | |
150 (origin (point)) | |
151 prod-list token rule) | |
152 (goto-char start) | |
153 (setq token (ebnf-bnf-lex)) | |
154 (and (eq token 'end-of-input) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
155 (error "Invalid EBNF file format")) |
27451 | 156 (while (not (eq token 'end-of-input)) |
157 (ebnf-message-float | |
158 "Parsing...%s%%" | |
159 (/ (* (- (point) bias) 100.0) total)) | |
160 (setq token (ebnf-production token) | |
161 rule (cdr token) | |
162 token (car token)) | |
163 (or (ebnf-add-empty-rule-list rule) | |
164 (setq prod-list (cons rule prod-list)))) | |
165 (goto-char origin) | |
166 prod-list)) | |
167 | |
168 | |
169 ;;; production = non-terminal "=" body ".". | |
170 | |
171 (defun ebnf-production (token) | |
172 (let ((header ebnf-bnf-lex) | |
173 (action ebnf-action) | |
174 body) | |
175 (setq ebnf-action nil) | |
176 (or (eq token 'non-terminal) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
177 (error "Invalid header production")) |
27451 | 178 (or (eq (ebnf-bnf-lex) 'equal) |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
179 (error "Invalid production: missing `='")) |
27451 | 180 (setq body (ebnf-body)) |
181 (or (eq (car body) 'period) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
182 (error "Invalid production: missing `.'")) |
27451 | 183 (setq body (cdr body)) |
184 (ebnf-eps-add-production header) | |
185 (cons (ebnf-bnf-lex) | |
186 (ebnf-make-production header body action)))) | |
187 | |
188 | |
189 ;;; body = {sequence || "|"}*. | |
190 | |
191 (defun ebnf-body () | |
192 (let (body sequence) | |
193 (while (eq (car (setq sequence (ebnf-sequence))) 'alternative) | |
194 (setq sequence (cdr sequence) | |
195 body (cons sequence body))) | |
196 (ebnf-token-alternative body sequence))) | |
197 | |
198 | |
199 ;;; sequence = {exception}*. | |
200 | |
201 (defun ebnf-sequence () | |
202 (let ((token (ebnf-bnf-lex)) | |
203 seq term) | |
204 (while (setq term (ebnf-exception token) | |
205 token (car term) | |
206 term (cdr term)) | |
207 (setq seq (cons term seq))) | |
208 (cons token | |
54714 | 209 (ebnf-token-sequence seq)))) |
27451 | 210 |
211 | |
212 ;;; exception = repeat [ "-" repeat]. | |
213 | |
214 (defun ebnf-exception (token) | |
215 (let ((term (ebnf-repeat token))) | |
216 (if (not (eq (car term) 'except)) | |
217 ;; repeat | |
218 term | |
219 ;; repeat - repeat | |
220 (let ((exception (ebnf-repeat (ebnf-bnf-lex)))) | |
221 (ebnf-no-non-terminal (cdr exception)) | |
222 (ebnf-token-except (cdr term) exception))))) | |
223 | |
224 | |
225 (defun ebnf-no-non-terminal (node) | |
226 (and (vectorp node) | |
227 (let ((kind (ebnf-node-kind node))) | |
228 (cond | |
229 ((eq kind 'ebnf-generate-non-terminal) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
230 (error "Exception sequence should not contain a non-terminal")) |
27451 | 231 ((eq kind 'ebnf-generate-repeat) |
232 (ebnf-no-non-terminal (ebnf-node-separator node))) | |
233 ((memq kind '(ebnf-generate-optional ebnf-generate-except)) | |
234 (ebnf-no-non-terminal (ebnf-node-list node))) | |
235 ((memq kind '(ebnf-generate-one-or-more ebnf-generate-zero-or-more)) | |
236 (ebnf-no-non-terminal (ebnf-node-list node)) | |
237 (ebnf-no-non-terminal (ebnf-node-separator node))) | |
238 ((memq kind '(ebnf-generate-alternative ebnf-generate-sequence)) | |
239 (let ((seq (ebnf-node-list node))) | |
240 (while seq | |
241 (ebnf-no-non-terminal (car seq)) | |
242 (setq seq (cdr seq))))) | |
243 )))) | |
244 | |
245 | |
54192 | 246 ;;; repeat = [ integer "*" [ integer ]] term. |
27451 | 247 |
248 (defun ebnf-repeat (token) | |
249 (if (not (eq token 'integer)) | |
250 (ebnf-term token) | |
54192 | 251 (let ((times ebnf-bnf-lex) |
252 upper) | |
27451 | 253 (or (eq (ebnf-bnf-lex) 'repeat) |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
254 (error "Missing `*'")) |
54192 | 255 (setq token (ebnf-bnf-lex)) |
256 (when (eq token 'integer) | |
257 (setq upper ebnf-bnf-lex | |
258 token (ebnf-bnf-lex))) | |
259 (ebnf-token-repeat times (ebnf-term token) upper)))) | |
27451 | 260 |
261 | |
262 ;;; term = factor | |
263 ;;; | [factor] "+" ;; one-or-more | |
264 ;;; | [factor] "/" [factor] ;; one-or-more | |
265 ;;; . | |
266 | |
267 (defun ebnf-term (token) | |
268 (let ((factor (ebnf-factor token))) | |
269 (and factor | |
270 (setq token (ebnf-bnf-lex))) | |
271 (cond | |
272 ;; [factor] + | |
273 ((eq token 'one-or-more) | |
274 (cons (ebnf-bnf-lex) | |
275 (and factor | |
276 (let ((kind (ebnf-node-kind factor))) | |
277 (cond | |
278 ;; { A }+ + ==> { A }+ | |
279 ;; { A }* + ==> { A }* | |
280 ((memq kind '(ebnf-generate-zero-or-more | |
281 ebnf-generate-one-or-more)) | |
282 factor) | |
283 ;; [ A ] + ==> { A }* | |
284 ((eq kind 'ebnf-generate-optional) | |
285 (ebnf-make-zero-or-more (list factor))) | |
286 ;; A + | |
287 (t | |
288 (ebnf-make-one-or-more (list factor))) | |
289 ))))) | |
290 ;; [factor] / [factor] | |
291 ((eq token 'list) | |
292 (setq token (ebnf-bnf-lex)) | |
293 (let ((sep (ebnf-factor token))) | |
294 (and sep | |
295 (setq factor (or factor (ebnf-make-empty)))) | |
296 (cons (if sep | |
297 (ebnf-bnf-lex) | |
298 token) | |
299 (and factor | |
300 (ebnf-make-one-or-more factor sep))))) | |
301 ;; factor | |
302 (t | |
303 (cons token factor)) | |
304 ))) | |
305 | |
306 | |
307 ;;; factor = [ "$" ] "\"" terminal "\"" ;; terminal | |
308 ;;; | [ "$" ] non_terminal ;; non-terminal | |
309 ;;; | [ "$" ] "?" special "?" ;; special | |
310 ;;; | "(" body ")" ;; group | |
311 ;;; | "[" body "]" ;; zero-or-one | |
312 ;;; | "{" body [ "||" body ] "}+" ;; one-or-more | |
313 ;;; | "{" body [ "||" body ] "}*" ;; zero-or-more | |
314 ;;; | "{" body [ "||" body ] "}" ;; zero-or-more | |
315 ;;; . | |
316 | |
317 (defun ebnf-factor (token) | |
318 (cond | |
319 ;; terminal | |
320 ((eq token 'terminal) | |
321 (ebnf-make-terminal ebnf-bnf-lex)) | |
322 ;; non-terminal | |
323 ((eq token 'non-terminal) | |
324 (ebnf-make-non-terminal ebnf-bnf-lex)) | |
325 ;; special | |
326 ((eq token 'special) | |
327 (ebnf-make-special ebnf-bnf-lex)) | |
328 ;; group | |
329 ((eq token 'begin-group) | |
330 (let ((body (ebnf-body))) | |
331 (or (eq (car body) 'end-group) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
332 (error "Missing `)'")) |
27451 | 333 (cdr body))) |
334 ;; optional | |
335 ((eq token 'begin-optional) | |
336 (let ((body (ebnf-body))) | |
337 (or (eq (car body) 'end-optional) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
338 (error "Missing `]'")) |
27451 | 339 (ebnf-token-optional (cdr body)))) |
340 ;; list | |
341 ((eq token 'begin-list) | |
342 (let* ((body (ebnf-body)) | |
343 (token (car body)) | |
344 (list-part (cdr body)) | |
345 sep-part) | |
346 (and (eq token 'list-separator) | |
347 ;; { A || B } | |
348 (setq body (ebnf-body) ; get separator | |
349 token (car body) | |
350 sep-part (cdr body))) | |
351 (cond | |
352 ;; { A }+ | |
353 ((eq token 'end-one-or-more) | |
354 (ebnf-make-one-or-more list-part sep-part)) | |
355 ;; { A }* | |
356 ((eq token 'end-zero-or-more) | |
357 (ebnf-make-zero-or-more list-part sep-part)) | |
358 (t | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34804
diff
changeset
|
359 (error "Missing `}+', `}*' or `}'")) |
27451 | 360 ))) |
361 ;; no term | |
362 (t | |
363 nil) | |
364 )) | |
365 | |
366 | |
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
368 ;; Lexical analyzer | |
369 | |
370 | |
371 (defconst ebnf-bnf-token-table (make-vector 256 'error) | |
372 "Vector used to map characters to a lexical token.") | |
373 | |
374 | |
375 (defun ebnf-bnf-initialize () | |
376 "Initialize EBNF token table." | |
377 ;; control character & control 8-bit character are set to `error' | |
378 (let ((char ?\040)) | |
379 ;; printable character: | |
380 (while (< char ?\060) | |
381 (aset ebnf-bnf-token-table char 'non-terminal) | |
382 (setq char (1+ char))) | |
383 ;; digits: | |
384 (while (< char ?\072) | |
385 (aset ebnf-bnf-token-table char 'integer) | |
386 (setq char (1+ char))) | |
387 ;; printable character: | |
388 (while (< char ?\177) | |
389 (aset ebnf-bnf-token-table char 'non-terminal) | |
390 (setq char (1+ char))) | |
391 ;; European 8-bit accentuated characters: | |
392 (setq char ?\240) | |
393 (while (< char ?\400) | |
394 (aset ebnf-bnf-token-table char 'non-terminal) | |
395 (setq char (1+ char))) | |
396 ;; Override space characters: | |
397 (aset ebnf-bnf-token-table ?\013 'space) ; [VT] vertical tab | |
398 (aset ebnf-bnf-token-table ?\n 'space) ; [NL] linefeed | |
399 (aset ebnf-bnf-token-table ?\r 'space) ; [CR] carriage return | |
400 (aset ebnf-bnf-token-table ?\t 'space) ; [HT] horizontal tab | |
401 (aset ebnf-bnf-token-table ?\ 'space) ; [SP] space | |
402 ;; Override form feed character: | |
403 (aset ebnf-bnf-token-table ?\f 'form-feed) ; [FF] form feed | |
404 ;; Override other lexical characters: | |
405 (aset ebnf-bnf-token-table ?\" 'terminal) | |
406 (aset ebnf-bnf-token-table ?\? 'special) | |
407 (aset ebnf-bnf-token-table ?\( 'begin-group) | |
408 (aset ebnf-bnf-token-table ?\) 'end-group) | |
409 (aset ebnf-bnf-token-table ?* 'repeat) | |
410 (aset ebnf-bnf-token-table ?- 'except) | |
411 (aset ebnf-bnf-token-table ?= 'equal) | |
412 (aset ebnf-bnf-token-table ?\[ 'begin-optional) | |
413 (aset ebnf-bnf-token-table ?\] 'end-optional) | |
414 (aset ebnf-bnf-token-table ?\{ 'begin-list) | |
415 (aset ebnf-bnf-token-table ?| 'alternative) | |
416 (aset ebnf-bnf-token-table ?\} 'end-list) | |
417 (aset ebnf-bnf-token-table ?/ 'list) | |
418 (aset ebnf-bnf-token-table ?+ 'one-or-more) | |
419 (aset ebnf-bnf-token-table ?$ 'default) | |
420 ;; Override comment character: | |
421 (aset ebnf-bnf-token-table ebnf-lex-comment-char 'comment) | |
422 ;; Override end of production character: | |
423 (aset ebnf-bnf-token-table ebnf-lex-eop-char 'period))) | |
424 | |
425 | |
39422
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
426 ;; replace the range "\240-\377" (see `ebnf-range-regexp'). |
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
427 (defconst ebnf-bnf-non-terminal-chars |
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
428 (ebnf-range-regexp "!#%&'*-,0-:<>@-Z\\\\^-z~" ?\240 ?\377)) |
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
429 |
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
430 |
27451 | 431 (defun ebnf-bnf-lex () |
63629
b42bf212f030
(ebnf-bnf-lex): Fix spellings.
Juanma Barranquero <lekktu@gmail.com>
parents:
60917
diff
changeset
|
432 "Lexical analyzer for EBNF. |
27451 | 433 |
434 Return a lexical token. | |
435 | |
436 See documentation for variable `ebnf-bnf-lex'." | |
437 (if (>= (point) ebnf-limit) | |
438 'end-of-input | |
439 (let (token) | |
440 ;; skip spaces and comments | |
441 (while (if (> (following-char) 255) | |
442 (progn | |
443 (setq token 'error) | |
444 nil) | |
445 (setq token (aref ebnf-bnf-token-table (following-char))) | |
446 (cond | |
447 ((eq token 'space) | |
448 (skip-chars-forward " \013\n\r\t" ebnf-limit) | |
449 (< (point) ebnf-limit)) | |
450 ((eq token 'comment) | |
451 (ebnf-bnf-skip-comment)) | |
452 ((eq token 'form-feed) | |
453 (forward-char) | |
454 (setq ebnf-action 'form-feed)) | |
455 (t nil) | |
456 ))) | |
457 (setq ebnf-default-p nil) | |
458 (cond | |
459 ;; end of input | |
460 ((>= (point) ebnf-limit) | |
461 'end-of-input) | |
462 ;; error | |
463 ((eq token 'error) | |
60917
87f9bb9d3718
* progmodes/ebnf-abn.el, progmodes/ebnf-bnf.el,
Werner LEMBERG <wl@gnu.org>
parents:
54714
diff
changeset
|
464 (error "Invalid character")) |
27451 | 465 ;; default |
466 ((eq token 'default) | |
467 (forward-char) | |
468 (if (memq (aref ebnf-bnf-token-table (following-char)) | |
469 '(terminal non-terminal special)) | |
470 (prog1 | |
471 (ebnf-bnf-lex) | |
472 (setq ebnf-default-p t)) | |
60917
87f9bb9d3718
* progmodes/ebnf-abn.el, progmodes/ebnf-bnf.el,
Werner LEMBERG <wl@gnu.org>
parents:
54714
diff
changeset
|
473 (error "Invalid `default' element"))) |
27451 | 474 ;; integer |
475 ((eq token 'integer) | |
476 (setq ebnf-bnf-lex (ebnf-buffer-substring "0-9")) | |
477 'integer) | |
478 ;; special: ?special? | |
479 ((eq token 'special) | |
54140
766aaa5bded5
ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
Vinicius Jose Latorre <viniciusjl@ig.com.br>
parents:
52401
diff
changeset
|
480 (setq ebnf-bnf-lex (concat (and ebnf-special-show-delimiter "?") |
27451 | 481 (ebnf-string " ->@-~" ?\? "special") |
54140
766aaa5bded5
ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
Vinicius Jose Latorre <viniciusjl@ig.com.br>
parents:
52401
diff
changeset
|
482 (and ebnf-special-show-delimiter "?"))) |
27451 | 483 'special) |
484 ;; terminal: "string" | |
485 ((eq token 'terminal) | |
486 (setq ebnf-bnf-lex (ebnf-unescape-string (ebnf-get-string))) | |
487 'terminal) | |
488 ;; non-terminal or terminal | |
489 ((eq token 'non-terminal) | |
39422
3e68652d365d
Fix character range regexp. Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents:
39343
diff
changeset
|
490 (setq ebnf-bnf-lex (ebnf-buffer-substring ebnf-bnf-non-terminal-chars)) |
27451 | 491 (let ((case-fold-search ebnf-case-fold-search) |
492 match) | |
493 (if (and ebnf-terminal-regexp | |
494 (setq match (string-match ebnf-terminal-regexp | |
495 ebnf-bnf-lex)) | |
496 (zerop match) | |
497 (= (match-end 0) (length ebnf-bnf-lex))) | |
498 'terminal | |
499 'non-terminal))) | |
500 ;; end of list: }+, }*, } | |
501 ((eq token 'end-list) | |
502 (forward-char) | |
503 (cond | |
504 ((= (following-char) ?+) | |
505 (forward-char) | |
506 'end-one-or-more) | |
507 ((= (following-char) ?*) | |
508 (forward-char) | |
509 'end-zero-or-more) | |
510 (t | |
511 'end-zero-or-more) | |
512 )) | |
513 ;; alternative: |, || | |
514 ((eq token 'alternative) | |
515 (forward-char) | |
516 (if (/= (following-char) ?|) | |
517 'alternative | |
518 (forward-char) | |
519 'list-separator)) | |
520 ;; miscellaneous: {, (, ), [, ], ., =, /, +, -, * | |
521 (t | |
522 (forward-char) | |
523 token) | |
524 )))) | |
525 | |
526 | |
34804
3347fac37b3d
Fix the same problem as described on ebnf2ps.el log
Gerd Moellmann <gerd@gnu.org>
parents:
27539
diff
changeset
|
527 ;; replace the range "\177-\237" (see `ebnf-range-regexp'). |
3347fac37b3d
Fix the same problem as described on ebnf2ps.el log
Gerd Moellmann <gerd@gnu.org>
parents:
27539
diff
changeset
|
528 (defconst ebnf-bnf-comment-chars |
3347fac37b3d
Fix the same problem as described on ebnf2ps.el log
Gerd Moellmann <gerd@gnu.org>
parents:
27539
diff
changeset
|
529 (ebnf-range-regexp "^\n\000-\010\016-\037" ?\177 ?\237)) |
27451 | 530 |
531 | |
532 (defun ebnf-bnf-skip-comment () | |
533 (forward-char) | |
534 (cond | |
535 ;; open EPS file | |
536 ((and ebnf-eps-executing (= (following-char) ?\[)) | |
537 (ebnf-eps-add-context (ebnf-bnf-eps-filename))) | |
538 ;; close EPS file | |
539 ((and ebnf-eps-executing (= (following-char) ?\])) | |
540 (ebnf-eps-remove-context (ebnf-bnf-eps-filename))) | |
541 ;; any other action in comment | |
542 (t | |
543 (setq ebnf-action (aref ebnf-comment-table (following-char))) | |
544 (skip-chars-forward ebnf-bnf-comment-chars ebnf-limit)) | |
545 ) | |
546 ;; check for a valid end of comment | |
547 (cond ((>= (point) ebnf-limit) | |
548 nil) | |
549 ((= (following-char) ?\n) | |
550 (forward-char) | |
551 t) | |
552 (t | |
60917
87f9bb9d3718
* progmodes/ebnf-abn.el, progmodes/ebnf-bnf.el,
Werner LEMBERG <wl@gnu.org>
parents:
54714
diff
changeset
|
553 (error "Invalid character")) |
27451 | 554 )) |
555 | |
556 | |
557 (defun ebnf-bnf-eps-filename () | |
558 (forward-char) | |
559 (ebnf-buffer-substring ebnf-bnf-comment-chars)) | |
560 | |
561 | |
562 (defun ebnf-unescape-string (str) | |
563 (let* ((len (length str)) | |
564 (size (1- len)) | |
565 (istr 0) | |
566 (n-esc 0)) | |
567 ;; count number of escapes | |
568 (while (< istr size) | |
569 (setq istr (+ istr | |
570 (if (= (aref str istr) ?\\) | |
571 (progn | |
572 (setq n-esc (1+ n-esc)) | |
573 2) | |
574 1)))) | |
575 (if (zerop n-esc) | |
576 ;; no escapes | |
577 str | |
578 ;; at least one escape | |
579 (let ((new (make-string (- len n-esc) ?\ )) | |
580 (inew 0)) | |
581 ;; eliminate all escapes | |
582 (setq istr 0) | |
583 (while (> n-esc 0) | |
584 (and (= (aref str istr) ?\\) | |
585 (setq istr (1+ istr) | |
586 n-esc (1- n-esc))) | |
587 (aset new inew (aref str istr)) | |
588 (setq inew (1+ inew) | |
589 istr (1+ istr))) | |
590 ;; remaining string has no escape | |
591 (while (< istr len) | |
592 (aset new inew (aref str istr)) | |
593 (setq inew (1+ inew) | |
594 istr (1+ istr))) | |
595 new)))) | |
596 | |
597 | |
598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
599 | |
600 | |
601 (provide 'ebnf-bnf) | |
602 | |
603 | |
52401 | 604 ;;; arch-tag: 3b1834d3-8367-475b-80d5-8e0bbd00ce50 |
27451 | 605 ;;; ebnf-bnf.el ends here |