Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / pattern / pattern.go
1 package pattern
2
3 import (
4         "fmt"
5         "go/token"
6         "reflect"
7         "strings"
8 )
9
10 var (
11         _ Node = Ellipsis{}
12         _ Node = Binding{}
13         _ Node = RangeStmt{}
14         _ Node = AssignStmt{}
15         _ Node = IndexExpr{}
16         _ Node = Ident{}
17         _ Node = Builtin{}
18         _ Node = String("")
19         _ Node = Any{}
20         _ Node = ValueSpec{}
21         _ Node = List{}
22         _ Node = GenDecl{}
23         _ Node = BinaryExpr{}
24         _ Node = ForStmt{}
25         _ Node = ArrayType{}
26         _ Node = DeferStmt{}
27         _ Node = MapType{}
28         _ Node = ReturnStmt{}
29         _ Node = SliceExpr{}
30         _ Node = StarExpr{}
31         _ Node = UnaryExpr{}
32         _ Node = SendStmt{}
33         _ Node = SelectStmt{}
34         _ Node = ImportSpec{}
35         _ Node = IfStmt{}
36         _ Node = GoStmt{}
37         _ Node = Field{}
38         _ Node = SelectorExpr{}
39         _ Node = StructType{}
40         _ Node = KeyValueExpr{}
41         _ Node = FuncType{}
42         _ Node = FuncLit{}
43         _ Node = FuncDecl{}
44         _ Node = Token(0)
45         _ Node = ChanType{}
46         _ Node = CallExpr{}
47         _ Node = CaseClause{}
48         _ Node = CommClause{}
49         _ Node = CompositeLit{}
50         _ Node = EmptyStmt{}
51         _ Node = SwitchStmt{}
52         _ Node = TypeSwitchStmt{}
53         _ Node = TypeAssertExpr{}
54         _ Node = TypeSpec{}
55         _ Node = InterfaceType{}
56         _ Node = BranchStmt{}
57         _ Node = IncDecStmt{}
58         _ Node = BasicLit{}
59         _ Node = Nil{}
60         _ Node = Object{}
61         _ Node = Function{}
62         _ Node = Not{}
63         _ Node = Or{}
64 )
65
66 type Function struct {
67         Name Node
68 }
69
70 type Token token.Token
71
72 type Nil struct {
73 }
74
75 type Ellipsis struct {
76         Elt Node
77 }
78
79 type IncDecStmt struct {
80         X   Node
81         Tok Node
82 }
83
84 type BranchStmt struct {
85         Tok   Node
86         Label Node
87 }
88
89 type InterfaceType struct {
90         Methods Node
91 }
92
93 type TypeSpec struct {
94         Name Node
95         Type Node
96 }
97
98 type TypeAssertExpr struct {
99         X    Node
100         Type Node
101 }
102
103 type TypeSwitchStmt struct {
104         Init   Node
105         Assign Node
106         Body   Node
107 }
108
109 type SwitchStmt struct {
110         Init Node
111         Tag  Node
112         Body Node
113 }
114
115 type EmptyStmt struct {
116 }
117
118 type CompositeLit struct {
119         Type Node
120         Elts Node
121 }
122
123 type CommClause struct {
124         Comm Node
125         Body Node
126 }
127
128 type CaseClause struct {
129         List Node
130         Body Node
131 }
132
133 type CallExpr struct {
134         Fun  Node
135         Args Node
136         // XXX handle ellipsis
137 }
138
139 // TODO(dh): add a ChanDir node, and a way of instantiating it.
140
141 type ChanType struct {
142         Dir   Node
143         Value Node
144 }
145
146 type FuncDecl struct {
147         Recv Node
148         Name Node
149         Type Node
150         Body Node
151 }
152
153 type FuncLit struct {
154         Type Node
155         Body Node
156 }
157
158 type FuncType struct {
159         Params  Node
160         Results Node
161 }
162
163 type KeyValueExpr struct {
164         Key   Node
165         Value Node
166 }
167
168 type StructType struct {
169         Fields Node
170 }
171
172 type SelectorExpr struct {
173         X   Node
174         Sel Node
175 }
176
177 type Field struct {
178         Names Node
179         Type  Node
180         Tag   Node
181 }
182
183 type GoStmt struct {
184         Call Node
185 }
186
187 type IfStmt struct {
188         Init Node
189         Cond Node
190         Body Node
191         Else Node
192 }
193
194 type ImportSpec struct {
195         Name Node
196         Path Node
197 }
198
199 type SelectStmt struct {
200         Body Node
201 }
202
203 type ArrayType struct {
204         Len Node
205         Elt Node
206 }
207
208 type DeferStmt struct {
209         Call Node
210 }
211
212 type MapType struct {
213         Key   Node
214         Value Node
215 }
216
217 type ReturnStmt struct {
218         Results Node
219 }
220
221 type SliceExpr struct {
222         X    Node
223         Low  Node
224         High Node
225         Max  Node
226 }
227
228 type StarExpr struct {
229         X Node
230 }
231
232 type UnaryExpr struct {
233         Op Node
234         X  Node
235 }
236
237 type SendStmt struct {
238         Chan  Node
239         Value Node
240 }
241
242 type Binding struct {
243         Name string
244         Node Node
245 }
246
247 type RangeStmt struct {
248         Key   Node
249         Value Node
250         Tok   Node
251         X     Node
252         Body  Node
253 }
254
255 type AssignStmt struct {
256         Lhs Node
257         Tok Node
258         Rhs Node
259 }
260
261 type IndexExpr struct {
262         X     Node
263         Index Node
264 }
265
266 type Node interface {
267         String() string
268         isNode()
269 }
270
271 type Ident struct {
272         Name Node
273 }
274
275 type Object struct {
276         Name Node
277 }
278
279 type Builtin struct {
280         Name Node
281 }
282
283 type String string
284
285 type Any struct{}
286
287 type ValueSpec struct {
288         Names  Node
289         Type   Node
290         Values Node
291 }
292
293 type List struct {
294         Head Node
295         Tail Node
296 }
297
298 type GenDecl struct {
299         Tok   Node
300         Specs Node
301 }
302
303 type BasicLit struct {
304         Kind  Node
305         Value Node
306 }
307
308 type BinaryExpr struct {
309         X  Node
310         Op Node
311         Y  Node
312 }
313
314 type ForStmt struct {
315         Init Node
316         Cond Node
317         Post Node
318         Body Node
319 }
320
321 type Or struct {
322         Nodes []Node
323 }
324
325 type Not struct {
326         Node Node
327 }
328
329 func stringify(n Node) string {
330         v := reflect.ValueOf(n)
331         var parts []string
332         parts = append(parts, v.Type().Name())
333         for i := 0; i < v.NumField(); i++ {
334                 //lint:ignore S1025 false positive in staticcheck 2019.2.3
335                 parts = append(parts, fmt.Sprintf("%s", v.Field(i)))
336         }
337         return "(" + strings.Join(parts, " ") + ")"
338 }
339
340 func (stmt AssignStmt) String() string     { return stringify(stmt) }
341 func (expr IndexExpr) String() string      { return stringify(expr) }
342 func (id Ident) String() string            { return stringify(id) }
343 func (spec ValueSpec) String() string      { return stringify(spec) }
344 func (decl GenDecl) String() string        { return stringify(decl) }
345 func (lit BasicLit) String() string        { return stringify(lit) }
346 func (expr BinaryExpr) String() string     { return stringify(expr) }
347 func (stmt ForStmt) String() string        { return stringify(stmt) }
348 func (stmt RangeStmt) String() string      { return stringify(stmt) }
349 func (typ ArrayType) String() string       { return stringify(typ) }
350 func (stmt DeferStmt) String() string      { return stringify(stmt) }
351 func (typ MapType) String() string         { return stringify(typ) }
352 func (stmt ReturnStmt) String() string     { return stringify(stmt) }
353 func (expr SliceExpr) String() string      { return stringify(expr) }
354 func (expr StarExpr) String() string       { return stringify(expr) }
355 func (expr UnaryExpr) String() string      { return stringify(expr) }
356 func (stmt SendStmt) String() string       { return stringify(stmt) }
357 func (spec ImportSpec) String() string     { return stringify(spec) }
358 func (stmt SelectStmt) String() string     { return stringify(stmt) }
359 func (stmt IfStmt) String() string         { return stringify(stmt) }
360 func (stmt IncDecStmt) String() string     { return stringify(stmt) }
361 func (stmt GoStmt) String() string         { return stringify(stmt) }
362 func (field Field) String() string         { return stringify(field) }
363 func (expr SelectorExpr) String() string   { return stringify(expr) }
364 func (typ StructType) String() string      { return stringify(typ) }
365 func (expr KeyValueExpr) String() string   { return stringify(expr) }
366 func (typ FuncType) String() string        { return stringify(typ) }
367 func (lit FuncLit) String() string         { return stringify(lit) }
368 func (decl FuncDecl) String() string       { return stringify(decl) }
369 func (stmt BranchStmt) String() string     { return stringify(stmt) }
370 func (expr CallExpr) String() string       { return stringify(expr) }
371 func (clause CaseClause) String() string   { return stringify(clause) }
372 func (typ ChanType) String() string        { return stringify(typ) }
373 func (clause CommClause) String() string   { return stringify(clause) }
374 func (lit CompositeLit) String() string    { return stringify(lit) }
375 func (stmt EmptyStmt) String() string      { return stringify(stmt) }
376 func (typ InterfaceType) String() string   { return stringify(typ) }
377 func (stmt SwitchStmt) String() string     { return stringify(stmt) }
378 func (expr TypeAssertExpr) String() string { return stringify(expr) }
379 func (spec TypeSpec) String() string       { return stringify(spec) }
380 func (stmt TypeSwitchStmt) String() string { return stringify(stmt) }
381 func (nil Nil) String() string             { return "nil" }
382 func (builtin Builtin) String() string     { return stringify(builtin) }
383 func (obj Object) String() string          { return stringify(obj) }
384 func (fn Function) String() string         { return stringify(fn) }
385 func (el Ellipsis) String() string         { return stringify(el) }
386 func (not Not) String() string             { return stringify(not) }
387
388 func (or Or) String() string {
389         s := "(Or"
390         for _, node := range or.Nodes {
391                 s += " "
392                 s += node.String()
393         }
394         s += ")"
395         return s
396 }
397
398 func isProperList(l List) bool {
399         if l.Head == nil && l.Tail == nil {
400                 return true
401         }
402         switch tail := l.Tail.(type) {
403         case nil:
404                 return false
405         case List:
406                 return isProperList(tail)
407         default:
408                 return false
409         }
410 }
411
412 func (l List) String() string {
413         if l.Head == nil && l.Tail == nil {
414                 return "[]"
415         }
416
417         if isProperList(l) {
418                 // pretty-print the list
419                 var objs []string
420                 for l.Head != nil {
421                         objs = append(objs, l.Head.String())
422                         l = l.Tail.(List)
423                 }
424                 return fmt.Sprintf("[%s]", strings.Join(objs, " "))
425         }
426
427         return fmt.Sprintf("%s:%s", l.Head, l.Tail)
428 }
429
430 func (bind Binding) String() string {
431         if bind.Node == nil {
432                 return bind.Name
433         }
434         return fmt.Sprintf("%s@%s", bind.Name, bind.Node)
435 }
436
437 func (s String) String() string { return fmt.Sprintf("%q", string(s)) }
438
439 func (tok Token) String() string {
440         return fmt.Sprintf("%q", strings.ToUpper(token.Token(tok).String()))
441 }
442
443 func (Any) String() string { return "_" }
444
445 func (AssignStmt) isNode()     {}
446 func (IndexExpr) isNode()      {}
447 func (Ident) isNode()          {}
448 func (ValueSpec) isNode()      {}
449 func (GenDecl) isNode()        {}
450 func (BasicLit) isNode()       {}
451 func (BinaryExpr) isNode()     {}
452 func (ForStmt) isNode()        {}
453 func (RangeStmt) isNode()      {}
454 func (ArrayType) isNode()      {}
455 func (DeferStmt) isNode()      {}
456 func (MapType) isNode()        {}
457 func (ReturnStmt) isNode()     {}
458 func (SliceExpr) isNode()      {}
459 func (StarExpr) isNode()       {}
460 func (UnaryExpr) isNode()      {}
461 func (SendStmt) isNode()       {}
462 func (ImportSpec) isNode()     {}
463 func (SelectStmt) isNode()     {}
464 func (IfStmt) isNode()         {}
465 func (IncDecStmt) isNode()     {}
466 func (GoStmt) isNode()         {}
467 func (Field) isNode()          {}
468 func (SelectorExpr) isNode()   {}
469 func (StructType) isNode()     {}
470 func (KeyValueExpr) isNode()   {}
471 func (FuncType) isNode()       {}
472 func (FuncLit) isNode()        {}
473 func (FuncDecl) isNode()       {}
474 func (BranchStmt) isNode()     {}
475 func (CallExpr) isNode()       {}
476 func (CaseClause) isNode()     {}
477 func (ChanType) isNode()       {}
478 func (CommClause) isNode()     {}
479 func (CompositeLit) isNode()   {}
480 func (EmptyStmt) isNode()      {}
481 func (InterfaceType) isNode()  {}
482 func (SwitchStmt) isNode()     {}
483 func (TypeAssertExpr) isNode() {}
484 func (TypeSpec) isNode()       {}
485 func (TypeSwitchStmt) isNode() {}
486 func (Nil) isNode()            {}
487 func (Builtin) isNode()        {}
488 func (Object) isNode()         {}
489 func (Function) isNode()       {}
490 func (Ellipsis) isNode()       {}
491 func (Or) isNode()             {}
492 func (List) isNode()           {}
493 func (String) isNode()         {}
494 func (Token) isNode()          {}
495 func (Any) isNode()            {}
496 func (Binding) isNode()        {}
497 func (Not) isNode()            {}