1 ;;; ebnf-iso --- Parser for ISO EBNF
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br>
6 ;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.com.br>
7 ;; Keywords: wp, ebnf, PostScript
8 ;; Time-stamp: <99/11/20 18:04:11 vinicius>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; This is part of ebnf2ps package.
35 ;; This package defines a parser for ISO EBNF.
37 ;; See ebnf2ps.el for documentation.
44 ;; `http://www.cl.cam.ac.uk/~mgk25/iso-ebnf.html'
45 ;; ("International Standard of the ISO EBNF Notation").
48 ;; ISO EBNF = syntax rule, {syntax rule};
50 ;; syntax rule = meta identifier, '=', definition list, ';';
52 ;; definition list = single definition, {'|', single definition};
54 ;; single definition = term, {',', term};
56 ;; term = factor, ['-', exception];
58 ;; exception = factor (* without <meta identifier> *);
60 ;; factor = [integer, '*'], primary;
62 ;; primary = optional sequence | repeated sequence | special sequence
63 ;; | grouped sequence | meta identifier | terminal string
68 ;; optional sequence = '[', definition list, ']';
70 ;; repeated sequence = '{', definition list, '}';
72 ;; grouped sequence = '(', definition list, ')';
74 ;; terminal string = "'", character - "'", {character - "'"}, "'"
75 ;; | '"', character - '"', {character - '"'}, '"';
77 ;; special sequence = '?', {character - '?'}, '?';
79 ;; meta identifier = letter, { letter | decimal digit | ' ' };
81 ;; integer = decimal digit, {decimal digit};
83 ;; comment = '(*', {comment symbol}, '*)';
85 ;; comment symbol = comment (* <== NESTED COMMENT *)
86 ;; | terminal string | special sequence | character;
88 ;; letter = ? A-Z a-z ?;
90 ;; decimal digit = ? 0-9 ?;
92 ;; character = letter | decimal digit
93 ;; | ',' | '=' | '|' | '/' | '!' | '*' | '(' | ')' | '[' | ']' | '{'
94 ;; | '}' | "'" | '"' | '?' | '-' | ';' | '.' | ' ' | ':' | '+' | '_'
95 ;; | '%' | '@' | '&' | '#' | '$' | '<' | '>' | '\' | '^' | '`' | '~';
98 ;; There is also the following alternative representation:
100 ;; STANDARD ALTERNATIVE
109 ;; Differences Between ISO EBNF And ebnf2ps ISO EBNF
110 ;; -------------------------------------------------
112 ;; ISO EBNF accepts the characters given by <character> production above,
113 ;; HORIZONTAL TAB (^I), VERTICAL TAB (^K), NEWLINE (^J or ^M) and FORM FEED
114 ;; (^L), any other characters are illegal. But ebnf2ps accepts also the
115 ;; european 8-bit accentuated characters (from \240 to \377).
118 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 (defvar ebnf-iso-lex nil
127 "Value returned by `ebnf-iso-lex' function.")
130 (defconst ebnf-no-meta-identifier nil
)
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;;; ISO EBNF = syntax rule, {syntax rule};
139 (defun ebnf-iso-parser (start)
141 (let ((total (+ (- ebnf-limit start
) 1))
144 syntax-list token rule
)
146 (setq token
(ebnf-iso-lex))
147 (and (eq token
'end-of-input
)
148 (error "Invalid ISO EBNF file format."))
149 (while (not (eq token
'end-of-input
))
152 (/ (* (- (point) bias
) 100.0) total
))
153 (setq token
(ebnf-iso-syntax-rule token
)
156 (or (ebnf-add-empty-rule-list rule
)
157 (setq syntax-list
(cons rule syntax-list
))))
162 ;;; syntax rule = meta identifier, '=', definition list, ';';
164 (defun ebnf-iso-syntax-rule (token)
165 (let ((header ebnf-iso-lex
)
168 (setq ebnf-action nil
)
169 (or (eq token
'non-terminal
)
170 (error "Invalid meta identifier syntax rule."))
171 (or (eq (ebnf-iso-lex) 'equal
)
172 (error "Invalid syntax rule: missing `='."))
173 (setq body
(ebnf-iso-definition-list))
174 (or (eq (car body
) 'period
)
175 (error "Invalid syntax rule: missing `;' or `.'."))
176 (setq body
(cdr body
))
177 (ebnf-eps-add-production header
)
179 (ebnf-make-production header body action
))))
182 ;;; definition list = single definition, {'|', single definition};
184 (defun ebnf-iso-definition-list ()
186 (while (eq (car (setq sequence
(ebnf-iso-single-definition)))
188 (setq sequence
(cdr sequence
)
189 body
(cons sequence body
)))
190 (ebnf-token-alternative body sequence
)))
193 ;;; single definition = term, {',', term};
195 (defun ebnf-iso-single-definition ()
196 (let (token seq term
)
197 (while (and (setq term
(ebnf-iso-term (ebnf-iso-lex))
200 (eq token
'catenate
))
201 (setq seq
(cons term seq
)))
207 ;; sequence with only one element
208 ((and (null term
) (= (length seq
) 1))
212 (ebnf-make-sequence (nreverse (cons term seq
))))
216 ;;; term = factor, ['-', exception];
218 ;;; exception = factor (* without <meta identifier> *);
220 (defun ebnf-iso-term (token)
221 (let ((factor (ebnf-iso-factor token
)))
222 (if (not (eq (car factor
) 'except
))
225 ;; factor - exception
226 (let ((ebnf-no-meta-identifier t
))
227 (ebnf-token-except (cdr factor
) (ebnf-iso-factor (ebnf-iso-lex)))))))
230 ;;; factor = [integer, '*'], primary;
232 (defun ebnf-iso-factor (token)
233 (if (eq token
'integer
)
234 (let ((times ebnf-iso-lex
))
235 (or (eq (ebnf-iso-lex) 'repeat
)
236 (error "Missing `*'."))
237 (ebnf-token-repeat times
(ebnf-iso-primary (ebnf-iso-lex))))
238 (ebnf-iso-primary token
)))
241 ;;; primary = optional sequence | repeated sequence | special sequence
242 ;;; | grouped sequence | meta identifier | terminal string
247 ;;; optional sequence = '[', definition list, ']';
249 ;;; repeated sequence = '{', definition list, '}';
251 ;;; grouped sequence = '(', definition list, ')';
253 ;;; terminal string = "'", character - "'", {character - "'"}, "'"
254 ;;; | '"', character - '"', {character - '"'}, '"';
256 ;;; special sequence = '?', {character - '?'}, '?';
258 ;;; meta identifier = letter, {letter | decimal digit};
260 (defun ebnf-iso-primary (token)
264 ((eq token
'terminal
)
265 (ebnf-make-terminal ebnf-iso-lex
))
267 ((eq token
'non-terminal
)
268 (ebnf-make-non-terminal ebnf-iso-lex
))
271 (ebnf-make-special ebnf-iso-lex
))
273 ((eq token
'begin-group
)
274 (let ((body (ebnf-iso-definition-list)))
275 (or (eq (car body
) 'end-group
)
276 (error "Missing `)'."))
279 ((eq token
'begin-optional
)
280 (let ((body (ebnf-iso-definition-list)))
281 (or (eq (car body
) 'end-optional
)
282 (error "Missing `]' or `/)'."))
283 (ebnf-token-optional (cdr body
))))
285 ((eq token
'begin-zero-or-more
)
286 (let* ((body (ebnf-iso-definition-list))
288 (or (eq (car body
) 'end-zero-or-more
)
289 (error "Missing `}' or `:)'."))
290 (ebnf-make-zero-or-more repeat
)))
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 (defconst ebnf-iso-token-table
306 ;; control character & 8-bit character are set to `error'
307 (let ((table (make-vector 256 'error
))
309 ;; printable character
310 (while (< char ?
\060)
311 (aset table char
'character
)
312 (setq char
(1+ char
)))
314 (while (< char ?
\072)
315 (aset table char
'integer
)
316 (setq char
(1+ char
)))
317 (while (< char ?
\101)
318 (aset table char
'character
)
319 (setq char
(1+ char
)))
320 ;; upper case letters:
321 (while (< char ?
\133)
322 (aset table char
'non-terminal
)
323 (setq char
(1+ char
)))
324 (while (< char ?
\141)
325 (aset table char
'character
)
326 (setq char
(1+ char
)))
327 ;; lower case letters:
328 (while (< char ?
\173)
329 (aset table char
'non-terminal
)
330 (setq char
(1+ char
)))
331 (while (< char ?
\177)
332 (aset table char
'character
)
333 (setq char
(1+ char
)))
334 ;; European 8-bit accentuated characters:
336 (while (< char ?
\400)
337 (aset table char
'non-terminal
)
338 (setq char
(1+ char
)))
339 ;; Override space characters:
340 (aset table ?
\013 'space
) ; [VT] vertical tab
341 (aset table ?
\n 'space
) ; [NL] linefeed
342 (aset table ?
\r 'space
) ; [CR] carriage return
343 (aset table ?
\t 'space
) ; [HT] horizontal tab
344 (aset table ?\
'space
) ; [SP] space
345 ;; Override form feed character:
346 (aset table ?
\f 'form-feed
) ; [FF] form feed
347 ;; Override other lexical characters:
348 (aset table ?
\" 'double-terminal
)
349 (aset table ?
\' 'single-terminal
)
350 (aset table ?
\? 'special
)
351 (aset table ?
* 'repeat
)
352 (aset table ?
, 'catenate
)
353 (aset table ?-
'except
)
354 (aset table ?
= 'equal
)
355 (aset table ?\
) 'end-group
)
357 "Vector used to map characters to a lexical token.")
360 (defun ebnf-iso-initialize ()
361 "Initialize ISO EBNF token table."
362 (if ebnf-iso-alternative-p
363 ;; Override alternative lexical characters:
365 (aset ebnf-iso-token-table ?\
( 'left-parenthesis
)
366 (aset ebnf-iso-token-table ?\
[ 'character
)
367 (aset ebnf-iso-token-table ?\
] 'character
)
368 (aset ebnf-iso-token-table ?\
{ 'character
)
369 (aset ebnf-iso-token-table ?\
} 'character
)
370 (aset ebnf-iso-token-table ?|
'character
)
371 (aset ebnf-iso-token-table ?\
; 'character)
372 (aset ebnf-iso-token-table ?
/ 'slash
)
373 (aset ebnf-iso-token-table ?
! 'alternative
)
374 (aset ebnf-iso-token-table ?
: 'colon
)
375 (aset ebnf-iso-token-table ?.
'period
))
376 ;; Override standard lexical characters:
377 (aset ebnf-iso-token-table ?\
( 'begin-parenthesis
)
378 (aset ebnf-iso-token-table ?\
[ 'begin-optional
)
379 (aset ebnf-iso-token-table ?\
] 'end-optional
)
380 (aset ebnf-iso-token-table ?\
{ 'begin-zero-or-more
)
381 (aset ebnf-iso-token-table ?\
} 'end-zero-or-more
)
382 (aset ebnf-iso-token-table ?|
'alternative
)
383 (aset ebnf-iso-token-table ?\
; 'period)
384 (aset ebnf-iso-token-table ?
/ 'character
)
385 (aset ebnf-iso-token-table ?
! 'character
)
386 (aset ebnf-iso-token-table ?
: 'character
)
387 (aset ebnf-iso-token-table ?.
'character
)))
390 (defun ebnf-iso-lex ()
391 "Lexical analyser for ISO EBNF.
393 Return a lexical token.
395 See documentation for variable `ebnf-iso-lex'."
396 (if (>= (point) ebnf-limit
)
399 ;; skip spaces and comments
400 (while (if (> (following-char) 255)
404 (setq token
(aref ebnf-iso-token-table
(following-char)))
407 (skip-chars-forward " \013\n\r\t" ebnf-limit
)
408 (< (point) ebnf-limit
))
409 ((or (eq token
'begin-parenthesis
)
410 (eq token
'left-parenthesis
))
412 (if (/= (following-char) ?
*)
416 (ebnf-iso-skip-comment)
418 ((eq token
'form-feed
)
420 (setq ebnf-action
'form-feed
))
425 ((>= (point) ebnf-limit
)
429 (error "Illegal character."))
432 (setq ebnf-iso-lex
(ebnf-buffer-substring "0-9"))
434 ;; special: ?special?
436 (setq ebnf-iso-lex
(concat "?"
437 (ebnf-string " ->@-~" ?
\? "special")
440 ;; terminal: "string"
441 ((eq token
'double-terminal
)
442 (setq ebnf-iso-lex
(ebnf-string " !#-~" ?
\" "terminal"))
444 ;; terminal: 'string'
445 ((eq token
'single-terminal
)
446 (setq ebnf-iso-lex
(ebnf-string " -&(-~" ?
\' "terminal"))
449 ((eq token
'non-terminal
)
450 (setq ebnf-iso-lex
(ebnf-iso-normalize
452 (ebnf-buffer-substring " 0-9A-Za-z\240-\377"))))
453 (and ebnf-no-meta-identifier
454 (error "Exception sequence should not contain a meta identifier."))
456 ;; begin optional, begin list or begin group
457 ((eq token
'left-parenthesis
)
459 (cond ((= (following-char) ?
/)
462 ((= (following-char) ?
:)
468 ;; end optional or alternative
471 (if (/= (following-char) ?\
))
478 (if (/= (following-char) ?\
))
483 ((eq token
'begin-parenthesis
)
492 (defconst ebnf-iso-comment-chars
"^*(\000-\010\016-\037\177-\237")
495 (defun ebnf-iso-skip-comment ()
499 ((and ebnf-eps-executing
(= (following-char) ?\
[))
500 (ebnf-eps-add-context (ebnf-iso-eps-filename)))
502 ((and ebnf-eps-executing
(= (following-char) ?\
]))
503 (ebnf-eps-remove-context (ebnf-iso-eps-filename)))
504 ;; any other action in comment
506 (setq ebnf-action
(aref ebnf-comment-table
(following-char))))
510 (skip-chars-forward ebnf-iso-comment-chars ebnf-limit
)
511 (cond ((>= (point) ebnf-limit
)
512 (error "Missing end of comment: `*)'."))
513 ((= (following-char) ?
*)
514 (skip-chars-forward "*" ebnf-limit
)
515 (when (= (following-char) ?\
))
518 (setq pair
(1- pair
))))
519 ((= (following-char) ?\
()
520 (skip-chars-forward "(" ebnf-limit
)
521 (when (= (following-char) ?
*)
522 ;; beginning of comment
524 (setq pair
(1+ pair
))))
526 (error "Illegal character."))
530 (defun ebnf-iso-eps-filename ()
532 (buffer-substring-no-properties
534 (let ((chars (concat ebnf-iso-comment-chars
"\n"))
537 (skip-chars-forward chars ebnf-limit
)
539 (cond ((>= (point) ebnf-limit
)
541 ((= (following-char) ?
*)
542 (skip-chars-forward "*" ebnf-limit
)
543 (if (/= (following-char) ?\
))
547 ((= (following-char) ?\
()
549 (if (/= (following-char) ?
*)
559 (defun ebnf-iso-normalize (str)
560 (if (not ebnf-iso-normalize-p
)
562 (let ((len (length str
))
565 ;; count exceeding spaces
567 (if (/= (aref str stri
) ?\
)
568 (setq stri
(1+ stri
))
569 (setq stri
(1+ stri
))
570 (while (and (< stri len
) (= (aref str stri
) ?\
))
572 spaces
(1+ spaces
)))))
574 ;; no exceeding space
576 ;; at least one exceeding space
577 (let ((new (make-string (- len spaces
) ?\
))
579 ;; eliminate exceeding spaces
582 (if (/= (aref str stri
) ?\
)
584 (aset new newi
(aref str stri
))
587 (aset new newi
(aref str stri
))
590 (while (and (> spaces
0) (= (aref str stri
) ?\
))
592 spaces
(1- spaces
)))))
593 ;; remaining is normalized
595 (aset new newi
(aref str stri
))
601 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
607 ;;; ebnf-iso.el ends here