Home | History | Annotate | Download | only in v3
      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 */
     30 tree grammar ANTLRv3Tree;
     31 
     32 options {
     33 	tokenVocab = ANTLRv3;
     34 	ASTLabelType = CommonTree;
     35 }
     36 
     37 @header {
     38 package org.antlr.grammar.v3;
     39 }
     40 
     41 grammarDef
     42     :   ^( grammarType ID DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ )
     43     ;
     44 
     45 grammarType
     46 	:	LEXER_GRAMMAR
     47     |	PARSER_GRAMMAR
     48     |	TREE_GRAMMAR
     49     |	COMBINED_GRAMMAR
     50     ;
     51 
     52 tokensSpec
     53 	:	^(TOKENS tokenSpec+)
     54 	;
     55 
     56 tokenSpec
     57 	:	^('=' TOKEN_REF STRING_LITERAL)
     58 	|	^('=' TOKEN_REF CHAR_LITERAL)
     59 	|	TOKEN_REF
     60 	;
     61 
     62 attrScope
     63 	:	^('scope' ID ACTION)
     64 	;
     65 
     66 action
     67 	:	^('@' ID ID ACTION)
     68 	|	^('@' ID ACTION)
     69 	;
     70 
     71 optionsSpec
     72 	:	^(OPTIONS option+)
     73 	;
     74 
     75 option
     76     :   qid // only allowed in element options
     77     |	^('=' ID optionValue)
     78  	;
     79 
     80 optionValue
     81     :   ID
     82     |   STRING_LITERAL
     83     |   CHAR_LITERAL
     84     |   INT
     85     ;
     86 
     87 rule
     88 	:	^( RULE ID modifier? (^(ARG ARG_ACTION))? (^(RET ARG_ACTION))?
     89 	       throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
     90 	       altList
     91 	       exceptionGroup? EOR
     92 	     )
     93 	;
     94 
     95 modifier
     96 	:	'protected'|'public'|'private'|'fragment'
     97 	;
     98 
     99 /** Match stuff like @init {int i;} */
    100 ruleAction
    101 	:	^('@' ID ACTION)
    102 	;
    103 
    104 throwsSpec
    105 	:	^('throws' ID+)
    106 	;
    107 
    108 ruleScopeSpec
    109 	:	^('scope' ACTION)
    110 	|	^('scope' ACTION ID+)
    111 	|	^('scope' ID+)
    112 	;
    113 
    114 block
    115     :   ^( BLOCK optionsSpec? (alternative rewrite)+ EOB )
    116     ;
    117 
    118 altList
    119     :   ^( BLOCK (alternative rewrite)+ EOB )
    120     ;
    121 
    122 alternative
    123     :   ^(ALT element+ EOA)
    124     |   ^(ALT EPSILON EOA)
    125     ;
    126 
    127 exceptionGroup
    128 	:	exceptionHandler+ finallyClause?
    129 	|	finallyClause
    130     ;
    131 
    132 exceptionHandler
    133     :    ^('catch' ARG_ACTION ACTION)
    134     ;
    135 
    136 finallyClause
    137     :    ^('finally' ACTION)
    138     ;
    139 
    140 element
    141 	:	^(('='|'+=') ID block)
    142 	|	^(('='|'+=') ID atom)
    143 	|	atom
    144 	|	ebnf
    145 	|   ACTION
    146 	|   SEMPRED
    147 	|	GATED_SEMPRED
    148 	|   ^(TREE_BEGIN element+)
    149 	;
    150 
    151 atom:   ^(('^'|'!') 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) */
    170 ebnf
    171 	:	^(SYNPRED block)
    172 	|	^(OPTIONAL block)
    173   	|	^(CLOSURE block)
    174    	|	^(POSITIVE_CLOSURE block)
    175 	|	SYN_SEMPRED
    176 	|	block
    177 	;
    178 
    179 notTerminal
    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 
    187 rewrite
    188 	:	(^('->' SEMPRED rewrite_alternative))* ^('->' rewrite_alternative)
    189 	|
    190 	;
    191 
    192 rewrite_alternative
    193 	:	rewrite_template
    194 	|	rewrite_tree_alternative
    195    	|   ^(ALT EPSILON EOA)
    196 	;
    197 
    198 rewrite_tree_block
    199     :   ^(BLOCK rewrite_tree_alternative EOB)
    200     ;
    201 
    202 rewrite_tree_alternative
    203     :	^(ALT rewrite_tree_element+ EOA)
    204     ;
    205 
    206 rewrite_tree_element
    207 	:	rewrite_tree_atom
    208 	|	rewrite_tree
    209 	|   rewrite_tree_block
    210 	|   rewrite_tree_ebnf
    211 	;
    212 
    213 rewrite_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 
    223 rewrite_tree_ebnf
    224 	:	^(OPTIONAL rewrite_tree_block)
    225   	|	^(CLOSURE rewrite_tree_block)
    226    	|	^(POSITIVE_CLOSURE rewrite_tree_block)
    227 	;
    228 
    229 rewrite_tree
    230 	:	^(TREE_BEGIN rewrite_tree_atom rewrite_tree_element* )
    231 	;
    232 
    233 rewrite_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={...}, ...) */
    243 rewrite_template_ref
    244 	:	^(TEMPLATE ID rewrite_template_args)
    245 	;
    246 
    247 /** ({expr})(a={...}, ...) */
    248 rewrite_indirect_template_head
    249 	:	^(TEMPLATE ACTION rewrite_template_args)
    250 	;
    251 
    252 rewrite_template_args
    253 	:	^(ARGLIST rewrite_template_arg+)
    254 	|	ARGLIST
    255 	;
    256 
    257 rewrite_template_arg
    258 	:   ^(ARG ID ACTION)
    259 	;
    260 
    261 qid	:	ID ('.' ID)* ;
    262