Skip to content

Commit 9e9aec4

Browse files
committed
Make parser config optional
1 parent 0c3d2a6 commit 9e9aec4

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

parser/lexer/lexer_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,13 @@ func TestLex_location(t *testing.T) {
311311
tokens, err := Lex(source)
312312
require.NoError(t, err)
313313
require.Equal(t, []Token{
314-
{Location: file.Location{From: 0, To: 1}, Kind: "Number", Value: "1"},
315-
{Location: file.Location{From: 1, To: 3}, Kind: "Operator", Value: ".."},
316-
{Location: file.Location{From: 3, To: 4}, Kind: "Number", Value: "2"},
317-
{Location: file.Location{From: 5, To: 6}, Kind: "Number", Value: "3"},
318-
{Location: file.Location{From: 6, To: 8}, Kind: "Operator", Value: ".."},
319-
{Location: file.Location{From: 8, To: 9}, Kind: "Number", Value: "4"},
320-
{Location: file.Location{From: 8, To: 9}, Kind: "EOF", Value: ""},
314+
{Location: file.Location{From: 0, To: 1}, Kind: Number, Value: "1"},
315+
{Location: file.Location{From: 1, To: 3}, Kind: Operator, Value: ".."},
316+
{Location: file.Location{From: 3, To: 4}, Kind: Number, Value: "2"},
317+
{Location: file.Location{From: 5, To: 6}, Kind: Number, Value: "3"},
318+
{Location: file.Location{From: 6, To: 8}, Kind: Operator, Value: ".."},
319+
{Location: file.Location{From: 8, To: 9}, Kind: Number, Value: "4"},
320+
{Location: file.Location{From: 8, To: 9}, Kind: EOF, Value: ""},
321321
}, tokens)
322322
}
323323

parser/parser.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ type parser struct {
5656

5757
func (p *parser) checkNodeLimit() error {
5858
p.nodeCount++
59+
if p.config == nil {
60+
if p.nodeCount > conf.DefaultMaxNodes {
61+
p.error("compilation failed: expression exceeds maximum allowed nodes")
62+
return nil
63+
}
64+
return nil
65+
}
5966
if p.config.MaxNodes > 0 && p.nodeCount > p.config.MaxNodes {
6067
p.error("compilation failed: expression exceeds maximum allowed nodes")
6168
return nil
@@ -91,9 +98,7 @@ type Tree struct {
9198
}
9299

93100
func Parse(input string) (*Tree, error) {
94-
return ParseWithConfig(input, &conf.Config{
95-
Disabled: map[string]bool{},
96-
})
101+
return ParseWithConfig(input, nil)
97102
}
98103

99104
func ParseWithConfig(input string, config *conf.Config) (*Tree, error) {
@@ -515,7 +520,10 @@ func (p *parser) toFloatNode(number float64) Node {
515520
func (p *parser) parseCall(token Token, arguments []Node, checkOverrides bool) Node {
516521
var node Node
517522

518-
isOverridden := p.config.IsOverridden(token.Value)
523+
isOverridden := false
524+
if p.config != nil {
525+
isOverridden = p.config.IsOverridden(token.Value)
526+
}
519527
isOverridden = isOverridden && checkOverrides
520528

521529
if b, ok := predicates[token.Value]; ok && !isOverridden {
@@ -562,7 +570,7 @@ func (p *parser) parseCall(token Token, arguments []Node, checkOverrides bool) N
562570
if node == nil {
563571
return nil
564572
}
565-
} else if _, ok := builtin.Index[token.Value]; ok && !p.config.Disabled[token.Value] && !isOverridden {
573+
} else if _, ok := builtin.Index[token.Value]; ok && (p.config == nil || !p.config.Disabled[token.Value]) && !isOverridden {
566574
node = p.createNode(&BuiltinNode{
567575
Name: token.Value,
568576
Arguments: p.parseArguments(arguments),

0 commit comments

Comments
 (0)