From a5da0354f200dc2e8b916efd095354498592e157 Mon Sep 17 00:00:00 2001 From: "Jim.Idle" Date: Mon, 22 Aug 2022 11:40:25 +0800 Subject: [PATCH 1/3] fix: #3758 Allow for string being a keyword and fix go template to use escapedName - The go template was ignoring the use of escapedName in many places and was not consistenet with the Java version. - Added 'string' to the list of reserved words for the Go target Signed-off-by: Jim.Idle --- .../antlr/v4/tool/templates/codegen/Go/Go.stg | 162 +++++++++--------- .../org/antlr/v4/codegen/target/GoTarget.java | 1 + 2 files changed, 82 insertions(+), 81 deletions(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg index 9704de156a..5605bef954 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg @@ -256,7 +256,7 @@ func (p *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex : var t *Context = nil if localctx != nil { t = localctx.(*Context) \} - return p._Sempred(t, predIndex)}; separator="\n\n"> + return p._Sempred(t, predIndex)}; separator="\n\n"> @@ -332,7 +332,7 @@ func (l *) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex * overriding implementation impossible to maintain. */ RuleActionFunction(r, actions) ::= << -func (l *) _Action(localctx antlr.RuleContext*, actionIndex int) { +func (l *) _Action(localctx antlr.RuleContext*, actionIndex int) { this := l _ = this @@ -353,7 +353,7 @@ func (l *) _Action(localctx ) _Sempred(localctx antlr.RuleContext, predIndex int) bool { +func (p *) _Sempred(localctx antlr.RuleContext, predIndex int) bool { this := p _ = this @@ -380,7 +380,7 @@ RuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs, namedAction -func (p *) ( }; separator=", ">) (localctx I) { +func (p *) ( }; separator=", ">) (localctx I) { this := p _ = this @@ -452,7 +452,7 @@ LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs -func (p *) ( }; separator=", ">) (localctx I) { +func (p *) ( }; separator=", ">) (localctx I) { return p.(0}>) } @@ -764,7 +764,7 @@ InvokeRule(r, argExprsChunks) ::= << var _x = p.(, ) - var _x = p.() + var _x = p.() @@ -773,7 +773,7 @@ InvokeRule(r, argExprsChunks) ::= << p.(, ) - p.() + p.() } @@ -784,11 +784,11 @@ MatchToken(m) ::= << p.SetState() - var _m = p.Match() + var _m = p.Match() = _m}; separator="\n"> - p.Match() + p.Match() } >> @@ -874,14 +874,14 @@ ActionTemplate(t) ::= "" ArgRef(a) ::= "." LocalRef(a) ::= "." RetValueRef(a) ::= "." -QRetValueRef(a) ::= ".Get().Get()" +QRetValueRef(a) ::= ".Get().Get()" /** How to translate $tokenLabel */ -TokenRef(t) ::= ".Get()" -LabelRef(t) ::= ".Get()" -ListLabelRef(t) ::= ".Get" +TokenRef(t) ::= ".Get()" +LabelRef(t) ::= ".Get()" +ListLabelRef(t) ::= ".Get" -SetAttr(s, rhsChunks) ::= ".Set()" +SetAttr(s, rhsChunks) ::= ".Set()" TokenLabelType() ::= "" InputSymbolType() ::= "" @@ -921,25 +921,25 @@ RuleContextListDecl(rdecl) ::= " []I" AttributeDecl(d) ::= " // TODO = " ContextTokenGetterDecl(t) ::= << -() antlr.TerminalNode { - return s.GetToken(, 0) +() antlr.TerminalNode { + return s.GetToken(, 0) } >> ContextTokenListGetterDecl(t) ::= << -All() []antlr.TerminalNode { - return s.GetTokens() +All() []antlr.TerminalNode { + return s.GetTokens() } >> ContextTokenListIndexedGetterDecl(t) ::= << -(i int) antlr.TerminalNode { - return s.GetToken(, i) +(i int) antlr.TerminalNode { + return s.GetToken(, i) } >> ContextRuleGetterDecl(r) ::= << -() I { +() I { var t antlr.RuleContext; for _, ctx := range s.GetChildren() { if _, ok := ctx.(I); ok { @@ -957,7 +957,7 @@ ContextRuleGetterDecl(r) ::= << >> ContextRuleListGetterDecl(r) ::= << -All() []I { +All() []I { children := s.GetChildren() len := 0 for _, ctx := range children { @@ -980,7 +980,7 @@ All() []I { >> ContextRuleListIndexedGetterDecl(r) ::= << -(i int) I { +(i int) I { var t antlr.RuleContext; j := 0 for _, ctx := range s.GetChildren() { @@ -1017,87 +1017,87 @@ CaptureNextToken(d) ::= " = p.GetTokenStream().LT(1)" CaptureNextTokenType(d) ::= " = p.GetTokenStream().LA(1)" StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers) ::= << -// I is an interface to support dynamic dispatch. -type I interface { +// I is an interface to support dynamic dispatch. +type I interface { antlr.ParserRuleContext // GetParser returns the parser. GetParser() antlr.Parser - returns the token. -Get() }; separator="\n\n"> + returns the token. +Get() }; separator="\n\n"> - sets the token. -Set() }; separator="\n\n"> + sets the token. +Set() }; separator="\n\n"> - returns the token type. -Get() int }; separator="\n\n"> + returns the token type. +Get() int }; separator="\n\n"> - sets the token type. -Set(int) }; separator="\n\n"> + sets the token type. +Set(int) }; separator="\n\n"> - returns the token list. -Get() []}; separator="\n\n"> + returns the token list. +Get() []}; separator="\n\n"> - sets the token list. -Set([])}; separator="\n\n"> + sets the token list. +Set([])}; separator="\n\n"> - returns the rule contexts. -Get() I}; separator="\n\n"> + returns the rule contexts. +Get() I}; separator="\n\n"> - sets the rule contexts. -Set(I)}; separator="\n\n"> + sets the rule contexts. +Set(I)}; separator="\n\n"> - returns the rule context list. -Get() []I}; separator="\n\n"> + returns the rule context list. +Get() []I}; separator="\n\n"> - sets the rule context list. -Set([]I) }; separator="\n\n"> + sets the rule context list. +Set([]I) }; separator="\n\n"> - returns the attribute. -Get() }; separator="\n\n"> + returns the attribute. +Get() }; separator="\n\n"> - sets the attribute. -Set()}; separator="\n\n"> + sets the attribute. +Set()}; separator="\n\n"> - // Is differentiates from other interfaces. - Is() + // Is differentiates from other interfaces. + Is() } type struct { @@ -1108,16 +1108,16 @@ type struct { } -func NewEmpty() * { +func NewEmpty() * { var p = new() p.BaseParserRuleContext = Newantlr.NewBaseParserRuleContext(nil, -1) p.RuleIndex = RULE_ return p } -func (*) Is() {} +func (*) Is() {} -func New(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int }>) * { +func New(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int }>) * { var p = new() p.BaseParserRuleContext = Newantlr.NewBaseParserRuleContext(parent, invokingState) @@ -1135,62 +1135,62 @@ func New(parser antlr.Parser, parent antlr.ParserRuleContext, invok func (s *) GetParser() antlr.Parser { return s.parser } -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> -) Get() int { return s. \}}; separator="\n\n"> +) Get() int { return s. \}}; separator="\n\n"> -) Set(v int) { s. = v \}}; separator="\n\n"> +) Set(v int) { s. = v \}}; separator="\n\n"> -) Get() [] { return s. \}}; separator="\n\n"> +) Get() [] { return s. \}}; separator="\n\n"> -) Set(v []) { s. = v \}}; separator="\n\n"> +) Set(v []) { s. = v \}}; separator="\n\n"> -) Get() I { return s. \}}; separator="\n\n"> +) Get() I { return s. \}}; separator="\n\n"> -) Set(v I) { s. = v \}}; separator="\n\n"> +) Set(v I) { s. = v \}}; separator="\n\n"> -) Get() []I { return s. \}}; separator="\n\n"> +) Get() []I { return s. \}}; separator="\n\n"> -) Set(v []I) { s. = v \}}; separator="\n\n"> +) Set(v []I) { s. = v \}}; separator="\n\n"> -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> @@ -1233,7 +1233,7 @@ type struct { } -func New(parser antlr.Parser, ctx antlr.ParserRuleContext) * { +func New(parser antlr.Parser, ctx antlr.ParserRuleContext) * { var p = new() p.Context = NewEmptyContext() @@ -1245,62 +1245,62 @@ func New(parser antlr.Parser, ctx antlr.ParserRuleContext) * -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> -) Get() int { return s. \}}; separator="\n\n"> +) Get() int { return s. \}}; separator="\n\n"> -) Set(v int) { s. = v \}}; separator="\n\n"> +) Set(v int) { s. = v \}}; separator="\n\n"> -) Get() [] { return s. \}}; separator="\n\n"> +) Get() [] { return s. \}}; separator="\n\n"> -) Set(v []) { s. = v \}}; separator="\n\n"> +) Set(v []) { s. = v \}}; separator="\n\n"> -) Get() I { return s. \}}; separator="\n\n"> +) Get() I { return s. \}}; separator="\n\n"> -) Set(v I) { s. = v \}}; separator="\n\n"> +) Set(v I) { s. = v \}}; separator="\n\n"> -) Get() []I { return s. \}}; separator="\n\n"> +) Get() []I { return s. \}}; separator="\n\n"> -) Set(v []I) { s. = v \}}; separator="\n\n"> +) Set(v []I) { s. = v \}}; separator="\n\n"> -) Get() { return s. \}}; separator="\n\n"> +) Get() { return s. \}}; separator="\n\n"> -) Set(v ) { s. = v \}}; separator="\n\n"> +) Set(v ) { s. = v \}}; separator="\n\n"> func (s *) GetRuleContext() antlr.RuleContext { @@ -1338,10 +1338,10 @@ func (s *) Accept(visitor antlr.ParseTreeVisitor) interface{ >> /** If we don't know location of label def x, use this template */ -labelref(x) ::= "localctx.(*)." +labelref(x) ::= "localctx.(*)." /** For any action chunk, what is correctly-typed context struct ptr? */ -ctx(actionChunk) ::= "localctx.(*)" +ctx(actionChunk) ::= "localctx.(*)" // used for left-recursive rules recRuleAltPredicate(ruleName, opPrec) ::= "p.Precpred(p.GetParserRuleContext(), )" diff --git a/tool/src/org/antlr/v4/codegen/target/GoTarget.java b/tool/src/org/antlr/v4/codegen/target/GoTarget.java index 672507883b..8c102431b7 100644 --- a/tool/src/org/antlr/v4/codegen/target/GoTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/GoTarget.java @@ -35,6 +35,7 @@ public class GoTarget extends Target { "true", "false", "iota", "nil", "append", "cap", "close", "complex", "copy", "delete", "imag", "len", "make", "new", "panic", "print", "println", "real", "recover", + "string", // interface definition of RuleContext from runtime/Go/antlr/rule_context.go "Accept", "GetAltNumber", "GetBaseRuleContext", "GetChild", "GetChildCount", From 8ece8b33d2c8478382823984c1f97ae29b4a22ec Mon Sep 17 00:00:00 2001 From: "Jim.Idle" Date: Tue, 23 Aug 2022 10:25:10 +0800 Subject: [PATCH 2/3] fix: #3758 Add go.sum to the repo Signed-off-by: Jim.Idle --- runtime/Go/antlr/go.sum | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 runtime/Go/antlr/go.sum diff --git a/runtime/Go/antlr/go.sum b/runtime/Go/antlr/go.sum new file mode 100644 index 0000000000..2b05f22a47 --- /dev/null +++ b/runtime/Go/antlr/go.sum @@ -0,0 +1,2 @@ +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= +golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= From 39c3cca8b5ebbcddfffb01941a4af4921dfc0254 Mon Sep 17 00:00:00 2001 From: "Jim.Idle" Date: Tue, 23 Aug 2022 10:27:20 +0800 Subject: [PATCH 3/3] fix: #3758 Ensure that standard runtime extensions are included in go.mod Signed-off-by: Jim.Idle --- runtime/Go/antlr/go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/Go/antlr/go.mod b/runtime/Go/antlr/go.mod index 6281e331eb..1646d8b13e 100644 --- a/runtime/Go/antlr/go.mod +++ b/runtime/Go/antlr/go.mod @@ -1,3 +1,5 @@ module github.com/antlr/antlr4/runtime/Go/antlr go 1.18 + +require golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e