1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
3 ;; Copyright (C) 2002, 2003, 2004 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 26 Aug 2002
9 ;; X-RCS: $Id: semantic-grammar.wy,v 1.1 2007-11-26 15:10:38 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This software 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 GNU
21 ;; 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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
29 ;; Current parsed nonterminal name.
30 (defvar semantic-grammar-wy--nterm nil)
31 ;; Index of rule in a nonterminal clause.
32 (defvar semantic-grammar-wy--rindx nil)
35 ;;%package semantic-grammar-wy
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"
57 %keyword QUOTEMODE "%quotemode"
58 %keyword RIGHT "%right"
59 %keyword SCOPESTART "%scopestart"
60 %keyword START "%start"
61 %keyword TOKEN "%token"
63 %keyword USE-MACROS "%use-macros"
67 %token <string> STRING
69 %type <symbol> syntax ":?\\(\\sw\\|\\s_\\)+"
70 %token <symbol> SYMBOL
71 %token <symbol> PERCENT_PERCENT "\\`%%\\'"
73 %type <char> syntax semantic-grammar-lex-c-char-re
74 %token <char> CHARACTER
76 %type <qlist> matchdatatype sexp syntax "\\s'\\s-*("
77 %token <qlist> PREFIXED_LIST
79 %type <sexp> matchdatatype sexp syntax "\\="
82 ;; Don't generate these analyzers which needs special handling code.
83 %token <code> PROLOGUE "%{...%}"
84 %token <code> EPILOGUE "%%...EOF"
86 ;; Blocks & Parenthesis
88 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
89 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
90 %token <open-paren> LPAREN "("
91 %token <close-paren> RPAREN ")"
92 %token <open-paren> LBRACE "{"
93 %token <close-paren> RBRACE "}"
97 %token <punctuation> COLON ":"
98 %token <punctuation> SEMI ";"
99 %token <punctuation> OR "|"
100 %token <punctuation> LT "<"
101 %token <punctuation> GT ">"
113 ;;; Prologue/Epilogue
117 (CODE-TAG "prologue" nil)
122 (CODE-TAG "epilogue" nil)
134 | no_default_prec_decl
150 `(TAG "default-prec" 'assoc :value '("t"))
153 no_default_prec_decl:
155 `(TAG "default-prec" 'assoc :value '("nil"))
160 `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
165 `(PACKAGE-TAG ',$2 nil)
169 associativity token_type_opt items
170 `(TAG ',$1 'assoc :type ',$2 :value ',$3)
183 PUT put_name put_value
184 `(TAG ',$2 'put :value ',(list $3))
185 | PUT put_name put_value_list
186 `(TAG ',$2 'put :value ',$3)
187 | PUT put_name_list put_value
188 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
189 | PUT put_name_list put_value_list
190 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
195 (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
204 ;; Must return a list of Semantic tags to EXPANDFULL!
215 (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
224 ;; Must return a list of Semantic tags to EXPANDFULL!
225 (CODE-TAG "put-value" $1)
235 `(TAG ',$2 'scopestart)
240 `(TAG ',$2 'quotemode)
245 `(TAG ',(car $2) 'start :rest ',(cdr $2))
249 KEYWORD SYMBOL string_value
250 `(TAG ',$2 'keyword :value ',$3)
254 TOKEN token_type_opt SYMBOL string_value
255 `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
256 | TOKEN token_type_opt symbols
257 `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
271 TYPE token_type plist_opt
272 `(TAG ',$2 'type :value ',$3)
282 (append (list $2) $1)
289 (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
298 ;; Must return a list of Semantic tags to EXPANDFULL!
303 USE-MACROS SYMBOL use_name_list
304 `(TAG "macro" 'macro :type ',$2 :value ',$3)
312 ;; Return a Lisp readable form
337 (setq semantic-grammar-wy--nterm $1
338 semantic-grammar-wy--rindx 0)
340 (TAG $1 'nonterminal :children $4)
345 (apply 'nconc (nreverse $1))
357 (let* ((nterm semantic-grammar-wy--nterm)
358 (rindx semantic-grammar-wy--rindx)
360 comps prec action elt)
361 (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
369 (error "duplicate %%prec in `%s:%d' rule" nterm rindx))
370 (setq prec (aref elt 0)))
373 ;; don't forget that rhs items are in reverse order, so
374 ;; the end-of-rule semantic action is the first item.
375 (if (or action comps)
377 (setq comps (cons elt comps)
378 ;; keep rule and action index synchronized
379 semantic-grammar-wy--rindx
380 (1+ semantic-grammar-wy--rindx))
381 ;; the end-of-rule action
382 (setq action (car elt))))
385 (setq comps (cons elt comps)))))
387 (TAG (format "%s:%d" nterm rindx) 'rule
388 :type (if comps "group" "empty")
389 :value comps :prec prec :expr action)))
399 (cons (vector $3) $1)
406 (format "(progn\n%s)"
408 (if (string-match "^{[\r\n\t ]*" s)
409 (setq s (substring s (match-end 0))))
410 (if (string-match "[\r\n\t ]*}$" s)
411 (setq s (substring s 0 (match-beginning 0))))
434 (define-lex semantic-grammar-lexer
435 "Lexical analyzer that handles Semantic grammar buffers.
436 It ignores whitespaces, newlines and comments."
437 semantic-lex-ignore-newline
438 semantic-lex-ignore-whitespace
439 ;; Must detect prologue/epilogue before other symbols/keywords!
440 semantic-grammar-lex-prologue
441 semantic-grammar-lex-epilogue
442 semantic-grammar-wy--<keyword>-keyword-analyzer
443 semantic-grammar-wy--<symbol>-regexp-analyzer
444 semantic-grammar-wy--<char>-regexp-analyzer
445 semantic-grammar-wy--<string>-sexp-analyzer
446 ;; Must detect comments after strings because `comment-start-skip'
447 ;; regexp match semicolons inside strings!
448 semantic-lex-ignore-comments
449 ;; Must detect prefixed list before punctuation because prefix chars
450 ;; are also punctuations!
451 semantic-grammar-wy--<qlist>-sexp-analyzer
452 ;; Must detect punctuations after comments because the semicolon can
453 ;; be a punctuation or a comment start!
454 semantic-grammar-wy--<punctuation>-string-analyzer
455 semantic-grammar-wy--<block>-block-analyzer
456 semantic-grammar-wy--<sexp>-sexp-analyzer
459 ;;; semantic-grammar.wy ends here