From a8f3d7d6136c52a02b32f80ee17c57f88360780f Mon Sep 17 00:00:00 2001 From: Nikunj Yadav Date: Tue, 24 Oct 2023 15:09:40 -0700 Subject: [PATCH] upgrading antlr runtime to github.com/antlr4-go/antlr (#33) --- .gitignore | 1 + go.mod | 4 +- go.sum | 8 +- parser/evaluate.go | 2 +- parser/jsonquery_lexer.go | 49 +- parser/jsonquery_parser.go | 788 +++++++++++++++++++------------ parser/jsonquery_visitor.go | 4 +- parser/jsonquery_visitor_impl.go | 2 +- parser/parse_logical_test.go | 7 +- 9 files changed, 524 insertions(+), 341 deletions(-) diff --git a/.gitignore b/.gitignore index 22d0d82..25db3b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor +.vscode diff --git a/go.mod b/go.mod index b2a5773..fab2d15 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/nikunjy/rules go 1.18 require ( - github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20220911224424-aa1f1f12a846 + github.com/antlr4-go/antlr/v4 v4.13.0 github.com/blang/semver v3.5.1+incompatible github.com/stretchr/testify v1.3.0 ) @@ -11,5 +11,5 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect + golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect ) diff --git a/go.sum b/go.sum index 9b41801..f34bacf 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20220911224424-aa1f1f12a846 h1:et5J11AOyUn9qwkIAF9kcxTxjTO8Z9oSmlOqH7MVSPo= -github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20220911224424-aa1f1f12a846/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= +github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI= +github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g= github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -10,5 +10,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -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= +golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU= +golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= diff --git a/parser/evaluate.go b/parser/evaluate.go index 2ca7230..19a7fe4 100644 --- a/parser/evaluate.go +++ b/parser/evaluate.go @@ -3,7 +3,7 @@ package parser import ( "fmt" - "github.com/antlr/antlr4/runtime/Go/antlr/v4" + "github.com/antlr4-go/antlr/v4" ) type Evaluator struct { diff --git a/parser/jsonquery_lexer.go b/parser/jsonquery_lexer.go index 496b7bb..de43e6c 100644 --- a/parser/jsonquery_lexer.go +++ b/parser/jsonquery_lexer.go @@ -1,13 +1,12 @@ -// Code generated from JsonQuery.g4 by ANTLR 4.10.1. DO NOT EDIT. +// Code generated from JsonQuery.g4 by ANTLR 4.13.1. DO NOT EDIT. package parser import ( "fmt" + "github.com/antlr4-go/antlr/v4" "sync" "unicode" - - "github.com/antlr/antlr4/runtime/Go/antlr/v4" ) // Suppress unused import error @@ -22,45 +21,45 @@ type JsonQueryLexer struct { // TODO: EOF string } -var jsonquerylexerLexerStaticData struct { +var JsonQueryLexerLexerStaticData struct { once sync.Once serializedATN []int32 - channelNames []string - modeNames []string - literalNames []string - symbolicNames []string - ruleNames []string - predictionContextCache *antlr.PredictionContextCache + ChannelNames []string + ModeNames []string + LiteralNames []string + SymbolicNames []string + RuleNames []string + PredictionContextCache *antlr.PredictionContextCache atn *antlr.ATN decisionToDFA []*antlr.DFA } func jsonquerylexerLexerInit() { - staticData := &jsonquerylexerLexerStaticData - staticData.channelNames = []string{ + staticData := &JsonQueryLexerLexerStaticData + staticData.ChannelNames = []string{ "DEFAULT_TOKEN_CHANNEL", "HIDDEN", } - staticData.modeNames = []string{ + staticData.ModeNames = []string{ "DEFAULT_MODE", } - staticData.literalNames = []string{ + staticData.LiteralNames = []string{ "", "'('", "')'", "'pr'", "'.'", "'-'", "'['", "']'", "", "", "", "'null'", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "'\\n'", } - staticData.symbolicNames = []string{ + staticData.SymbolicNames = []string{ "", "", "", "", "", "", "", "", "NOT", "LOGICAL_OPERATOR", "BOOLEAN", "NULL", "IN", "EQ", "NE", "GT", "LT", "GE", "LE", "CO", "SW", "EW", "ATTRNAME", "VERSION", "STRING", "DOUBLE", "INT", "EXP", "NEWLINE", "COMMA", "SP", } - staticData.ruleNames = []string{ + staticData.RuleNames = []string{ "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "NOT", "LOGICAL_OPERATOR", "BOOLEAN", "NULL", "IN", "EQ", "NE", "GT", "LT", "GE", "LE", "CO", "SW", "EW", "ATTRNAME", "ATTR_NAME_CHAR", "DIGIT", "ALPHA", "VERSION", "STRING", "ESC", "UNICODE", "HEX", "DOUBLE", "INT", "EXP", "NEWLINE", "COMMA", "SP", } - staticData.predictionContextCache = antlr.NewPredictionContextCache() + staticData.PredictionContextCache = antlr.NewPredictionContextCache() staticData.serializedATN = []int32{ 4, 0, 30, 279, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, @@ -208,7 +207,7 @@ func jsonquerylexerLexerInit() { // NewJsonQueryLexer(). You can call this function if you wish to initialize the static state ahead // of time. func JsonQueryLexerInit() { - staticData := &jsonquerylexerLexerStaticData + staticData := &JsonQueryLexerLexerStaticData staticData.once.Do(jsonquerylexerLexerInit) } @@ -217,13 +216,13 @@ func NewJsonQueryLexer(input antlr.CharStream) *JsonQueryLexer { JsonQueryLexerInit() l := new(JsonQueryLexer) l.BaseLexer = antlr.NewBaseLexer(input) - staticData := &jsonquerylexerLexerStaticData - l.Interpreter = antlr.NewLexerATNSimulator(l, staticData.atn, staticData.decisionToDFA, staticData.predictionContextCache) - l.channelNames = staticData.channelNames - l.modeNames = staticData.modeNames - l.RuleNames = staticData.ruleNames - l.LiteralNames = staticData.literalNames - l.SymbolicNames = staticData.symbolicNames + staticData := &JsonQueryLexerLexerStaticData + l.Interpreter = antlr.NewLexerATNSimulator(l, staticData.atn, staticData.decisionToDFA, staticData.PredictionContextCache) + l.channelNames = staticData.ChannelNames + l.modeNames = staticData.ModeNames + l.RuleNames = staticData.RuleNames + l.LiteralNames = staticData.LiteralNames + l.SymbolicNames = staticData.SymbolicNames l.GrammarFileName = "JsonQuery.g4" // TODO: l.EOF = antlr.TokenEOF diff --git a/parser/jsonquery_parser.go b/parser/jsonquery_parser.go index b68e2f7..35638a4 100644 --- a/parser/jsonquery_parser.go +++ b/parser/jsonquery_parser.go @@ -1,4 +1,4 @@ -// Code generated from JsonQuery.g4 by ANTLR 4.10.1. DO NOT EDIT. +// Code generated from JsonQuery.g4 by ANTLR 4.13.1. DO NOT EDIT. package parser // JsonQuery @@ -7,7 +7,7 @@ import ( "strconv" "sync" - "github.com/antlr/antlr4/runtime/Go/antlr/v4" + "github.com/antlr4-go/antlr/v4" ) // Suppress unused import errors @@ -19,34 +19,34 @@ type JsonQueryParser struct { *antlr.BaseParser } -var jsonqueryParserStaticData struct { +var JsonQueryParserStaticData struct { once sync.Once serializedATN []int32 - literalNames []string - symbolicNames []string - ruleNames []string - predictionContextCache *antlr.PredictionContextCache + LiteralNames []string + SymbolicNames []string + RuleNames []string + PredictionContextCache *antlr.PredictionContextCache atn *antlr.ATN decisionToDFA []*antlr.DFA } func jsonqueryParserInit() { - staticData := &jsonqueryParserStaticData - staticData.literalNames = []string{ + staticData := &JsonQueryParserStaticData + staticData.LiteralNames = []string{ "", "'('", "')'", "'pr'", "'.'", "'-'", "'['", "']'", "", "", "", "'null'", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "'\\n'", } - staticData.symbolicNames = []string{ + staticData.SymbolicNames = []string{ "", "", "", "", "", "", "", "", "NOT", "LOGICAL_OPERATOR", "BOOLEAN", "NULL", "IN", "EQ", "NE", "GT", "LT", "GE", "LE", "CO", "SW", "EW", "ATTRNAME", "VERSION", "STRING", "DOUBLE", "INT", "EXP", "NEWLINE", "COMMA", "SP", } - staticData.ruleNames = []string{ + staticData.RuleNames = []string{ "query", "attrPath", "subAttr", "value", "listStrings", "subListOfStrings", "listDoubles", "subListOfDoubles", "listInts", "subListOfInts", } - staticData.predictionContextCache = antlr.NewPredictionContextCache() + staticData.PredictionContextCache = antlr.NewPredictionContextCache() staticData.serializedATN = []int32{ 4, 1, 30, 108, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 1, 0, 1, @@ -109,7 +109,7 @@ func jsonqueryParserInit() { // NewJsonQueryParser(). You can call this function if you wish to initialize the static state ahead // of time. func JsonQueryParserInit() { - staticData := &jsonqueryParserStaticData + staticData := &JsonQueryParserStaticData staticData.once.Do(jsonqueryParserInit) } @@ -118,11 +118,11 @@ func NewJsonQueryParser(input antlr.TokenStream) *JsonQueryParser { JsonQueryParserInit() this := new(JsonQueryParser) this.BaseParser = antlr.NewBaseParser(input) - staticData := &jsonqueryParserStaticData - this.Interpreter = antlr.NewParserATNSimulator(this, staticData.atn, staticData.decisionToDFA, staticData.predictionContextCache) - this.RuleNames = staticData.ruleNames - this.LiteralNames = staticData.literalNames - this.SymbolicNames = staticData.symbolicNames + staticData := &JsonQueryParserStaticData + this.Interpreter = antlr.NewParserATNSimulator(this, staticData.atn, staticData.decisionToDFA, staticData.PredictionContextCache) + this.RuleNames = staticData.RuleNames + this.LiteralNames = staticData.LiteralNames + this.SymbolicNames = staticData.SymbolicNames this.GrammarFileName = "JsonQuery.g4" return this @@ -183,29 +183,33 @@ type IQueryContext interface { // GetParser returns the parser. GetParser() antlr.Parser - // IsQueryContext differentiates from other interfaces. IsQueryContext() } type QueryContext struct { - *antlr.BaseParserRuleContext + antlr.BaseParserRuleContext parser antlr.Parser } func NewEmptyQueryContext() *QueryContext { var p = new(QueryContext) - p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + antlr.InitBaseParserRuleContext(&p.BaseParserRuleContext, nil, -1) p.RuleIndex = JsonQueryParserRULE_query return p } +func InitEmptyQueryContext(p *QueryContext) { + antlr.InitBaseParserRuleContext(&p.BaseParserRuleContext, nil, -1) + p.RuleIndex = JsonQueryParserRULE_query +} + func (*QueryContext) IsQueryContext() {} func NewQueryContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *QueryContext { var p = new(QueryContext) - p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + antlr.InitBaseParserRuleContext(&p.BaseParserRuleContext, parent, invokingState) p.parser = parser p.RuleIndex = JsonQueryParserRULE_query @@ -215,8 +219,8 @@ func NewQueryContext(parser antlr.Parser, parent antlr.ParserRuleContext, invoki func (s *QueryContext) GetParser() antlr.Parser { return s.parser } -func (s *QueryContext) CopyFrom(ctx *QueryContext) { - s.BaseParserRuleContext.CopyFrom(ctx.BaseParserRuleContext) +func (s *QueryContext) CopyAll(ctx *QueryContext) { + s.CopyFrom(&ctx.BaseParserRuleContext) } func (s *QueryContext) GetRuleContext() antlr.RuleContext { @@ -228,16 +232,16 @@ func (s *QueryContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) } type CompareExpContext struct { - *QueryContext + QueryContext op antlr.Token } func NewCompareExpContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *CompareExpContext { var p = new(CompareExpContext) - p.QueryContext = NewEmptyQueryContext() + InitEmptyQueryContext(&p.QueryContext) p.parser = parser - p.CopyFrom(ctx.(*QueryContext)) + p.CopyAll(ctx.(*QueryContext)) return p } @@ -341,15 +345,15 @@ func (s *CompareExpContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { } type ParenExpContext struct { - *QueryContext + QueryContext } func NewParenExpContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *ParenExpContext { var p = new(ParenExpContext) - p.QueryContext = NewEmptyQueryContext() + InitEmptyQueryContext(&p.QueryContext) p.parser = parser - p.CopyFrom(ctx.(*QueryContext)) + p.CopyAll(ctx.(*QueryContext)) return p } @@ -393,15 +397,15 @@ func (s *ParenExpContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { } type PresentExpContext struct { - *QueryContext + QueryContext } func NewPresentExpContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *PresentExpContext { var p = new(PresentExpContext) - p.QueryContext = NewEmptyQueryContext() + InitEmptyQueryContext(&p.QueryContext) p.parser = parser - p.CopyFrom(ctx.(*QueryContext)) + p.CopyAll(ctx.(*QueryContext)) return p } @@ -441,15 +445,15 @@ func (s *PresentExpContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { } type LogicalExpContext struct { - *QueryContext + QueryContext } func NewLogicalExpContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *LogicalExpContext { var p = new(LogicalExpContext) - p.QueryContext = NewEmptyQueryContext() + InitEmptyQueryContext(&p.QueryContext) p.parser = parser - p.CopyFrom(ctx.(*QueryContext)) + p.CopyAll(ctx.(*QueryContext)) return p } @@ -526,10 +530,8 @@ func (p *JsonQueryParser) Query() (localctx IQueryContext) { } func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { - this := p - _ = this - var _parentctx antlr.ParserRuleContext = p.GetParserRuleContext() + _parentState := p.GetState() localctx = NewQueryContext(p, p.GetParserRuleContext(), _parentState) var _prevctx IQueryContext = localctx @@ -538,28 +540,16 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { p.EnterRecursionRule(localctx, 0, JsonQueryParserRULE_query, _p) var _la int - defer func() { - p.UnrollRecursionContexts(_parentctx) - }() - - defer func() { - if err := recover(); err != nil { - if v, ok := err.(antlr.RecognitionException); ok { - localctx.SetException(v) - p.GetErrorHandler().ReportError(p, v) - p.GetErrorHandler().Recover(p, v) - } else { - panic(err) - } - } - }() - var _alt int p.EnterOuterAlt(localctx, 1) p.SetState(41) p.GetErrorHandler().Sync(p) - switch p.GetInterpreter().AdaptivePredict(p.GetTokenStream(), 2, p.GetParserRuleContext()) { + if p.HasError() { + goto errorExit + } + + switch p.GetInterpreter().AdaptivePredict(p.BaseParser, p.GetTokenStream(), 2, p.GetParserRuleContext()) { case 1: localctx = NewParenExpContext(p, localctx) p.SetParserRuleContext(localctx) @@ -567,29 +557,47 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { p.SetState(22) p.GetErrorHandler().Sync(p) + if p.HasError() { + goto errorExit + } _la = p.GetTokenStream().LA(1) if _la == JsonQueryParserNOT { { p.SetState(21) p.Match(JsonQueryParserNOT) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } } p.SetState(25) p.GetErrorHandler().Sync(p) + if p.HasError() { + goto errorExit + } _la = p.GetTokenStream().LA(1) if _la == JsonQueryParserSP { { p.SetState(24) p.Match(JsonQueryParserSP) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } } { p.SetState(27) p.Match(JsonQueryParserT__0) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } { p.SetState(28) @@ -598,6 +606,10 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { { p.SetState(29) p.Match(JsonQueryParserT__1) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } case 2: @@ -611,10 +623,18 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { { p.SetState(32) p.Match(JsonQueryParserSP) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } { p.SetState(33) p.Match(JsonQueryParserT__2) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } case 3: @@ -628,6 +648,10 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { { p.SetState(36) p.Match(JsonQueryParserSP) + if p.HasError() { + // Recognition error - abort rule + goto errorExit + } } { p.SetState(37) @@ -638,7 +662,7 @@ func (p *JsonQueryParser) query(_p int) (localctx IQueryContext) { _la = p.GetTokenStream().LA(1) - if !(((_la)&-(0x1f+1)) == 0 && ((1<