org.antlr.codegen.templates.Java.Java.stg Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of antlr-complete Show documentation
Show all versions of antlr-complete Show documentation
Complete distribution for ANTLR 3
/*
[The "BSD license"]
Copyright (c) 2010 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
javaTypeInitMap ::= [
"int":"0",
"long":"0",
"float":"0.0f",
"double":"0.0",
"boolean":"false",
"byte":"0",
"short":"0",
"char":"0",
default:"null" // anything other than an atomic type
]
// System.Boolean.ToString() returns "True" and "False", but the proper C# literals are "true" and "false"
// The Java version of Boolean returns "true" and "false", so they map to themselves here.
booleanLiteral ::= [
"True":"true",
"False":"false",
"true":"true",
"false":"false",
default:"false"
]
/** The overall file structure of a recognizer; stores methods for rules
* and cyclic DFAs plus support code.
*/
outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
docComment, recognizer,
name, tokens, tokenNames, rules, cyclicDFAs,
bitsets, buildTemplate, buildAST, rewriteMode, profile,
backtracking, synpreds, memoize, numRules,
fileName, ANTLRVersion, generatedTimestamp, trace,
scopes, superClass, literals) ::=
<<
// $ANTLR
<@imports>
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
<@end>
@SuppressWarnings("all")
>>
lexer(grammar, name, tokens, scopes, rules, numRules, filterMode, labelType="CommonToken",
superClass="Lexer") ::= <<
public class extends <@superClassName><@end> {
=;}; separator="\n">
}>
// delegates
;}; separator="\n">
// delegators
;}; separator="\n">
gParent;}>
public [] getDelegates() {
return new [] {}; separator = ", ">};
}
public () {}
public (CharStream input }>) {
this(input, new RecognizerSharedState()}>);
}
public (CharStream input, RecognizerSharedState state }>) {
super(input,state);
state.ruleMemo = new HashMap[+1];<\n>
= new (input, state}>, this);}; separator="\n">
= ;}; separator="\n">
;}>
}
@Override public String getGrammarFileName() { return ""; }
}>
dfa = new DFA(this);}; separator="\n">
}
>>
/** A override of Lexer.nextToken() that backtracks over mTokens() looking
* for matches. No error can be generated upon error; just rewind, consume
* a token and then try again. backtracking needs to be set as well.
* Make rule memoization happen only at levels above 1 as we start mTokens
* at backtracking==1.
*/
filteringNextToken() ::= <<
@Override
public Token nextToken() {
while (true) {
if ( input.LA(1)==CharStream.EOF ) {
Token eof = new CommonToken(input,Token.EOF,
Token.DEFAULT_CHANNEL,
input.index(),input.index());
eof.setLine(getLine());
eof.setCharPositionInLine(getCharPositionInLine());
return eof;
}
state.token = null;
state.channel = Token.DEFAULT_CHANNEL;
state.tokenStartCharIndex = input.index();
state.tokenStartCharPositionInLine = input.getCharPositionInLine();
state.tokenStartLine = input.getLine();
state.text = null;
try {
int m = input.mark();
state.backtracking=1;
state.failed=false;
mTokens();
state.backtracking=0;
if ( state.failed ) {
input.rewind(m);
input.consume();
}
else {
emit();
return state.token;
}
}
catch (RecognitionException re) {
// shouldn't happen in backtracking mode, but...
reportError(re);
recover(re);
}
}
}
@Override
public void memoize(IntStream input,
int ruleIndex,
int ruleStartIndex)
{
if ( state.backtracking>1 ) super.memoize(input, ruleIndex, ruleStartIndex);
}
@Override
public boolean alreadyParsedRule(IntStream input, int ruleIndex) {
if ( state.backtracking>1 ) return super.alreadyParsedRule(input, ruleIndex);
return false;
}
>>
actionGate() ::= "state.backtracking==0"
filteringActionGate() ::= "state.backtracking==1"
/** How to generate a parser */
genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
bitsets, inputStreamType, superClass,
labelType, members, rewriteElementType,
filterMode, ASTLabelType="Object") ::= <<
public class extends <@superClassName><@end> {
public static final String[] tokenNames = new String[] {
"\", "\", "\", "\",
};
=;}; separator="\n">
// delegates
;}; separator="\n">
public [] getDelegates() {
return new [] {}; separator = ", ">};
}
// delegators
;}; separator="\n">
gParent;}>
}>
<@members>
public ( input }>) {
this(input, new RecognizerSharedState()}>);
}
public ( input, RecognizerSharedState state }>) {
super(input, state);
= new (input, state}>, this);}; separator="\n">
= .;}; separator="\n">
;}>
}
<@end>
@Override public String[] getTokenNames() { return .tokenNames; }
@Override public String getGrammarFileName() { return ""; }
// Delegated rules
() throws { return .(}; separator=", ">); \}}; separator="\n">
}>
dfa = new DFA(this);}; separator="\n">
_in_},
words64=it.bits)>}; separator="\n">
}
>>
parserCtorBody() ::= <<
this.state.ruleMemo = new HashMap[+1];<\n>
= ;}; separator="\n">
>>
parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
ASTLabelType="Object", superClass="Parser", labelType="Token",
members={}) ::= <<
>>
/** How to generate a tree parser; same as parser except the input
* stream is a different type.
*/
treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
numRules, bitsets, filterMode, labelType={}, ASTLabelType="Object",
superClass={TreeRewriterTreeFilterTreeParser},
members={}
) ::= <<
>>
/** A simpler version of a rule template that is specific to the imaginary
* rules created for syntactic predicates. As they never have return values
* nor parameters etc..., just give simplest possible method. Don't do
* any of the normal memoization stuff in here either; it's a waste.
* As predicates cannot be inlined into the invoking rule, they need to
* be in a rule by themselves.
*/
synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
<<
// $ANTLR start
public final void _fragment() throws }; separator=", "> {
traceIn("_fragment", );
try {
}
finally {
traceOut("_fragment", );
}
}
// $ANTLR end
>>
synpred(name) ::= <<
public final boolean () {
state.backtracking++;
<@start()>
int start = input.mark();
try {
_fragment(); // can never throw exception
} catch (RecognitionException re) {
System.err.println("impossible: "+re);
}
boolean success = !state.failed;
input.rewind(start);
<@stop()>
state.backtracking--;
state.failed=false;
return success;
}<\n>
>>
lexerSynpred(name) ::= <<
>>
ruleMemoization(name) ::= <<
if ( state.backtracking>0 && alreadyParsedRule(input, ) ) { }))> }
>>
/** How to test for failure and return from rule */
checkRuleBacktrackFailure() ::= <<
if (state.failed) }))>
>>
/** This rule has failed, exit indicating failure during backtrack */
ruleBacktrackFailure() ::= <<
if (state.backtracking>0) {state.failed=true; }))>}
>>
/** How to generate code for a rule. This includes any return type
* data aggregates required for multiple return values.
*/
rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= <<
// $ANTLR start ""
// :
@Override
public final () throws }; separator=", "> {
traceIn("", );
<@preamble()>
try {
<(ruleDescriptor.actions.after):execAction()>
}
<\n>}>
catch (RecognitionException re) {
reportError(re);
recover(input,re);
<@setErrorReturnValue()>
}
finally {
// do for sure before leaving
traceOut("", );
}
<@postamble()>
}), false)>
}
// $ANTLR end ""
>>
returnStatement(returnValue, force=true) ::= <%
return ;
return;
%>
catch(decl,action) ::= <<
catch () {
}
>>
ruleDeclarations() ::= <<
retval = new ();
retval.start = input.LT(1);
= ;
}>
int _StartIndex = input.index();
>>
ruleScopeSetUp() ::= <<
_stack.push(new _scope());}; separator="\n">
_stack.push(new _scope());}; separator="\n">
>>
ruleScopeCleanUp() ::= <<
_stack.pop();}; separator="\n">
_stack.pop();}; separator="\n">
>>
ruleLabelDefs() ::= <<
<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,
ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
:{it | =null;}; separator="\n"
>
<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels,ruleDescriptor.wildcardTreeListLabels]
:{it |List\