Improved lexer performance!

This commit is contained in:
Mike Farah 2021-02-25 16:47:55 +11:00
parent 111c6e0be1
commit 2ed5b2ff59
2 changed files with 6 additions and 7 deletions

View File

@ -5,9 +5,6 @@ import (
"strings" "strings"
) )
var myPathTokeniser = newExpressionTokeniser()
var myPathPostfixer = newExpressionPostFixer()
type ExpressionNode struct { type ExpressionNode struct {
Operation *Operation Operation *Operation
Lhs *ExpressionNode Lhs *ExpressionNode
@ -19,19 +16,21 @@ type ExpressionParser interface {
} }
type expressionParserImpl struct { type expressionParserImpl struct {
pathTokeniser expressionTokeniser
pathPostFixer expressionPostFixer
} }
func NewExpressionParser() ExpressionParser { func NewExpressionParser() ExpressionParser {
return &expressionParserImpl{} return &expressionParserImpl{newExpressionTokeniser(), newExpressionPostFixer()}
} }
func (p *expressionParserImpl) ParseExpression(expression string) (*ExpressionNode, error) { func (p *expressionParserImpl) ParseExpression(expression string) (*ExpressionNode, error) {
tokens, err := myPathTokeniser.Tokenise(expression) tokens, err := p.pathTokeniser.Tokenise(expression)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var Operations []*Operation var Operations []*Operation
Operations, err = myPathPostfixer.ConvertToPostfix(tokens) Operations, err = p.pathPostFixer.ConvertToPostfix(tokens)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -328,7 +328,7 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`\$[a-zA-Z_-0-9]+`), getVariableOpToken()) lexer.Add([]byte(`\$[a-zA-Z_-0-9]+`), getVariableOpToken())
lexer.Add([]byte(`as`), opToken(assignVariableOpType)) lexer.Add([]byte(`as`), opToken(assignVariableOpType))
err := lexer.Compile() err := lexer.CompileNFA()
if err != nil { if err != nil {
return nil, err return nil, err
} }