Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / progmodes / ebnf-yac.el
bloba7f1851cffb496f9e2c865e1a0650ed57290192f
1 ;;; ebnf-yac.el --- parser for Yacc/Bison
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
9 ;; Version: 1.4
10 ;; Package: ebnf2ps
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/>.
27 ;;; Commentary:
29 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;; This is part of ebnf2ps package.
34 ;; This package defines a parser for Yacc/Bison.
36 ;; See ebnf2ps.el for documentation.
39 ;; Yacc/Bison Syntax
40 ;; -----------------
42 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
44 ;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
45 ;; [ "<" Name ">" ] Name-List
46 ;; | "%prec" Name
47 ;; | "any other Yacc definition"
48 ;; .
50 ;; YACC-Code = "any C definition".
52 ;; YACC-Rule = Name ":" Alternative ";".
54 ;; Alternative = { Sequence || "|" }*.
56 ;; Sequence = { Factor }*.
58 ;; Factor = Name
59 ;; | "'" "character" "'"
60 ;; | "error"
61 ;; | "{" "C like commands" "}"
62 ;; .
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.
77 ;; Acknowledgements
78 ;; ----------------
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 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;;; Code:
90 (require 'ebnf-otz)
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)
118 "yacc/Bison parser."
119 (let ((total (+ (- ebnf-limit start) 1))
120 (bias (1- start))
121 (origin (point))
122 syntax-list token rule)
123 (goto-char start)
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)))
131 (ebnf-message-float
132 "Parsing...%s%%"
133 (/ (* (- (point) bias) 100.0) total))
134 (setq token (ebnf-yac-rule token)
135 rule (cdr token)
136 token (car token))
137 (or (ebnf-add-empty-rule-list rule)
138 (setq syntax-list (cons rule syntax-list))))
139 (goto-char origin)
140 syntax-list))
143 ;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
144 ;;; [ "<" Name ">" ] Name-List
145 ;;; | "%prec" Name
146 ;;; | "any other Yacc definition"
147 ;;; .
149 (defun ebnf-yac-definitions (token)
150 (let ((ebnf-yac-skip-char t))
151 (while (not (memq token '(yac-separator end-of-input)))
152 (setq token
153 (cond
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))
167 (car token))
168 ;; "%prec" Name
169 ((eq token 'yac-prec)
170 (or (eq (ebnf-yac-lex) 'non-terminal)
171 (error "Missing prec name"))
172 (ebnf-yac-lex))
173 ;; "any other Yacc definition"
175 (ebnf-yac-lex))
177 token))
180 ;;; YACC-Rule = Name ":" Alternative ";".
182 (defun ebnf-yac-rule (token)
183 (let ((header ebnf-yac-lex)
184 (action ebnf-action)
185 body)
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)
196 (cons (ebnf-yac-lex)
197 (ebnf-make-production header body action))))
200 ;;; Alternative = { Sequence || "|" }*.
202 (defun ebnf-yac-alternative ()
203 (let (body sequence)
204 (while (eq (car (setq sequence (ebnf-yac-sequence)))
205 'alternative)
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)))
218 (cons token
219 (if (and ebnf-yac-ignore-error-recovery ebnf-yac-error)
220 ;; ignore error recovery
222 (ebnf-token-sequence seq)))))
225 ;;; Factor = Name
226 ;;; | "'" "character" "'"
227 ;;; | "error"
228 ;;; | "{" "C like commands" "}"
229 ;;; .
231 (defun ebnf-yac-factor (token)
232 (cond
233 ;; 'character'
234 ((eq token 'terminal)
235 (ebnf-make-terminal ebnf-yac-lex))
236 ;; Name
237 ((eq token 'non-terminal)
238 (ebnf-make-non-terminal ebnf-yac-lex))
239 ;; "error"
240 ((eq token 'yac-error)
241 (ebnf-make-special ebnf-yac-lex))
242 ;; not a factor
244 nil)
248 ;;; Name-List = { Name || "," }*.
250 (defun ebnf-yac-name-list (token)
251 (let (names)
252 (when (eq token 'non-terminal)
253 (while (progn
254 (setq names (cons ebnf-yac-lex names)
255 token (ebnf-yac-lex))
256 (eq token 'comma))
257 (or (eq (ebnf-yac-lex) 'non-terminal)
258 (error "Missing token name"))))
259 (cons token names)))
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ;; Lexical analyzer
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:
275 (mapc
276 #'(lambda (char)
277 (aset table char 'non-terminal))
278 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
279 ;; printable characters:
280 (mapc
281 #'(lambda (char)
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)
303 table)
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)
319 'end-of-input
320 (let (token)
321 ;; skip spaces, code blocks and comments
322 (while (if (> (following-char) 255)
323 (progn
324 (setq token 'error)
325 nil)
326 (setq token (aref ebnf-yac-token-table (following-char)))
327 (cond
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))
334 ((eq token 'slash)
335 (ebnf-yac-handle-comment))
336 ((eq token 'form-feed)
337 (forward-char)
338 (setq ebnf-action 'form-feed))
339 (t nil)
341 (cond
342 ;; end of input
343 ((>= (point) ebnf-limit)
344 'end-of-input)
345 ;; error
346 ((eq token 'error)
347 (error "Invalid character"))
348 ;; "string"
349 ((eq token 'string)
350 (setq ebnf-yac-lex (ebnf-get-string))
351 'string)
352 ;; terminal: 'char'
353 ((eq token 'terminal)
354 (setq ebnf-yac-lex (ebnf-string " -&(-~" ?\' "terminal"))
355 '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)
360 'terminal)
361 ((string= ebnf-yac-lex "error")
362 (setq ebnf-yac-error t)
363 'yac-error)
365 'non-terminal)
367 ;; %% and Yacc pragmas (%TOKEN, %START, etc).
368 ((eq token 'yac-pragma)
369 (forward-char)
370 (cond
371 ;; Yacc separator
372 ((eq (following-char) ?%)
373 (forward-char)
374 'yac-separator)
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)
379 ("LEFT" . yac-token)
380 ("NONASSOC" . yac-token)
381 ("PREC" . yac-prec)))))
382 ;; other Yacc pragmas
384 'yac-pragma)
386 ;; miscellaneous
388 (forward-char)
389 token)
390 ))))
393 (defun ebnf-yac-skip-spaces ()
394 (skip-chars-forward
395 (if ebnf-yac-skip-char
396 "\n\r\t !#$&()*+-.0123456789=?@[\\\\]^_`~"
397 "\n\r\t ")
398 ebnf-limit)
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 ()
408 (forward-char)
409 (let ((pair 1))
410 (while (> pair 0)
411 (skip-chars-forward ebnf-yac-skip-chars ebnf-limit)
412 (cond
413 ((= (following-char) ?{)
414 (forward-char)
415 (setq pair (1+ pair)))
416 ((= (following-char) ?})
417 (forward-char)
418 (setq pair (1- pair)))
419 ((= (following-char) ?/)
420 (ebnf-yac-handle-comment))
421 ((= (following-char) ?\")
422 (ebnf-get-string))
423 ((= (following-char) ?\')
424 (ebnf-string " -&(-~" ?\' "character"))
426 (error "Invalid character"))
428 (ebnf-yac-skip-spaces))
431 (defun ebnf-yac-handle-comment ()
432 (forward-char)
433 (cond
434 ;; begin comment
435 ((= (following-char) ?*)
436 (ebnf-yac-skip-comment)
437 (ebnf-yac-skip-spaces))
438 ;; line comment
439 ((= (following-char) ?/)
440 (end-of-line)
441 (ebnf-yac-skip-spaces))
442 ;; no comment
443 (t nil)
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 ()
453 (forward-char)
454 (cond
455 ;; open EPS file
456 ((and ebnf-eps-executing (= (following-char) ?\[))
457 (ebnf-eps-add-context (ebnf-yac-eps-filename)))
458 ;; close EPS file
459 ((and ebnf-eps-executing (= (following-char) ?\]))
460 (ebnf-eps-remove-context (ebnf-yac-eps-filename)))
461 ;; EPS header
462 ((and ebnf-eps-executing (= (following-char) ?H))
463 (ebnf-eps-header-comment (ebnf-yac-eps-filename)))
464 ;; EPS footer
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))))
471 (let ((not-end t))
472 (while not-end
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) ?/)
479 ;; end of comment
480 (forward-char)
481 (setq not-end nil)))
483 (error "Invalid character"))
484 ))))
487 (defun ebnf-yac-eps-filename ()
488 (forward-char)
489 (buffer-substring-no-properties
490 (point)
491 (let ((chars (concat ebnf-yac-comment-chars "\n"))
492 found)
493 (while (not found)
494 (skip-chars-forward chars ebnf-limit)
495 (setq found
496 (cond ((>= (point) ebnf-limit)
497 (point))
498 ((= (following-char) ?*)
499 (skip-chars-forward "*" ebnf-limit)
500 (if (/= (following-char) ?\/)
502 (backward-char)
503 (point)))
505 (point))
507 found)))
510 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 (provide 'ebnf-yac)
516 ;; arch-tag: 8a96989c-0b1d-42ba-a020-b2901f9a2a4d
517 ;;; ebnf-yac.el ends here