Skip to content

Commit

Permalink
refactor: remove lexer and parser pools (#838)
Browse files Browse the repository at this point in the history
Signed-off-by: aimuz <mr.imuz@gmail.com>
  • Loading branch information
aimuz committed Sep 25, 2023
1 parent f719bfa commit 5840875
Showing 1 changed file with 7 additions and 40 deletions.
47 changes: 7 additions & 40 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"regexp"
"strconv"
"strings"
"sync"

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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 5840875

Please sign in to comment.