// Copyright 2016 The ANTLR Project. All rights reserved. // Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information. 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, contextSuperClass) ::= << import { ATN } from "/atn/ATN"; import { ATNDeserializer } from "/atn/ATNDeserializer"; import { FailedPredicateException } from "/FailedPredicateException"; import { NotNull } from "/Decorators"; import { NoViableAltException } from "/NoViableAltException"; import { Override } from "/Decorators"; import { Parser } from "/Parser"; import { ParserRuleContext } from "/ParserRuleContext"; import { ParserATNSimulator } from "/atn/ParserATNSimulator"; import { ParseTreeListener } from "/tree/ParseTreeListener"; import { ParseTreeVisitor } from "/tree/ParseTreeVisitor"; import { RecognitionException } from "/RecognitionException"; import { RuleContext } from "/RuleContext"; //import { RuleVersion } from "/RuleVersion"; import { TerminalNode } from "/tree/TerminalNode"; import { Token } from "/Token"; import { TokenStream } from "/TokenStream"; import { Vocabulary } from "/Vocabulary"; import { VocabularyImpl } from "/VocabularyImpl"; import * as Utils from "/misc/Utils"; import { Listener } from "./Listener"; import { Visitor } from "./Visitor"; >> ListenerFile(file, header, namedActions) ::= <<
import { ParseTreeListener } from "/tree/ParseTreeListener"; Context \} from "./";}; separator="\n"> /** * This interface defines a complete listener for a parse tree produced by * ``. */ export interface Listener extends ParseTreeListener { * Enter a parse tree produced by the `` * labeled alternative in `.`. * Enter a parse tree produced by `.`. * @param ctx the parse tree */ enter?: (ctx: Context) => void; /** * Exit a parse tree produced by the `` * labeled alternative in `.`. * Exit a parse tree produced by `.`. * @param ctx the parse tree */ exit?: (ctx: Context) => void;}; separator="\n"> } >> VisitorFile(file, header, namedActions) ::= <<
import { ParseTreeVisitor } from "/tree/ParseTreeVisitor"; Context \} from "./";}; separator="\n"> /** * This interface defines a complete generic visitor for a parse tree produced * by ``. * * @param \ The return type of the visit operation. Use `void` for * operations with no return type. */ export interface Visitor\ extends ParseTreeVisitor\ { * Visit a parse tree produced by the `` * labeled alternative in `.`. * Visit a parse tree produced by `.`. * @param ctx the parse tree * @return the visitor result */ visit?: (ctx: Context) => Result;}; separator="\n"> } >> fileHeader(grammarFileName, ANTLRVersion) ::= << // Generated from by ANTLR >> Parser(parser, funcs, atn, sempredFuncs, superClass, isLexer=false) ::= << >> Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= << export abstract class extends { = ;}; separator="\n", wrap, anchor> = ;}; separator="\n", wrap, anchor> // tslint:disable:no-trailing-whitespace public static readonly ruleNames: string[] = [ ",}; separator=" ", wrap, anchor> ]; // tslint:enable:no-trailing-whitespace // @Override public override get grammarFileName(): string { return ""; } // @Override public override get ruleNames(): string[] { return .ruleNames; } // @Override public override get serializedATN(): string { return ._serializedATN; } protected createFailedPredicateException(predicate?: string, message?: string): FailedPredicateException { return new FailedPredicateException(this, predicate, message); } public sempred(_localctx: RuleContext, ruleIndex: number, predIndex: number): boolean { switch (ruleIndex) { : return this._sempred(_localctx as , predIndex);}; separator="\n"> } return true; } } }; separator="\n"> }; separator="\n\n"> >> vocabulary(className, literalNames, symbolicNames) ::= << private static readonly _LITERAL_NAMES: Array\ = [ ,}; null="undefined,", separator=" ", wrap, anchor> ]; private static readonly _SYMBOLIC_NAMES: Array\ = [ ,}; null="undefined,", separator=" ", wrap, anchor> ]; public static readonly VOCABULARY: Vocabulary = new VocabularyImpl(._LITERAL_NAMES, ._SYMBOLIC_NAMES, []); // @Override // @NotNull public get vocabulary(): Vocabulary { return .VOCABULARY; } >> dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << // @Override public override action(_localctx: RuleContext, ruleIndex: number, actionIndex: number): void { switch (ruleIndex) { : this._action(()_localctx, actionIndex); break;}; separator="\n"> } } // @Override public sempred(_localctx: RuleContext, ruleIndex: number, predIndex: number): boolean { switch (ruleIndex) { : return this._sempred(_localctx as , predIndex);}; separator="\n"> } return true; } >> parser_ctor(p) ::= << constructor(input: TokenStream) { super(input); this._interp = new ParserATNSimulator(._ATN, this); } >> /* This generates a private method since the actionIndex is generated, making an * overriding implementation impossible to maintain. */ RuleActionFunction(r, actions) ::= << private _action(_localctx: , actionIndex: number): void { 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 _sempred(_localctx: , predIndex: number): boolean { switch (predIndex) { : return ;}; separator="\n"> } return true; } >> RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << // @RuleVersion() }>public (): { let _localctx: = new (this._ctx, this.state}>); this.enterRule(_localctx, , .RULE_); try { let _alt: number; } catch (re) { if (re instanceof RecognitionException) { _localctx.exception = re; this._errHandler.reportError(this, re); this._errHandler.recover(this, re); } else { throw re; } } finally { this.exitRule(); } return _localctx; } >> LeftFactoredRuleFunction(currentRule,args,code,locals,namedActions,finallyAction,postamble) ::= << }>private () { let _localctx: = new (this._ctx, this.state}>); this.enterLeftFactoredRule(_localctx, , .RULE_); try { let _alt: number; } catch (re) { if (re instanceof RecognitionException) { _localctx.exception = re; this._errHandler.reportError(this, re); this._errHandler.recover(this, re); } else { throw re; } } finally { this.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,args,code,locals,namedActions,finallyAction,postamble) ::= << }>private () { let _localctx: = new (this._ctx, this.state}>); this.enterRule(_localctx, , .RULE_); try { let _alt: number; } catch (re) { if (re instanceof RecognitionException) { _localctx.exception = re; this._errHandler.reportError(this, re); this._errHandler.recover(this, re); } else { throw re; } } finally { this.exitRule(); } return _localctx; } >> LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, namedActions,finallyAction,postamble) ::= << }>public (): ; }>public (, _p: number): ; // @RuleVersion() }>public (, _p?: number): { if (_p === undefined) { _p = 0; } let _parentctx: ParserRuleContext = this._ctx; let _parentState: number = this.state; let _localctx: = new (this._ctx, _parentState}>); let _prevctx: = _localctx; let _startState: number = ; this.enterRecursionRule(_localctx, , .RULE_, _p); try { let _alt: number; } catch (re) { if (re instanceof RecognitionException) { _localctx.exception = re; this._errHandler.reportError(this, re); this._errHandler.recover(this, re); } else { throw re; } } finally { this.unrollRecursionContexts(_parentctx); } return _localctx; } >> CodeBlockForOuterMostAlt(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= << _localctx = new Context(_localctx); this.enterOuterAlt(_localctx, ); >> CodeBlockForAlt(currentAltCodeBlock, locals, preamble, ops) ::= << // tslint:disable-next-line:no-empty { } >> LL1AltBlock(choice, preamble, alts, error) ::= << this.state = ; this._errHandler.sync(this); = this._input.LT(1); switch (this._input.LA(1)) { break;}; separator="\n"> default: } >> LL1OptionalBlock(choice, alts, error) ::= << this.state = ; this._errHandler.sync(this); switch (this._input.LA(1)) { break;}; separator="\n"> default: break; } >> LL1OptionalBlockSingleAlt(choice, expr, alts, preamble, error, followExpr) ::= << this.state = ; this._errHandler.sync(this); if () { } )) !> >> LL1StarBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << this.state = ; this._errHandler.sync(this); while () { this.state = ; this._errHandler.sync(this); } >> LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << this.state = ; this._errHandler.sync(this); do { this.state = ; this._errHandler.sync(this); } while (); >> // LL(*) stuff AltBlock(choice, preamble, alts, error) ::= << this.state = ; this._errHandler.sync(this); = this._input.LT(1); switch ( this.interpreter.adaptivePredict(this._input, , this._ctx) ) { : break;}; separator="\n"> } >> OptionalBlock(choice, alts, error) ::= << this.state = ; this._errHandler.sync(this); switch ( this.interpreter.adaptivePredict(this._input, , this._ctx) ) { + 1: break;}; separator="\n"> } >> StarBlock(choice, alts, sync, iteration) ::= << this.state = ; this._errHandler.sync(this); _alt = this.interpreter.adaptivePredict(this._input, , this._ctx); while (_alt !== && _alt !== ATN.INVALID_ALT_NUMBER) { if (_alt === 1 + 1) { } this.state = ; this._errHandler.sync(this); _alt = this.interpreter.adaptivePredict(this._input, , this._ctx); } >> PlusBlock(choice, alts, error) ::= << this.state = ; this._errHandler.sync(this); _alt = 1 + 1; do { switch (_alt) { + 1: break;}; separator="\n"> default: } this.state = ; this._errHandler.sync(this); _alt = this.interpreter.adaptivePredict(this._input, , this._ctx); } while (_alt !== && _alt !== ATN.INVALID_ALT_NUMBER); >> Sync(s) ::= "this.sync();" ThrowNoViableAlt(t) ::= "throw new NoViableAltException(this);" TestSetInline(s) ::= << }; separator=" || "> >> // Need a range test to make sure the shift amount will result in a testable bitmask testShiftInRange(shiftAmount) ::= << (() & ~0x1F) === 0 >> // produces smaller bytecode only when bits.ttypes contains more than two items bitsetBitfieldComparison(s, bits) ::= <% (})> && ((1 \<\< ) & (.}, bits.shift)>)}; 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) ::= << this.state = ; = }>this.(,); >> MatchToken(m) ::= << this.state = ; = }>this.match(.); >> MatchSet(m, expr, capture) ::= "" MatchNotSet(m, expr, capture) ::= "" CommonSetStuff(m, expr, capture, invert) ::= << this.state = ; = }>this._input.LT(1); if ( \<= 0 || !()) { = }>this._errHandler.recoverInline(this); } else { if (this._input.LA(1) === Token.EOF) { this.matchedEOF = true; } this._errHandler.reportMatch(this); this.consume(); } >> Wildcard(w) ::= << this.state = ; = }>this.matchWildcard(); >> // ACTION STUFF Action(a, foo, chunks) ::= "" ArgAction(a, chunks) ::= "" SemPred(p, chunks, failChunks) ::= << this.state = ; if (!()) { throw this.createFailedPredicateException(, , ); } >> ExceptionClause(e, catchArg, catchAction) ::= << catch () { } >> // lexer actions are not associated with model objects LexerSkipCommand() ::= "this.skip();" LexerMoreCommand() ::= "this.more();" LexerPopModeCommand() ::= "this.popMode();" LexerTypeCommand(arg, grammar) ::= "this._type = ;" LexerChannelCommand(arg, grammar) ::= "this._channel = ;" LexerModeCommand(arg, grammar) ::= "this._mode = ;" LexerPushModeCommand(arg, grammar) ::= "this.pushMode();" 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 ? ._.text : undefined)" TokenPropertyRef_type(t) ::= "(._ != null ? ._.type : 0)" TokenPropertyRef_line(t) ::= "(._ != null ? ._.line : 0)" TokenPropertyRef_pos(t) ::= "(._ != null ? ._.charPositionInLine : 0)" TokenPropertyRef_channel(t) ::= "(._ != null ? ._.channel : 0)" TokenPropertyRef_index(t) ::= "(._ != null ? ._.tokenIndex : 0)" TokenPropertyRef_int(t) ::= "(._ != null ? Number(._.text) : 0)" RulePropertyRef_start(r) ::= "(._ != null ? (._._start) : undefined)" RulePropertyRef_stop(r) ::= "(._ != null ? (._._stop) : undefined)" RulePropertyRef_text(r) ::= "(._ != null ? this._input.getTextFromRange(._._start, ._._stop) : undefined)" RulePropertyRef_ctx(r) ::= "._" RulePropertyRef_parser(r) ::= "this" ThisRulePropertyRef_start(r) ::= "_localctx._start" ThisRulePropertyRef_stop(r) ::= "_localctx._stop" ThisRulePropertyRef_text(r) ::= "this._input.getTextFromRange(_localctx._start, this._input.tryLT(-1))" ThisRulePropertyRef_ctx(r) ::= "_localctx" ThisRulePropertyRef_parser(r) ::= "this" NonLocalAttrRef(s) ::= "(this.getInvokingContext() as Context)." SetNonLocalAttr(s, rhsChunks) ::= "(this.getInvokingContext() as Context). = ;" AddToLabelList(a) ::= "._.push();" TokenDecl(t) ::= "_!: " TokenTypeDecl(t) ::= "let : number;" TokenListDecl(t) ::= "_: Token[] = []" RuleContextDecl(r) ::= "_!: " RuleContextListDecl(rdecl) ::= "_: [] = []" ContextTokenGetterDecl(t) ::= "public (): TerminalNode | undefined { return this.tryGgetToken(., 0); }" ContextTokenListGetterDecl(t) ::= "public (): TerminalNode[];" ContextTokenListIndexedGetterDecl(t) ::= << public (i: number): TerminalNode; public (i?: number): TerminalNode | TerminalNode[] { if (i === undefined) { return this.getTokens(.); } else { return this.getToken(., i); } } >> ContextRuleGetterDecl(r) ::= << public (): | undefined { return this.tryGgetRuleContext(0, ); } >> ContextRuleListGetterDecl(r) ::= << public (): []; >> ContextRuleListIndexedGetterDecl(r) ::= << public (i: number): ; public (i?: number): | [] { if (i === undefined) { return this.getRuleContexts(); } else { return this.getRuleContext(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) ::= "