forked from tbaud0n/go-rql-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexer.go
201 lines (169 loc) · 3.68 KB
/
Lexer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package rqlParser
import (
"bufio"
"bytes"
"fmt"
"io"
)
const (
// Special tokens
ILLEGAL Token = iota
EOF
// Literals
IDENT // fields, function names
// Reserved characters
SPACE //
AMPERSAND // &
OPENING_PARENTHESIS // (
CLOSING_PARENTHESIS // )
COMMA // ,
EQUAL_SIGN // =
SLASH // /
SEMI_COLON // ;
QUESTION_MARK // ?
AT_SYMBOL // @
PIPE // |
// Keywords
AND
OR
EQUAL
GREATER
GREATER_OR_EQUAL
LOWER
LOWER_OR_EQUAL
NOT_EQUAL
)
var (
ReservedRunes []rune = []rune{' ', '&', '(', ')', ',', '=', '/', ';', '?', '@', '|'}
eof = rune(0)
)
type TokenString struct {
t Token
s string
}
type Token int
func NewTokenString(t Token, s string) TokenString {
return TokenString{t: t, s: s}
}
type Scanner struct {
r *bufio.Reader
}
func NewScanner() *Scanner {
return &Scanner{}
}
// Scan returns the next token and literal value.
func (s *Scanner) Scan(r io.Reader) (out []TokenString, err error) {
s.r = bufio.NewReader(r)
for true {
tok, lit := s.ScanToken()
if tok == EOF {
break
} else if tok == ILLEGAL {
return out, fmt.Errorf("Illegal Token : %s", lit)
} else {
out = append(out, NewTokenString(tok, lit))
}
}
return
}
func (s *Scanner) ScanToken() (tok Token, lit string) {
ch := s.read()
if isReservedRune(ch) {
s.unread()
return s.scanReservedRune()
} else if isIdent(ch) {
s.unread()
return s.scanIdent()
}
if ch == eof {
return EOF, ""
}
return ILLEGAL, string(ch)
}
func (s *Scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *Scanner) unread() { _ = s.r.UnreadRune() }
func (s *Scanner) scanReservedRune() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
lit = buf.String()
// Read every subsequent whitespace character into the buffer.
// Non-whitespace characters and EOF will cause the loop to exit.
for _, rr := range ReservedRunes {
if string(rr) == lit {
switch rr {
case '&':
return AMPERSAND, lit
case '(':
return OPENING_PARENTHESIS, lit
case ')':
return CLOSING_PARENTHESIS, lit
case ',':
return COMMA, lit
case '=':
return EQUAL_SIGN, lit
case '/':
return SLASH, lit
case ';':
return SEMI_COLON, lit
case '?':
return QUESTION_MARK, lit
case '@':
return AT_SYMBOL, lit
case '|':
return PIPE, lit
case eof:
return EOF, lit
default:
return ILLEGAL, lit
}
}
}
return ILLEGAL, lit
}
func isReservedRune(ch rune) bool {
for _, rr := range ReservedRunes {
if ch == rr {
return true
}
}
return false
}
func isIdent(ch rune) bool {
return isLetter(ch) || isDigit(ch) || isSpecialChar(ch)
}
func isSpecialChar(ch rune) bool {
return ch == '*' || ch == '_' || ch == '%' ||
ch == '+' || ch == '-' || ch == '.'
}
// isLetter returns true if the rune is a letter.
func isLetter(ch rune) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
// isDigit returns true if the rune is a digit.
func isDigit(ch rune) bool { return (ch >= '0' && ch <= '9') }
func (s *Scanner) scanIdent() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isIdent(ch) {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
return IDENT, buf.String()
}