Debian Codegraph @

format/rewrite.go

raw symbols SCIP
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package format
6
7 import (
8 "go/ast"
9 "go/token"
10 "reflect"
11 "unicode"
12 "unicode/utf8"
13 )
14
15 // Values/types for special cases.
16 var (
17 identType = reflect.TypeOf((*ast.Ident)(nil))
18 objectPtrType = reflect.TypeOf((*ast.Object)(nil))
19 positionType = reflect.TypeOf(token.NoPos)
20 callExprType = reflect.TypeOf((*ast.CallExpr)(nil))
21 )
22
23 func isWildcard(s string) bool {
24 rune, size := utf8.DecodeRuneInString(s)
25 return size == len(s) && unicode.IsLower(rune)
26 }
27
28 // match reports whether pattern matches val,
29 // recording wildcard submatches in m.
30 // If m == nil, match checks whether pattern == val.
31 func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
32 // Wildcard matches any expression. If it appears multiple
33 // times in the pattern, it must match the same expression
34 // each time.
35 if m != nil && pattern.IsValid() && pattern.Type() == identType {
36 name := pattern.Interface().(*ast.Ident).Name
37 if isWildcard(name) && val.IsValid() {
38 // wildcards only match valid (non-nil) expressions.
39 if _, ok := val.Interface().(ast.Expr); ok && !val.IsNil() {
40 if old, ok := m[name]; ok {
41 return match(nil, old, val)
42 }
43 m[name] = val
44 return true
45 }
46 }
47 }
48
49 // Otherwise, pattern and val must match recursively.
50 if !pattern.IsValid() || !val.IsValid() {
51 return !pattern.IsValid() && !val.IsValid()
52 }
53 if pattern.Type() != val.Type() {
54 return false
55 }
56
57 // Special cases.
58 switch pattern.Type() {
59 case identType:
60 // For identifiers, only the names need to match
61 // (and none of the other *ast.Object information).
62 // This is a common case, handle it all here instead
63 // of recursing down any further via reflection.
64 p := pattern.Interface().(*ast.Ident)
65 v := val.Interface().(*ast.Ident)
66 return p == nil && v == nil || p != nil && v != nil && p.Name == v.Name
67 case objectPtrType, positionType:
68 // object pointers and token positions always match
69 return true
70 case callExprType:
71 // For calls, the Ellipsis fields (token.Position) must
72 // match since that is how f(x) and f(x...) are different.
73 // Check them here but fall through for the remaining fields.
74 p := pattern.Interface().(*ast.CallExpr)
75 v := val.Interface().(*ast.CallExpr)
76 if p.Ellipsis.IsValid() != v.Ellipsis.IsValid() {
77 return false
78 }
79 }
80
81 p := reflect.Indirect(pattern)
82 v := reflect.Indirect(val)
83 if !p.IsValid() || !v.IsValid() {
84 return !p.IsValid() && !v.IsValid()
85 }
86
87 switch p.Kind() {
88 case reflect.Slice:
89 if p.Len() != v.Len() {
90 return false
91 }
92 for i := 0; i < p.Len(); i++ {
93 if !match(m, p.Index(i), v.Index(i)) {
94 return false
95 }
96 }
97 return true
98
99 case reflect.Struct:
100 for i := 0; i < p.NumField(); i++ {
101 if !match(m, p.Field(i), v.Field(i)) {
102 return false
103 }
104 }
105 return true
106
107 case reflect.Interface:
108 return match(m, p.Elem(), v.Elem())
109 }
110
111 // Handle token integers, etc.
112 return p.Interface() == v.Interface()
113 }