Home | History | Annotate | Download | only in tool
      1 header {
      2 /*
      3  [The "BSD license"]
      4  Copyright (c) 2005-2008 Terence Parr
      5  All rights reserved.
      6 
      7  Redistribution and use in source and binary forms, with or without
      8  modification, are permitted provided that the following conditions
      9  are met:
     10  1. Redistributions of source code must retain the above copyright
     11     notice, this list of conditions and the following disclaimer.
     12  2. Redistributions in binary form must reproduce the above copyright
     13     notice, this list of conditions and the following disclaimer in the
     14     documentation and/or other materials provided with the distribution.
     15  3. The name of the author may not be used to endorse or promote products
     16     derived from this software without specific prior written permission.
     17 
     18  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 	package org.antlr.tool;
     30 	import java.util.*;
     31 	import org.antlr.analysis.*;
     32 	import org.antlr.misc.*;
     33 	import java.io.*;
     34 }
     35 
     36 class SerializerWalker extends TreeParser;
     37 
     38 options {
     39 	importVocab = ANTLR;
     40 	ASTLabelType = "GrammarAST";
     41     codeGenBitsetTestThreshold=999;
     42 }
     43 
     44 {
     45     public void reportError(RecognitionException ex) {
     46 		Token token = null;
     47 		if ( ex instanceof MismatchedTokenException ) {
     48 			token = ((MismatchedTokenException)ex).token;
     49 		}
     50 		else if ( ex instanceof NoViableAltException ) {
     51 			token = ((NoViableAltException)ex).token;
     52 		}
     53         ErrorManager.syntaxError(
     54             ErrorManager.MSG_SYNTAX_ERROR,
     55             grammar,
     56             token,
     57             "serialize: "+ex.toString(),
     58             ex);
     59     }
     60 
     61 protected Grammar grammar;
     62 protected String currentRuleName;
     63 protected GrammarSerializer out;
     64 }
     65 
     66 grammar[GrammarSerializer out]
     67 {
     68 	this.out = out;
     69 }
     70     :   #( LEXER_GRAMMAR 	grammarSpec[#grammar.getType()] )
     71 	|   #( PARSER_GRAMMAR   grammarSpec[#grammar.getType()] )
     72 	|   #( TREE_GRAMMAR     grammarSpec[#grammar.getType()] )
     73 	|   #( COMBINED_GRAMMAR grammarSpec[#grammar.getType()] )
     74     ;
     75 
     76 grammarSpec[int gtokentype]
     77 	:	id:ID {out.grammar(gtokentype, #id.getText());}
     78 		(cmt:DOC_COMMENT)?
     79 		(optionsSpec)?
     80         (delegateGrammars)?
     81         (tokensSpec)?
     82         (attrScope)*
     83         (AMPERSAND)* // skip actions
     84         rules
     85 	;
     86 
     87 attrScope
     88 	:	#( "scope" ID ACTION )
     89 	;
     90 
     91 optionsSpec
     92     :   #( OPTIONS (option)+ )
     93     ;
     94 
     95 option
     96     :   #( ASSIGN ID optionValue )
     97     ;
     98 
     99 optionValue
    100     :   id:ID
    101     |   s:STRING_LITERAL
    102     |   c:CHAR_LITERAL
    103     |   i:INT
    104     ;
    105 
    106 charSet
    107 	:   #( CHARSET charSetElement )
    108 	;
    109 
    110 charSetElement
    111 	:   c:CHAR_LITERAL
    112 	|   #( OR c1:CHAR_LITERAL c2:CHAR_LITERAL )
    113 	|   #( RANGE c3:CHAR_LITERAL c4:CHAR_LITERAL )
    114 	;
    115 
    116 delegateGrammars
    117 	:	#( "import"
    118             (   #(ASSIGN ID ID)
    119             |   ID
    120             )+
    121         )
    122 	;
    123 
    124 tokensSpec
    125 	:	#( TOKENS ( tokenSpec )+ )
    126 	;
    127 
    128 tokenSpec
    129 	:	t:TOKEN_REF
    130 	|	#( ASSIGN
    131 		   t2:TOKEN_REF
    132 		   ( s:STRING_LITERAL
    133 		   | c:CHAR_LITERAL
    134 		   )
    135 		 )
    136 	;
    137 
    138 rules
    139     :   ( rule )+
    140     ;
    141 
    142 rule
    143     :   #( RULE id:ID           {out.rule(#id.getText());}
    144            (m:modifier)?
    145            (ARG (ARG_ACTION)?)
    146            (RET (ARG_ACTION)?)
    147            (optionsSpec)?
    148            (ruleScopeSpec)?
    149        	   (AMPERSAND)*
    150            b:block
    151            (exceptionGroup)?
    152            EOR                  {out.endRule();}
    153          )
    154     ;
    155 
    156 modifier
    157 	:	"protected"
    158 	|	"public"
    159 	|	"private"
    160 	|	"fragment"
    161 	;
    162 
    163 ruleScopeSpec
    164  	:	#( "scope" (ACTION)? ( ID )* )
    165  	;
    166 
    167 block
    168     :   #(  BLOCK {out.block(#BLOCK.getNumberOfChildrenWithType(ALT));}
    169             (optionsSpec)?
    170             ( alternative rewrite )+
    171             EOB
    172          )
    173     ;
    174 
    175 alternative
    176     :   #( ALT {out.alt(#alternative);} (element)+ EOA {out.endAlt();} )
    177     ;
    178 
    179 exceptionGroup
    180 	:	( exceptionHandler )+ (finallyClause)?
    181 	|	finallyClause
    182     ;
    183 
    184 exceptionHandler
    185     :    #("catch" ARG_ACTION ACTION)
    186     ;
    187 
    188 finallyClause
    189     :    #("finally" ACTION)
    190     ;
    191 
    192 rewrite
    193 	:	( #( REWRITE (SEMPRED)? (ALT|TEMPLATE|ACTION|ETC) ) )*
    194 	;
    195 
    196 element
    197     :   #(ROOT element)
    198     |   #(BANG element)
    199     |   atom
    200     |   #(NOT {out.not();} element)
    201     |   #(RANGE atom atom)
    202     |   #(CHAR_RANGE {out.range();} atom atom)
    203     |	#(ASSIGN ID element)
    204     |	#(PLUS_ASSIGN ID element)
    205     |   ebnf
    206     |   tree
    207     |   #( SYNPRED block )
    208     |   FORCED_ACTION
    209     |   ACTION
    210     |   SEMPRED
    211     |   SYN_SEMPRED
    212     |   BACKTRACK_SEMPRED
    213     |   GATED_SEMPRED
    214     |   EPSILON
    215     ;
    216 
    217 ebnf:   block
    218     |   #( OPTIONAL block )
    219     |   #( CLOSURE block )
    220     |   #( POSITIVE_CLOSURE block )
    221     ;
    222 
    223 tree:   #(TREE_BEGIN  element (element)*  )
    224     ;
    225 
    226 atom
    227     :   #( rr:RULE_REF (rarg:ARG_ACTION)? )     {out.ruleRef(#rr);}
    228     |   #( t:TOKEN_REF (targ:ARG_ACTION )? )    {out.token(#t);}
    229     |   c:CHAR_LITERAL                          {out.charLiteral(#c);}
    230     |   s:STRING_LITERAL                        {out.charLiteral(#s);}
    231     |   WILDCARD                                {out.wildcard(#WILDCARD);}
    232     |   #(DOT ID atom) // scope override on rule
    233     ;
    234 
    235 ast_suffix
    236 	:	ROOT
    237 	|	BANG
    238 	;
    239