1 ;;; ebnf-yac.el --- parser for Yacc/Bison
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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
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 3 of the License, or
17 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;; This is part of ebnf2ps package.
34 ;; This package defines a parser for Yacc/Bison.
36 ;; See ebnf2ps.el for documentation.
42 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
44 ;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
45 ;; [ "<" Name ">" ] Name-List
47 ;; | "any other Yacc definition"
50 ;; YACC-Code = "any C definition".
52 ;; YACC-Rule = Name ":" Alternative ";".
54 ;; Alternative = { Sequence || "|" }*.
56 ;; Sequence = { Factor }*.
59 ;; | "'" "character" "'"
61 ;; | "{" "C like commands" "}"
64 ;; Name-List = { Name || "," }*.
66 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
68 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
69 ;; | "//" "any character, but the newline \"\\n\"" "\\n".
72 ;; In other words, a valid Name begins with a letter (upper or lower case)
73 ;; followed by letters, decimal digits, underscore (_) or point (.). For
74 ;; example: this_is_a_valid.name, Another_EXAMPLE, mIxEd.CaSe.
80 ;; Thanks to Matthew K. Junker <junker@alum.mit.edu> for the suggestion to deal
81 ;; with %right, %left and %prec pragmas. His suggestion was extended to deal
82 ;; with %nonassoc pragma too.
85 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 (defvar ebnf-yac-lex nil
94 "Value returned by `ebnf-yac-lex' function.")
97 (defvar ebnf-yac-token-list nil
98 "List of `%TOKEN' names.")
101 (defvar ebnf-yac-skip-char nil
102 "Non-nil means skip printable characters with no grammatical meaning.")
105 (defvar ebnf-yac-error nil
106 "Non-nil means \"error\" occurred.")
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;; Syntactic analyzer
113 ;;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
115 ;;; YACC-Code = "any C definition".
117 (defun ebnf-yac-parser (start)
119 (let ((total (+ (- ebnf-limit start) 1))
122 syntax-list token rule)
124 (setq token (ebnf-yac-lex))
125 (and (eq token 'end-of-input)
126 (error "Invalid Yacc/Bison file format"))
127 (or (eq (ebnf-yac-definitions token) 'yac-separator)
128 (error "Missing `%%%%'"))
129 (setq token (ebnf-yac-lex))
130 (while (not (memq token '(end-of-input yac-separator)))
133 (/ (* (- (point) bias) 100.0) total))
134 (setq token (ebnf-yac-rule token)
137 (or (ebnf-add-empty-rule-list rule)
138 (setq syntax-list (cons rule syntax-list))))
143 ;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
144 ;;; [ "<" Name ">" ] Name-List
146 ;;; | "any other Yacc definition"
149 (defun ebnf-yac-definitions (token)
150 (let ((ebnf-yac-skip-char t))
151 (while (not (memq token '(yac-separator end-of-input)))
154 ;; ( "%token" | "%left" | "%right" | "%nonassoc" )
155 ;; [ "<" Name ">" ] Name-List
156 ((eq token 'yac-token)
157 (setq token (ebnf-yac-lex))
158 (when (eq token 'open-angle)
159 (or (eq (ebnf-yac-lex) 'non-terminal)
160 (error "Missing type name"))
161 (or (eq (ebnf-yac-lex) 'close-angle)
162 (error "Missing `>'"))
163 (setq token (ebnf-yac-lex)))
164 (setq token (ebnf-yac-name-list token)
165 ebnf-yac-token-list (nconc (cdr token)
166 ebnf-yac-token-list))
169 ((eq token 'yac-prec)
170 (or (eq (ebnf-yac-lex) 'non-terminal)
171 (error "Missing prec name"))
173 ;; "any other Yacc definition"
180 ;;; YACC-Rule = Name ":" Alternative ";".
182 (defun ebnf-yac-rule (token)
183 (let ((header ebnf-yac-lex)
186 (setq ebnf-action nil)
187 (or (eq token 'non-terminal)
188 (error "Invalid rule name"))
189 (or (eq (ebnf-yac-lex) 'colon)
190 (error "Invalid rule: missing `:'"))
191 (setq body (ebnf-yac-alternative))
192 (or (eq (car body) 'period)
193 (error "Invalid rule: missing `;'"))
194 (setq body (cdr body))
195 (ebnf-eps-add-production header)
197 (ebnf-make-production header body action))))
200 ;;; Alternative = { Sequence || "|" }*.
202 (defun ebnf-yac-alternative ()
204 (while (eq (car (setq sequence (ebnf-yac-sequence)))
206 (and (setq sequence (cdr sequence))
207 (setq body (cons sequence body))))
208 (ebnf-token-alternative body sequence)))
211 ;;; Sequence = { Factor }*.
213 (defun ebnf-yac-sequence ()
214 (let (ebnf-yac-error token seq factor)
215 (while (setq token (ebnf-yac-lex)
216 factor (ebnf-yac-factor token))
217 (setq seq (cons factor seq)))
219 (if (and ebnf-yac-ignore-error-recovery ebnf-yac-error)
220 ;; ignore error recovery
222 (ebnf-token-sequence seq)))))
226 ;;; | "'" "character" "'"
228 ;;; | "{" "C like commands" "}"
231 (defun ebnf-yac-factor (token)
234 ((eq token 'terminal)
235 (ebnf-make-terminal ebnf-yac-lex))
237 ((eq token 'non-terminal)
238 (ebnf-make-non-terminal ebnf-yac-lex))
240 ((eq token 'yac-error)
241 (ebnf-make-special ebnf-yac-lex))
248 ;;; Name-List = { Name || "," }*.
250 (defun ebnf-yac-name-list (token)
252 (when (eq token 'non-terminal)
254 (setq names (cons ebnf-yac-lex names)
255 token (ebnf-yac-lex))
257 (or (eq (ebnf-yac-lex) 'non-terminal)
258 (error "Missing token name"))))
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;;; Name = "[A-Za-z][A-Za-z0-9_.]*".
268 ;;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
269 ;;; | "//" "any character" "\\n".
271 (defconst ebnf-yac-token-table
272 ;; control character & 8-bit character are set to `error'
273 (let ((table (make-vector 256 'error)))
274 ;; upper & lower case letters:
277 (aset table char 'non-terminal))
278 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
279 ;; printable characters:
282 (aset table char 'character))
283 "!#$&()*+-.0123456789=?@[\\]^_`~")
284 ;; Override space characters:
285 (aset table ?\n 'space) ; [NL] linefeed
286 (aset table ?\r 'space) ; [CR] carriage return
287 (aset table ?\t 'space) ; [HT] horizontal tab
288 (aset table ?\ 'space) ; [SP] space
289 ;; Override form feed character:
290 (aset table ?\f 'form-feed) ; [FF] form feed
291 ;; Override other lexical characters:
292 (aset table ?< 'open-angle)
293 (aset table ?> 'close-angle)
294 (aset table ?, 'comma)
295 (aset table ?% 'yac-pragma)
296 (aset table ?/ 'slash)
297 (aset table ?\{ 'yac-code)
298 (aset table ?\" 'string)
299 (aset table ?\' 'terminal)
300 (aset table ?: 'colon)
301 (aset table ?| 'alternative)
302 (aset table ?\; 'period)
304 "Vector used to map characters to a lexical token.")
307 (defun ebnf-yac-initialize ()
308 "Initializations for Yacc/Bison parser."
309 (setq ebnf-yac-token-list nil))
312 (defun ebnf-yac-lex ()
313 "Lexical analyzer for Yacc/Bison.
315 Return a lexical token.
317 See documentation for variable `ebnf-yac-lex'."
318 (if (>= (point) ebnf-limit)
321 ;; skip spaces, code blocks and comments
322 (while (if (> (following-char) 255)
326 (setq token (aref ebnf-yac-token-table (following-char)))
328 ((or (eq token 'space)
329 (and ebnf-yac-skip-char
330 (eq token 'character)))
331 (ebnf-yac-skip-spaces))
332 ((eq token 'yac-code)
333 (ebnf-yac-skip-code))
335 (ebnf-yac-handle-comment))
336 ((eq token 'form-feed)
338 (setq ebnf-action 'form-feed))
343 ((>= (point) ebnf-limit)
347 (error "Invalid character"))
350 (setq ebnf-yac-lex (ebnf-get-string))
353 ((eq token 'terminal)
354 (setq ebnf-yac-lex (ebnf-string " -&(-~" ?\' "terminal"))
356 ;; non-terminal, terminal or "error"
357 ((eq token 'non-terminal)
358 (setq ebnf-yac-lex (ebnf-buffer-substring "0-9A-Za-z_."))
359 (cond ((member ebnf-yac-lex ebnf-yac-token-list)
361 ((string= ebnf-yac-lex "error")
362 (setq ebnf-yac-error t)
367 ;; %% and Yacc pragmas (%TOKEN, %START, etc).
368 ((eq token 'yac-pragma)
372 ((eq (following-char) ?%)
375 ;; %TOKEN, %RIGHT, %LEFT, %PREC, %NONASSOC
376 ((cdr (assoc (upcase (ebnf-buffer-substring "0-9A-Za-z_"))
377 '(("TOKEN" . yac-token)
378 ("RIGHT" . yac-token)
380 ("NONASSOC" . yac-token)
381 ("PREC" . yac-prec)))))
382 ;; other Yacc pragmas
393 (defun ebnf-yac-skip-spaces ()
395 (if ebnf-yac-skip-char
396 "\n\r\t !#$&()*+-.0123456789=?@[\\\\]^_`~"
399 (< (point) ebnf-limit))
402 ;; replace the range "\177-\377" (see `ebnf-range-regexp').
403 (defconst ebnf-yac-skip-chars
404 (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037" ?\177 ?\377))
407 (defun ebnf-yac-skip-code ()
411 (skip-chars-forward ebnf-yac-skip-chars ebnf-limit)
413 ((= (following-char) ?{)
415 (setq pair (1+ pair)))
416 ((= (following-char) ?})
418 (setq pair (1- pair)))
419 ((= (following-char) ?/)
420 (ebnf-yac-handle-comment))
421 ((= (following-char) ?\")
423 ((= (following-char) ?\')
424 (ebnf-string " -&(-~" ?\' "character"))
426 (error "Invalid character"))
428 (ebnf-yac-skip-spaces))
431 (defun ebnf-yac-handle-comment ()
435 ((= (following-char) ?*)
436 (ebnf-yac-skip-comment)
437 (ebnf-yac-skip-spaces))
439 ((= (following-char) ?/)
441 (ebnf-yac-skip-spaces))
447 ;; replace the range "\177-\237" (see `ebnf-range-regexp').
448 (defconst ebnf-yac-comment-chars
449 (ebnf-range-regexp "^*\000-\010\013\016-\037" ?\177 ?\237))
452 (defun ebnf-yac-skip-comment ()
456 ((and ebnf-eps-executing (= (following-char) ?\[))
457 (ebnf-eps-add-context (ebnf-yac-eps-filename)))
459 ((and ebnf-eps-executing (= (following-char) ?\]))
460 (ebnf-eps-remove-context (ebnf-yac-eps-filename)))
462 ((and ebnf-eps-executing (= (following-char) ?H))
463 (ebnf-eps-header-comment (ebnf-yac-eps-filename)))
465 ((and ebnf-eps-executing (= (following-char) ?F))
466 (ebnf-eps-footer-comment (ebnf-yac-eps-filename)))
467 ;; any other action in comment
469 (setq ebnf-action (aref ebnf-comment-table (following-char))))
473 (skip-chars-forward ebnf-yac-comment-chars ebnf-limit)
474 (cond ((>= (point) ebnf-limit)
475 (error "Missing end of comment: `*/'"))
476 ((= (following-char) ?*)
477 (skip-chars-forward "*" ebnf-limit)
478 (when (= (following-char) ?/)
483 (error "Invalid character"))
487 (defun ebnf-yac-eps-filename ()
489 (buffer-substring-no-properties
491 (let ((chars (concat ebnf-yac-comment-chars "\n"))
494 (skip-chars-forward chars ebnf-limit)
496 (cond ((>= (point) ebnf-limit)
498 ((= (following-char) ?*)
499 (skip-chars-forward "*" ebnf-limit)
500 (if (/= (following-char) ?\/)
510 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
516 ;;; ebnf-yac.el ends here