Skip to content

Commit

Permalink
Add a null token
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Oct 1, 2024
1 parent 5058ae6 commit b792aaf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 31 deletions.
1 change: 1 addition & 0 deletions selector/lexer/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
LessThan
LessThanOrEqual
Exclamation
Null
)

type Tokens []Token
Expand Down
3 changes: 3 additions & 0 deletions selector/lexer/tokenize.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (p *Tokenizer) parseCurRune() (Token, error) {
default:
pos := p.i

if pos+3 < p.srcLen && strings.EqualFold(p.src[pos:pos+4], "null") {
return NewToken(Null, p.src[pos:pos+4], p.i, 4), nil
}
if pos+3 < p.srcLen && strings.EqualFold(p.src[pos:pos+4], "true") {
return NewToken(Bool, p.src[pos:pos+4], p.i, 4), nil
}
Expand Down
33 changes: 3 additions & 30 deletions selector/lexer/tokenize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,17 @@ func TestTokenizer_Parse(t *testing.T) {
}))

t.Run("everything", runTest(testCase{
in: "foo.bar.baz[1] != 42.123 || foo.bar.baz['hello'] == 42 && x == 'a\\'b' + false . .... asd... $name",
in: "foo.bar.baz[1] != 42.123 || foo.bar.baz['hello'] == 42 && x == 'a\\'b' + false true . .... asd... $name null",
out: []TokenKind{
Symbol, Dot, Symbol, Dot, Symbol, OpenBracket, Number, CloseBracket, NotEqual, Number,
Or,
Symbol, Dot, Symbol, Dot, Symbol, OpenBracket, String, CloseBracket, Equal, Number,
And,
Symbol, Equal, String,
Plus, Bool,
Plus, Bool, Bool,
Dot, Spread, Dot,
Symbol, Spread,
Variable,
Variable, Null,
},
}))

tok := NewTokenizer("foo.bar.baz[1] != 42.123 || foo.bar.baz['hello'] == 42 && x == 'a\\'b' + false . .... asd... $name")
tokens, err := tok.Tokenize()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
exp := []TokenKind{
Symbol, Dot, Symbol, Dot, Symbol, OpenBracket, Number, CloseBracket, NotEqual, Number,
Or,
Symbol, Dot, Symbol, Dot, Symbol, OpenBracket, String, CloseBracket, Equal, Number,
And,
Symbol, Equal, String,
Plus, Bool,
Dot, Spread, Dot,
Symbol, Spread,
Variable,
}
if len(tokens) != len(exp) {
t.Fatalf("unexpected number of tokens: %d", len(tokens))
}

for i := range tokens {
if tokens[i].Kind != exp[i] {
t.Errorf("unexpected token kind at position %d: exp %v, got %v", i, exp[i], tokens[i].Kind)
return
}
}
}
2 changes: 1 addition & 1 deletion selector/parser/denotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var tokenBindingPowers = map[lexer.TokenKind]bindingPower{
lexer.String: bpLiteral,
lexer.Number: bpLiteral,
lexer.Bool: bpLiteral,
//lexer.Null: bpLiteral,
lexer.Null: bpLiteral,

lexer.Variable: bpProperty,
lexer.Dot: bpProperty,
Expand Down

0 comments on commit b792aaf

Please sign in to comment.