Skip to content

Commit a99d233

Browse files
committed
Start implementing a query parser
1 parent 4815158 commit a99d233

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

query/parser.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package query
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/msp301/zb/graph"
8+
)
9+
10+
type Parser struct {
11+
lexer *Lexer
12+
currentToken Token
13+
nextToken Token
14+
position int
15+
}
16+
17+
func NewParser(lexer *Lexer) *Parser {
18+
p := Parser{
19+
lexer: lexer,
20+
}
21+
p.readToken()
22+
p.readToken()
23+
p.position = 0
24+
return &p
25+
}
26+
27+
func (p *Parser) readToken() {
28+
p.currentToken = p.nextToken
29+
p.nextToken = p.lexer.NextToken()
30+
p.position = p.position + 1
31+
}
32+
33+
func (p *Parser) Parse() *graph.Graph {
34+
ast := graph.Directed()
35+
36+
for p.currentToken.Type != END {
37+
ast.AddVertex(graph.Vertex{
38+
Id: uint64(p.position + 1),
39+
Label: fmt.Sprint(p.currentToken.Type),
40+
})
41+
p.readToken()
42+
}
43+
44+
ast.Walk(func(vertex graph.Vertex, depth int) bool {
45+
indent := strings.Repeat("\t", depth)
46+
fmt.Printf("%s%s\n", indent, vertex.Label)
47+
return true
48+
}, -1)
49+
50+
return ast
51+
}

query/parser_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package query
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParse(t *testing.T) {
8+
lexer := New("test a thing")
9+
parser := NewParser(lexer)
10+
11+
// fmt.Println(parser.currentToken.Value)
12+
// fmt.Println(parser.nextToken.Value)
13+
// fmt.Println(parser.position)
14+
//
15+
// fmt.Println("-------")
16+
//
17+
// parser.readToken()
18+
// fmt.Println(parser.currentToken.Value)
19+
// fmt.Println(parser.nextToken.Value)
20+
// fmt.Println(parser.position)
21+
//
22+
// fmt.Println("-------")
23+
//
24+
// parser.readToken()
25+
// fmt.Println(parser.currentToken.Value)
26+
// fmt.Println(parser.nextToken.Value)
27+
// fmt.Println(parser.nextToken.Type)
28+
// fmt.Println(parser.position)
29+
//
30+
// fmt.Println("-------")
31+
//
32+
// parser.readToken()
33+
// fmt.Println(parser.currentToken.Value)
34+
// fmt.Println(parser.currentToken.Type)
35+
// fmt.Println(parser.nextToken.Value)
36+
// fmt.Println(parser.nextToken.Type)
37+
// fmt.Println(parser.position)
38+
39+
parser.Parse()
40+
41+
t.Fail()
42+
}

0 commit comments

Comments
 (0)