From 5840875b182c4af1f2471caca290f10a7d56f46b Mon Sep 17 00:00:00 2001 From: aimuz Date: Mon, 25 Sep 2023 23:27:20 +0800 Subject: [PATCH] refactor: remove lexer and parser pools (#838) Signed-off-by: aimuz --- parser/parser.go | 47 +++++++---------------------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index c408bd35..1363d1c0 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -21,7 +21,6 @@ import ( "regexp" "strconv" "strings" - "sync" antlr "github.com/antlr/antlr4/runtime/Go/antlr/v4" @@ -299,53 +298,21 @@ type parser struct { enableVariadicOperatorASTs bool } -var ( - _ gen.CELVisitor = (*parser)(nil) - - lexerPool *sync.Pool = &sync.Pool{ - New: func() any { - l := gen.NewCELLexer(nil) - l.RemoveErrorListeners() - return l - }, - } - - parserPool *sync.Pool = &sync.Pool{ - New: func() any { - p := gen.NewCELParser(nil) - p.RemoveErrorListeners() - return p - }, - } -) +var _ gen.CELVisitor = (*parser)(nil) func (p *parser) parse(expr runes.Buffer, desc string) ast.Expr { - // TODO: get rid of these pools once https://github.com/antlr/antlr4/pull/3571 is in a release - lexer := lexerPool.Get().(*gen.CELLexer) - prsr := parserPool.Get().(*gen.CELParser) + lexer := gen.NewCELLexer(newCharStream(expr, desc)) + lexer.RemoveErrorListeners() + lexer.AddErrorListener(p) + + prsr := gen.NewCELParser(antlr.NewCommonTokenStream(lexer, 0)) + prsr.RemoveErrorListeners() prsrListener := &recursionListener{ maxDepth: p.maxRecursionDepth, ruleTypeDepth: map[int]*int{}, } - defer func() { - // Unfortunately ANTLR Go runtime is missing (*antlr.BaseParser).RemoveParseListeners, - // so this is good enough until that is exported. - // Reset the lexer and parser before putting them back in the pool. - lexer.RemoveErrorListeners() - prsr.RemoveParseListener(prsrListener) - prsr.RemoveErrorListeners() - lexer.SetInputStream(nil) - prsr.SetInputStream(nil) - lexerPool.Put(lexer) - parserPool.Put(prsr) - }() - - lexer.SetInputStream(newCharStream(expr, desc)) - prsr.SetInputStream(antlr.NewCommonTokenStream(lexer, 0)) - - lexer.AddErrorListener(p) prsr.AddErrorListener(p) prsr.AddParseListener(prsrListener)