This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
legacy.go
178 lines (163 loc) · 3.75 KB
/
legacy.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
package uast1
import (
"fmt"
"sort"
"strings"
"github.com/bblfsh/sdk/v3/uast"
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/bblfsh/sdk/v3/uast/role"
uast1 "gopkg.in/bblfsh/sdk.v1/uast"
)
// ToNode converts a generic AST node to Node object used in the protocol.
func ToNode(n nodes.Node) (*uast1.Node, error) {
nd, err := asNode(n, "")
if err != nil {
return nil, err
}
switch len(nd) {
case 0:
return nil, nil
case 1:
return nd[0], nil
default:
return &uast1.Node{Children: nd}, nil
}
}
func arrayAsNode(n nodes.Array, field string) ([]*uast1.Node, error) {
arr := make([]*uast1.Node, 0, len(n))
for _, s := range n {
nd, err := asNode(s, field)
if err != nil {
return arr, err
}
arr = append(arr, nd...)
}
return arr, nil
}
func pos(p *uast.Position) *uast1.Position {
return (*uast1.Position)(p)
}
func roles(arr role.Roles) []uast1.Role {
out := make([]uast1.Role, 0, len(arr))
for _, r := range arr {
out = append(out, uast1.Role(r))
}
return out
}
func objectAsNode(n nodes.Object, field string) ([]*uast1.Node, error) {
ps := uast.PositionsOf(n)
typ := uast.TypeOf(n)
if i := strings.Index(typ, ":"); i >= 0 {
typ = typ[i+1:]
}
nd := &uast1.Node{
InternalType: typ,
Token: uast.TokenOf(n),
Roles: roles(uast.RolesOf(n)),
StartPosition: pos(ps.Start()),
EndPosition: pos(ps.End()),
Properties: make(map[string]string),
}
if field != "" {
nd.Properties[uast1.InternalRoleKey] = field
}
for k, np := range ps {
switch k {
case uast.KeyStart, uast.KeyEnd:
// already processed
continue
}
sn, err := asNode(np.ToObject(), k)
if err != nil {
return nil, err
}
p := *pos(&np)
sp := p
ep := sp
ep.Col++
ep.Offset++
sn[0].StartPosition = &sp
sn[0].EndPosition = &ep
nd.Children = append(nd.Children, sn...)
}
for k, v := range n {
switch k {
case uast.KeyType, uast.KeyToken, uast.KeyRoles, uast.KeyPos:
// already processed
continue
}
if nv, ok := v.(nodes.Value); ok {
nd.Properties[k] = fmt.Sprint(nv.Native())
} else {
sn, err := asNode(v, k)
if err != nil {
return nil, err
}
nd.Children = append(nd.Children, sn...)
}
}
sort.Stable(byOffset(nd.Children))
return []*uast1.Node{nd}, nil
}
func valueAsNode(n nodes.Value, field string) ([]*uast1.Node, error) {
nd := &uast1.Node{
Token: fmt.Sprint(n),
Properties: make(map[string]string),
}
if field != "" {
nd.Properties[uast1.InternalRoleKey] = field
}
return []*uast1.Node{nd}, nil
}
func asNode(n nodes.Node, field string) ([]*uast1.Node, error) {
switch n := n.(type) {
case nil:
return nil, nil
case nodes.Array:
return arrayAsNode(n, field)
case nodes.Object:
return objectAsNode(n, field)
case nodes.Value:
return valueAsNode(n, field)
default:
return nil, fmt.Errorf("argument should be a node or a list, got: %T", n)
}
}
type byOffset []*uast1.Node
func (s byOffset) Len() int { return len(s) }
func (s byOffset) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byOffset) Less(i, j int) bool {
a := s[i]
b := s[j]
apos := startPosition(a)
bpos := startPosition(b)
if apos != nil && bpos != nil {
if apos.Offset != bpos.Offset {
return apos.Offset < bpos.Offset
}
} else if (apos == nil && bpos != nil) || (apos != nil && bpos == nil) {
return bpos != nil
}
field1, ok1 := a.Properties[uast1.InternalRoleKey]
field2, ok2 := b.Properties[uast1.InternalRoleKey]
if ok1 && ok2 {
return field1 < field2
}
return false
}
func startPosition(n *uast1.Node) *uast1.Position {
if n.StartPosition != nil {
return n.StartPosition
}
var min *uast1.Position
for _, c := range n.Children {
other := startPosition(c)
if other == nil {
continue
}
if min == nil || other.Offset < min.Offset {
min = other
}
}
return min
}