Debian Codegraph @

tree/tree.go

raw symbols SCIP
1 // Copyright 2019 Red Hat, Inc
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.)
14
15 package tree
16
17 import (
18 "errors"
19 "fmt"
20 "sort"
21
22 "github.com/coreos/vcontext/path"
23 )
24
25 var (
26 ErrBadPath = errors.New("invalid path")
27 )
28
29 // Node is generic representation of a json or yaml node.
30 type Node interface {
31 Start() (int64, int64) // line, col
32 End() (int64, int64)
33 Get(cxt path.ContextPath) (Node, error)
34 GetMarker() Marker
35 pos() []*Pos // just used for iterating through the markers to fill in line and column from index
36 }
37
38 // FixLineColumn populates the Line and Column of nodes that only have Index set.
39 func FixLineColumn(n Node, source []byte) {
40 fixLineColumn(n.pos(), source)
41 }
42
43 func fixLineColumn(p []*Pos, source []byte) {
44 sort.Slice(p, func(i, j int) bool {
45 return p[i].Index < p[j].Index
46 })
47 pi := 0
48 line, col := int64(1), int64(1)
49 for i, c := range source {
50 if pi == len(p) {
51 return
52 }
53 for int64(i) == p[pi].Index {
54 p[pi].Line = line
55 p[pi].Column = col
56 pi++
57 if pi == len(p) {
58 return
59 }
60 }
61 col++
62 if c == '\n' {
63 line++
64 col = 1
65 }
66 }
67 }
68
69 // Key is used to differentiate leaves describing the start and end of where
70 // a key starts and where a value starts.
71 type Key string
72
73 // Pos represents a single location in a string.
74 type Pos struct {
75 Index int64
76 Line int64
77 Column int64
78 }
79
80 func posString(p *Pos) string {
81 if p == nil {
82 return ""
83 }
84 return fmt.Sprintf("line %d col %d", p.Line, p.Column)
85 }
86
87 func posLC(p *Pos) (int64, int64) {
88 if p == nil {
89 return 0, 0
90 }
91 return p.Line, p.Column
92 }
93
94 // Markers are composed of information regarding the start and
95 // end of where a Node exists in its source.
96 type Marker struct {
97 StartP *Pos
98 EndP *Pos
99 }
100
101 func (m Marker) Start() (int64, int64) {
102 return posLC(m.StartP)
103 }
104
105 func (m Marker) End() (int64, int64) {
106 return posLC(m.EndP)
107 }
108
109 func (m Marker) String() string {
110 // Just do start for now, figure out end later
111 return posString(m.StartP)
112 }
113
114 func (marker Marker) GetMarker() Marker {
115 return marker
116 }
117
118 func MarkerFromIndices(start, end int64) Marker {
119 m := Marker{}
120 if start >= 0 {
121 m.StartP = &Pos{Index: start}
122 }
123 if end >= 0 {
124 m.EndP = &Pos{Index: end}
125 }
126 return m
127 }
128
129 func appendPos(l []*Pos, p *Pos) []*Pos {
130 if p != nil {
131 return append(l, p)
132 }
133 return l
134 }
135
136 type MapNode struct {
137 Marker
138 Children map[string]Node
139 Keys map[string]Leaf
140 }
141
142 func (m MapNode) Get(cxt path.ContextPath) (Node, error) {
143 if cxt.Len() == 0 {
144 return m, nil
145 }
146 switch p := cxt.Head().(type) {
147 case string:
148 if r, ok := m.Children[p]; ok {
149 return r.Get(cxt.Tail())
150 } else {
151 return nil, ErrBadPath
152 }
153 case Key:
154 if r, ok := m.Keys[string(p)]; ok {
155 return r.Get(cxt.Tail())
156 } else {
157 return nil, ErrBadPath
158 }
159 default:
160 return nil, ErrBadPath
161 }
162 }
163
164 func (m MapNode) pos() []*Pos {
165 ret := appendPos(nil, m.StartP)
166 for _, v := range m.Children {
167 ret = append(ret, v.pos()...)
168 }
169 for _, v := range m.Keys {
170 ret = append(ret, v.pos()...)
171 }
172 ret = appendPos(ret, m.EndP)
173 return ret
174 }
175
176 type Leaf struct {
177 Marker
178 }
179
180 func (l Leaf) pos() []*Pos {
181 return appendPos(appendPos(nil, l.StartP), l.EndP)
182 }
183
184 func (k Leaf) Get(ctx path.ContextPath) (Node, error) {
185 if ctx.Len() == 0 {
186 return k, nil
187 }
188 return nil, ErrBadPath
189 }
190
191 type SliceNode struct {
192 Marker
193 Children []Node
194 }
195
196 func (s SliceNode) Get(ctx path.ContextPath) (Node, error) {
197 if ctx.Len() == 0 {
198 return s, nil
199 }
200 if i, ok := ctx.Head().(int); ok {
201 if i >= len(s.Children) {
202 return nil, ErrBadPath
203 }
204 return s.Children[i].Get(ctx.Tail())
205 }
206 return nil, ErrBadPath
207 }
208
209 func (s SliceNode) pos() []*Pos {
210 ret := appendPos(nil, s.StartP)
211 for _, v := range s.Children {
212 ret = append(ret, v.pos()...)
213 }
214 ret = appendPos(ret, s.EndP)
215 return ret
216 }