/
parser.go
485 lines (402 loc) · 11.5 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package node
import (
"fmt"
"go/token"
"go/scanner"
"go/ast"
"io/ioutil"
"errors"
"io"
"bytes"
"strings"
"./vector"
"go/parser"
)
// The parser structure holds the parser's internal state.
type parser struct {
file *token.File
errors scanner.ErrorList
scanner scanner.Scanner
// Tracing/debugging
trace bool // == (mode & Trace != 0)
indent int // indentation used for tracing output
// Comments
comments []*ast.CommentGroup
leadComment *ast.CommentGroup // last lead comment
lineComment *ast.CommentGroup // last line comment
// Next token
pos token.Pos // token position
tok token.Token // one token look-ahead
lit string // token literal
// Error recovery
// (used to limit the number of calls to syncXXX functions
// w/o making scanning progress - avoids potential endless
// loops across multiple parser functions during error recovery)
syncPos token.Pos // last synchronization position
syncCnt int // number of calls to syncXXX without progress
// Non-syntactic parser control
exprLev int // < 0: in control clause, >= 0: in expression
inRhs bool // if set, the parser is parsing a rhs expression
// Ordinary identifier scopes
pkgScope *ast.Scope // pkgScope.Outer == nil
topScope *ast.Scope // top-most scope; may be pkgScope
unresolved []*ast.Ident // unresolved identifiers
imports []*ast.ImportSpec // list of imports
// Label scopes
// (maintained by open/close LabelScope)
labelScope *ast.Scope // label scope for current function
targetStack [][]*ast.Ident // stack of unresolved labels
}
// parseOperand may return an expression or a raw type (incl. array
// types of the form [...]T. Callers must verify the result.
// If lhs is set and the result is an identifier, it is not resolved.
//
func (p *parser) parseOperand(lhs bool) ast.Expr {
switch p.tok {
case token.IDENT:
x := p.parseIdent()
// if !lhs {
// p.resolve(x)
// }
return x
case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
x := &ast.BasicLit{ValuePos: p.pos, Kind: p.tok, Value: p.lit}
p.next()
return x
case token.LPAREN:
lparen := p.pos
p.next()
p.exprLev++
x := p.parseOperand(false)
p.exprLev--
rparen := p.expect(token.RPAREN)
return &ast.ParenExpr{Lparen: lparen, X: x, Rparen: rparen}
default:
p.errorExpected(p.pos, "operand")
return &ast.BadExpr{From: p.pos - 10, To: p.pos + 10}
}
}
// Advance to the next non-comment token. In the process, collect
// any comment groups encountered, and remember the last lead and
// and line comments.
//
// A lead comment is a comment group that starts and ends in a
// line without any other tokens and that is followed by a non-comment
// token on the line immediately after the comment group.
//
// A line comment is a comment group that follows a non-comment
// token on the same line, and that has no tokens after it on the line
// where it ends.
//
// Lead and line comments may be considered documentation that is
// stored in the AST.
//
func (p *parser) next() {
p.leadComment = nil
p.lineComment = nil
// prev := p.pos
p.pos, p.tok, p.lit = p.scanner.Scan()
}
func (p *parser) printTrace(a ...interface{}) {
const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
const n = len(dots)
pos := p.file.Position(p.pos)
fmt.Printf("%5d:%3d: ", pos.Line, pos.Column)
i := 2 * p.indent
for i > n {
fmt.Print(dots)
i -= n
}
// i <= n
fmt.Print(dots[0:i])
fmt.Println(a...)
}
func trace(p *parser, msg string) *parser {
p.printTrace(msg, "(")
p.indent++
return p
}
// Usage pattern: defer un(trace(p, "..."))
func un(p *parser) {
p.indent--
p.printTrace(")")
}
func (p *parser) init(fset *token.FileSet, filename string, src []byte) {
p.file = fset.AddFile(filename, -1, len(src))
var m scanner.Mode
eh := func(pos token.Position, msg string) { p.errors.Add(pos, msg) }
p.scanner.Init(p.file, src, eh, m)
p.next()
}
// A bailout panic is raised to indicate early termination.
type bailout struct{}
func (p *parser) error(pos token.Pos, msg string) {
epos := p.file.Position(pos)
p.errors.Add(epos, msg)
}
func (p *parser) errorExpected(pos token.Pos, msg string) {
msg = "expected " + msg
if pos == p.pos {
// the error happened at the current position;
// make the error message more specific
if p.tok == token.SEMICOLON && p.lit == "\n" {
msg += ", found newline"
} else {
msg += ", found '" + p.tok.String() + "'"
if p.tok.IsLiteral() {
msg += " " + p.lit
}
}
}
p.error(pos, msg)
}
func (p *parser) expect(tok token.Token) token.Pos {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next() // make progress
return pos
}
func assert(cond bool, msg string) {
if !cond {
panic("go/parser internal error: " + msg)
}
}
// The unresolved object is a sentinel to mark identifiers that have been added
// to the list of unresolved identifiers. The sentinel is only used for verifying
// internal consistency.
var unresolved = new(ast.Object)
// If x is an identifier, tryResolve attempts to resolve x by looking up
// the object it denotes. If no object is found and collectUnresolved is
// set, x is marked as unresolved and collected in the list of unresolved
// identifiers.
//
func (p *parser) tryResolve(x ast.Expr, collectUnresolved bool) {
// nothing to do if x is not an identifier or the blank identifier
ident, _ := x.(*ast.Ident)
if ident == nil {
return
}
assert(ident.Obj == nil, "identifier already declared or resolved")
if ident.Name == "_" {
return
}
// try to resolve the identifier
for s := p.topScope; s != nil; s = s.Outer {
if obj := s.Lookup(ident.Name); obj != nil {
ident.Obj = obj
return
}
}
// all local scopes are known, so any unresolved identifier
// must be found either in the file scope, package scope
// (perhaps in another file), or universe scope --- collect
// them so that they can be resolved later
if collectUnresolved {
ident.Obj = unresolved
p.unresolved = append(p.unresolved, ident)
}
}
func (p *parser) resolve(x ast.Expr) {
p.tryResolve(x, true)
}
// ----------------------------------------------------------------------------
// Identifiers
func (p *parser) parseIdent() *ast.Ident {
pos := p.pos
name := "_"
if p.tok == token.IDENT {
name = p.lit
p.next()
} else {
p.expect(token.IDENT) // use expect() error handling
}
return &ast.Ident{NamePos: pos, Name: name}
}
// If src != nil, readSource converts src to a []byte if possible;
// otherwise it returns an error. If src == nil, readSource returns
// the result of reading the file specified by filename.
//
func readSource(filename string, src interface{}) ([]byte, error) {
if src != nil {
switch s := src.(type) {
case string:
return []byte(s), nil
case []byte:
return s, nil
case *bytes.Buffer:
// is io.Reader, but src is already available in []byte form
if s != nil {
return s.Bytes(), nil
}
case io.Reader:
var buf bytes.Buffer
if _, err := io.Copy(&buf, s); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
return nil, errors.New("invalid source")
}
return ioutil.ReadFile(filename)
}
// ParseFile parses the source code of a single Go source file and returns
// the corresponding ast.File node. The source code may be provided via
// the filename of the source file, or via the src parameter.
//
// If src != nil, ParseFile parses the source from src and the filename is
// only used when recording position information. The type of the argument
// for the src parameter must be string, []byte, or io.Reader.
// If src == nil, ParseFile parses the file specified by filename.
//
// The mode parameter controls the amount of source text parsed and other
// optional parser functionality. Position information is recorded in the
// file set fset.
//
// If the source couldn't be read, the returned AST is nil and the error
// indicates the specific failure. If the source was read but syntax
// errors were found, the result is a partial AST (with ast.Bad* nodes
// representing the fragments of erroneous source code). Multiple errors
// are returned via a scanner.ErrorList which is sorted by file position.
//
func ParseFile(fset *token.FileSet, filename string, src interface{}) *ast.File {
// get source
text, err := readSource(filename, src)
if err != nil {
return nil
}
var p parser
// parse source
p.init(fset, filename, text)
// Don't bother parsing the rest if we had errors scanning the first token.
// Likely not a Go source file at all.
if p.errors.Len() != 0 {
return nil
}
// xxx := p.parseOperand(false)
return &ast.File{
Name: &ast.Ident{Name: "p"},
Decls: []ast.Decl{
&ast.FuncDecl{
Name: &ast.Ident{Name: "f"},
Type: &ast.FuncType{},
},
},
}
}
func StringsToArray(arr0 []string) []interface{} {
res := make([]interface{}, len(arr0))
for i,x := range arr0 {
res[i] = x
}
return res
}
func ArrayJoin(arr []interface{}, sep interface{}) []interface{} {
if len(arr) == 0 {
return []interface{}{}
} else {
var res []interface{}
res = append(res, arr[0])
for _, z := range arr[1:] {
res = append(res, sep)
res = append(res, z)
}
return res
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
func oldImplementation() {
dat, err := ioutil.ReadFile("/Users/obaskakov/IdeaProjects/goCrazy/code.scm")
check(err)
code := string(dat)
lines := strings.Split(code, "\n")
lines1 := StringsToArray(lines)
// fmt.Println(len(lines1))
tokens := vector.FromArray(lines1).FlatMap(func(x interface{}) []interface{} {
lll := x.(string)
tmp := strings.Split(lll, "\t")
return StringsToArray(tmp)
}).FlatMap(func(x interface{}) []interface{} {
return StringsToArray(strings.Split(x.(string), " "))
}).FlatMap(func(x interface{}) []interface{} {
tmp := StringsToArray(strings.Split(x.(string), "("))
return ArrayJoin(tmp, "(")
}).FlatMap(func(x interface{}) []interface{} {
tmp := StringsToArray(strings.Split(x.(string), ")"))
return ArrayJoin(tmp, ")")
}).Filter(func(x interface{}) bool {
return len(x.(string)) != 0
}).ToArray()
fmt.Println("tokens:")
for _, x := range tokens {
fmt.Println(x)
}
fmt.Println("----")
}
func oldImplementation2() {
ll := vector.New()
ll.PushBack("dasdsa")
ll.PushBack("das")
// fmt.Println("----")
// fmt.Println(e4.Value)
tmp2 := ll.
Map(func(x interface{}) interface{} {
return len(x.(string))
}).Map(func(x interface{}) interface{} {
return x.(int) + 1
})
fmt.Println("----")
// Iterate through list and print its contents.
for e := tmp2.Front(); e != nil; e = e.Next() {
fmt.Println("val =", e.Value)
}
fmt.Println("----")
fmt.Println(tmp2.ToArray())
}
func main3() {
// fset := token.NewFileSet()
//
//// decl := "func sum(x, y int) int\t{ return x + y }";
//// file, err := ParseFile(fset, "", "package p;"+decl, 0)
// if err != nil {
// panic(err) // error in test
// }
// fmt.Println(file)
//
// fmt.Println("------------------")
// err = Print(fset, file)
//
// // fmt.Println(res)
//
//
//
// // var buf bytes.Buffer
// fset = token.NewFileSet()
}
// filename := "/Users/obaskakov/IdeaProjects/goCrazy/code.scm"
// src := node.ParseFile(fset, filename, nil)
// var s simplifierX
// ast.Walk(&s, src)
// src := *ast.File {
// Package: 0,
// Name: *ast.Ident {
// NamePos: 0,
// Name: "p",
// },
// Decls: []ast.Decl {
// *ast.FuncDecl {
// Name: *ast.Ident {
// NamePos: 0,
// Name: "f",
// },
// Type: *ast.FuncType {
// Func: 0,
// },
// },
//},
//}