-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
342 lines (304 loc) · 7.52 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
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
package ast
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// Pos is a position marker within a file set. Use the FileSet's PosInfo
// method to obtain human-oriented details for the position.
type Pos int
// The Node interface is implemented by all AST nodes that result
// from parsing twik code.
type Node interface {
Pos() Pos
End() Pos
}
// Int represents an integer literal in parsed twik code.
type Int struct {
Input string
InputPos Pos
Value int64
}
func (l *Int) Pos() Pos { return l.InputPos }
func (l *Int) End() Pos { return l.InputPos + Pos(len(l.Input)) }
// Float represents a float literal in parsed twik code.
type Float struct {
Input string
InputPos Pos
Value float64
}
func (l *Float) Pos() Pos { return l.InputPos }
func (l *Float) End() Pos { return l.InputPos + Pos(len(l.Input)) }
// String represents a string literal in parsed twik code.
type String struct {
Input string
InputPos Pos
Value string
}
func (l *String) Pos() Pos { return l.InputPos }
func (l *String) End() Pos { return l.InputPos + Pos(len(l.Input)) }
// Symbol represents a symbol in parsed twik code.
type Symbol struct {
Name string
NamePos Pos
}
func (s *Symbol) Pos() Pos { return s.NamePos }
func (s *Symbol) End() Pos { return s.NamePos + Pos(len(s.Name)) }
// List represents a list of entries from parsed twik code.
type List struct {
LParens Pos
RParens Pos
Nodes []Node
}
func (s *List) Pos() Pos { return s.LParens }
func (s *List) End() Pos { return s.RParens + 1 }
// Root represents the root of parsed twik code.
type Root struct {
First Pos
After Pos
Nodes []Node
}
func (s *Root) Pos() Pos { return s.First }
func (s *Root) End() Pos { return s.After }
// Parse parses a byte slice containing twik code and returns
// the resulting parsed tree.
//
// Positioning information for the parsed code will be stored in
// fset under the given name.
func Parse(fset *FileSet, name string, code []byte) (Node, error) {
return ParseString(fset, name, string(code))
}
// ParseString parses a string containing twik code and returns
// the resulting parsed tree.
//
// Positioning information for the parsed code will be stored in
// fset under the given name.
func ParseString(fset *FileSet, name string, code string) (Node, error) {
base := fset.nextBase()
fset.files = append(fset.files, file{name: name, code: code, base: base})
p := parser{fset: fset, code: code, base: base}
root := Root{First: p.pos(0)}
node, err := p.next()
for err == nil {
root.Nodes = append(root.Nodes, node)
node, err = p.next()
}
if err != io.EOF {
if err == errOpened || err == errClosed {
return nil, p.ierrorf(p.i, "%v", err)
}
return nil, err
}
root.After = p.pos(p.i)
return &root, nil
}
type parser struct {
fset *FileSet
code string
base Pos
i int
}
var errClosed = errors.New("unexpected )")
var errOpened = errors.New("missing )")
type closedError struct {
}
func (p *parser) pos(i int) Pos {
return p.base + Pos(i)
}
func (p *parser) ierrorf(i int, format string, args ...interface{}) error {
pinfo := p.fset.PosInfo(p.pos(i))
return fmt.Errorf("%s %s", pinfo, fmt.Sprintf(format, args...))
}
func (p *parser) next() (Node, error) {
if p.i == len(p.code) {
return nil, io.EOF
}
r, size := utf8.DecodeRuneInString(p.code[p.i:])
for r == ';' || unicode.IsSpace(r) {
p.i += size
if r == ';' {
for p.i < len(p.code) && r != '\n' {
r, size = utf8.DecodeRuneInString(p.code[p.i:])
p.i += size
}
}
if p.i == len(p.code) {
return nil, io.EOF
}
r, size = utf8.DecodeRuneInString(p.code[p.i:])
}
start := p.i
p.i += size
if r == ')' {
return nil, errClosed
}
if r == '(' {
var nodes []Node
for {
node, err := p.next()
if err == errClosed {
break
}
if err == io.EOF {
return nil, errOpened
}
if err != nil {
return nil, err
}
nodes = append(nodes, node)
}
list := &List{
LParens: p.pos(start),
RParens: p.pos(p.i - 1),
Nodes: nodes,
}
return list, nil
}
if r == '-' && p.i < len(p.code) {
r, size = utf8.DecodeRuneInString(p.code[p.i:])
if r >= '0' && r <= '9' {
// It's a digit; consume minus now and fall onto number case.
p.i += size
}
}
// int, float
if r >= '0' && r <= '9' {
dot := false
for p.i < len(p.code) {
r, size = utf8.DecodeRuneInString(p.code[p.i:])
if r == '.' {
dot = true
} else if r == ')' || unicode.IsSpace(r) {
break
}
p.i += size
}
input := p.code[start:p.i]
if dot {
value, err := strconv.ParseFloat(input, 64)
if err != nil {
return nil, p.ierrorf(start, "invalid float literal: %s", input)
}
return &Float{Input: input, InputPos: p.pos(start), Value: value}, nil
} else {
value, err := strconv.ParseInt(input, 0, 64)
if err != nil {
return nil, p.ierrorf(start, "invalid int literal: %s", input)
}
return &Int{Input: input, InputPos: p.pos(start), Value: value}, nil
}
}
if r == '\'' {
var c rune
if p.i < len(p.code) {
c, size = utf8.DecodeRuneInString(p.code[p.i:])
p.i += size
if c == '\\' && p.i < len(p.code) {
c, size = utf8.DecodeRuneInString(p.code[p.i:])
p.i += size
} else if c == '\'' {
return nil, p.ierrorf(start, "invalid single quote")
}
}
if p.i == len(p.code) {
return nil, p.ierrorf(start, "invalid single quote")
}
r, size = utf8.DecodeRuneInString(p.code[p.i:])
p.i += size
if r != '\'' {
return nil, p.ierrorf(start, "unclosed single quote")
}
return &Int{Input: p.code[start:p.i], InputPos: p.pos(start), Value: int64(c)}, nil
}
// string
if r == '"' {
escaped := false
for {
if p.i == len(p.code) {
return nil, p.ierrorf(start, "unclosed string literal: %s", p.code[start:])
}
r, size = utf8.DecodeRuneInString(p.code[p.i:])
p.i += size
if r == '"' && !escaped {
break
}
escaped = r == '\\'
}
input := p.code[start:p.i]
value, err := strconv.Unquote(input)
if err != nil {
return nil, p.ierrorf(start, "invalid string literal: %s", input)
}
return &String{Input: input, InputPos: p.pos(start), Value: value}, nil
}
// symbol
for p.i < len(p.code) {
r, size = utf8.DecodeRuneInString(p.code[p.i:])
if r == ')' || unicode.IsSpace(r) {
break
}
p.i += size
}
symbol := &Symbol{
Name: p.code[start:p.i],
NamePos: p.pos(start),
}
return symbol, nil
}
// NewFileSet returns a new FileSet.
func NewFileSet() *FileSet {
return &FileSet{}
}
// FileSet holds positioning information for parsed twik code.
type FileSet struct {
files []file
}
type file struct {
name string
code string
base Pos
}
func (fset *FileSet) nextBase() Pos {
if len(fset.files) == 0 {
return 1
} else {
last := fset.files[len(fset.files)-1]
return last.base + Pos(len(last.code)) + 1
}
}
// PosInfo returns the line and column for pos, and the name the
// file containing that position was parsed with.
func (fset *FileSet) PosInfo(pos Pos) *PosInfo {
pinfo := &PosInfo{}
for _, f := range fset.files {
if pos <= f.base+Pos(len(f.code)) {
offset := int(pos - f.base)
code := f.code[:offset]
pinfo.Name = f.name
pinfo.Line = 1 + strings.Count(code, "\n")
if i := strings.LastIndex(code, "\n"); i >= 0 {
pinfo.Column = offset - i
} else {
pinfo.Column = 1 + len(code)
}
}
}
return pinfo
}
// PosInfo holds human-oriented positioning details about a Pos.
type PosInfo struct {
Name string
Line int
Column int
}
func (info *PosInfo) String() string {
name := "twik source"
if info.Name != "" {
name = info.Name
}
return fmt.Sprintf("%s:%d:%d:", name, info.Line, info.Column)
}