ANTLRv3Tree.g revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 [The "BSD license"]
3 Copyright (c) 2010 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15    derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29/** ANTLR v3 tree grammar to walk trees created by ANTLRv3.g */
30tree grammar ANTLRv3Tree;
31
32options {
33	tokenVocab = ANTLRv3;
34	ASTLabelType = CommonTree;
35}
36
37@header {
38package org.antlr.grammar.v3;
39}
40
41grammarDef
42    :   ^( grammarType ID DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ )
43    ;
44
45grammarType
46	:	LEXER_GRAMMAR
47    |	PARSER_GRAMMAR
48    |	TREE_GRAMMAR
49    |	COMBINED_GRAMMAR
50    ;
51
52tokensSpec
53	:	^(TOKENS tokenSpec+)
54	;
55
56tokenSpec
57	:	^('=' TOKEN_REF STRING_LITERAL)
58	|	^('=' TOKEN_REF CHAR_LITERAL)
59	|	TOKEN_REF
60	;
61
62attrScope
63	:	^('scope' ID ACTION)
64	;
65
66action
67	:	^('@' ID ID ACTION)
68	|	^('@' ID ACTION)
69	;
70
71optionsSpec
72	:	^(OPTIONS option+)
73	;
74
75option
76    :   qid // only allowed in element options
77    |	^('=' ID optionValue)
78 	;
79
80optionValue
81    :   ID
82    |   STRING_LITERAL
83    |   CHAR_LITERAL
84    |   INT
85    ;
86
87rule
88	:	^( RULE ID modifier? (^(ARG ARG_ACTION))? (^(RET ARG_ACTION))?
89	       throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
90	       altList
91	       exceptionGroup? EOR
92	     )
93	;
94
95modifier
96	:	'protected'|'public'|'private'|'fragment'
97	;
98
99/** Match stuff like @init {int i;} */
100ruleAction
101	:	^('@' ID ACTION)
102	;
103
104throwsSpec
105	:	^('throws' ID+)
106	;
107
108ruleScopeSpec
109	:	^('scope' ACTION)
110	|	^('scope' ACTION ID+)
111	|	^('scope' ID+)
112	;
113
114block
115    :   ^( BLOCK optionsSpec? (alternative rewrite)+ EOB )
116    ;
117
118altList
119    :   ^( BLOCK (alternative rewrite)+ EOB )
120    ;
121
122alternative
123    :   ^(ALT element+ EOA)
124    |   ^(ALT EPSILON EOA)
125    ;
126
127exceptionGroup
128	:	exceptionHandler+ finallyClause?
129	|	finallyClause
130    ;
131
132exceptionHandler
133    :    ^('catch' ARG_ACTION ACTION)
134    ;
135
136finallyClause
137    :    ^('finally' ACTION)
138    ;
139
140element
141	:	^(('='|'+=') ID block)
142	|	^(('='|'+=') ID atom)
143	|	atom
144	|	ebnf
145	|   ACTION
146	|   SEMPRED
147	|	GATED_SEMPRED
148	|   ^(TREE_BEGIN element+)
149	;
150
151atom:   ^(('^'|'!') atom)
152	|	^(CHAR_RANGE CHAR_LITERAL CHAR_LITERAL optionsSpec?)
153	|	^('~' notTerminal optionsSpec?)
154	|	^('~' block optionsSpec?)
155    |	^(RULE_REF ARG_ACTION)
156    |	RULE_REF
157    |   CHAR_LITERAL
158    |   ^(CHAR_LITERAL optionsSpec)
159    |	TOKEN_REF
160    |	^(TOKEN_REF optionsSpec)
161    |	^(TOKEN_REF ARG_ACTION optionsSpec)
162    |	^(TOKEN_REF ARG_ACTION)
163    |	STRING_LITERAL
164    |	^(STRING_LITERAL optionsSpec)
165    |	'.'
166    |	^('.' optionsSpec?)
167    ;
168
169/** Matches ENBF blocks (and token sets via block rule) */
170ebnf
171	:	^(SYNPRED block)
172	|	^(OPTIONAL block)
173  	|	^(CLOSURE block)
174   	|	^(POSITIVE_CLOSURE block)
175	|	SYN_SEMPRED
176	|	block
177	;
178
179notTerminal
180	:   CHAR_LITERAL
181	|	TOKEN_REF
182	|	STRING_LITERAL
183	;
184
185// R E W R I T E  S Y N T A X
186
187rewrite
188	:	(^('->' SEMPRED rewrite_alternative))* ^('->' rewrite_alternative)
189	|
190	;
191
192rewrite_alternative
193	:	rewrite_template
194	|	rewrite_tree_alternative
195   	|   ^(ALT EPSILON EOA)
196	;
197
198rewrite_tree_block
199    :   ^(BLOCK rewrite_tree_alternative EOB)
200    ;
201
202rewrite_tree_alternative
203    :	^(ALT rewrite_tree_element+ EOA)
204    ;
205
206rewrite_tree_element
207	:	rewrite_tree_atom
208	|	rewrite_tree
209	|   rewrite_tree_block
210	|   rewrite_tree_ebnf
211	;
212
213rewrite_tree_atom
214    :   CHAR_LITERAL
215	|   TOKEN_REF
216	|   ^(TOKEN_REF ARG_ACTION) // for imaginary nodes
217    |   RULE_REF
218	|   STRING_LITERAL
219	|   LABEL
220	|	ACTION
221	;
222
223rewrite_tree_ebnf
224	:	^(OPTIONAL rewrite_tree_block)
225  	|	^(CLOSURE rewrite_tree_block)
226   	|	^(POSITIVE_CLOSURE rewrite_tree_block)
227	;
228
229rewrite_tree
230	:	^(TREE_BEGIN rewrite_tree_atom rewrite_tree_element* )
231	;
232
233rewrite_template
234	:   ^( TEMPLATE ID rewrite_template_args
235		   (DOUBLE_QUOTE_STRING_LITERAL | DOUBLE_ANGLE_STRING_LITERAL)
236		 )
237	|	rewrite_template_ref
238	|	rewrite_indirect_template_head
239	|	ACTION
240	;
241
242/** foo(a={...}, ...) */
243rewrite_template_ref
244	:	^(TEMPLATE ID rewrite_template_args)
245	;
246
247/** ({expr})(a={...}, ...) */
248rewrite_indirect_template_head
249	:	^(TEMPLATE ACTION rewrite_template_args)
250	;
251
252rewrite_template_args
253	:	^(ARGLIST rewrite_template_arg+)
254	|	ARGLIST
255	;
256
257rewrite_template_arg
258	:   ^(ARG ID ACTION)
259	;
260
261qid	:	ID ('.' ID)* ;
262