Initial Commit
[packages] / xemacs-packages / semantic / wisent / wisent-grammar-macros.el
1 ;;; wisent-grammar-macros.el --- Semantic macros for LALR grammars
2 ;;
3 ;; Copyright (C) 2003 David Ponce
4 ;;
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 02 Aug 2003
8 ;; Keywords: syntax
9 ;; X-RCS: $Id: wisent-grammar-macros.el,v 1.1 2007-11-26 15:12:31 michaels Exp $
10 ;;
11 ;; This file is not part of GNU Emacs.
12 ;;
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.
17 ;;
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.
22 ;;
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.
27
28 ;;; Commentary:
29 ;;
30 ;; This library defines the default set of Semantic grammar macros
31 ;; used in wisent (.wy) grammars.
32
33 ;;; History:
34 ;;
35
36 ;;; Code:
37
38 (defsubst wisent-grammar-region-placeholder (symb)
39   "Given a $N placeholder symbol in SYMB, return a $regionN symbol.
40 Return nil if $N is not a valid placeholder symbol."
41   (let ((n (symbol-name symb)))
42     (if (string-match "^[$]\\([1-9][0-9]*\\)$" n)
43         (intern (concat "$region" (match-string 1 n))))))
44
45 (defun wisent-grammar-EXPAND (symb nonterm)
46   "Expand call to EXPAND grammar macro.
47 Return the form to parse from within a nonterminal.
48 SYMB is a $I placeholder symbol that gives the bounds of the area to
49 parse.
50 NONTERM is the nonterminal symbol to start with."
51   (unless (member nonterm (semantic-grammar-start))
52     (error "EXPANDFULL macro called with %s, but not used with %%start"
53            nonterm))
54   (let (($ri (wisent-grammar-region-placeholder symb)))
55     (if $ri
56         `(semantic-bovinate-from-nonterminal
57           (car ,$ri) (cdr ,$ri) ',nonterm)
58       (error "Invalid form (EXPAND %s %s)" symb nonterm))))
59
60 (defun wisent-grammar-EXPANDFULL (symb nonterm)
61   "Expand call to EXPANDFULL grammar macro.
62 Return the form to recursively parse an area.
63 SYMB is a $I placeholder symbol that gives the bounds of the area.
64 NONTERM is the nonterminal symbol to start with."
65   (unless (member nonterm (semantic-grammar-start))
66     (error "EXPANDFULL macro called with %s, but not used with %%start"
67            nonterm))
68   (let (($ri (wisent-grammar-region-placeholder symb)))
69     (if $ri
70         `(semantic-parse-region
71           (car ,$ri) (cdr ,$ri) ',nonterm 1)
72       (error "Invalid form (EXPANDFULL %s %s)" symb nonterm))))
73
74 (defun wisent-grammar-TAG (name class &rest attributes)
75   "Expand call to TAG grammar macro.
76 Return the form to create a generic semantic tag.
77 See the function `semantic-tag' for the meaning of arguments NAME,
78 CLASS and ATTRIBUTES."
79   `(wisent-raw-tag
80     (semantic-tag ,name ,class ,@attributes)))
81
82 (defun wisent-grammar-VARIABLE-TAG (name type default-value &rest attributes)
83   "Expand call to VARIABLE-TAG grammar macro.
84 Return the form to create a semantic tag of class variable.
85 See the function `semantic-tag-new-variable' for the meaning of
86 arguments NAME, TYPE, DEFAULT-VALUE and ATTRIBUTES."
87   `(wisent-raw-tag
88     (semantic-tag-new-variable ,name ,type ,default-value ,@attributes)))
89
90 (defun wisent-grammar-FUNCTION-TAG (name type arg-list &rest attributes)
91   "Expand call to FUNCTION-TAG grammar macro.
92 Return the form to create a semantic tag of class function.
93 See the function `semantic-tag-new-function' for the meaning of
94 arguments NAME, TYPE, ARG-LIST and ATTRIBUTES."
95   `(wisent-raw-tag
96     (semantic-tag-new-function ,name ,type ,arg-list ,@attributes)))
97
98 (defun wisent-grammar-TYPE-TAG (name type members parents &rest attributes)
99   "Expand call to TYPE-TAG grammar macro.
100 Return the form to create a semantic tag of class type.
101 See the function `semantic-tag-new-type' for the meaning of arguments
102 NAME, TYPE, MEMBERS, PARENTS and ATTRIBUTES."
103   `(wisent-raw-tag
104     (semantic-tag-new-type ,name ,type ,members ,parents ,@attributes)))
105
106 (defun wisent-grammar-INCLUDE-TAG (name system-flag &rest attributes)
107   "Expand call to INCLUDE-TAG grammar macro.
108 Return the form to create a semantic tag of class include.
109 See the function `semantic-tag-new-include' for the meaning of
110 arguments NAME, SYSTEM-FLAG and ATTRIBUTES."
111   `(wisent-raw-tag
112     (semantic-tag-new-include ,name ,system-flag ,@attributes)))
113
114 (defun wisent-grammar-PACKAGE-TAG (name detail &rest attributes)
115   "Expand call to PACKAGE-TAG grammar macro.
116 Return the form to create a semantic tag of class package.
117 See the function `semantic-tag-new-package' for the meaning of
118 arguments NAME, DETAIL and ATTRIBUTES."
119   `(wisent-raw-tag
120     (semantic-tag-new-package ,name ,detail ,@attributes)))
121
122 (defun wisent-grammar-CODE-TAG (name detail &rest attributes)
123   "Expand call to CODE-TAG grammar macro.
124 Return the form to create a semantic tag of class code.
125 See the function `semantic-tag-new-code' for the meaning of arguments
126 NAME, DETAIL and ATTRIBUTES."
127   `(wisent-raw-tag
128     (semantic-tag-new-code ,name ,detail ,@attributes)))
129
130 (defun wisent-grammar-ALIAS-TAG (name aliasclass definition &rest attributes)
131   "Expand call to ALIAS-TAG grammar macro.
132 Return the form to create a semantic tag of class alias.
133 See the function `semantic-tag-new-alias' for the meaning of arguments
134 NAME, ALIASCLASS, DEFINITION and ATTRIBUTES."
135   `(wisent-raw-tag
136     (semantic-tag-new-alias ,name ,aliasclass ,definition ,@attributes)))
137
138 (defun wisent-grammar-EXPANDTAG (raw-tag)
139   "Expand call to EXPANDTAG grammar macro.
140 Return the form to produce a list of cooked tags from raw form of
141 Semantic tag RAW-TAG."
142   `(wisent-cook-tag ,raw-tag))
143
144 (defun wisent-grammar-AST-ADD (ast &rest nodes)
145   "Expand call to AST-ADD grammar macro.
146 Return the form to update the abstract syntax tree AST with NODES.
147 See also the function `semantic-ast-add'."
148   `(semantic-ast-add ,ast ,@nodes))
149
150 (defun wisent-grammar-AST-PUT (ast &rest nodes)
151   "Expand call to AST-PUT grammar macro.
152 Return the form to update the abstract syntax tree AST with NODES.
153 See also the function `semantic-ast-put'."
154   `(semantic-ast-put ,ast ,@nodes))
155
156 (defun wisent-grammar-AST-GET (ast node)
157   "Expand call to AST-GET grammar macro.
158 Return the form to get, from the abstract syntax tree AST, the value
159 of NODE.
160 See also the function `semantic-ast-get'."
161   `(semantic-ast-get ,ast ,node))
162
163 (defun wisent-grammar-AST-GET1 (ast node)
164   "Expand call to AST-GET1 grammar macro.
165 Return the form to get, from the abstract syntax tree AST, the first
166 value of NODE.
167 See also the function `semantic-ast-get1'."
168   `(semantic-ast-get1 ,ast ,node))
169
170 (defun wisent-grammar-AST-GET-STRING (ast node)
171   "Expand call to AST-GET-STRING grammar macro.
172 Return the form to get, from the abstract syntax tree AST, the value
173 of NODE as a string.
174 See also the function `semantic-ast-get-string'."
175   `(semantic-ast-get-string ,ast ,node))
176
177 (defun wisent-grammar-AST-MERGE (ast1 ast2)
178   "Expand call to AST-MERGE grammar macro.
179 Return the form to merge the abstract syntax trees AST1 and AST2.
180 See also the function `semantic-ast-merge'."
181   `(semantic-ast-merge ,ast1 ,ast2))
182
183 (defun wisent-grammar-SKIP-BLOCK (&optional symb)
184   "Expand call to SKIP-BLOCK grammar macro.
185 Return the form to skip a parenthesized block.
186 Optional argument SYMB is a $I placeholder symbol that gives the
187 bounds of the block to skip.  By default, skip the block at `$1'.
188 See also the function `wisent-skip-block'."
189   (let ($ri)
190     (when symb
191       (unless (setq $ri (wisent-grammar-region-placeholder symb))
192         (error "Invalid form (SKIP-BLOCK %s)" symb)))
193     `(wisent-skip-block ,$ri)))
194
195 (defun wisent-grammar-SKIP-TOKEN ()
196   "Expand call to SKIP-TOKEN grammar macro.
197 Return the form to skip the lookahead token.
198 See also the function `wisent-skip-token'."
199   `(wisent-skip-token))
200
201 (defvar-mode-local wisent-grammar-mode semantic-grammar-macros
202   '(
203     (ASSOC          . semantic-grammar-ASSOC)
204     (EXPAND         . wisent-grammar-EXPAND)
205     (EXPANDFULL     . wisent-grammar-EXPANDFULL)
206     (TAG            . wisent-grammar-TAG)
207     (VARIABLE-TAG   . wisent-grammar-VARIABLE-TAG)
208     (FUNCTION-TAG   . wisent-grammar-FUNCTION-TAG)
209     (TYPE-TAG       . wisent-grammar-TYPE-TAG)
210     (INCLUDE-TAG    . wisent-grammar-INCLUDE-TAG)
211     (PACKAGE-TAG    . wisent-grammar-PACKAGE-TAG)
212     (EXPANDTAG      . wisent-grammar-EXPANDTAG)
213     (CODE-TAG       . wisent-grammar-CODE-TAG)
214     (ALIAS-TAG      . wisent-grammar-ALIAS-TAG)
215     (AST-ADD        . wisent-grammar-AST-ADD)
216     (AST-PUT        . wisent-grammar-AST-PUT)
217     (AST-GET        . wisent-grammar-AST-GET)
218     (AST-GET1       . wisent-grammar-AST-GET1)
219     (AST-GET-STRING . wisent-grammar-AST-GET-STRING)
220     (AST-MERGE      . wisent-grammar-AST-MERGE)
221     (SKIP-BLOCK     . wisent-grammar-SKIP-BLOCK)
222     (SKIP-TOKEN     . wisent-grammar-SKIP-TOKEN)
223     )
224   "Semantic grammar macros used in wisent grammars.")
225
226 (provide 'wisent-grammar-macros)
227
228 ;;; wisent-grammar-macros.el ends here