1 ;;; ebnf-abn.el --- parser for ABNF (Augmented BNF)
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Keywords: wp, ebnf, PostScript
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; This is part of ebnf2ps package.
33 ;; This package defines a parser for ABNF (Augmented BNF).
35 ;; See ebnf2ps.el for documentation.
42 ;; `http://www.ietf.org/rfc/rfc2234.txt'
44 ;; `http://www.faqs.org/rfcs/rfc2234.html'
46 ;; `http://www.rnp.br/ietf/rfc/rfc2234.txt'
47 ;; ("Augmented BNF for Syntax Specifications: ABNF").
50 ;; rulelist = 1*( rule / (*c-wsp c-nl) )
52 ;; rule = rulename defined-as elements c-nl
53 ;; ; continues if next line starts with white space
55 ;; rulename = ALPHA *(ALPHA / DIGIT / "-")
57 ;; defined-as = *c-wsp ("=" / "=/") *c-wsp
58 ;; ; basic rules definition and incremental
61 ;; elements = alternation *c-wsp
63 ;; c-wsp = WSP / (c-nl WSP)
65 ;; c-nl = comment / CRLF
66 ;; ; comment or newline
68 ;; comment = ";" *(WSP / VCHAR) CRLF
70 ;; alternation = concatenation
71 ;; *(*c-wsp "/" *c-wsp concatenation)
73 ;; concatenation = repetition *(1*c-wsp repetition)
75 ;; repetition = [repeat] element
77 ;; repeat = 1*DIGIT / (*DIGIT "*" *DIGIT)
79 ;; element = rulename / group / option /
80 ;; char-val / num-val / prose-val
82 ;; group = "(" *c-wsp alternation *c-wsp ")"
84 ;; option = "[" *c-wsp alternation *c-wsp "]"
86 ;; char-val = DQUOTE *(%x20-21 / %x23-7E) DQUOTE
87 ;; ; quoted string of SP and VCHAR without DQUOTE
89 ;; num-val = "%" (bin-val / dec-val / hex-val)
91 ;; bin-val = "b" 1*BIT
92 ;; [ 1*("." 1*BIT) / ("-" 1*BIT) ]
93 ;; ; series of concatenated bit values
94 ;; ; or single ONEOF range
96 ;; dec-val = "d" 1*DIGIT
97 ;; [ 1*("." 1*DIGIT) / ("-" 1*DIGIT) ]
99 ;; hex-val = "x" 1*HEXDIG
100 ;; [ 1*("." 1*HEXDIG) / ("-" 1*HEXDIG) ]
102 ;; prose-val = "<" *(%x20-3D / %x3F-7E) ">"
103 ;; ; bracketed string of SP and VCHAR without
105 ;; ; prose description, to be used as last resort
107 ;; ; Core rules -- the coding depends on the system, here is used 7-bit ASCII
109 ;; ALPHA = %x41-5A / %x61-7A
115 ;; ; any 7-bit US-ASCII character, excluding NUL
121 ;; ; Internet standard newline
123 ;; CTL = %x00-1F / %x7F
130 ;; ; " (Double Quote)
132 ;; HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
140 ;; LWSP = *(WSP / CRLF WSP)
141 ;; ; linear white space (past newline)
150 ;; ; visible (printing) characters
158 ;; 1. Rules name and terminal strings are case INSENSITIVE.
159 ;; So, the following rule names are all equals:
160 ;; Rule-name, rule-Name, rule-name, RULE-NAME
161 ;; Also, the following strings are equals:
162 ;; "abc", "ABC", "aBc", "Abc", "aBC", etc.
164 ;; 2. To have a case SENSITIVE string, use the character notation.
165 ;; For example, to specify the lowercase string "abc", use:
168 ;; 3. There are no implicit spaces between elements, for example, the
175 ;; mumble = foo bar foo
177 ;; Are equivalent to the following rule:
179 ;; mumble = %x61.62.61
181 ;; If spaces are needed, it should be explicit specified, like:
183 ;; spaces = 1*(%x20 / %x09) ; one or more spaces or tabs
185 ;; mumble = foo spaces bar spaces foo
187 ;; 4. Lines starting with space or tab are considered a continuation line.
188 ;; For example, the rule:
198 ;; Differences Between ABNF And ebnf2ps ABNF
199 ;; -----------------------------------------
201 ;; Besides the characters that ABNF accepts, ebnf2ps ABNF accepts also the
202 ;; underscore (_) for rule name and european 8-bit accentuated characters (from
203 ;; \240 to \377) for rule name, string and comment.
206 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214 (defvar ebnf-abn-lex nil
215 "Value returned by `ebnf-abn-lex' function.")
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;; Syntactic analyzer
222 ;;; rulelist = 1*( rule / (*c-wsp c-nl) )
224 (defun ebnf-abn-parser (start)
226 (let ((total (+ (- ebnf-limit start
) 1))
229 rule-list token rule
)
231 (setq token
(ebnf-abn-lex))
232 (and (eq token
'end-of-input
)
233 (error "Invalid ABNF file format"))
234 (and (eq token
'end-of-rule
)
235 (setq token
(ebnf-abn-lex)))
236 (while (not (eq token
'end-of-input
))
239 (/ (* (- (point) bias
) 100.0) total
))
240 (setq token
(ebnf-abn-rule token
)
243 (or (ebnf-add-empty-rule-list rule
)
244 (setq rule-list
(cons rule rule-list
))))
249 ;;; rule = rulename defined-as elements c-nl
250 ;;; ; continues if next line starts with white space
252 ;;; rulename = ALPHA *(ALPHA / DIGIT / "-")
254 ;;; defined-as = *c-wsp ("=" / "=/") *c-wsp
255 ;;; ; basic rules definition and incremental
258 ;;; elements = alternation *c-wsp
260 ;;; c-wsp = WSP / (c-nl WSP)
262 ;;; c-nl = comment / CRLF
263 ;;; ; comment or newline
265 ;;; comment = ";" *(WSP / VCHAR) CRLF
268 (defun ebnf-abn-rule (token)
269 (let ((name ebnf-abn-lex
)
272 (setq ebnf-action nil
)
273 (or (eq token
'non-terminal
)
274 (error "Invalid rule name"))
275 (setq token
(ebnf-abn-lex))
276 (or (memq token
'(equal incremental-alternative
))
277 (error "Invalid rule: missing `=' or `=/'"))
278 (and (eq token
'incremental-alternative
)
279 (setq name
(concat name
" =/")))
280 (setq elements
(ebnf-abn-alternation))
281 (or (memq (car elements
) '(end-of-rule end-of-input
))
282 (error "Invalid rule: there is no end of rule"))
283 (setq elements
(cdr elements
))
284 (ebnf-eps-add-production name
)
286 (ebnf-make-production name elements action
))))
289 ;;; alternation = concatenation
290 ;;; *(*c-wsp "/" *c-wsp concatenation)
293 (defun ebnf-abn-alternation ()
294 (let (body concatenation
)
295 (while (eq (car (setq concatenation
296 (ebnf-abn-concatenation (ebnf-abn-lex))))
298 (setq body
(cons (cdr concatenation
) body
)))
299 (ebnf-token-alternative body concatenation
)))
302 ;;; concatenation = repetition *(1*c-wsp repetition)
305 (defun ebnf-abn-concatenation (token)
306 (let ((term (ebnf-abn-repetition token
))
308 (or (setq token
(car term
)
310 (error "Empty element"))
311 (setq seq
(cons term seq
))
312 (while (setq term
(ebnf-abn-repetition token
)
315 (setq seq
(cons term seq
)))
317 (ebnf-token-sequence seq
))))
320 ;;; repetition = [repeat] element
322 ;;; repeat = 1*DIGIT / (*DIGIT "*" *DIGIT)
325 (defun ebnf-abn-repetition (token)
327 ;; INTEGER [ "*" [ INTEGER ] ]
328 (when (eq token
'integer
)
329 (setq lower ebnf-abn-lex
330 token
(ebnf-abn-lex))
331 (or (eq token
'repeat
)
334 (when (eq token
'repeat
)
335 ;; only * ==> lower & upper are empty string
339 (when (eq (setq token
(ebnf-abn-lex)) 'integer
)
340 (setq upper ebnf-abn-lex
341 token
(ebnf-abn-lex))))
342 (let ((element (ebnf-abn-element token
)))
344 ;; there is a repetition
347 (error "Missing element repetition"))
348 (setq token
(ebnf-abn-lex))
351 ((and (string= lower
"1") (null upper
))
352 (cons token
(ebnf-make-one-or-more element
)))
354 ((or (and (string= lower
"0") (null upper
))
355 (and (string= lower
"") (string= upper
"")))
356 (cons token
(ebnf-make-zero-or-more element
)))
359 (ebnf-token-repeat lower
(cons token element
) upper
))))
360 ;; there is an element
362 (cons (ebnf-abn-lex) element
))
363 ;; something that caller has to deal
365 (cons token nil
))))))
368 ;;; element = rulename / group / option /
369 ;;; char-val / num-val / prose-val
371 ;;; group = "(" *c-wsp alternation *c-wsp ")"
373 ;;; option = "[" *c-wsp alternation *c-wsp "]"
375 ;;; char-val = DQUOTE *(%x20-21 / %x23-7E) DQUOTE
376 ;;; ; quoted string of SP and VCHAR without DQUOTE
378 ;;; num-val = "%" (bin-val / dec-val / hex-val)
380 ;;; bin-val = "b" 1*BIT
381 ;;; [ 1*("." 1*BIT) / ("-" 1*BIT) ]
382 ;;; ; series of concatenated bit values
383 ;;; ; or single ONEOF range
385 ;;; dec-val = "d" 1*DIGIT
386 ;;; [ 1*("." 1*DIGIT) / ("-" 1*DIGIT) ]
388 ;;; hex-val = "x" 1*HEXDIG
389 ;;; [ 1*("." 1*HEXDIG) / ("-" 1*HEXDIG) ]
391 ;;; prose-val = "<" *(%x20-3D / %x3F-7E) ">"
392 ;;; ; bracketed string of SP and VCHAR without
394 ;;; ; prose description, to be used as last resort
397 (defun ebnf-abn-element (token)
400 ((eq token
'terminal
)
401 (ebnf-make-terminal ebnf-abn-lex
))
403 ((eq token
'non-terminal
)
404 (ebnf-make-non-terminal ebnf-abn-lex
))
406 ((eq token
'begin-group
)
407 (let ((body (ebnf-abn-alternation)))
408 (or (eq (car body
) 'end-group
)
409 (error "Missing `)'"))
412 ((eq token
'begin-optional
)
413 (let ((body (ebnf-abn-alternation)))
414 (or (eq (car body
) 'end-optional
)
415 (error "Missing `]'"))
416 (ebnf-token-optional (cdr body
))))
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
427 (defconst ebnf-abn-token-table
(make-vector 256 'error
)
428 "Vector used to map characters to a lexical token.")
431 (defun ebnf-abn-initialize ()
432 "Initialize EBNF token table."
433 ;; control character & control 8-bit character are set to `error'
436 (while (< char ?
\072)
437 (aset ebnf-abn-token-table char
'integer
)
438 (setq char
(1+ char
)))
439 ;; printable character: A-Z
441 (while (< char ?
\133)
442 (aset ebnf-abn-token-table char
'non-terminal
)
443 (setq char
(1+ char
)))
444 ;; printable character: a-z
446 (while (< char ?
\173)
447 (aset ebnf-abn-token-table char
'non-terminal
)
448 (setq char
(1+ char
)))
449 ;; European 8-bit accentuated characters:
451 (while (< char ?
\400)
452 (aset ebnf-abn-token-table char
'non-terminal
)
453 (setq char
(1+ char
)))
454 ;; Override end of line characters:
455 (aset ebnf-abn-token-table ?
\n 'end-of-rule
) ; [NL] linefeed
456 (aset ebnf-abn-token-table ?
\r 'end-of-rule
) ; [CR] carriage return
457 ;; Override space characters:
458 (aset ebnf-abn-token-table ?
\013 'space
) ; [VT] vertical tab
459 (aset ebnf-abn-token-table ?
\t 'space
) ; [HT] horizontal tab
460 (aset ebnf-abn-token-table ?\
'space
) ; [SP] space
461 ;; Override form feed character:
462 (aset ebnf-abn-token-table ?
\f 'form-feed
) ; [FF] form feed
463 ;; Override other lexical characters:
464 (aset ebnf-abn-token-table ?
< 'non-terminal
)
465 (aset ebnf-abn-token-table ?%
'terminal
)
466 (aset ebnf-abn-token-table ?
\" 'terminal
)
467 (aset ebnf-abn-token-table ?\
( 'begin-group
)
468 (aset ebnf-abn-token-table ?\
) 'end-group
)
469 (aset ebnf-abn-token-table ?
* 'repeat
)
470 (aset ebnf-abn-token-table ?
= 'equal
)
471 (aset ebnf-abn-token-table ?\
[ 'begin-optional
)
472 (aset ebnf-abn-token-table ?\
] 'end-optional
)
473 (aset ebnf-abn-token-table ?
/ 'alternative
)
474 ;; Override comment character:
475 (aset ebnf-abn-token-table ?\
; 'comment)))
478 ;; replace the range "\240-\377" (see `ebnf-range-regexp').
479 (defconst ebnf-abn-non-terminal-chars
480 (ebnf-range-regexp "-_0-9A-Za-z" ?
\240 ?
\377))
481 (defconst ebnf-abn-non-terminal-letter-chars
482 (ebnf-range-regexp "A-Za-z" ?
\240 ?
\377))
485 (defun ebnf-abn-lex ()
486 "Lexical analyzer for ABNF.
488 Return a lexical token.
490 See documentation for variable `ebnf-abn-lex'."
491 (if (>= (point) ebnf-limit
)
494 ;; skip spaces and comments
495 (while (if (> (following-char) 255)
499 (setq token
(aref ebnf-abn-token-table
(following-char)))
502 (skip-chars-forward " \013\t" ebnf-limit
)
503 (< (point) ebnf-limit
))
505 (ebnf-abn-skip-comment))
506 ((eq token
'form-feed
)
508 (setq ebnf-action
'form-feed
))
509 ((eq token
'end-of-rule
)
510 (ebnf-abn-skip-end-of-rule))
515 ((>= (point) ebnf-limit
)
519 (error "Invalid character"))
521 ((eq token
'end-of-rule
)
525 (setq ebnf-abn-lex
(ebnf-buffer-substring "0-9"))
527 ;; terminal: "string" or %[bdx]NNN((.NNN)+|-NNN)?
528 ((eq token
'terminal
)
530 (if (= (following-char) ?
\")
532 (ebnf-abn-character)))
534 ;; non-terminal: NAME or <NAME>
535 ((eq token
'non-terminal
)
536 (let ((prose-p (= (following-char) ?
<)))
539 (or (looking-at ebnf-abn-non-terminal-letter-chars
)
540 (error "Invalid prose value")))
542 (ebnf-buffer-substring ebnf-abn-non-terminal-chars
))
544 (or (= (following-char) ?
>)
545 (error "Invalid prose value"))
546 (setq ebnf-abn-lex
(concat "<" ebnf-abn-lex
">"))))
551 (if (/= (following-char) ?
/)
554 'incremental-alternative
))
555 ;; miscellaneous: (, ), [, ], /, *
562 (defun ebnf-abn-skip-end-of-rule ()
565 ;; end of rule ==> 2 or more consecutive end of lines
566 (setq eor-p
(or (> (skip-chars-forward "\r\n" ebnf-limit
) 1)
569 (skip-chars-forward " \013\t" ebnf-limit
)
571 (and (= (following-char) ?\
;)
572 (ebnf-abn-skip-comment))))
576 ;; replace the range "\177-\237" (see `ebnf-range-regexp').
577 (defconst ebnf-abn-comment-chars
578 (ebnf-range-regexp "^\n\000-\010\016-\037" ?
\177 ?
\237))
581 (defun ebnf-abn-skip-comment ()
585 ((and ebnf-eps-executing
(= (following-char) ?\
[))
586 (ebnf-eps-add-context (ebnf-abn-eps-filename)))
588 ((and ebnf-eps-executing
(= (following-char) ?\
]))
589 (ebnf-eps-remove-context (ebnf-abn-eps-filename)))
591 ((and ebnf-eps-executing
(= (following-char) ?H
))
592 (ebnf-eps-header-comment (ebnf-abn-eps-filename)))
594 ((and ebnf-eps-executing
(= (following-char) ?F
))
595 (ebnf-eps-footer-comment (ebnf-abn-eps-filename)))
596 ;; any other action in comment
598 (setq ebnf-action
(aref ebnf-comment-table
(following-char)))
599 (skip-chars-forward ebnf-abn-comment-chars ebnf-limit
))
601 ;; check for a valid end of comment
602 (cond ((>= (point) ebnf-limit
)
604 ((= (following-char) ?
\n)
607 (error "Invalid character"))
611 (defun ebnf-abn-eps-filename ()
613 (ebnf-buffer-substring ebnf-abn-comment-chars
))
616 ;; replace the range "\240-\377" (see `ebnf-range-regexp').
617 (defconst ebnf-abn-string-chars
618 (ebnf-range-regexp " -!#-~" ?
\240 ?
\377))
621 (defun ebnf-abn-string ()
622 (buffer-substring-no-properties
627 (skip-chars-forward ebnf-abn-string-chars ebnf-limit
)
628 (or (= (following-char) ?
\")
629 (error "Missing `\"'"))
635 (defun ebnf-abn-character ()
636 ;; %[bdx]NNN((-NNN)|(.NNN)+)?
637 (buffer-substring-no-properties
641 (let* ((char (following-char))
642 (chars (cond ((or (= char ?B
) (= char ?b
)) "01")
643 ((or (= char ?D
) (= char ?d
)) "0-9")
644 ((or (= char ?X
) (= char ?x
)) "0-9A-Fa-f")
645 (t (error "Invalid terminal value")))))
647 (or (> (skip-chars-forward chars ebnf-limit
) 0)
648 (error "Invalid terminal value"))
649 (if (= (following-char) ?-
)
652 (or (> (skip-chars-forward chars ebnf-limit
) 0)
653 (error "Invalid terminal value range")))
654 (while (= (following-char) ?.
)
656 (or (> (skip-chars-forward chars ebnf-limit
) 0)
657 (error "Invalid terminal value")))))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 ;; arch-tag: 8d1b3c4d-4226-4393-b9ae-b7ccf07cf779
667 ;;; ebnf-abn.el ends here