1- // Package parser parse the filter expression.
2- package parser
1+ package goqrius
32
43import (
54 "fmt"
@@ -11,72 +10,34 @@ import (
1110// Precedences.
1211const (
1312 _ int = iota
14- LOWEST
15- OR // or
16- AND // and
17- PREFIX // not
18- COMPARE // eq, ne, gt, ge, lt, le
13+ lowest
14+ or // or
15+ and // and
16+ prefix // not
17+ compare // eq, ne, gt, ge, lt, le
1918)
2019
2120//nolint:exhaustive,gochecknoglobals // no need to put all the tokens.
2221var precedences = map [token.Type ]int {
23- token .Or : OR ,
24- token .And : AND ,
25- token .Eq : COMPARE ,
26- token .NotEq : COMPARE ,
27- token .GreaterThan : COMPARE ,
28- token .GreaterThanOrEqual : COMPARE ,
29- token .LessThan : COMPARE ,
30- token .LessThanOrEqual : COMPARE ,
22+ token .Or : or ,
23+ token .And : and ,
24+ token .Eq : compare ,
25+ token .NotEq : compare ,
26+ token .GreaterThan : compare ,
27+ token .GreaterThanOrEqual : compare ,
28+ token .LessThan : compare ,
29+ token .LessThanOrEqual : compare ,
3130}
3231
33- // AST nodes
34-
35- type Node interface { String () string }
36-
37- type Expression interface {
38- Node
39- expressionNode ()
40- }
41-
42- type Identifier struct { Value string }
43-
44- func (i * Identifier ) String () string { return i .Value }
45- func (i * Identifier ) expressionNode () {}
46-
47- type IntegerLiteral struct { Value string }
48-
49- func (il * IntegerLiteral ) String () string { return il .Value }
50- func (il * IntegerLiteral ) expressionNode () {}
51-
52- type StringLiteral struct { Value string }
53-
54- func (sl * StringLiteral ) String () string { return fmt .Sprintf ("'%s'" , sl .Value ) }
55- func (sl * StringLiteral ) expressionNode () {}
56-
57- type NotExpr struct { Right Expression }
58-
59- func (ne * NotExpr ) String () string { return fmt .Sprintf ("(not %s)" , ne .Right .String ()) }
60- func (ne * NotExpr ) expressionNode () {}
61-
62- type InfixExpr struct {
63- Left Expression
64- Operator token.Type
65- Right Expression
66- }
67-
68- func (ie * InfixExpr ) String () string {
69- return fmt .Sprintf ("(%s %s %s)" , ie .Left .String (), string (ie .Operator ), ie .Right .String ())
70- }
71- func (ie * InfixExpr ) expressionNode () {}
72-
7332type Parser struct {
7433 l * lexer.Lexer
7534 curToken token.Token
7635 peekToken token.Token
7736 errors []string
7837}
7938
39+ // New creates a new Parser based on a Lexer.
40+ // It's recommended to use goqrius.Parse instead of this.
8041func New (l * lexer.Lexer ) * Parser {
8142 p := & Parser {
8243 l : l ,
@@ -99,7 +60,7 @@ func (p *Parser) Parse() Expression {
9960 p .nextToken ()
10061 }
10162
102- expr := p .parseExpression (LOWEST )
63+ expr := p .parseExpression (lowest )
10364
10465 // consume trailing tokens until EOF
10566 for p .peekToken .Type != token .EOF {
@@ -128,15 +89,15 @@ func (p *Parser) peekPrecedence() int {
12889 return pr
12990 }
13091
131- return LOWEST
92+ return lowest
13293}
13394
13495func (p * Parser ) curPrecedence () int {
13596 if pr , ok := precedences [p .curToken .Type ]; ok {
13697 return pr
13798 }
13899
139- return LOWEST
100+ return lowest
140101}
141102
142103func (p * Parser ) peekError (t token.Type ) {
@@ -156,12 +117,12 @@ func (p *Parser) parseExpression(precedence int) Expression {
156117 leftExp = & StringLiteral {Value : p .curToken .Literal }
157118 case token .Not :
158119 p .nextToken ()
159- right := p .parseExpression (PREFIX )
120+ right := p .parseExpression (prefix )
160121 leftExp = & NotExpr {Right : right }
161122 case token .Lparen :
162123 p .nextToken ()
163124
164- leftExp = p .parseExpression (LOWEST )
125+ leftExp = p .parseExpression (lowest )
165126 if ! p .expectPeek (token .Rparen ) {
166127 return nil
167128 }
@@ -182,7 +143,7 @@ func (p *Parser) parseExpression(precedence int) Expression {
182143 prec := p .curPrecedence ()
183144 p .nextToken () // advance to the right expression's first token
184145 right := p .parseExpression (prec )
185- leftExp = & InfixExpr {Left : leftExp , Operator : op , Right : right }
146+ leftExp = & FilterExpr {Left : leftExp , Operator : op , Right : right }
186147 default :
187148 return leftExp
188149 }
0 commit comments