From f5cf1cbf688d36b82501db32f80b0bde31ec9d63 Mon Sep 17 00:00:00 2001 From: Peter Boyer Date: Wed, 23 Dec 2015 11:00:30 -0600 Subject: [PATCH] Various refactorings to properly support package export --- runtime/Go/src/antlr4/1.stg | 861 ++++++++++++++++++ runtime/Go/src/antlr4/ATN.go | 9 +- runtime/Go/src/antlr4/ATNConfig.go | 12 +- runtime/Go/src/antlr4/ATNConfigSet.go | 6 +- runtime/Go/src/antlr4/ATNDeserializer.go | 8 +- runtime/Go/src/antlr4/ATNSimulator.go | 2 +- runtime/Go/src/antlr4/ATNState.go | 14 +- runtime/Go/src/antlr4/BufferedTokenStream.go | 20 +- runtime/Go/src/antlr4/CharStream.go | 2 +- runtime/Go/src/antlr4/CommonTokenFactory.go | 6 +- runtime/Go/src/antlr4/CommonTokenStream.go | 6 +- runtime/Go/src/antlr4/DFA.go | 4 +- runtime/Go/src/antlr4/DFASerializer.go | 6 +- runtime/Go/src/antlr4/DFAState.go | 2 +- .../Go/src/antlr4/DiagnosticErrorListener.go | 8 +- runtime/Go/src/antlr4/ErrorStrategy.go | 212 ++--- runtime/Go/src/antlr4/Errors.go | 28 +- runtime/Go/src/antlr4/InputStream.go | 4 +- runtime/Go/src/antlr4/IntStream.go | 2 +- runtime/Go/src/antlr4/LL1Analyzer.go | 6 +- runtime/Go/src/antlr4/Lexer.go | 34 +- runtime/Go/src/antlr4/LexerATNSimulator.go | 70 +- runtime/Go/src/antlr4/LexerAction.go | 2 +- runtime/Go/src/antlr4/LexerActionExecutor.go | 8 +- runtime/Go/src/antlr4/Parser.go | 117 +-- runtime/Go/src/antlr4/ParserATNSimulator.go | 104 +-- runtime/Go/src/antlr4/ParserRuleContext.go | 24 +- runtime/Go/src/antlr4/PredictionContext.go | 38 +- runtime/Go/src/antlr4/PredictionMode.go | 18 +- runtime/Go/src/antlr4/Recognizer.go | 39 +- runtime/Go/src/antlr4/RuleContext.go | 12 +- runtime/Go/src/antlr4/SemanticContext.go | 6 +- runtime/Go/src/antlr4/Token.go | 10 +- runtime/Go/src/antlr4/TokenSource.go | 2 +- runtime/Go/src/antlr4/TokenStream.go | 10 +- runtime/Go/src/antlr4/Transition.go | 24 +- runtime/Go/src/antlr4/Tree.go | 18 +- runtime/Go/src/antlr4/Trees.go | 4 +- runtime/Go/src/scratch/.gitignore | 3 + runtime/Go/src/scratch/Arithmetic.g4 | 101 ++ runtime/Go/src/scratch/T.g | 3 + .../antlr/v4/tool/templates/codegen/Go/Go.stg | 242 +++-- .../org/antlr/v4/codegen/target/GoTarget.java | 5 +- 43 files changed, 1538 insertions(+), 574 deletions(-) create mode 100644 runtime/Go/src/scratch/.gitignore create mode 100644 runtime/Go/src/scratch/Arithmetic.g4 create mode 100644 runtime/Go/src/scratch/T.g diff --git a/runtime/Go/src/antlr4/1.stg b/runtime/Go/src/antlr4/1.stg index e69de29bb..bb54f1418 100644 --- a/runtime/Go/src/antlr4/1.stg +++ b/runtime/Go/src/antlr4/1.stg @@ -0,0 +1,861 @@ +/** ANTLR tool checks output templates are compatible with tool code generation. + * For now, a simple string Match used on x.y of x.y.z scheme. + * Must Match Tool.VERSION during load to templates. + * + * REQUIRED. + */ + +fileHeader(grammarFileName, ANTLRVersion) ::= << +// Generated from by ANTLR +>> + +// args must be , + +ParserFile(file, parser, namedActions) ::= << + +package parser // + +import ( + "antlr4" + "strings" +) + + + + + +>> + + + +ListenerFile(file, header) ::= << + +package parser // + +import "antlr4" + +// This class defines a complete listener for a parse tree produced by . + +type Listener struct { + +} + +#. +func (l *Listener) enter(ctx antlr4.IParserRuleContext) { +\} + +// Exit a parse tree produced by #. +func (l *Listener) exit(ctx antlr4.IParserRuleContext) { +\} + +}; separator="\n"> + +>> + + +VisitorFile(file, header) ::= << + +package parser // + +import "antlr4" + +
+ +// This class defines a complete generic visitor for a parse tree produced by . + +type Visitor struct { + +} + +#. +func (l Visitor) visit(ctx IParserRuleContext) { +\} + +}; separator="\n"> + +>> + +Parser(parser, funcs, atn, sempredFuncs, superClass) ::= << + + +var = require('./'). // TODO + + +var parserATN = +var deserializer = antlr4.NewATNDeserializer() +var deserializedATN = deserializer.Deserialize( []rune( parserATN ) ) + +var literalNames = []string{ }; null="nil", separator=", ", wrap, anchor> } +var symbolicNames = []string{ }; null="nil", separator=", ", wrap, anchor> } +var ruleNames = []string{ "}; separator=", ", wrap, anchor> } + +type struct { + + + ruleNames []string + literalNames []string + symbolicNames []string + grammarFileName string +} + +func New(input antlr4.TokenStream) * { + + var decisionToDFA = make([]*antlr4.DFA,len(deserializedATN.DecisionToState)) + var sharedContextCache = antlr4.NewPredictionContextCache() + + for index, ds := range deserializedATN.DecisionToState { + decisionToDFA[index] = antlr4.NewDFA(ds, index) + } + + parser := new() + + parser.InitParser(input) + + parser.Interpreter = antlr4.NewParserATNSimulator(parser, deserializedATN, decisionToDFA, sharedContextCache) + parser.ruleNames = ruleNames + parser.literalNames = literalNames + parser.symbolicNames = symbolicNames + + parser.grammarFileName = "" + + return parser +} + +const( + EOF = antlr4.TokenEOF + + = }; separator="\n", wrap, anchor> + +) + +const ( + RULE_ = }; separator="\n", wrap, anchor> +) + + + + +func (p *) Sempred(localctx, ruleIndex int, predIndex int) { + switch ruleIndex { + : + return p._Sempred(localctx, predIndex);}; separator="\n"> + default: + panic("No predicate with index:" + ruleIndex) + } +} + + + + +>> + +dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << + +func (l *) Action(localctx, ruleIndex int, actionIndex int) { + switch ruleIndex) { + : + p._Action(localctx, actionIndex) + }; separator="\n"> + default: + panic("No registered action for:" + ruleIndex) + } +} + + + + +func (l *) Sempred(localctx, ruleIndex, predIndex) { + switch ruleIndex) { + : + return l._Sempred(localctx, predIndex);}; separator="\n"> + default: + panic("No registered predicate for:" + ruleIndex) + } +} + + + +>> + + +/* This generates a private method since the actionIndex is generated, making an + * overriding implementation impossible to maintain. + */ +RuleActionFunction(r, actions) ::= << + +func (l *) _Action(localctx , actionIndex) { + switch actionIndex) { + : + + }; separator="\n"> + default: + panic("No registered action for:" + actionIndex) + } +} +>> + +/* This generates a private method since the predIndex is generated, making an + * overriding implementation impossible to maintain. + */ +RuleSempredFunction(r, actions) ::= << +func (s *) _Sempred(localctx, predIndex int) { + switch predIndex { + : + return ;}; separator="\n"> + default: + panic("No predicate with index:" + predIndex) + } +} + +>> + + + +RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << + + + +}; separator="\n"> + +func (p *) (}; separator=", ">) { + + localctx := New(p, p.GetParserRuleContext(), p.GetState()}>) + p.EnterRule(localctx, , RULE_) + + + + defer func() { + if err := recover(); err != nil { + + // TODO not sure how exceptions are passed into clause + + if v, ok = x.(antlr4.RecognitionException); ok { + localctx.SetException( v ) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(re) + } + + // TODO if the above panic call is invoked then the below finally clause may not be called + + p.ExitRule() + } + } + + + + + + return localctx +} + +>> + +LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, + namedActions,finallyAction,postamble) ::= +<< + + +}; separator="\n"> + +func (p *) (_p, }>) { + + _parentctx := p.GetParent().(IParserRuleContext) + _parentState := p.GetState() + localctx := New(p, p.GetParserRuleContext(), _parentState}>) + _prevctx := localctx + _startState := + p.EnterRecursionRule(localctx, , RULE_, _p) + + + + defer func(){ + + p.UnrollRecursionContexts(_parentctx) + } + + try { + + + + } catch( error) { + if v, ok = x.(antlr4.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(error) + } + } finally { + + p.UnrollRecursionContexts(_parentctx) + } + return localctx +} + +>> + +CodeBlockForOuterMostAlt(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= << +localctx = NewContext(p, localctx) +p.EnterOuterAlt(localctx, ) + +>> + + +CodeBlockForAlt(currentAltCodeBlock, locals, preamble, ops) ::= << + + + +>> + +LL1AltBlock(choice, preamble, alts, error) ::= << +p.SetState() + = p.GetTokenStream().LT(1) + +switch p.GetTokenStream().LA(1) { + + + break;}; separator="\n"> +default: + +} +>> + +LL1OptionalBlock(choice, alts, error) ::= << +p.SetState() +switch p.GetTokenStream().LA(1) { + + + break;}; separator="\n"> +default: + +} +>> + +LL1OptionalBlockSingleAlt(choice, expr, alts, preamble, error, followExpr) ::= << +p.SetState() + +if { + +} +) ) !> +>> + +LL1StarBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) + +for { + + p.SetState() + p.GetErrorHandler().Sync(p) + +} +>> + +LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) + +for ok := true; ok; ok = { + + p.SetState(); + p.GetErrorHandler().Sync(p) + +} +>> + +// LL(*) stuff + +AltBlock(choice, preamble, alts, error) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) + = _input.LT(1) + +la_ := p.GetInterpreter().AdaptivePredict(p.GetTokenStream(),,p.GetParserRuleContext()) +switch la_) { +: + +}; separator="\n"> +} +>> + +OptionalBlock(choice, alts, error) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) +la_ := p.GetInterpreter().AdaptivePredict(p.GetTokenStream(),,p.GetParserRuleContext()) ++1 { + +}; separator="\n} else "> +} +>> + +StarBlock(choice, alts, Sync, iteration) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) +_alt := p.GetInterpreter().AdaptivePredict(p.GetTokenStream(),,p.GetParserRuleContext()) +for _alt!= && _alt!= antlr4.ATNINVALID_ALT_NUMBER { + if(_alt==1+1) { + + + } + p.SetState() + p.GetErrorHandler().Sync(p) + _alt = p.GetInterpreter().AdaptivePredict(p.GetTokenStream(),,p.GetParserRuleContext()) +} + +>> + +PlusBlock(choice, alts, error) ::= << +p.SetState() +p.GetErrorHandler().Sync(p) +_alt := 1+1 +for ok := true; ok; ok = _alt!= && _alt!= antlr4.ATNINVALID_ALT_NUMBER { + switch _alt) { + +1: + + //}; separator="\n"> + default: + + } + p.SetState() + p.GetErrorHandler().Sync(p) + _alt = p.GetInterpreter().AdaptivePredict(p.GetTokenStream(),, p.GetParserRuleContext()) +} +>> + +Sync(s) ::= "Sync()" + +ThrowNoViableAlt(t) ::= "panic(NewNoViableAltException(p))" + +TestSetInline(s) ::= << +}; separator=" || "> +>> + +// Javascript language spec - shift operators are 32 bits long max +testShiftInRange(shiftAmount) ::= << +(() & ~0x1f) == 0 +>> + +// produces smaller bytecode only when bits.ttypes contains more than two items +bitsetBitfieldComparison(s, bits) ::= <% +(})> && ((1 \<\< ) & ()}; separator=" | ">)) !== 0) +%> + +isZero ::= [ +"0":true, +default:false +] + +offsetShiftVar(shiftAmount, offset) ::= <% +( - ) +%> + +offsetShiftType(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) ::= << +p.SetState() + = }>p.(,) +>> + +MatchToken(m) ::= << +p.SetState() + = }>p.Match() +>> + +MatchSet(m, expr, capture) ::= "" + +MatchNotSet(m, expr, capture) ::= "" + +CommonSetStuff(m, expr, capture, invert) ::= << +p.SetState() + = }>p.GetTokenStream().LT(1); + +if \<=0 || if !() { + = }>p.GetErrorHandler().RecoverInline(p) +} +else { + p.Consume() +} +>> + +Wildcard(w) ::= << +p.SetState() + = }>MatchWildcard() +>> + +// ACTION STUFF + +Action(a, foo, chunks) ::= "" + +ArgAction(a, chunks) ::= "" + +SemPred(p, chunks, failChunks) ::= << +p.SetState() +if !( ) { + panic( FailedPredicateException(p, , , )) +} +>> + +ExceptionClause(e, catchArg, catchAction) ::= << +catch () { + +} +>> + +// lexer actions are not associated with model objects + +LexerSkipCommand() ::= "p.skip()" +LexerMoreCommand() ::= "p.more()" +LexerPopModeCommand() ::= "p.popMode()" +LexerTypeCommand(arg) ::= "p._type = " +LexerChannelCommand(arg) ::= "p._channel = " +LexerModeCommand(arg) ::= "p._mode = " +LexerPushModeCommand(arg) ::= "p.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 ? null : ..text)" +TokenPropertyRef_type(t) ::= "(. == null ? 0 : ..type)" +TokenPropertyRef_line(t) ::= "(. == null ? 0 : ..line)" +TokenPropertyRef_pos(t) ::= "(. == null ? 0 : ..column)" +TokenPropertyRef_channel(t) ::= "(. == null ? 0 : ..channel)" +TokenPropertyRef_index(t) ::= "(. == null ? 0 : ..tokenIndex)" +TokenPropertyRef_int(t) ::= "(. == null ? 0 : parseInt(..text))" + +RulePropertyRef_start(r) ::= "(.==null ? null : ..start)" +RulePropertyRef_stop(r) ::= "(.==null ? null : ..stop)" +RulePropertyRef_text(r) ::= "(.==null ? null : p.GetTokenStream().GetTextFromInterval(NewInterval(..GetStart(),..GetStop())))" +RulePropertyRef_ctx(r) ::= "." +RulePropertyRef_parser(r) ::= "this" + +ThisRulePropertyRef_start(r) ::= "localctx.start" +ThisRulePropertyRef_stop(r) ::= "localctx.stop" +ThisRulePropertyRef_text(r) ::= "p.GetTokenStream().GetTextFromInterval(NewInterval(localctx.GetStart(), p.GetTokenStream().LT(-1)))" +ThisRulePropertyRef_ctx(r) ::= "localctx" +ThisRulePropertyRef_parser(r) ::= "p" + +NonLocalAttrRef(s) ::= "getInvokingContext()." +SetNonLocalAttr(s, rhsChunks) ::= "getInvokingContext(). = " + +AddToLabelList(a) ::= ". = append(., push()" + +TokenDecl(t) ::= "p. = nil // " +TokenTypeDecl(t) ::= " := 0 // type" +TokenListDecl(t) ::= "p. = [] // of s" +RuleContextDecl(r) ::= "p. = nil // " +RuleContextListDecl(rdecl) ::= "p. = [] // of s" + +ContextTokenGetterDecl(t) ::= << +() { + return s.GetToken(, 0) +} +>> + +// should never be called +ContextTokenListGetterDecl(t) ::= << +def _list(self): + return self.GetTokens() +>> + +ContextTokenListIndexedGetterDecl(t) ::= << +(i int) { + if 0 > i { + return s.GetTokens() + } else { + return s.GetToken(, i) + } +} + +>> + +ContextRuleGetterDecl(r) ::= << +() { + return s.GetTypedRuleContext(,0) +} +>> + +// should never be called +ContextRuleListGetterDecl(r) ::= << +func _list(self): + return s.GetTypedRuleContexts() + +>> + +ContextRuleListIndexedGetterDecl(r) ::= << +(i int) { + if 0 > i { + return s.GetTypedRuleContexts() + } else { + return s.GetTypedRuleContext(,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) ::= "