-
Notifications
You must be signed in to change notification settings - Fork 4
/
property.go
338 lines (294 loc) · 8.68 KB
/
property.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2023, Pulumi Corporation.
//
// 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 ast
import (
"fmt"
"strconv"
"strings"
"unicode"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/esc/syntax"
)
type PropertyAccess struct {
Accessors []PropertyAccessor
}
func (p *PropertyAccess) String() string {
var str strings.Builder
for _, accessor := range p.Accessors {
switch accessor := accessor.(type) {
case *PropertyName:
if str.Len() != 0 {
str.WriteByte('.')
}
str.WriteString(accessor.Name)
case *PropertySubscript:
switch i := accessor.Index.(type) {
case string:
fmt.Fprintf(&str, "[\"%s\"]", strings.ReplaceAll(i, `"`, `\"`))
case int:
fmt.Fprintf(&str, "[%d]", i)
}
}
}
return str.String()
}
func (p *PropertyAccess) RootName() string {
return p.Accessors[0].rootName()
}
type PropertyAccessor interface {
isAccessor()
rootName() string
Range() *hcl.Range
}
type PropertyName struct {
Name string
AccessorRange *hcl.Range
}
func (p *PropertyName) isAccessor() {}
func (p *PropertyName) rootName() string {
return p.Name
}
func (p *PropertyName) Range() *hcl.Range {
return p.AccessorRange
}
type PropertySubscript struct {
Index interface{}
AccessorRange *hcl.Range
}
func (p *PropertySubscript) isAccessor() {}
func (p *PropertySubscript) rootName() string {
return p.Index.(string)
}
func (p *PropertySubscript) Range() *hcl.Range {
return p.AccessorRange
}
type propertyAccessParser struct {
parent syntax.Node
getRange func(start, end int) *hcl.Range
offset int
text string
accessors []PropertyAccessor
diags syntax.Diagnostics
}
func (p *propertyAccessParser) error(start int, msg string) {
rng := p.rangeFrom(start)
if rng != nil {
p.diags.Extend(syntax.Error(rng, msg, p.parent.Syntax().Path()))
} else {
p.diags.Extend(syntax.NodeError(p.parent, msg))
}
}
// Returns a document range starting at start and ending at the current offset.
func (p *propertyAccessParser) rangeFrom(start int) *hcl.Range {
return p.getRange(start, p.offset)
}
// Returns true if the given byte terminates a property name. Also used during error recovery for
// unterminated numeric subscipts.
func (p *propertyAccessParser) terminatesName(c byte) bool {
return c == '.' || c == '[' || c == '}' || unicode.IsSpace(rune(c))
}
// Appends a property accessor.
func (p *propertyAccessParser) append(accessor PropertyAccessor) {
p.accessors = append(p.accessors, accessor)
}
// Consumes a byte of input. Use peek() prior to next() to determine what the byte is if one is
// available. Returns the offset of the consumed byte.
func (p *propertyAccessParser) next() int {
at := p.offset
p.text, p.offset = p.text[1:], p.offset+1
return at
}
// Returns (but does not consume) the next byte of input, if any.
func (p *propertyAccessParser) peek() (byte, bool) {
if len(p.text) == 0 {
return 0, false
}
return p.text[0], true
}
// Parses a property access. See the comment on parsePropertyAccess for the grammar and examples.
func (p *propertyAccessParser) parse() (int, string, *PropertyAccess, syntax.Diagnostics) {
for {
c, ok := p.peek()
if !ok {
p.error(p.offset, "missing closing brace '}' in interpolation")
return p.finish(p.offset)
}
switch c {
case '}':
return p.finish(p.next())
case '.':
p.append(p.parseName(p.next()))
case '[':
p.append(p.parseSubscript(p.next()))
default:
if unicode.IsSpace(rune(c)) {
p.error(p.offset, "missing closing brace '}' in interpolation")
return p.finish(p.offset)
}
start := p.offset
p.append(p.parseName(start))
}
}
}
// Terminates parsing. If there are no accessors (e.g. `${` or `${}`), appends an empty property name
// accessor. Returns the rest of the string, the access, and any diagnostics.
func (p *propertyAccessParser) finish(start int) (int, string, *PropertyAccess, syntax.Diagnostics) {
if len(p.accessors) == 0 {
p.append(&PropertyName{Name: "", AccessorRange: p.rangeFrom(start)})
}
end := p.offset
rest := p.text
access := &PropertyAccess{Accessors: p.accessors}
return end, rest, access, p.diags
}
// Parses a property name (e.g. `foo`).
func (p *propertyAccessParser) parseName(start int) *PropertyName {
var b strings.Builder
for {
c, ok := p.peek()
if !ok || p.terminatesName(c) {
break
}
p.next()
b.WriteByte(c)
}
if b.Len() == 0 {
p.error(start, "property name must not be empty")
}
return &PropertyName{Name: b.String(), AccessorRange: p.rangeFrom(start)}
}
// Parses a subscript accessor (e.g. `["foo"]` or `[1]`).
//
// At this point we are already past the opening `[`. Consumes the terminating `]`, if any.
func (p *propertyAccessParser) parseSubscript(start int) *PropertySubscript {
c, ok := p.peek()
if !ok {
p.error(start, "subscript is missing closing bracket ']'")
return &PropertySubscript{Index: "", AccessorRange: p.rangeFrom(start)}
}
var index any
if c == '"' {
p.next()
index = p.parseStringSubscript(start)
} else {
index = p.parseIndexSubscript(start)
}
c, ok = p.peek()
if !ok || c != ']' {
p.error(start, "subscript is missing closing bracket ']'")
} else {
p.next()
}
return &PropertySubscript{Index: index, AccessorRange: p.rangeFrom(start)}
}
// Parses a string subscript.
//
// At this point we are already past the opening `["`. Ends on EOF or an unescaped `"`. Consumes
// the terminating `"` if any.
func (p *propertyAccessParser) parseStringSubscript(start int) string {
var propertyKey strings.Builder
for {
c, ok := p.peek()
if !ok {
// skip the opening brace when reporting the error.
p.error(start+1, "key subscript is missing closing quote '\"'")
return propertyKey.String()
}
p.next()
if c == '"' {
if propertyKey.Len() == 0 {
// skip the opening brace when reporting the error.
p.error(start+1, "key subscript must not be empty")
}
return propertyKey.String()
}
if c == '\\' {
if n, ok := p.peek(); ok && n == '"' {
p.next()
c = n
}
}
propertyKey.WriteByte(c)
}
}
// Parses an index subscript.
//
// At this point we are already past the opening `[`. Ends on EOF, `]`, or a name terminator.
// Does not consume the terminator.
func (p *propertyAccessParser) parseIndexSubscript(start int) any {
var index strings.Builder
for {
c, ok := p.peek()
if !ok || c == ']' || p.terminatesName(c) {
break
}
p.next()
index.WriteByte(c)
}
indexStr := index.String()
num, err := strconv.ParseInt(indexStr, 10, 0)
if err != nil {
// skip the opening brace when reporting the error.
p.error(start+1, "numeric subscript must be a positive base-10 integer")
return indexStr
}
if len(p.accessors) == 0 {
p.error(start, "the first accessor must be a property name or key subscript, not a numeric subscript")
}
return int(num)
}
// parsePropertyAccess parses a property access into a PropertyAccess value.
//
// A property access string is essentially a Javascript property access expression in which all elements are literals.
// Valid property accesses obey the following EBNF-ish grammar:
//
// propertyName := [a-zA-Z_$] { [a-zA-Z0-9_$] }
// quotedPropertyName := '"' ( '\' '"' | [^"] ) { ( '\' '"' | [^"] ) } '"'
// arrayIndex := { [0-9] }
//
// propertyIndex := '[' ( quotedPropertyName | arrayIndex ) ']'
// rootProperty := ( propertyName | propertyIndex )
// propertyAccessor := ( ( '.' propertyName ) | propertyIndex )
// path := rootProperty { propertyAccessor }
//
// Examples of valid paths:
// - root
// - root.nested
// - root["nested"]
// - root.double.nest
// - root["double"].nest
// - root["double"]["nest"]
// - root.array[0]
// - root.array[100]
// - root.array[0].nested
// - root.array[0][1].nested
// - root.nested.array[0].double[1]
// - root["key with \"escaped\" quotes"]
// - root["key with a ."]
// - ["root key with \"escaped\" quotes"].nested
// - ["root key with a ."][100]
func parsePropertyAccess(node syntax.Node, start int, access string) (int, string, *PropertyAccess, syntax.Diagnostics) {
// TODO: diagnostic ranges
getRange := func(start, end int) *hcl.Range { return nil }
if scalar, ok := node.Syntax().(syntax.Scalar); ok {
getRange = scalar.ScalarRange
}
p := &propertyAccessParser{
parent: node,
getRange: getRange,
offset: start,
text: access,
}
return p.parse()
}