-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
98 lines (81 loc) · 2.21 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sql
import (
"fmt"
"io"
)
// SelectStatment respresents a SQL SELECT statement.
type SelectStatement struct {
Fields []string
TableName string
}
// Parser represents a parser.
type Parser struct {
s *Scanner
buf struct {
tok Token // last read token
lit string // last read literal
n int // buffer size (max=1)
}
}
// NewParser returns a new instance of Parser
func NewParser(r io.Reader) *Parser {
return &Parser{s: NewScanner(r)}
}
// scan returns the next token from the underlying scanner.
// If a token has been unscanned the read that instead.
func (p *Parser) scan() (tok Token, lit string) {
// If we have a token on the buffer, then return it.
if p.buf.n != 0 {
p.buf.n = 0
return p.buf.tok, p.buf.lit
}
// Otherwise read the next token from the scanner.
tok, lit = p.s.Scan()
// Save it to the buffer in case we unscan later.
p.buf.tok, p.buf.lit = tok, lit
return
}
// unscan pushes the previously read token back into the buffer
func (p *Parser) unscan() {
p.buf.n = 1
}
// scan IgnoreWhitespace scans the next non-whitespace token.
func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) {
tok, lit = p.scan()
if tok == WS {
tok, lit = p.scan()
}
return
}
func (p *Parser) Parse() (*SelectStatement, error) {
stmt := &SelectStatement{}
// First token should be a "SELECT" keyword.
if tok, lit := p.scanIgnoreWhitespace(); tok != SELECT {
return nil, fmt.Errorf("found %q, expected SELECT", lit)
}
// Next we should loop over all our comma-delimited fields.
for {
// Read a field.
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT && tok != ASTERISK {
return nil, fmt.Errorf("found %q, expected field", lit)
}
stmt.Fields = append(stmt.Fields, lit)
if tok, _ := p.scanIgnoreWhitespace(); tok != COMMA {
p.unscan()
break
}
}
// Next we should see the "FROM" keyword.
if tok, lit := p.scanIgnoreWhitespace(); tok != FROM {
return nil, fmt.Errorf("found %q, expected FROM", lit)
}
// Finally we should read the table name.
tok, lit := p.scanIgnoreWhitespace()
if tok != IDENT {
return nil, fmt.Errorf("found %q, expected table name", lit)
}
stmt.TableName = lit
// Return the successfully parsed statement.
return stmt, nil
}