1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
3 ;; Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Created: 26 Aug 2002
8 ;; X-RCS: $Id: semantic-grammar.wy,v 1.16 2005/09/30 20:20:27 zappo Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 %package semantic-grammar-wy
26 %provide semantic/grammar-wy
29 (defvar semantic-grammar-lex-c-char-re)
31 ;; Current parsed nonterminal name.
32 (defvar semantic-grammar-wy--nterm nil)
33 ;; Index of rule in a nonterminal clause.
34 (defvar semantic-grammar-wy--rindx nil)
42 %start prologue epilogue declaration nonterminal rule
44 %start put_names put_values use_names
48 %keyword DEFAULT-PREC "%default-prec"
49 %keyword NO-DEFAULT-PREC "%no-default-prec"
50 %keyword KEYWORD "%keyword"
51 %keyword LANGUAGEMODE "%languagemode"
53 %keyword NONASSOC "%nonassoc"
54 %keyword PACKAGE "%package"
55 %keyword EXPECTEDCONFLICTS "%expectedconflicts"
56 %keyword PROVIDE "%provide"
59 %keyword QUOTEMODE "%quotemode"
60 %keyword RIGHT "%right"
61 %keyword SCOPESTART "%scopestart"
62 %keyword START "%start"
63 %keyword TOKEN "%token"
65 %keyword USE-MACROS "%use-macros"
69 %token <string> STRING
71 %type <symbol> syntax ":?\\(\\sw\\|\\s_\\)+"
72 %token <symbol> SYMBOL
73 %token <symbol> PERCENT_PERCENT "\\`%%\\'"
75 %type <char> syntax semantic-grammar-lex-c-char-re
76 %token <char> CHARACTER
78 %type <qlist> matchdatatype sexp syntax "\\s'\\s-*("
79 %token <qlist> PREFIXED_LIST
81 %type <sexp> matchdatatype sexp syntax "\\="
84 ;; Don't generate these analyzers which needs special handling code.
85 %token <code> PROLOGUE "%{...%}"
86 %token <code> EPILOGUE "%%...EOF"
88 ;; Blocks & Parenthesis
90 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
91 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
92 %token <open-paren> LPAREN "("
93 %token <close-paren> RPAREN ")"
94 %token <open-paren> LBRACE "{"
95 %token <close-paren> RBRACE "}"
99 %token <punctuation> COLON ":"
100 %token <punctuation> SEMI ";"
101 %token <punctuation> OR "|"
102 %token <punctuation> LT "<"
103 %token <punctuation> GT ">"
115 ;;; Prologue/Epilogue
119 (CODE-TAG "prologue" nil)
124 (CODE-TAG "epilogue" nil)
136 | no_default_prec_decl
139 | expectedconflicts_decl
154 `(TAG "default-prec" 'assoc :value '("t"))
157 no_default_prec_decl:
159 `(TAG "default-prec" 'assoc :value '("nil"))
164 `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
169 `(PACKAGE-TAG ',$2 nil)
172 expectedconflicts_decl:
173 EXPECTEDCONFLICTS symbols
174 `(TAG ',(car $2) 'expectedconflicts :rest ',(cdr $2))
183 associativity token_type_opt items
184 `(TAG ',$1 'assoc :type ',$2 :value ',$3)
197 PUT put_name put_value
198 `(TAG ',$2 'put :value ',(list $3))
199 | PUT put_name put_value_list
200 `(TAG ',$2 'put :value ',$3)
201 | PUT put_name_list put_value
202 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
203 | PUT put_name_list put_value_list
204 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
209 (mapcar #'semantic-tag-name (EXPANDFULL $1 put_names))
218 ;; Must return a list of Semantic tags to EXPANDFULL!
229 (mapcar #'semantic-tag-code-detail (EXPANDFULL $1 put_values))
238 ;; Must return a list of Semantic tags to EXPANDFULL!
239 (CODE-TAG "put-value" $1)
249 `(TAG ',$2 'scopestart)
254 `(TAG ',$2 'quotemode)
259 `(TAG ',(car $2) 'start :rest ',(cdr $2))
263 KEYWORD SYMBOL string_value
264 `(TAG ',$2 'keyword :value ',$3)
268 TOKEN token_type_opt SYMBOL string_value
269 `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
270 | TOKEN token_type_opt symbols
271 `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
285 TYPE token_type plist_opt
286 `(TAG ',$2 'type :value ',$3)
296 (append (list $2) $1)
303 (mapcar #'semantic-tag-name (EXPANDFULL $1 use_names))
312 ;; Must return a list of Semantic tags to EXPANDFULL!
317 USE-MACROS SYMBOL use_name_list
318 `(TAG "macro" 'macro :type ',$2 :value ',$3)
326 ;; Return a Lisp readable form
351 (setq semantic-grammar-wy--nterm $1
352 semantic-grammar-wy--rindx 0)
354 (TAG $1 'nonterminal :children $4)
359 (apply #'nconc (nreverse $1))
371 (let* ((nterm semantic-grammar-wy--nterm)
372 (rindx semantic-grammar-wy--rindx)
374 comps prec action elt)
375 (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
383 (error "Duplicate %%prec in `%s:%d' rule" nterm rindx))
384 (setq prec (aref elt 0)))
387 ;; don't forget that rhs items are in reverse order, so
388 ;; the end-of-rule semantic action is the first item.
389 (if (or action comps)
391 (setq comps (cons elt comps)
392 ;; keep rule and action index synchronized
393 semantic-grammar-wy--rindx
394 (1+ semantic-grammar-wy--rindx))
395 ;; the end-of-rule action
396 (setq action (car elt))))
399 (setq comps (cons elt comps)))))
401 (TAG (format "%s:%d" nterm rindx) 'rule
402 :type (if comps "group" "empty")
403 :value comps :prec prec :expr action)))
413 (cons (vector $3) $1)
420 (format "(progn\n%s)"
422 (if (string-match "^{[\r\n\t ]*" s)
423 (setq s (substring s (match-end 0))))
424 (if (string-match "[\r\n\t ]*}$" s)
425 (setq s (substring s 0 (match-beginning 0))))
448 ;;; grammar.wy ends here