Skip to content

Commit 5840875

Browse files
authored
refactor: remove lexer and parser pools (#838)
Signed-off-by: aimuz <mr.imuz@gmail.com>
1 parent f719bfa commit 5840875

File tree

1 file changed

+7
-40
lines changed

1 file changed

+7
-40
lines changed

‎parser/parser.go‎

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"regexp"
2222
"strconv"
2323
"strings"
24-
"sync"
2524

2625
antlr "github.com/antlr/antlr4/runtime/Go/antlr/v4"
2726

@@ -299,53 +298,21 @@ type parser struct {
299298
enableVariadicOperatorASTs bool
300299
}
301300

302-
var (
303-
_ gen.CELVisitor = (*parser)(nil)
304-
305-
lexerPool *sync.Pool = &sync.Pool{
306-
New: func() any {
307-
l := gen.NewCELLexer(nil)
308-
l.RemoveErrorListeners()
309-
return l
310-
},
311-
}
312-
313-
parserPool *sync.Pool = &sync.Pool{
314-
New: func() any {
315-
p := gen.NewCELParser(nil)
316-
p.RemoveErrorListeners()
317-
return p
318-
},
319-
}
320-
)
301+
var _ gen.CELVisitor = (*parser)(nil)
321302

322303
func (p *parser) parse(expr runes.Buffer, desc string) ast.Expr {
323-
// TODO: get rid of these pools once https://github.com/antlr/antlr4/pull/3571 is in a release
324-
lexer := lexerPool.Get().(*gen.CELLexer)
325-
prsr := parserPool.Get().(*gen.CELParser)
304+
lexer := gen.NewCELLexer(newCharStream(expr, desc))
305+
lexer.RemoveErrorListeners()
306+
lexer.AddErrorListener(p)
307+
308+
prsr := gen.NewCELParser(antlr.NewCommonTokenStream(lexer, 0))
309+
prsr.RemoveErrorListeners()
326310

327311
prsrListener := &recursionListener{
328312
maxDepth: p.maxRecursionDepth,
329313
ruleTypeDepth: map[int]*int{},
330314
}
331315

332-
defer func() {
333-
// Unfortunately ANTLR Go runtime is missing (*antlr.BaseParser).RemoveParseListeners,
334-
// so this is good enough until that is exported.
335-
// Reset the lexer and parser before putting them back in the pool.
336-
lexer.RemoveErrorListeners()
337-
prsr.RemoveParseListener(prsrListener)
338-
prsr.RemoveErrorListeners()
339-
lexer.SetInputStream(nil)
340-
prsr.SetInputStream(nil)
341-
lexerPool.Put(lexer)
342-
parserPool.Put(prsr)
343-
}()
344-
345-
lexer.SetInputStream(newCharStream(expr, desc))
346-
prsr.SetInputStream(antlr.NewCommonTokenStream(lexer, 0))
347-
348-
lexer.AddErrorListener(p)
349316
prsr.AddErrorListener(p)
350317
prsr.AddParseListener(prsrListener)
351318

0 commit comments

Comments
 (0)