Skip to content

Commit 12bd722

Browse files
committed
Print token names to help reading parser output
1 parent a99d233 commit 12bd722

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

query/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (p *Parser) Parse() *graph.Graph {
3636
for p.currentToken.Type != END {
3737
ast.AddVertex(graph.Vertex{
3838
Id: uint64(p.position + 1),
39-
Label: fmt.Sprint(p.currentToken.Type),
39+
Label: p.currentToken.String(),
4040
})
4141
p.readToken()
4242
}

query/tokens.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package query
22

3+
import "fmt"
4+
35
const (
46
TERM = iota
57
AND
@@ -18,3 +20,28 @@ type Token struct {
1820
Type TokenType
1921
Value string
2022
}
23+
24+
func (t *Token) String() string {
25+
switch t.Type {
26+
case TERM:
27+
return "TERM"
28+
case AND:
29+
return "AND"
30+
case OR:
31+
return "OR"
32+
case NOT:
33+
return "NOT"
34+
case LEFT_BRACKET:
35+
return "("
36+
case RIGHT_BRACKET:
37+
return ")"
38+
case SINGLE_QUOTE:
39+
return "'"
40+
case DOUBLE_QUOTE:
41+
return "\""
42+
case END:
43+
return "END"
44+
default:
45+
panic(fmt.Sprintf("Unknown token: %d", t.Type))
46+
}
47+
}

0 commit comments

Comments
 (0)