1 ;;; ebnf-yac.el --- parser for Yacc/Bison
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
4 ;; Free Sofware Foundation, Inc.
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Time-stamp: <2004/04/03 16:50:46 vinicius>
9 ;; Keywords: wp, ebnf, PostScript
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
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)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
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
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 ;; This is part of ebnf2ps package.
36 ;; This package defines a parser for Yacc/Bison.
38 ;; See ebnf2ps.el for documentation.
44 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
46 ;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
47 ;; [ "<" Name ">" ] Name-List
49 ;; | "any other Yacc definition"
52 ;; YACC-Code = "any C definition".
54 ;; YACC-Rule = Name ":" Alternative ";".
56 ;; Alternative = { Sequence || "|" }*.
58 ;; Sequence = { Factor }*.
61 ;; | "'" "character" "'"
63 ;; | "{" "C like commands" "}"
66 ;; Name-List = { Name || "," }*.
68 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
70 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
71 ;; | "//" "any character, but the newline \"\\n\"" "\\n".
74 ;; In other words, a valid Name begins with a letter (upper or lower case)
75 ;; followed by letters, decimal digits, underscore (_) or point (.). For
76 ;; example: this_is_a_valid.name, Another_EXAMPLE, mIxEd.CaSe.
82 ;; Thanks to Matthew K. Junker <junker@alum.mit.edu> for the suggestion to deal
83 ;; with %right, %left and %prec pragmas. His suggestion was extended to deal
84 ;; with %nonassoc pragma too.
87 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 (defvar ebnf-yac-lex nil
96 "Value returned by `ebnf-yac-lex' function.")
99 (defvar ebnf-yac-token-list nil
100 "List of `%TOKEN' names.")
103 (defvar ebnf-yac-skip-char nil
104 "Non-nil means skip printable characters with no grammatical meaning.")
107 (defvar ebnf-yac-error nil
108 "Non-nil means \"error\" occurred.")
111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;; Syntactic analyzer
115 ;;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
117 ;;; YACC-Code = "any C definition".
119 (defun ebnf-yac-parser (start)
121 (let ((total (+ (- ebnf-limit start) 1))
124 syntax-list token rule)
126 (setq token (ebnf-yac-lex))
127 (and (eq token 'end-of-input)
128 (error "Invalid Yacc/Bison file format"))
129 (or (eq (ebnf-yac-definitions token) 'yac-separator)
130 (error "Missing `%%%%'"))
131 (setq token (ebnf-yac-lex))
132 (while (not (memq token '(end-of-input yac-separator)))
135 (/ (* (- (point) bias) 100.0) total))
136 (setq token (ebnf-yac-rule token)
139 (or (ebnf-add-empty-rule-list rule)
140 (setq syntax-list (cons rule syntax-list))))
145 ;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
146 ;;; [ "<" Name ">" ] Name-List
148 ;;; | "any other Yacc definition"
151 (defun ebnf-yac-definitions (token)
152 (let ((ebnf-yac-skip-char t))
153 (while (not (memq token '(yac-separator end-of-input)))
156 ;; ( "%token" | "%left" | "%right" | "%nonassoc" )
157 ;; [ "<" Name ">" ] Name-List
158 ((eq token 'yac-token)
159 (setq token (ebnf-yac-lex))
160 (when (eq token 'open-angle)
161 (or (eq (ebnf-yac-lex) 'non-terminal)
162 (error "Missing type name"))
163 (or (eq (ebnf-yac-lex) 'close-angle)
164 (error "Missing `>'"))
165 (setq token (ebnf-yac-lex)))
166 (setq token (ebnf-yac-name-list token)
167 ebnf-yac-token-list (nconc (cdr token)
168 ebnf-yac-token-list))
171 ((eq token 'yac-prec)
172 (or (eq (ebnf-yac-lex) 'non-terminal)
173 (error "Missing prec name"))
175 ;; "any other Yacc definition"
182 ;;; YACC-Rule = Name ":" Alternative ";".
184 (defun ebnf-yac-rule (token)
185 (let ((header ebnf-yac-lex)
188 (setq ebnf-action nil)
189 (or (eq token 'non-terminal)
190 (error "Invalid rule name"))
191 (or (eq (ebnf-yac-lex) 'colon)
192 (error "Invalid rule: missing `:'"))
193 (setq body (ebnf-yac-alternative))
194 (or (eq (car body) 'period)
195 (error "Invalid rule: missing `;'"))
196 (setq body (cdr body))
197 (ebnf-eps-add-production header)
199 (ebnf-make-production header body action))))
202 ;;; Alternative = { Sequence || "|" }*.
204 (defun ebnf-yac-alternative ()
206 (while (eq (car (setq sequence (ebnf-yac-sequence)))
208 (and (setq sequence (cdr sequence))
209 (setq body (cons sequence body))))
210 (ebnf-token-alternative body sequence)))
213 ;;; Sequence = { Factor }*.
215 (defun ebnf-yac-sequence ()
216 (let (ebnf-yac-error token seq factor)
217 (while (setq token (ebnf-yac-lex)
218 factor (ebnf-yac-factor token))
219 (setq seq (cons factor seq)))
221 (if (and ebnf-yac-ignore-error-recovery ebnf-yac-error)
222 ;; ignore error recovery
224 (ebnf-token-sequence seq)))))
228 ;;; | "'" "character" "'"
230 ;;; | "{" "C like commands" "}"
233 (defun ebnf-yac-factor (token)
236 ((eq token 'terminal)
237 (ebnf-make-terminal ebnf-yac-lex))
239 ((eq token 'non-terminal)
240 (ebnf-make-non-terminal ebnf-yac-lex))
242 ((eq token 'yac-error)
243 (ebnf-make-special ebnf-yac-lex))
250 ;;; Name-List = { Name || "," }*.
252 (defun ebnf-yac-name-list (token)
254 (when (eq token 'non-terminal)
256 (setq names (cons ebnf-yac-lex names)
257 token (ebnf-yac-lex))
259 (or (eq (ebnf-yac-lex) 'non-terminal)
260 (error "Missing token name"))))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 ;;; Name = "[A-Za-z][A-Za-z0-9_.]*".
270 ;;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
271 ;;; | "//" "any character" "\\n".
273 (defconst ebnf-yac-token-table
274 ;; control character & 8-bit character are set to `error'
275 (let ((table (make-vector 256 'error)))
276 ;; upper & lower case letters:
279 (aset table char 'non-terminal))
280 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
281 ;; printable characters:
284 (aset table char 'character))
285 "!#$&()*+-.0123456789=?@[\\]^_`~")
286 ;; Override space characters:
287 (aset table ?\n 'space) ; [NL] linefeed
288 (aset table ?\r 'space) ; [CR] carriage return
289 (aset table ?\t 'space) ; [HT] horizontal tab
290 (aset table ?\ 'space) ; [SP] space
291 ;; Override form feed character:
292 (aset table ?\f 'form-feed) ; [FF] form feed
293 ;; Override other lexical characters:
294 (aset table ?< 'open-angle)
295 (aset table ?> 'close-angle)
296 (aset table ?, 'comma)
297 (aset table ?% 'yac-pragma)
298 (aset table ?/ 'slash)
299 (aset table ?\{ 'yac-code)
300 (aset table ?\" 'string)
301 (aset table ?\' 'terminal)
302 (aset table ?: 'colon)
303 (aset table ?| 'alternative)
304 (aset table ?\; 'period)
306 "Vector used to map characters to a lexical token.")
309 (defun ebnf-yac-initialize ()
310 "Initializations for Yacc/Bison parser."
311 (setq ebnf-yac-token-list nil))
314 (defun ebnf-yac-lex ()
315 "Lexical analyser for Yacc/Bison.
317 Return a lexical token.
319 See documentation for variable `ebnf-yac-lex'."
320 (if (>= (point) ebnf-limit)
323 ;; skip spaces, code blocks and comments
324 (while (if (> (following-char) 255)
328 (setq token (aref ebnf-yac-token-table (following-char)))
330 ((or (eq token 'space)
331 (and ebnf-yac-skip-char
332 (eq token 'character)))
333 (ebnf-yac-skip-spaces))
334 ((eq token 'yac-code)
335 (ebnf-yac-skip-code))
337 (ebnf-yac-handle-comment))
338 ((eq token 'form-feed)
340 (setq ebnf-action 'form-feed))
345 ((>= (point) ebnf-limit)
349 (error "Illegal character"))
352 (setq ebnf-yac-lex (ebnf-get-string))
355 ((eq token 'terminal)
356 (setq ebnf-yac-lex (ebnf-string " -&(-~" ?\' "terminal"))
358 ;; non-terminal, terminal or "error"
359 ((eq token 'non-terminal)
360 (setq ebnf-yac-lex (ebnf-buffer-substring "0-9A-Za-z_."))
361 (cond ((member ebnf-yac-lex ebnf-yac-token-list)
363 ((string= ebnf-yac-lex "error")
364 (setq ebnf-yac-error t)
369 ;; %% and Yacc pragmas (%TOKEN, %START, etc).
370 ((eq token 'yac-pragma)
374 ((eq (following-char) ?%)
377 ;; %TOKEN, %RIGHT, %LEFT, %PREC, %NONASSOC
378 ((cdr (assoc (upcase (ebnf-buffer-substring "0-9A-Za-z_"))
379 '(("TOKEN" . yac-token)
380 ("RIGHT" . yac-token)
382 ("NONASSOC" . yac-token)
383 ("PREC" . yac-prec)))))
384 ;; other Yacc pragmas
395 (defun ebnf-yac-skip-spaces ()
397 (if ebnf-yac-skip-char
398 "\n\r\t !#$&()*+-.0123456789=?@[\\\\]^_`~"
401 (< (point) ebnf-limit))
404 ;; replace the range "\177-\377" (see `ebnf-range-regexp').
405 (defconst ebnf-yac-skip-chars
406 (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037" ?\177 ?\377))
409 (defun ebnf-yac-skip-code ()
413 (skip-chars-forward ebnf-yac-skip-chars ebnf-limit)
415 ((= (following-char) ?{)
417 (setq pair (1+ pair)))
418 ((= (following-char) ?})
420 (setq pair (1- pair)))
421 ((= (following-char) ?/)
422 (ebnf-yac-handle-comment))
423 ((= (following-char) ?\")
425 ((= (following-char) ?\')
426 (ebnf-string " -&(-~" ?\' "character"))
428 (error "Illegal character"))
430 (ebnf-yac-skip-spaces))
433 (defun ebnf-yac-handle-comment ()
437 ((= (following-char) ?*)
438 (ebnf-yac-skip-comment)
439 (ebnf-yac-skip-spaces))
441 ((= (following-char) ?/)
443 (ebnf-yac-skip-spaces))
449 ;; replace the range "\177-\237" (see `ebnf-range-regexp').
450 (defconst ebnf-yac-comment-chars
451 (ebnf-range-regexp "^*\000-\010\013\016-\037" ?\177 ?\237))
454 (defun ebnf-yac-skip-comment ()
458 ((and ebnf-eps-executing (= (following-char) ?\[))
459 (ebnf-eps-add-context (ebnf-yac-eps-filename)))
461 ((and ebnf-eps-executing (= (following-char) ?\]))
462 (ebnf-eps-remove-context (ebnf-yac-eps-filename)))
463 ;; any other action in comment
465 (setq ebnf-action (aref ebnf-comment-table (following-char))))
469 (skip-chars-forward ebnf-yac-comment-chars ebnf-limit)
470 (cond ((>= (point) ebnf-limit)
471 (error "Missing end of comment: `*/'"))
472 ((= (following-char) ?*)
473 (skip-chars-forward "*" ebnf-limit)
474 (when (= (following-char) ?/)
479 (error "Illegal character"))
483 (defun ebnf-yac-eps-filename ()
485 (buffer-substring-no-properties
487 (let ((chars (concat ebnf-yac-comment-chars "\n"))
490 (skip-chars-forward chars ebnf-limit)
492 (cond ((>= (point) ebnf-limit)
494 ((= (following-char) ?*)
495 (skip-chars-forward "*" ebnf-limit)
496 (if (/= (following-char) ?\/)
506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
512 ;;; arch-tag: 8a96989c-0b1d-42ba-a020-b2901f9a2a4d
513 ;;; ebnf-yac.el ends here