All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.antlr.v4.tool.templates.codegen.Java.Java.stg Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
/*
 * [The "BSD license"]
 *  Copyright (c) 2012 Terence Parr
 *  Copyright (c) 2012 Sam Harwell
 *  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 a primitive type is an object
]

// args must be , 

ParserFile(file, parser, namedActions) ::= <<
// Generated from  by ANTLR 

package ;


import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.*;
import org.antlr.v4.runtime.tree.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;


>>

ListenerFile(file, header) ::= <<
// Generated from  by ANTLR 

package ;

import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.Token; public interface Listener extends ParseTreeListener\<\> { (.Context ctx); void exit(.Context ctx);}; separator="\n"> } >> BaseListenerFile(file, header) ::= << // Generated from by ANTLR package ;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.ErrorNode; public class BaseListener implements Listener { (.Context ctx) { \} @Override public void exit(.Context ctx) { \}}; separator="\n"> @Override public void enterEveryRule(ParserRuleContext\\> ctx) { } @Override public void exitEveryRule(ParserRuleContext\\> ctx) { } @Override public void visitTerminal(TerminalNode\\> node) { } @Override public void visitErrorNode(ErrorNode\\> node) { } } >> VisitorFile(file, header) ::= << // Generated from by ANTLR package ;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.Token; public interface Visitor\, Result> extends ParseTreeVisitor\ { (.Context ctx);}; separator="\n"> } >> BaseVisitorFile(file, header) ::= << // Generated from by ANTLR package ;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.ParserRuleContext; public class BaseVisitor\ extends AbstractParseTreeVisitor\<, Result> implements Visitor\<, Result> { (.Context ctx) { return visitChildren(ctx); \}}; separator="\n"> } >> Parser(parser, funcs, atn, sempredFuncs, superClass) ::= << >> Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << public abstract class extends { public static final int =}; separator=", ", wrap, anchor>; public static final String[] tokenNames = { }; null="\"\\"", separator=", ", wrap, anchor> }; public static final int = }; separator=", ", wrap, anchor>; public static final String[] ruleNames = { "}; separator=", ", wrap, anchor> }; @Override public String getGrammarFileName() { return ""; } @Override public String[] getTokenNames() { return tokenNames; } @Override public String[] getRuleNames() { return ruleNames; } public boolean sempred(RuleContext\<\> _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } } >> dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << @Override public void action(RuleContext\<Integer\> _localctx, int ruleIndex, int actionIndex) { switch (ruleIndex) { : _action(()_localctx, actionIndex); break;}; separator="\n"> } } @Override public boolean sempred(RuleContext\<Integer\> _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { : return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } >> parser_ctor(p) ::= << public (TokenStream\\> input) { super(input); _interp = new ParserATNSimulator\<\>(this,_ATN); } >> /* This generates a private method since the actionIndex is generated, making an * overriding implementation impossible to maintain. */ RuleActionFunction(r, actions) ::= << private void _action( _localctx, int actionIndex) { switch (actionIndex) { : break;}; separator="\n"> } } >> /* This generates a private method since the predIndex is generated, making an * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << private boolean _sempred( _localctx, int predIndex) { switch (predIndex) { : return ;}; separator="\n"> } return true; } >> RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << }; separator="\n"> @RuleVersion() }>public final () throws RecognitionException { _localctx = new (_ctx, getState()}>); enterRule(_localctx, , RULE_); try { int _alt; } catch (RecognitionException re) { _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } finally { exitRule(); } return _localctx; } >> LeftFactoredRuleFunction(currentRule,code,locals,namedActions,finallyAction,postamble) ::= << }>private final () throws RecognitionException { _localctx = new (_ctx, getState()}>); enterLeftFactoredRule(_localctx, , RULE_); try { int _alt; } catch (RecognitionException re) { _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } finally { exitRule(); } return _localctx; } >> // This behaves similar to RuleFunction (enterRule is called, and no adjustments // are made to the parse tree), but since it's still a variant no context class // needs to be generated. LeftUnfactoredRuleFunction(currentRule,code,locals,namedActions,finallyAction,postamble) ::= << }>private final () throws RecognitionException { _localctx = new (_ctx, getState()}>); enterRule(_localctx, , RULE_); try { int _alt; } catch (RecognitionException re) { _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } finally { exitRule(); } return _localctx; } >> LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs, namedActions,finallyAction,postamble) ::= << }; separator="\n"> @RuleVersion() }>public final (int _p}>) throws RecognitionException { ParserRuleContext\ _parentctx = _ctx; int _parentState = getState(); _localctx = new (_ctx, _parentState}>); _prevctx = _localctx; int _startState = ; enterRecursionRule(_localctx, RULE_, _p); try { int _alt; } catch (RecognitionException re) { _localctx.exception = re; _errHandler.reportError(this, re); _errHandler.recover(this, re); } finally { unrollRecursionContexts(_parentctx); } return _localctx; } >> CodeBlockForOuterMostAlt(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= << _localctx = new Context(_localctx); enterOuterAlt(_localctx, ); >> CodeBlockForAlt(currentAltCodeBlock, locals, preamble, ops) ::= << { } >> LL1AltBlock(choice, preamble, alts, error) ::= << setState(); = _input.LT(1); switch (_input.LA(1)) { break;}; separator="\n"> default: } >> LL1OptionalBlock(choice, alts, error) ::= << setState(); switch (_input.LA(1)) { break;}; separator="\n"> default: } >> LL1OptionalBlockSingleAlt(choice, expr, alts, preamble, error, followExpr) ::= << setState(); if () { } ) ) !> >> LL1StarBlock(choice, alts) ::= << setState(); _errHandler.sync(this); : while (true) { switch (_input.LA(1)) { break;}; separator="\n"> break ; } setState(); _errHandler.sync(this); } >> LL1StarBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << setState(); _errHandler.sync(this); while () { setState(); _errHandler.sync(this); } >> LL1PlusBlock(choice, alts, iteration, loopExpr, error) ::= << setState(); _errHandler.sync(this); do { switch (_input.LA(1)) { break;}; separator="\n"> default: } setState(); _errHandler.sync(this); } while ( ); >> LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << setState(); _errHandler.sync(this); do { setState(); _errHandler.sync(this); } while ( ); >> // LL(*) stuff AltBlock(choice, preamble, alts, error) ::= << setState(); = _input.LT(1); switch ( getInterpreter().adaptivePredict(_input,,_ctx) ) { : break;}; separator="\n"> } >> OptionalBlock(choice, alts, error) ::= << setState(); switch ( getInterpreter().adaptivePredict(_input,,_ctx) ) { +1: break;}; separator="\n"> } >> StarBlock(choice, alts, sync, iteration) ::= << setState(); _errHandler.sync(this); _alt = getInterpreter().adaptivePredict(_input,,_ctx); while ( _alt!= && _alt!=-1 ) { if ( _alt==1+1 ) { } setState(); _errHandler.sync(this); _alt = getInterpreter().adaptivePredict(_input,,_ctx); } >> PlusBlock(choice, alts, error) ::= << setState(); _errHandler.sync(this); _alt = getInterpreter().adaptivePredict(_input,,_ctx); do { switch (_alt) { +1: break;}; separator="\n"> default: } setState(); _errHandler.sync(this); _alt = getInterpreter().adaptivePredict(_input,,_ctx); } while ( _alt!= && _alt!=-1 ); >> Sync(s) ::= "sync();" ThrowNoViableAlt(t) ::= "throw new NoViableAltException(this);" TestSetInline(s) ::= << }; separator=" || "> >> // Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test testShiftInRange(shiftAmount) ::= << (() & ~0x3f) == 0 >> // produces smaller bytecode only when bits.ttypes contains more than two items bitsetBitfieldComparison(s, bits) ::= <% (})> && ((1L \<\< ) & ()}; separator=" | ">)) != 0) %> isZero ::= [ "0":true, default:false ] offsetShift(shiftAmount, offset) ::= <% ( - ) %> // produces more efficient bytecode when bits.ttypes contains at most two items bitsetInlineComparison(s, bits) ::= <% ==}; separator=" || "> %> cases(ttypes) ::= << :}; separator="\n"> >> InvokeRule(r, argExprsChunks) ::= << setState(); = }>(,); >> MatchToken(m) ::= << setState(); = }>match(); >> MatchSet(m, expr, capture) ::= "" MatchNotSet(m, expr, capture) ::= "" CommonSetStuff(m, expr, capture, invert) ::= << setState(); = }>_input.LT(1); if ( \<= 0 || !() ) { = }>_errHandler.recoverInline(this); } consume(); >> Wildcard(w) ::= << setState(); = }>matchWildcard(); >> // ACTION STUFF Action(a, foo, chunks) ::= "" ArgAction(a, chunks) ::= "" SemPred(p, chunks, failChunks) ::= << setState(); if (!()) throw new FailedPredicateException(this, , , ); >> ExceptionClause(e, catchArg, catchAction) ::= << catch () { } >> // lexer actions are not associated with model objects LexerSkipCommand() ::= "skip();" LexerMoreCommand() ::= "more();" LexerPopModeCommand() ::= "popMode();" LexerTypeCommand(arg) ::= "_type = ;" LexerChannelCommand(arg) ::= "_channel = ;" LexerModeCommand(arg) ::= "_mode = ;" LexerPushModeCommand(arg) ::= "pushMode();" DefaultParserSuperClass(s) ::= "Parser\<>" DefaultLexerSuperClass(s) ::= "Lexer" ActionText(t) ::= "" ActionTemplate(t) ::= "" ArgRef(a) ::= "_localctx." LocalRef(a) ::= "_localctx." RetValueRef(a) ::= "_localctx." QRetValueRef(a) ::= ".." /** How to translate $tokenLabel */ TokenRef(t) ::= "." LabelRef(t) ::= "." ListLabelRef(t) ::= "." SetAttr(s,rhsChunks) ::= ". = ;" TokenLabelType() ::= "" InputSymbolType() ::= "" TokenPropertyRef_text(t) ::= "(.!=null?..getText():null)" TokenPropertyRef_type(t) ::= "(.!=null?..getType():0)" TokenPropertyRef_line(t) ::= "(.!=null?..getLine():0)" TokenPropertyRef_pos(t) ::= "(.!=null?..getCharPositionInLine():0)" TokenPropertyRef_channel(t) ::= "(.!=null?..getChannel():0)" TokenPropertyRef_index(t) ::= "(.!=null?..getTokenIndex():0)" TokenPropertyRef_int(t) ::= "(.!=null?Integer.valueOf(..getText()):0)" RulePropertyRef_start(r) ::= "(.!=null?(..start):null)" RulePropertyRef_stop(r) ::= "(.!=null?(..stop):null)" RulePropertyRef_text(r) ::= "(.!=null?_input.getText(..start,..stop):null)" RulePropertyRef_ctx(r) ::= "." ThisRulePropertyRef_start(r) ::= "_localctx.start" ThisRulePropertyRef_stop(r) ::= "_localctx.stop" ThisRulePropertyRef_text(r) ::= "_input.getText(_localctx.start, _input.LT(-1))" ThisRulePropertyRef_ctx(r) ::= "_localctx" NonLocalAttrRef(s) ::= "((Context)getInvokingContext())." SetNonLocalAttr(s, rhsChunks) ::= "((Context)getInvokingContext()). = ;" AddToLabelList(a) ::= "..add();" TokenDecl(t) ::= "public ;" TokenTypeDecl(t) ::= "int ;" TokenListDecl(t) ::= "public List\ = new ArrayList\();" RuleContextDecl(r) ::= "public ;" RuleContextListDecl(rdecl) ::= "public List\<> = new ArrayList\<>();" ContextTokenGetterDecl(t) ::= "public TerminalNode\<> () { return getToken(., 0); }" ContextTokenListGetterDecl(t) ::= "public List\>> () { return getTokens(.); }" ContextTokenListIndexedGetterDecl(t) ::= << public TerminalNode\<\> (int i) { return getToken(., i); } >> ContextRuleGetterDecl(r) ::= << public () { return getRuleContext(.class,0); } >> ContextRuleListGetterDecl(r) ::= << public List\\> () { return getRuleContexts(.class); } >> ContextRuleListIndexedGetterDecl(r) ::= << public (int i) { return getRuleContext(.class,i); } >> LexerRuleContext() ::= "RuleContext\" /** The rule context name is the rule followed by a suffix; e.g., * r becomes rContext. */ RuleContextNameSuffix() ::= "Context" ImplicitTokenLabel(tokenName) ::= "" ImplicitRuleLabel(ruleName) ::= "" ImplicitSetLabel(id) ::= "_tset" ListLabelName(label) ::= "