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 language=Java; 34 tokenVocab = ANTLRv3; 35 ASTLabelType = CommonTree; 36} 37 38@header { 39package org.antlr.grammar.v3; 40} 41 42grammarDef 43 : ^( grammarType ID DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ ) 44 ; 45 46grammarType 47 : LEXER_GRAMMAR 48 | PARSER_GRAMMAR 49 | TREE_GRAMMAR 50 | COMBINED_GRAMMAR 51 ; 52 53tokensSpec 54 : ^(TOKENS tokenSpec+) 55 ; 56 57tokenSpec 58 : ^('=' TOKEN_REF STRING_LITERAL) 59 | ^('=' TOKEN_REF CHAR_LITERAL) 60 | TOKEN_REF 61 ; 62 63attrScope 64 : ^('scope' ID ACTION) 65 ; 66 67action 68 : ^('@' ID ID ACTION) 69 | ^('@' ID ACTION) 70 ; 71 72optionsSpec 73 : ^(OPTIONS option+) 74 ; 75 76option 77 : qid // only allowed in element options 78 | ^('=' ID optionValue) 79 ; 80 81optionValue 82 : ID 83 | STRING_LITERAL 84 | CHAR_LITERAL 85 | INT 86 ; 87 88rule 89 : ^( RULE ID modifier? (^(ARG ARG_ACTION))? (^(RET ARG_ACTION))? 90 throwsSpec? optionsSpec? ruleScopeSpec? ruleAction* 91 altList 92 exceptionGroup? EOR 93 ) 94 ; 95 96modifier 97 : 'protected'|'public'|'private'|'fragment' 98 ; 99 100/** Match stuff like @init {int i;} */ 101ruleAction 102 : ^('@' ID ACTION) 103 ; 104 105throwsSpec 106 : ^('throws' ID+) 107 ; 108 109ruleScopeSpec 110 : ^('scope' ACTION) 111 | ^('scope' ACTION ID+) 112 | ^('scope' ID+) 113 ; 114 115block 116 : ^( BLOCK optionsSpec? (alternative rewrite)+ EOB ) 117 ; 118 119altList 120 : ^( BLOCK (alternative rewrite)+ EOB ) 121 ; 122 123alternative 124 : ^(ALT element+ EOA) 125 | ^(ALT EPSILON EOA) 126 ; 127 128exceptionGroup 129 : exceptionHandler+ finallyClause? 130 | finallyClause 131 ; 132 133exceptionHandler 134 : ^('catch' ARG_ACTION ACTION) 135 ; 136 137finallyClause 138 : ^('finally' ACTION) 139 ; 140 141element 142 : ^(('='|'+=') ID block) 143 | ^(('='|'+=') ID atom) 144 | atom 145 | ebnf 146 | ACTION 147 | SEMPRED 148 | GATED_SEMPRED 149 | ^(TREE_BEGIN element+) 150 ; 151 152atom: ^(('^'|'!') atom) 153 | ^(CHAR_RANGE CHAR_LITERAL CHAR_LITERAL optionsSpec?) 154 | ^('~' notTerminal optionsSpec?) 155 | ^('~' block optionsSpec?) 156 | ^(RULE_REF ARG_ACTION) 157 | RULE_REF 158 | CHAR_LITERAL 159 | ^(CHAR_LITERAL optionsSpec) 160 | TOKEN_REF 161 | ^(TOKEN_REF optionsSpec) 162 | ^(TOKEN_REF ARG_ACTION optionsSpec) 163 | ^(TOKEN_REF ARG_ACTION) 164 | STRING_LITERAL 165 | ^(STRING_LITERAL optionsSpec) 166 | '.' 167 | ^('.' optionsSpec?) 168 ; 169 170/** Matches ENBF blocks (and token sets via block rule) */ 171ebnf 172 : ^(SYNPRED block) 173 | ^(OPTIONAL block) 174 | ^(CLOSURE block) 175 | ^(POSITIVE_CLOSURE block) 176 | SYN_SEMPRED 177 | block 178 ; 179 180notTerminal 181 : CHAR_LITERAL 182 | TOKEN_REF 183 | STRING_LITERAL 184 ; 185 186// R E W R I T E S Y N T A X 187 188rewrite 189 : (^('->' SEMPRED rewrite_alternative))* ^('->' rewrite_alternative) 190 | 191 ; 192 193rewrite_alternative 194 : rewrite_template 195 | rewrite_tree_alternative 196 | ^(ALT EPSILON EOA) 197 ; 198 199rewrite_tree_block 200 : ^(BLOCK rewrite_tree_alternative EOB) 201 ; 202 203rewrite_tree_alternative 204 : ^(ALT rewrite_tree_element+ EOA) 205 ; 206 207rewrite_tree_element 208 : rewrite_tree_atom 209 | rewrite_tree 210 | rewrite_tree_block 211 | rewrite_tree_ebnf 212 ; 213 214rewrite_tree_atom 215 : CHAR_LITERAL 216 | TOKEN_REF 217 | ^(TOKEN_REF ARG_ACTION) // for imaginary nodes 218 | RULE_REF 219 | STRING_LITERAL 220 | LABEL 221 | ACTION 222 ; 223 224rewrite_tree_ebnf 225 : ^(OPTIONAL rewrite_tree_block) 226 | ^(CLOSURE rewrite_tree_block) 227 | ^(POSITIVE_CLOSURE rewrite_tree_block) 228 ; 229 230rewrite_tree 231 : ^(TREE_BEGIN rewrite_tree_atom rewrite_tree_element* ) 232 ; 233 234rewrite_template 235 : ^( TEMPLATE ID rewrite_template_args 236 (DOUBLE_QUOTE_STRING_LITERAL | DOUBLE_ANGLE_STRING_LITERAL) 237 ) 238 | rewrite_template_ref 239 | rewrite_indirect_template_head 240 | ACTION 241 ; 242 243/** foo(a={...}, ...) */ 244rewrite_template_ref 245 : ^(TEMPLATE ID rewrite_template_args) 246 ; 247 248/** ({expr})(a={...}, ...) */ 249rewrite_indirect_template_head 250 : ^(TEMPLATE ACTION rewrite_template_args) 251 ; 252 253rewrite_template_args 254 : ^(ARGLIST rewrite_template_arg+) 255 | ARGLIST 256 ; 257 258rewrite_template_arg 259 : ^(ARG ID ACTION) 260 ; 261 262qid : ID ('.' ID)* ; 263