Do not use ranges in FSF copyright years in emacs-23 (do not merge to trunk)
[emacs.git] / etc / grammars / grammar.wy
blob55aae13a20339563ccb1d0ccbb70ebc4eec5cc4c
1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
2 ;;
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 ;;   2011, 2012 Free Software Foundation, Inc.
5 ;;
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Maintainer: David Ponce <david@dponce.com>
8 ;; Created: 26 Aug 2002
9 ;; Keywords: syntax
10 ;; X-RCS: $Id: semantic-grammar.wy,v 1.16 2005/09/30 20:20:27 zappo Exp $
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/>.
28   ;; Current parsed nonterminal name.
29   (defvar semantic-grammar-wy--nterm nil)
30   ;; Index of rule in a nonterminal clause.
31   (defvar semantic-grammar-wy--rindx nil)
32   }
34 %package semantic-grammar-wy
36 %languagemode wy-mode
38 ;; Main
39 %start grammar
40 ;; Reparse
41 %start prologue epilogue declaration nonterminal rule
42 ;; EXPANDFULL
43 %start put_names put_values use_names
45 ;; Keywords
46 %type    <keyword>
47 %keyword DEFAULT-PREC    "%default-prec"
48 %keyword NO-DEFAULT-PREC "%no-default-prec"
49 %keyword KEYWORD         "%keyword"
50 %keyword LANGUAGEMODE    "%languagemode"
51 %keyword LEFT            "%left"
52 %keyword NONASSOC        "%nonassoc"
53 %keyword PACKAGE         "%package"
54 %keyword PREC            "%prec"
55 %keyword PUT             "%put"
56 %keyword QUOTEMODE       "%quotemode"
57 %keyword RIGHT           "%right"
58 %keyword SCOPESTART      "%scopestart"
59 %keyword START           "%start"
60 %keyword TOKEN           "%token"
61 %keyword TYPE            "%type"
62 %keyword USE-MACROS      "%use-macros"
64 ;; Literals
65 %type  <string>
66 %token <string>      STRING
68 %type  <symbol>      syntax ":?\\(\\sw\\|\\s_\\)+"
69 %token <symbol>      SYMBOL
70 %token <symbol>      PERCENT_PERCENT "\\`%%\\'"
72 %type  <char>        syntax semantic-grammar-lex-c-char-re
73 %token <char>        CHARACTER
75 %type  <qlist>       matchdatatype sexp syntax "\\s'\\s-*("
76 %token <qlist>       PREFIXED_LIST
78 %type  <sexp>        matchdatatype sexp syntax "\\="
79 %token <sexp>        SEXP
81 ;; Don't generate these analyzers which needs special handling code.
82 %token <code>        PROLOGUE "%{...%}"
83 %token <code>        EPILOGUE "%%...EOF"
85 ;; Blocks & Parenthesis
86 %type  <block>
87 %token <block>       PAREN_BLOCK "(LPAREN RPAREN)"
88 %token <block>       BRACE_BLOCK "(LBRACE RBRACE)"
89 %token <open-paren>  LPAREN      "("
90 %token <close-paren> RPAREN      ")"
91 %token <open-paren>  LBRACE      "{"
92 %token <close-paren> RBRACE      "}"
94 ;; Punctuations
95 %type  <punctuation>
96 %token <punctuation> COLON       ":"
97 %token <punctuation> SEMI        ";"
98 %token <punctuation> OR          "|"
99 %token <punctuation> LT          "<"
100 %token <punctuation> GT          ">"
104 grammar:
105     prologue
106   | epilogue
107   | declaration
108   | nonterminal
109   | PERCENT_PERCENT
110   ;
112 ;;; Prologue/Epilogue
114 prologue:
115     PROLOGUE
116     (CODE-TAG "prologue" nil)
117   ;
119 epilogue:
120     EPILOGUE
121     (CODE-TAG "epilogue" nil)
122   ;
124 ;;; Declarations
126 declaration:
127     decl
128     (eval $1)
129   ;
131 decl:
132     default_prec_decl
133   | no_default_prec_decl
134   | languagemode_decl
135   | package_decl
136   | precedence_decl
137   | put_decl
138   | quotemode_decl
139   | scopestart_decl
140   | start_decl
141   | keyword_decl
142   | token_decl
143   | type_decl
144   | use_macros_decl
145   ;
147 default_prec_decl:
148     DEFAULT-PREC
149     `(TAG "default-prec" 'assoc :value '("t"))
150   ;
152 no_default_prec_decl:
153     NO-DEFAULT-PREC
154     `(TAG "default-prec" 'assoc :value '("nil"))
155   ;
157 languagemode_decl:
158     LANGUAGEMODE symbols
159     `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
160   ;
162 package_decl:
163     PACKAGE SYMBOL
164     `(PACKAGE-TAG ',$2 nil)
165   ;
167 precedence_decl:
168     associativity token_type_opt items
169     `(TAG ',$1 'assoc :type ',$2 :value ',$3)
170   ;
172 associativity:
173     LEFT
174     (progn "left")
175   | RIGHT
176     (progn "right")
177   | NONASSOC
178     (progn "nonassoc")
179   ;
181 put_decl:
182     PUT put_name put_value
183     `(TAG ',$2 'put :value ',(list $3))
184   | PUT put_name put_value_list
185     `(TAG ',$2 'put :value ',$3)
186   | PUT put_name_list put_value
187     `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
188   | PUT put_name_list put_value_list
189     `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
190   ;
192 put_name_list:
193     BRACE_BLOCK
194     (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
195   ;
197 put_names:
198     LBRACE
199     ()
200   | RBRACE
201     ()
202   | put_name
203  ;; Must return a list of Semantic tags to EXPANDFULL!
204     (TAG $1 'put-name)
205   ;
207 put_name:
208     SYMBOL
209   | token_type
210   ;
212 put_value_list:
213     BRACE_BLOCK
214     (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
215   ;
217 put_values:
218     LBRACE
219     ()
220   | RBRACE
221     ()
222   | put_value
223  ;; Must return a list of Semantic tags to EXPANDFULL!
224     (CODE-TAG "put-value" $1)
225   ;
227 put_value:
228     SYMBOL any_value
229     (cons $1 $2)
230   ;
232 scopestart_decl:
233     SCOPESTART SYMBOL
234     `(TAG ',$2 'scopestart)
235   ;
237 quotemode_decl:
238     QUOTEMODE SYMBOL
239     `(TAG ',$2 'quotemode)
240   ;
242 start_decl:
243     START symbols
244     `(TAG ',(car $2) 'start :rest ',(cdr $2))
245   ;
247 keyword_decl:
248     KEYWORD SYMBOL string_value
249     `(TAG ',$2 'keyword :value ',$3)
250   ;
252 token_decl:
253     TOKEN token_type_opt SYMBOL string_value
254     `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
255   | TOKEN token_type_opt symbols
256     `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
257   ;
259 token_type_opt:
260  ;; EMPTY
261   | token_type
262   ;
264 token_type:
265     LT SYMBOL GT
266     (progn $2)
267   ;
269 type_decl:
270     TYPE token_type plist_opt
271     `(TAG ',$2 'type :value ',$3)
272   ;
274 plist_opt:
275  ;;EMPTY
276   | plist
277   ;
279 plist:
280     plist put_value
281     (append (list $2) $1)
282   | put_value
283     (list $1)
284   ;
286 use_name_list:
287     BRACE_BLOCK
288     (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
289   ;
291 use_names:
292     LBRACE
293     ()
294   | RBRACE
295     ()
296   | SYMBOL
297  ;; Must return a list of Semantic tags to EXPANDFULL!
298     (TAG $1 'use-name)
299   ;
301 use_macros_decl:
302     USE-MACROS SYMBOL use_name_list
303     `(TAG "macro" 'macro :type ',$2 :value ',$3)
304   ;
306 string_value:
307     STRING
308     (read $1)
309   ;
311 ;; Return a Lisp readable form
312 any_value:
313     SYMBOL
314   | STRING
315   | PAREN_BLOCK
316   | PREFIXED_LIST
317   | SEXP
318   ;
320 symbols:
321     lifo_symbols
322     (nreverse $1)
323   ;
325 lifo_symbols:
326     lifo_symbols SYMBOL
327     (cons $2 $1)
328   | SYMBOL
329     (list $1)
330   ;
332 ;;; Grammar rules
334 nonterminal:
335     SYMBOL
336     (setq semantic-grammar-wy--nterm $1
337           semantic-grammar-wy--rindx 0)
338     COLON rules SEMI
339     (TAG $1 'nonterminal :children $4)
340   ;
342 rules:
343     lifo_rules
344     (apply 'nconc (nreverse $1))
345   ;
347 lifo_rules:
348     lifo_rules OR rule
349     (cons $3 $1)
350   | rule
351     (list $1)
352   ;
354 rule:
355     rhs
356     (let* ((nterm semantic-grammar-wy--nterm)
357            (rindx semantic-grammar-wy--rindx)
358            (rhs   $1)
359            comps prec action elt)
360       (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
361       (while rhs
362         (setq elt (car rhs)
363               rhs (cdr rhs))
364         (cond
365          ;; precedence level
366          ((vectorp elt)
367           (if prec
368               (error "Duplicate %%prec in `%s:%d' rule" nterm rindx))
369           (setq prec (aref elt 0)))
370          ;; action
371          ((consp elt)
372           ;; don't forget that rhs items are in reverse order, so
373           ;; the end-of-rule semantic action is the first item.
374           (if (or action comps)
375               ;; a mid-rule action
376               (setq comps (cons elt comps)
377                     ;; keep rule and action index synchronized
378                     semantic-grammar-wy--rindx
379                     (1+ semantic-grammar-wy--rindx))
380             ;; the end-of-rule action
381             (setq action (car elt))))
382          ;; item
383          (t
384           (setq comps (cons elt comps)))))
385       (EXPANDTAG
386        (TAG (format "%s:%d" nterm rindx) 'rule
387             :type (if comps "group" "empty")
388             :value comps :prec prec :expr action)))
389   ;
391 rhs:
392  ;; EMPTY
393   | rhs item
394     (cons $2 $1)
395   | rhs action
396     (cons (list $2) $1)
397   | rhs PREC item
398     (cons (vector $3) $1)
399   ;
401 action:
402     PAREN_BLOCK
403   | PREFIXED_LIST
404   | BRACE_BLOCK
405     (format "(progn\n%s)"
406             (let ((s $1))
407               (if (string-match "^{[\r\n\t ]*" s)
408                   (setq s (substring s (match-end 0))))
409               (if (string-match "[\r\n\t ]*}$" s)
410                   (setq s (substring s 0 (match-beginning 0))))
411               s))
412   ;
414 items:
415     lifo_items
416     (nreverse $1)
417   ;
419 lifo_items:
420     lifo_items item
421     (cons $2 $1)
422   | item
423     (list $1)
424   ;
426 item:
427     SYMBOL
428   | CHARACTER
429   ;
433 ;;; grammar.wy ends here