comparison lisp/progmodes/ebnf-yac.el @ 54140:766aaa5bded5

ABNF parser. Fix bug on productions like test = {"test"}* | ("tt" ["test"]). Reported by Markus Dreyer.
author Vinicius Jose Latorre <viniciusjl@ig.com.br>
date Wed, 25 Feb 2004 00:07:33 +0000
parents 695cf19ef79e
children 7d439240423b
comparison
equal deleted inserted replaced
54139:3aff710a91c0 54140:766aaa5bded5
1 ;;; ebnf-yac.el --- parser for Yacc/Bison 1 ;;; ebnf-yac.el --- parser for Yacc/Bison
2 2
3 ;; Copyright (C) 1999, 2000, 2001 Free Sofware Foundation, Inc. 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
4 4 ;; Free Sofware Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <vinicius@cpqd.com.br> 5
6 ;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.com.br> 6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Time-stamp: <2004/02/22 14:24:17 vinicius>
7 ;; Keywords: wp, ebnf, PostScript 9 ;; Keywords: wp, ebnf, PostScript
8 ;; Time-stamp: <2003-02-10 10:47:04 jbarranquero> 10 ;; Version: 1.2.1
9 ;; Version: 1.2
10 11
11 ;; This file is part of GNU Emacs. 12 ;; This file is part of GNU Emacs.
12 13
13 ;; GNU Emacs is free software; you can redistribute it and/or modify 14 ;; 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 ;; it under the terms of the GNU General Public License as published by
40 ;; Yacc/Bison Syntax 41 ;; Yacc/Bison Syntax
41 ;; ----------------- 42 ;; -----------------
42 ;; 43 ;;
43 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ]. 44 ;; YACC = { YACC-Definitions }* "%%" { YACC-Rule }* [ "%%" [ YACC-Code ] ].
44 ;; 45 ;;
45 ;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List 46 ;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
47 ;; [ "<" Name ">" ] Name-List
48 ;; | "%prec" Name
46 ;; | "any other Yacc definition" 49 ;; | "any other Yacc definition"
47 ;; . 50 ;; .
48 ;; 51 ;;
49 ;; YACC-Code = "any C definition". 52 ;; YACC-Code = "any C definition".
50 ;; 53 ;;
64 ;; 67 ;;
65 ;; Name = "[A-Za-z][A-Za-z0-9_.]*". 68 ;; Name = "[A-Za-z][A-Za-z0-9_.]*".
66 ;; 69 ;;
67 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/" 70 ;; Comment = "/*" "any character, but the sequence \"*/\"" "*/"
68 ;; | "//" "any character" "\\n". 71 ;; | "//" "any character" "\\n".
72 ;;
73 ;;
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.
77 ;;
78 ;;
79 ;; Acknowledgements
80 ;; ----------------
81 ;;
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.
69 ;; 85 ;;
70 ;; 86 ;;
71 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 87 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72 88
73 ;;; Code: 89 ;;; Code:
124 (setq syntax-list (cons rule syntax-list)))) 140 (setq syntax-list (cons rule syntax-list))))
125 (goto-char origin) 141 (goto-char origin)
126 syntax-list)) 142 syntax-list))
127 143
128 144
129 ;;; YACC-Definitions = "%token" [ "<" Name ">" ] Name-List 145 ;;; YACC-Definitions = ( "%token" | "%left" | "%right" | "%nonassoc" )
146 ;;; [ "<" Name ">" ] Name-List
147 ;;; | "%prec" Name
130 ;;; | "any other Yacc definition" 148 ;;; | "any other Yacc definition"
131 ;;; . 149 ;;; .
132 150
133 (defun ebnf-yac-definitions (token) 151 (defun ebnf-yac-definitions (token)
134 (let ((ebnf-yac-skip-char t)) 152 (let ((ebnf-yac-skip-char t))
135 (while (not (memq token '(yac-separator end-of-input))) 153 (while (not (memq token '(yac-separator end-of-input)))
136 (setq token 154 (setq token
137 (cond 155 (cond
138 ;; "%token" [ "<" Name ">" ] Name-List 156 ;; ( "%token" | "%left" | "%right" | "%nonassoc" )
157 ;; [ "<" Name ">" ] Name-List
139 ((eq token 'yac-token) 158 ((eq token 'yac-token)
140 (setq token (ebnf-yac-lex)) 159 (setq token (ebnf-yac-lex))
141 (when (eq token 'open-angle) 160 (when (eq token 'open-angle)
142 (or (eq (ebnf-yac-lex) 'non-terminal) 161 (or (eq (ebnf-yac-lex) 'non-terminal)
143 (error "Missing type name")) 162 (error "Missing type name"))
146 (setq token (ebnf-yac-lex))) 165 (setq token (ebnf-yac-lex)))
147 (setq token (ebnf-yac-name-list token) 166 (setq token (ebnf-yac-name-list token)
148 ebnf-yac-token-list (nconc (cdr token) 167 ebnf-yac-token-list (nconc (cdr token)
149 ebnf-yac-token-list)) 168 ebnf-yac-token-list))
150 (car token)) 169 (car token))
151 ;; "any other Yacc definition" 170 ;; "%prec" Name
171 ((eq token 'yac-prec)
172 (or (eq (ebnf-yac-lex) 'non-terminal)
173 (error "Missing prec name"))
174 (ebnf-yac-lex))
175 ;; "any other Yacc definition"
152 (t 176 (t
153 (ebnf-yac-lex)) 177 (ebnf-yac-lex))
154 ))) 178 )))
155 token)) 179 token))
156 180
358 (cond 382 (cond
359 ;; Yacc separator 383 ;; Yacc separator
360 ((eq (following-char) ?%) 384 ((eq (following-char) ?%)
361 (forward-char) 385 (forward-char)
362 'yac-separator) 386 'yac-separator)
363 ;; %TOKEN 387 ;; %TOKEN, %RIGHT, %LEFT, %PREC, %NONASSOC
364 ((string= (upcase (ebnf-buffer-substring "0-9A-Za-z_")) "TOKEN") 388 ((cdr (assoc (upcase (ebnf-buffer-substring "0-9A-Za-z_"))
365 'yac-token) 389 '(("TOKEN" . yac-token)
390 ("RIGHT" . yac-token)
391 ("LEFT" . yac-token)
392 ("NONASSOC" . yac-token)
393 ("PREC" . yac-prec)))))
366 ;; other Yacc pragmas 394 ;; other Yacc pragmas
367 (t 395 (t
368 'yac-pragma) 396 'yac-pragma)
369 )) 397 ))
370 ;; miscellaneous 398 ;; miscellaneous