-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.go
349 lines (290 loc) · 6.17 KB
/
lex.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Copyright 2020 Marius Ackerman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The lexer part of the AST
package ast
import (
"bytes"
"fmt"
"github.com/bruceiv/pegll/token"
"github.com/bruceiv/pegll/util/runeset"
)
// This file contains the AST components for lexical rules
// TriState has values: {Undefined, False, True}
type TriState int
const (
// Undefined is a TriState value
Undefined TriState = iota
// False is a TriState value
False
// True is a TriState value
True
)
type Any struct {
tok *token.Token
}
type AnyOf struct {
any *token.Token
strLit *token.Token
Set *runeset.RuneSet
}
type CharLiteral struct {
tok *token.Token
Literal []rune
}
type LexBracket struct {
leftBracket *token.Token
Type BracketType
Alternates []*RegExp
}
type BracketType int
const (
LexGroup BracketType = iota
LexOptional
LexZeroOrMore
LexOneOrMore
)
type LexBase interface {
isLexBase()
LexSymbol
Equal(LexBase) bool
}
func (*Any) isLexBase() {}
func (*AnyOf) isLexBase() {}
func (*CharLiteral) isLexBase() {}
func (*Not) isLexBase() {}
func (*UnicodeClass) isLexBase() {}
type LexRule struct {
Suppress bool
TokID *TokID
RegExp *RegExp
}
type LexSymbol interface {
isLexSymbol()
Lext() int
String() string
}
func (*Any) isLexSymbol() {}
func (*AnyOf) isLexSymbol() {}
func (*CharLiteral) isLexSymbol() {}
func (*LexBracket) isLexSymbol() {}
func (*Not) isLexSymbol() {}
func (*UnicodeClass) isLexSymbol() {}
type Not struct {
not *token.Token
strLit *token.Token
Set *runeset.RuneSet
}
type RegExp struct {
Symbols []LexSymbol
}
type StringLit struct {
tok *token.Token
}
type UnicodeClass struct {
tok *token.Token
Type UnicodeClassType
}
type UnicodeClassType int
const (
Letter UnicodeClassType = iota
Upcase
Lowcase
Number
Space
)
/*** Methods ***/
func (*Any) Equal(other LexBase) bool {
if other == nil {
return false
}
_, ok := other.(*Any)
return ok
}
func (a *Any) Lext() int {
return a.tok.Lext()
}
func (ao *AnyOf) Equal(other LexBase) bool {
if other == nil {
return false
}
ao1, ok := other.(*AnyOf)
if !ok {
return false
}
return ao.Set.Equal(ao1.Set)
}
func (a *AnyOf) Lext() int {
return a.any.Lext()
}
func NewCharLiteral(tok *token.Token, literal []rune) *CharLiteral {
return &CharLiteral{
tok: tok,
Literal: literal,
}
}
func (c *CharLiteral) Char() rune {
if c.Literal[1] == '\\' {
switch c.tok.Literal()[2] {
case '\'':
return '\''
case '\\':
return '\\'
case 't':
return '\t'
case 'n':
return '\n'
case 'r':
return '\r'
default:
panic(fmt.Sprintf("invalid '%c'", c.Literal[2]))
}
} else {
return c.Literal[1]
}
}
func (c *CharLiteral) Equal(other LexBase) bool {
if other == nil {
return false
}
c1, ok := other.(*CharLiteral)
if !ok {
return false
}
// fmt.Printf("'%c'.Equal('%c') = %t\n", c.Char(), c1.Char(), c.Char() == c1.Char())
return c.Char() == c1.Char()
}
func (c *CharLiteral) Lext() int {
return c.tok.Lext()
}
func (l *LexBracket) LeftBracket() string {
switch l.Type {
case LexGroup:
return "("
case LexOptional:
return "["
case LexZeroOrMore:
return "{"
case LexOneOrMore:
return "<"
}
panic("invalid")
}
func (l *LexBracket) RightBracket() string {
switch l.Type {
case LexGroup:
return ")"
case LexOptional:
return "]"
case LexZeroOrMore:
return "}"
case LexOneOrMore:
return ">"
}
panic("invalid")
}
// Returns the id of the lex rule
func (l *LexRule) ID() string {
return l.TokID.ID()
}
func (l *LexRule) Lext() int {
return l.TokID.Lext()
}
func (l *LexRule) String() string {
return fmt.Sprintf("%s : %s ;", l.ID(), l.RegExp)
}
func (b *LexBracket) Lext() int {
return b.leftBracket.Lext()
}
func (n *Not) Equal(other LexBase) bool {
if other == nil {
return false
}
n1, ok := other.(*Not)
if !ok {
return false
}
return n.Set.Equal(n1.Set)
}
func (n *Not) Lext() int {
return n.not.Lext()
}
func (re *RegExp) String() string {
w := new(bytes.Buffer)
for _, symbol := range re.Symbols {
fmt.Fprint(w, symbol)
}
return w.String()
}
func (u *UnicodeClass) Equal(other LexBase) bool {
if other == nil {
return false
}
u1, ok := other.(*UnicodeClass)
if !ok {
return false
}
return u.Type == u1.Type
}
func (u *UnicodeClass) Lext() int {
return u.Lext()
}
func (*Any) String() string {
return "."
}
func (a *AnyOf) String() string {
return fmt.Sprintf("any %s", string(a.strLit.Literal()))
}
func (c *CharLiteral) String() string {
return string(c.Literal)
}
func (lb *LexBracket) String() string {
w := new(bytes.Buffer)
fmt.Fprint(w, lb.LeftBracket())
for i, alt := range lb.Alternates {
if i > 0 {
fmt.Fprint(w, " | ")
}
fmt.Fprint(w, alt)
}
fmt.Fprint(w, lb.RightBracket())
return w.String()
}
func (n *Not) String() string {
return fmt.Sprintf("not %s", string(n.strLit.Literal()))
}
func (sl *StringLit) ID() string {
lit := sl.tok.Literal()
return string(lit[1 : len(lit)-1])
}
func (sl *StringLit) Literal() []rune {
return sl.tok.Literal()
}
func (sl *StringLit) Value() []rune {
return sl.tok.Literal()[1 : len(sl.tok.Literal())-1]
}
func (u *UnicodeClass) String() string {
return string(u.tok.Literal())
}
/*** Utils ***/
// StringLitToTokID returns a dummy TokID with ID = id
func StringLitToTokID(id *StringLit) *TokID {
return &TokID{
token.New(token.StringToType["tokid"],
id.tok.Lext()+1, id.tok.Rext()-1, id.tok.GetInput()),
}
}
// CharLitFromStringLit returns a dummy CharLiteral with Literal sl.Literal[i]
func CharLitFromStringLit(sl *StringLit, i int) *CharLiteral {
return NewCharLiteral(token.New(token.StringToType["char_lit"], sl.Lext()+i, sl.Lext()+i+1, sl.tok.GetInput()),
[]rune{'\'', sl.Value()[i], '\''})
}