mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
symbols.go
274 lines (246 loc) · 7.03 KB
/
symbols.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
// Copyright 2019 The LUCI Authors.
//
// 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.
// Package symbols defines a data model representing Starlark symbols.
//
// A symbol is a like a variable: it has a name and points to some object
// somewhere. This package allows to load symbols defined in a starlark module,
// following references. For example, if "a = b", then symbol 'a' points to the
// same object as symbol 'b'.
//
// The loader understands how to follow references across module boundaries and
// struct()s.
package symbols
import (
"fmt"
"go.chromium.org/luci/lucicfg/docgen/ast"
"go.chromium.org/luci/lucicfg/docgen/docstring"
)
// Symbol is something defined in a Starlark module.
//
// It has a name and it points to some declaration.
type Symbol interface {
// Name is a name of this symbol within its parent namespace.
//
// E.g. this is just "a", not "parent.a".
Name() string
// Def is an AST node where the object this symbol points to was defined.
//
// Nil for broken symbols.
Def() ast.Node
// Doc is a parsed docstring for this symbol.
Doc() *docstring.Parsed
}
// symbol is common base for different types of symbols.
//
// Implements Symbol interface for them.
type symbol struct {
name string
def ast.Node
doc *docstring.Parsed
}
func (s *symbol) Name() string { return s.name }
func (s *symbol) Def() ast.Node { return s.def }
func (s *symbol) Doc() *docstring.Parsed {
if s.doc == nil {
if s.def != nil {
s.doc = docstring.Parse(s.def.Doc())
} else {
s.doc = &docstring.Parsed{Description: "broken"}
}
}
return s.doc
}
func (s *symbol) String() string {
node := s.Def()
pos, _ := node.Span()
return fmt.Sprintf("%s = %s %T at %s", s.name, node.Name(), node, pos)
}
// BrokenSymbol is a symbol that refers to something we can't resolve.
//
// For example, if "b" is undefined in "a = b", then "a" becomes BrokenSymbol.
type BrokenSymbol struct {
symbol
}
// newBrokenSymbol returns a new broken symbol with the given name.
func newBrokenSymbol(name string) *BrokenSymbol {
return &BrokenSymbol{
symbol: symbol{
name: name,
},
}
}
// Term is a symbol that represents some single terminal definition, not a
// struct nor a function invocation.
type Term struct {
symbol
}
// newTerm returns a new Term symbol.
func newTerm(name string, def ast.Node) *Term {
return &Term{
symbol: symbol{
name: name,
def: def,
},
}
}
// Invocation is a symbol assigned a return value of some function call.
//
// The name of the function, as well as value of all keyword arguments, are
// represented by symbols too.
type Invocation struct {
symbol
fn Symbol
args []Symbol
}
// newInvocation returns a new Invocation symbol.
func newInvocation(name string, def ast.Node, fn Symbol, args []Symbol) *Invocation {
return &Invocation{
symbol: symbol{
name: name,
def: def,
},
fn: fn,
args: args,
}
}
// Func is a symbol that represents the function being invoked.
func (inv *Invocation) Func() Symbol { return inv.fn }
// Args is keyword arguments passed to the function.
func (inv *Invocation) Args() []Symbol { return inv.args }
// Struct is a symbol that represents a struct (or equivalent) that has more
// symbols inside it.
//
// Basically, a struct symbol is something that supports "." operation to
// "look" inside it.
type Struct struct {
symbol
// symbols is a list of symbols inside this struct.
symbols []Symbol
// frozen is true if 'symbols' must not be modified anymore.
frozen bool
}
// newStruct returns a new struct with empty list of symbols inside.
//
// The caller then can populate it via AddSymbol and finalize with Freeze when
// done.
func newStruct(name string, def ast.Node) *Struct {
return &Struct{
symbol: symbol{
name: name,
def: def,
},
}
}
// addSymbol appends a symbol to the symbols list in the struct.
//
// Note that starlark forbids reassigning variables in the module scope, so we
// don't check that 'sym' wasn't added before.
//
// Panics if the struct is frozen.
func (s *Struct) addSymbol(sym Symbol) {
if s.frozen {
panic("frozen")
}
s.symbols = append(s.symbols, sym)
}
// freeze makes the struct immutable.
func (s *Struct) freeze() {
s.frozen = true
}
// Symbols returns all symbols in the struct.
//
// The caller must not modify the returned slice.
func (s *Struct) Symbols() []Symbol {
return s.symbols
}
// Transform returns a new struct made by applying a transformation to the
// receiver struct, recursively.
func (s *Struct) Transform(tr func(Symbol) (Symbol, error)) (*Struct, error) {
out := &Struct{
symbol: s.symbol,
symbols: make([]Symbol, 0, len(s.symbols)),
}
for _, sym := range s.symbols {
// Recursive branch.
if strct, ok := sym.(*Struct); ok {
t, err := strct.Transform(tr)
if err != nil {
return nil, err
}
out.symbols = append(out.symbols, t)
continue
}
// Leafs.
switch t, err := tr(sym); {
case err != nil:
return nil, err
case t != nil:
out.symbols = append(out.symbols, t)
}
}
out.frozen = true
return out, nil
}
// Lookup essentially does "ns.p0.p1.p2" operation.
//
// Returns a broken symbol if this lookup is not possible, e.g. some field path
// element is not a struct or doesn't exist at all.
func Lookup(ns Symbol, path ...string) Symbol {
cur := ns
for _, p := range path {
var next Symbol
if strct, _ := cur.(*Struct); strct != nil {
for _, sym := range strct.symbols {
if sym.Name() == p {
next = sym
break
}
}
}
if next == nil {
return newBrokenSymbol(p)
}
cur = next
}
return cur
}
// NewAlias handles definitions like "a = <symbol>".
//
// It returns a new symbol of the same type as the RHS and new name ('a'). It
// points to the same definition the symbol on the RHS points to.
func NewAlias(name string, symbol Symbol) Symbol {
switch s := symbol.(type) {
case *BrokenSymbol:
return newBrokenSymbol(name)
case *Term:
return newTerm(name, s.Def())
case *Invocation:
return newInvocation(name, s.Def(), s.fn, s.args)
case *Struct:
// Structs are copied by value too, but we check they are frozen at this
// point, so it should be fine. It is possible the struct is not frozen yet
// in the following self-referential case: 'a = {}; a = struct(k = a)'. But
// it is not allowed by starlark (redefinitions are forbidden). We still
// cautiously treat this case as broken.
if !s.frozen {
return newBrokenSymbol(name)
}
strct := newStruct(name, s.Def())
strct.symbols = s.symbols // it is immutable, copying the pointer is fine
strct.freeze()
return strct
default:
panic(fmt.Sprintf("unrecognized symbol type %T", s))
}
}