1 ;;; ebnf-yac.el --- parser for Yacc/Bison
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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, 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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; This is part of ebnf2ps package.
35 ;; This package defines a parser for Yacc/Bison.
37 ;; See ebnf2ps.el for documentation.
43 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
45 ;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
46 ;; [ "<" Name ">" ] Name-List
48 ;; | "any other Yacc definition"
51 ;; YACC-Code = "any C definition".
53 ;; YACC-Rule = Name ":" Alternative ";".
55 ;; Alternative = { Sequence || "|" }*.
57 ;; Sequence = { Factor }*.
60 ;; | "'" "character" "'"
62 ;; | "{" "C like commands" "}"
65 ;; Name-List = { Name || "," }*.
67 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
69 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
70 ;; | "//" "any character, but the newline \"\\n\"" "\\n".
73 ;; In other words, a valid Name begins with a letter (upper or lower case)
74 ;; followed by letters, decimal digits, underscore (_) or point (.). For
75 ;; example: this_is_a_valid.name, Another_EXAMPLE, mIxEd.CaSe.
81 ;; Thanks to Matthew K. Junker <junker@alum.mit.edu> for the suggestion to deal
82 ;; with %right, %left and %prec pragmas. His suggestion was extended to deal
83 ;; with %nonassoc pragma too.
86 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 (defvar ebnf-yac-lex nil
95 "Value returned by `ebnf-yac-lex' function.")
98 (defvar ebnf-yac-token-list nil
99 "List of `%TOKEN' names.")
102 (defvar ebnf-yac-skip-char nil
103 "Non-nil means skip printable characters with no grammatical meaning.")
106 (defvar ebnf-yac-error nil
107 "Non-nil means \"error\" occurred.")
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;; Syntactic analyzer
114 ;;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
116 ;;; YACC-Code = "any C definition".
118 (defun ebnf-yac-parser (start)
120 (let ((total (+ (- ebnf-limit start) 1))
123 syntax-list token rule)
125 (setq token (ebnf-yac-lex))
126 (and (eq token 'end-of-input)
127 (error "Invalid Yacc/Bison file format"))
128 (or (eq (ebnf-yac-definitions token) 'yac-separator)
129 (error "Missing `%%%%'"))
130 (setq token (ebnf-yac-lex))
131 (while (not (memq token '(end-of-input yac-separator)))
134 (/ (* (- (point) bias) 100.0) total))
135 (setq token (ebnf-yac-rule token)
138 (or (ebnf-add-empty-rule-list rule)
139 (setq syntax-list (cons rule syntax-list))))
144 ;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
145 ;;; [ "<" Name ">" ] Name-List
147 ;;; | "any other Yacc definition"
150 (defun ebnf-yac-definitions (token)
151 (let ((ebnf-yac-skip-char t))
152 (while (not (memq token '(yac-separator end-of-input)))
155 ;; ( "%token" | "%left" | "%right" | "%nonassoc" )
156 ;; [ "<" Name ">" ] Name-List
157 ((eq token 'yac-token)
158 (setq token (ebnf-yac-lex))
159 (when (eq token 'open-angle)
160 (or (eq (ebnf-yac-lex) 'non-terminal)
161 (error "Missing type name"))
162 (or (eq (ebnf-yac-lex) 'close-angle)
163 (error "Missing `>'"))
164 (setq token (ebnf-yac-lex)))
165 (setq token (ebnf-yac-name-list token)
166 ebnf-yac-token-list (nconc (cdr token)
167 ebnf-yac-token-list))
170 ((eq token 'yac-prec)
171 (or (eq (ebnf-yac-lex) 'non-terminal)
172 (error "Missing prec name"))
174 ;; "any other Yacc definition"
181 ;;; YACC-Rule = Name ":" Alternative ";".
183 (defun ebnf-yac-rule (token)
184 (let ((header ebnf-yac-lex)
187 (setq ebnf-action nil)
188 (or (eq token 'non-terminal)
189 (error "Invalid rule name"))
190 (or (eq (ebnf-yac-lex) 'colon)
191 (error "Invalid rule: missing `:'"))
192 (setq body (ebnf-yac-alternative))
193 (or (eq (car body) 'period)
194 (error "Invalid rule: missing `;'"))
195 (setq body (cdr body))
196 (ebnf-eps-add-production header)
198 (ebnf-make-production header body action))))
201 ;;; Alternative = { Sequence || "|" }*.
203 (defun ebnf-yac-alternative ()
205 (while (eq (car (setq sequence (ebnf-yac-sequence)))
207 (and (setq sequence (cdr sequence))
208 (setq body (cons sequence body))))
209 (ebnf-token-alternative body sequence)))
212 ;;; Sequence = { Factor }*.
214 (defun ebnf-yac-sequence ()
215 (let (ebnf-yac-error token seq factor)
216 (while (setq token (ebnf-yac-lex)
217 factor (ebnf-yac-factor token))
218 (setq seq (cons factor seq)))
220 (if (and ebnf-yac-ignore-error-recovery ebnf-yac-error)
221 ;; ignore error recovery
223 (ebnf-token-sequence seq)))))
227 ;;; | "'" "character" "'"
229 ;;; | "{" "C like commands" "}"
232 (defun ebnf-yac-factor (token)
235 ((eq token 'terminal)
236 (ebnf-make-terminal ebnf-yac-lex))
238 ((eq token 'non-terminal)
239 (ebnf-make-non-terminal ebnf-yac-lex))
241 ((eq token 'yac-error)
242 (ebnf-make-special ebnf-yac-lex))
249 ;;; Name-List = { Name || "," }*.
251 (defun ebnf-yac-name-list (token)
253 (when (eq token 'non-terminal)
255 (setq names (cons ebnf-yac-lex names)
256 token (ebnf-yac-lex))
258 (or (eq (ebnf-yac-lex) 'non-terminal)
259 (error "Missing token name"))))
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 ;;; Name = "[A-Za-z][A-Za-z0-9_.]*".
269 ;;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
270 ;;; | "//" "any character" "\\n".
272 (defconst ebnf-yac-token-table
273 ;; control character & 8-bit character are set to `error'
274 (let ((table (make-vector 256 'error)))
275 ;; upper & lower case letters:
278 (aset table char 'non-terminal))
279 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
280 ;; printable characters:
283 (aset table char 'character))
284 "!#$&()*+-.0123456789=?@[\\]^_`~")
285 ;; Override space characters:
286 (aset table ?\n 'space) ; [NL] linefeed
287 (aset table ?\r 'space) ; [CR] carriage return
288 (aset table ?\t 'space) ; [HT] horizontal tab
289 (aset table ?\ 'space) ; [SP] space
290 ;; Override form feed character:
291 (aset table ?\f 'form-feed) ; [FF] form feed
292 ;; Override other lexical characters:
293 (aset table ?< 'open-angle)
294 (aset table ?> 'close-angle)
295 (aset table ?, 'comma)
296 (aset table ?% 'yac-pragma)
297 (aset table ?/ 'slash)
298 (aset table ?\{ 'yac-code)
299 (aset table ?\" 'string)
300 (aset table ?\' 'terminal)
301 (aset table ?: 'colon)
302 (aset table ?| 'alternative)
303 (aset table ?\; 'period)
305 "Vector used to map characters to a lexical token.")
308 (defun ebnf-yac-initialize ()
309 "Initializations for Yacc/Bison parser."
310 (setq ebnf-yac-token-list nil))
313 (defun ebnf-yac-lex ()
314 "Lexical analyzer for Yacc/Bison.
316 Return a lexical token.
318 See documentation for variable `ebnf-yac-lex'."
319 (if (>= (point) ebnf-limit)
322 ;; skip spaces, code blocks and comments
323 (while (if (> (following-char) 255)
327 (setq token (aref ebnf-yac-token-table (following-char)))
329 ((or (eq token 'space)
330 (and ebnf-yac-skip-char
331 (eq token 'character)))
332 (ebnf-yac-skip-spaces))
333 ((eq token 'yac-code)
334 (ebnf-yac-skip-code))
336 (ebnf-yac-handle-comment))
337 ((eq token 'form-feed)
339 (setq ebnf-action 'form-feed))
344 ((>= (point) ebnf-limit)
348 (error "Invalid character"))
351 (setq ebnf-yac-lex (ebnf-get-string))
354 ((eq token 'terminal)
355 (setq ebnf-yac-lex (ebnf-string " -&(-~" ?\' "terminal"))
357 ;; non-terminal, terminal or "error"
358 ((eq token 'non-terminal)
359 (setq ebnf-yac-lex (ebnf-buffer-substring "0-9A-Za-z_."))
360 (cond ((member ebnf-yac-lex ebnf-yac-token-list)
362 ((string= ebnf-yac-lex "error")
363 (setq ebnf-yac-error t)
368 ;; %% and Yacc pragmas (%TOKEN, %START, etc).
369 ((eq token 'yac-pragma)
373 ((eq (following-char) ?%)
376 ;; %TOKEN, %RIGHT, %LEFT, %PREC, %NONASSOC
377 ((cdr (assoc (upcase (ebnf-buffer-substring "0-9A-Za-z_"))
378 '(("TOKEN" . yac-token)
379 ("RIGHT" . yac-token)
381 ("NONASSOC" . yac-token)
382 ("PREC" . yac-prec)))))
383 ;; other Yacc pragmas
394 (defun ebnf-yac-skip-spaces ()
396 (if ebnf-yac-skip-char
397 "\n\r\t !#$&()*+-.0123456789=?@[\\\\]^_`~"
400 (< (point) ebnf-limit))
403 ;; replace the range "\177-\377" (see `ebnf-range-regexp').
404 (defconst ebnf-yac-skip-chars
405 (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037" ?\177 ?\377))
408 (defun ebnf-yac-skip-code ()
412 (skip-chars-forward ebnf-yac-skip-chars ebnf-limit)
414 ((= (following-char) ?{)
416 (setq pair (1+ pair)))
417 ((= (following-char) ?})
419 (setq pair (1- pair)))
420 ((= (following-char) ?/)
421 (ebnf-yac-handle-comment))
422 ((= (following-char) ?\")
424 ((= (following-char) ?\')
425 (ebnf-string " -&(-~" ?\' "character"))
427 (error "Invalid character"))
429 (ebnf-yac-skip-spaces))
432 (defun ebnf-yac-handle-comment ()
436 ((= (following-char) ?*)
437 (ebnf-yac-skip-comment)
438 (ebnf-yac-skip-spaces))
440 ((= (following-char) ?/)
442 (ebnf-yac-skip-spaces))
448 ;; replace the range "\177-\237" (see `ebnf-range-regexp').
449 (defconst ebnf-yac-comment-chars
450 (ebnf-range-regexp "^*\000-\010\013\016-\037" ?\177 ?\237))
453 (defun ebnf-yac-skip-comment ()
457 ((and ebnf-eps-executing (= (following-char) ?\[))
458 (ebnf-eps-add-context (ebnf-yac-eps-filename)))
460 ((and ebnf-eps-executing (= (following-char) ?\]))
461 (ebnf-eps-remove-context (ebnf-yac-eps-filename)))
463 ((and ebnf-eps-executing (= (following-char) ?H))
464 (ebnf-eps-header-comment (ebnf-yac-eps-filename)))
466 ((and ebnf-eps-executing (= (following-char) ?F))
467 (ebnf-eps-footer-comment (ebnf-yac-eps-filename)))
468 ;; any other action in comment
470 (setq ebnf-action (aref ebnf-comment-table (following-char))))
474 (skip-chars-forward ebnf-yac-comment-chars ebnf-limit)
475 (cond ((>= (point) ebnf-limit)
476 (error "Missing end of comment: `*/'"))
477 ((= (following-char) ?*)
478 (skip-chars-forward "*" ebnf-limit)
479 (when (= (following-char) ?/)
484 (error "Invalid character"))
488 (defun ebnf-yac-eps-filename ()
490 (buffer-substring-no-properties
492 (let ((chars (concat ebnf-yac-comment-chars "\n"))
495 (skip-chars-forward chars ebnf-limit)
497 (cond ((>= (point) ebnf-limit)
499 ((= (following-char) ?*)
500 (skip-chars-forward "*" ebnf-limit)
501 (if (/= (following-char) ?\/)
511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 ;; arch-tag: 8a96989c-0b1d-42ba-a020-b2901f9a2a4d
518 ;;; ebnf-yac.el ends here