1 ;;; ebnf-yac.el --- parser for Yacc/Bison
3 ;; Copyright (C) 1999, 2000, 2001 Free Sofware 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: <2002-07-09 11:43:10 jbarranquero>
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 Yacc/Bison.
37 ;; See ebnf2ps.el for documentation.
43 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
45 ;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List
46 ;; | "any other Yacc definition"
49 ;; YACC-Code = "any C definition".
51 ;; YACC-Rule = Name ":" Alternative ";".
53 ;; Alternative = { Sequence || "|" }*.
55 ;; Sequence = { Factor }*.
58 ;; | "'" "character" "'"
60 ;; | "{" "C like commands" "}"
63 ;; Name-List = { Name || "," }*.
65 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
67 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
68 ;; | "//" "any character" "\\n".
71 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 (defvar ebnf-yac-lex nil
80 "Value returned by `ebnf-yac-lex' function.")
83 (defvar ebnf-yac-token-list nil
84 "List of `%TOKEN' names.")
87 (defvar ebnf-yac-skip-char nil
88 "Non-nil means skip printable characters with no grammatical meaning.")
91 (defvar ebnf-yac-error nil
92 "Non-nil means \"error\" occurred.")
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 ;;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
101 ;;; YACC-Code = "any C definition".
103 (defun ebnf-yac-parser (start)
105 (let ((total (+ (- ebnf-limit start
) 1))
108 syntax-list token rule
)
110 (setq token
(ebnf-yac-lex))
111 (and (eq token
'end-of-input
)
112 (error "Invalid Yacc/Bison file format"))
113 (or (eq (ebnf-yac-definitions token
) 'yac-separator
)
114 (error "Missing `%%%%'"))
115 (setq token
(ebnf-yac-lex))
116 (while (not (memq token
'(end-of-input yac-separator
)))
119 (/ (* (- (point) bias
) 100.0) total
))
120 (setq token
(ebnf-yac-rule token
)
123 (or (ebnf-add-empty-rule-list rule
)
124 (setq syntax-list
(cons rule syntax-list
))))
129 ;;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List
130 ;;; | "any other Yacc definition"
133 (defun ebnf-yac-definitions (token)
134 (let ((ebnf-yac-skip-char t
))
135 (while (not (memq token
'(yac-separator end-of-input
)))
138 ;; "%token" [ "<" Name ">" ] Name-List
139 ((eq token
'yac-token
)
140 (setq token
(ebnf-yac-lex))
141 (when (eq token
'open-angle
)
142 (or (eq (ebnf-yac-lex) 'non-terminal
)
143 (error "Missing type name"))
144 (or (eq (ebnf-yac-lex) 'close-angle
)
145 (error "Missing `>'"))
146 (setq token
(ebnf-yac-lex)))
147 (setq token
(ebnf-yac-name-list token
)
148 ebnf-yac-token-list
(nconc (cdr token
)
149 ebnf-yac-token-list
))
151 ;; "any other Yacc definition"
158 ;;; YACC-Rule = Name ":" Alternative ";".
160 (defun ebnf-yac-rule (token)
161 (let ((header ebnf-yac-lex
)
164 (setq ebnf-action nil
)
165 (or (eq token
'non-terminal
)
166 (error "Invalid rule name"))
167 (or (eq (ebnf-yac-lex) 'colon
)
168 (error "Invalid rule: missing `:'"))
169 (setq body
(ebnf-yac-alternative))
170 (or (eq (car body
) 'period
)
171 (error "Invalid rule: missing `;'"))
172 (setq body
(cdr body
))
173 (ebnf-eps-add-production header
)
175 (ebnf-make-production header body action
))))
178 ;;; Alternative = { Sequence || "|" }*.
180 (defun ebnf-yac-alternative ()
182 (while (eq (car (setq sequence
(ebnf-yac-sequence)))
184 (and (setq sequence
(cdr sequence
))
185 (setq body
(cons sequence body
))))
186 (ebnf-token-alternative body sequence
)))
189 ;;; Sequence = { Factor }*.
191 (defun ebnf-yac-sequence ()
192 (let (ebnf-yac-error token seq factor
)
193 (while (setq token
(ebnf-yac-lex)
194 factor
(ebnf-yac-factor token
))
195 (setq seq
(cons factor seq
)))
198 ;; ignore error recovery
199 ((and ebnf-yac-ignore-error-recovery ebnf-yac-error
)
204 ;; sequence with only one element
209 (ebnf-make-sequence (nreverse seq
)))
214 ;;; | "'" "character" "'"
216 ;;; | "{" "C like commands" "}"
219 (defun ebnf-yac-factor (token)
222 ((eq token
'terminal
)
223 (ebnf-make-terminal ebnf-yac-lex
))
225 ((eq token
'non-terminal
)
226 (ebnf-make-non-terminal ebnf-yac-lex
))
228 ((eq token
'yac-error
)
229 (ebnf-make-special ebnf-yac-lex
))
236 ;;; Name-List = { Name || "," }*.
238 (defun ebnf-yac-name-list (token)
240 (when (eq token
'non-terminal
)
242 (setq names
(cons ebnf-yac-lex names
)
243 token
(ebnf-yac-lex))
245 (or (eq (ebnf-yac-lex) 'non-terminal
)
246 (error "Missing token name"))))
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;; Name = "[A-Za-z][A-Za-z0-9_.]*".
256 ;;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
257 ;;; | "//" "any character" "\\n".
259 (defconst ebnf-yac-token-table
260 ;; control character & 8-bit character are set to `error'
261 (let ((table (make-vector 256 'error
)))
262 ;; upper & lower case letters:
265 (aset table char
'non-terminal
))
266 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
267 ;; printable characters:
270 (aset table char
'character
))
271 "!#$&()*+-.0123456789=?@[\\]^_`~")
272 ;; Override space characters:
273 (aset table ?
\n 'space
) ; [NL] linefeed
274 (aset table ?
\r 'space
) ; [CR] carriage return
275 (aset table ?
\t 'space
) ; [HT] horizontal tab
276 (aset table ?\
'space
) ; [SP] space
277 ;; Override form feed character:
278 (aset table ?
\f 'form-feed
) ; [FF] form feed
279 ;; Override other lexical characters:
280 (aset table ?
< 'open-angle
)
281 (aset table ?
> 'close-angle
)
282 (aset table ?
, 'comma
)
283 (aset table ?%
'yac-pragma
)
284 (aset table ?
/ 'slash
)
285 (aset table ?\
{ 'yac-code
)
286 (aset table ?
\" 'string
)
287 (aset table ?
\' 'terminal
)
288 (aset table ?
: 'colon
)
289 (aset table ?|
'alternative
)
290 (aset table ?\
; 'period)
292 "Vector used to map characters to a lexical token.")
295 (defun ebnf-yac-initialize ()
296 "Initializations for Yacc/Bison parser."
297 (setq ebnf-yac-token-list nil
))
300 (defun ebnf-yac-lex ()
301 "Lexical analyser for Yacc/Bison.
303 Return a lexical token.
305 See documentation for variable `ebnf-yac-lex'."
306 (if (>= (point) ebnf-limit
)
309 ;; skip spaces, code blocks and comments
310 (while (if (> (following-char) 255)
314 (setq token
(aref ebnf-yac-token-table
(following-char)))
316 ((or (eq token
'space
)
317 (and ebnf-yac-skip-char
318 (eq token
'character
)))
319 (ebnf-yac-skip-spaces))
320 ((eq token
'yac-code
)
321 (ebnf-yac-skip-code))
323 (ebnf-yac-handle-comment))
324 ((eq token
'form-feed
)
326 (setq ebnf-action
'form-feed
))
331 ((>= (point) ebnf-limit
)
335 (error "Illegal character"))
338 (setq ebnf-yac-lex
(ebnf-get-string))
341 ((eq token
'terminal
)
342 (setq ebnf-yac-lex
(ebnf-string " -&(-~" ?
\' "terminal"))
344 ;; non-terminal, terminal or "error"
345 ((eq token
'non-terminal
)
346 (setq ebnf-yac-lex
(ebnf-buffer-substring "0-9A-Za-z_."))
347 (cond ((member ebnf-yac-lex ebnf-yac-token-list
)
349 ((string= ebnf-yac-lex
"error")
350 (setq ebnf-yac-error t
)
355 ;; %% and Yacc pragmas (%TOKEN, %START, etc).
356 ((eq token
'yac-pragma
)
360 ((eq (following-char) ?%
)
364 ((string= (upcase (ebnf-buffer-substring "0-9A-Za-z_")) "TOKEN")
366 ;; other Yacc pragmas
377 (defun ebnf-yac-skip-spaces ()
379 (if ebnf-yac-skip-char
380 "\n\r\t !#$&()*+-.0123456789=?@[\\\\]^_`~"
383 (< (point) ebnf-limit
))
386 ;; replace the range "\177-\377" (see `ebnf-range-regexp').
387 (defconst ebnf-yac-skip-chars
388 (ebnf-range-regexp "^{}/'\"\000-\010\013\016-\037" ?
\177 ?
\377))
391 (defun ebnf-yac-skip-code ()
395 (skip-chars-forward ebnf-yac-skip-chars ebnf-limit
)
397 ((= (following-char) ?
{)
399 (setq pair
(1+ pair
)))
400 ((= (following-char) ?
})
402 (setq pair
(1- pair
)))
403 ((= (following-char) ?
/)
404 (ebnf-yac-handle-comment))
405 ((= (following-char) ?
\")
407 ((= (following-char) ?
\')
408 (ebnf-string " -&(-~" ?
\' "character"))
410 (error "Illegal character"))
412 (ebnf-yac-skip-spaces))
415 (defun ebnf-yac-handle-comment ()
419 ((= (following-char) ?
*)
420 (ebnf-yac-skip-comment)
421 (ebnf-yac-skip-spaces))
423 ((= (following-char) ?
/)
425 (ebnf-yac-skip-spaces))
431 ;; replace the range "\177-\237" (see `ebnf-range-regexp').
432 (defconst ebnf-yac-comment-chars
433 (ebnf-range-regexp "^*\000-\010\013\016-\037" ?
\177 ?
\237))
436 (defun ebnf-yac-skip-comment ()
440 ((and ebnf-eps-executing
(= (following-char) ?\
[))
441 (ebnf-eps-add-context (ebnf-yac-eps-filename)))
443 ((and ebnf-eps-executing
(= (following-char) ?\
]))
444 (ebnf-eps-remove-context (ebnf-yac-eps-filename)))
445 ;; any other action in comment
447 (setq ebnf-action
(aref ebnf-comment-table
(following-char))))
451 (skip-chars-forward ebnf-yac-comment-chars ebnf-limit
)
452 (cond ((>= (point) ebnf-limit
)
453 (error "Missing end of comment: `*/'"))
454 ((= (following-char) ?
*)
455 (skip-chars-forward "*" ebnf-limit
)
456 (when (= (following-char) ?
/)
461 (error "Illegal character"))
465 (defun ebnf-yac-eps-filename ()
467 (buffer-substring-no-properties
469 (let ((chars (concat ebnf-yac-comment-chars
"\n"))
472 (skip-chars-forward chars ebnf-limit
)
474 (cond ((>= (point) ebnf-limit
)
476 ((= (following-char) ?
*)
477 (skip-chars-forward "*" ebnf-limit
)
478 (if (/= (following-char) ?\
/)
488 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
494 ;;; ebnf-yac.el ends here