-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
168 lines (144 loc) · 3.41 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
package lunar
import (
"fmt"
"go/ast"
"go/types"
"io"
"log"
"golang.org/x/tools/go/loader"
)
type Parser struct {
prog *loader.Program
transient map[string]bool
testPkgName string // for testing purposes
}
func NewParser(prog *loader.Program) *Parser {
return &Parser{
prog: prog,
transient: make(map[string]bool),
}
}
func (p *Parser) ParseNode(w io.Writer, n ast.Node) (err error) {
// Handle panics
defer func() {
e := recover()
if e != nil {
switch e.(type) {
case ParseError, WriteError:
err = e.(error)
return
}
// Panic again if it wasn't a parse error
panic(e)
}
}()
writer := NewWriter(w)
p.parseNode(writer, n, true)
return nil
}
func (p *Parser) parseNode(w *Writer, n ast.Node, topLevel bool) {
switch t := n.(type) {
case *ast.GenDecl:
p.parseGenDecl(w, t, topLevel)
case *ast.BlockStmt:
p.parseBlockStmt(w, t)
case *ast.FuncDecl:
p.parseFuncDecl(w, t)
case *ast.File:
p.parseFile(w, t)
case *ast.Package:
p.parsePackage(w, t)
default:
p.log("Unhandled node type %T", t)
}
}
func (p *Parser) log(format string, args ...interface{}) {
log.Printf(format, args...)
}
func (p *Parser) error(node ast.Node, err string) {
panic(ParseError{node: node, err: err})
}
func (p *Parser) errorf(node ast.Node, format string, args ...interface{}) {
err := fmt.Sprintf(format, args...)
p.error(node, err)
}
func (p *Parser) exprType(x ast.Expr) types.Type {
pkg := p.nodePkg(x)
if typ := pkg.Info.TypeOf(x); typ != nil {
return typ.Underlying()
}
p.error(x, "Could not determine type of expr")
return nil // unreachable
}
func (p *Parser) exprTypeRaw(x ast.Expr) types.Type {
pkg := p.nodePkg(x)
if typ := pkg.Info.TypeOf(x); typ != nil {
return typ
}
p.error(x, "Could not determine type of expr")
return nil // unreachable
}
func (p *Parser) identObject(i *ast.Ident) types.Object {
pkg := p.nodePkg(i)
if obj := pkg.Info.ObjectOf(i); obj != nil {
return obj
}
p.error(i, "Could not determine ident object")
return nil // unreachable
}
func (p *Parser) importObject(i *ast.ImportSpec) types.Object {
pkg := p.nodePkg(i)
if obj := pkg.Info.Implicits[i]; obj != nil {
return obj
}
p.error(i, "Could not determine import object")
return nil // unreachable
}
func (p *Parser) exprTypeAndValue(x ast.Expr) types.TypeAndValue {
if p.testPkgName != "" {
// for testing purposes
return types.TypeAndValue{}
}
pkg := p.nodePkg(x)
if tav, ok := pkg.Info.Types[x]; ok {
return tav
}
p.error(x, "Could not determine type and value of expr")
return types.TypeAndValue{} // unreachable
}
func (p *Parser) pkgName(n ast.Node) string {
if p.testPkgName != "" {
// for testing purposes
return p.testPkgName
}
pkg := p.nodePkg(n)
return pkg.Pkg.Name()
}
func (p *Parser) nodePkg(n ast.Node) *loader.PackageInfo {
pkg, _, _ := p.prog.PathEnclosingInterval(n.Pos(), n.End())
if pkg == nil {
if ident, ok := n.(*ast.Ident); ok {
f := p.prog.Fset.File(n.Pos())
fmt.Println("ident", ident.Name, f, n.Pos())
}
panic("Could not get package for node")
p.errorf(n, "Could not get package for node %T", n)
}
return pkg
}
func (p *Parser) MarkTransientPackage(path string) {
p.transient[path] = true
}
func (p *Parser) IsTransientPkg(pkg *types.Package) bool {
if pkg == nil {
return false
}
return p.transient[pkg.Path()]
}
type ParseError struct {
node ast.Node
err string
}
func (e ParseError) Error() string {
return e.err
}