Skip to content

Commit 5c1b34b

Browse files
committed
Add array index support to expression grammar
1 parent 47479f7 commit 5c1b34b

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

tree-sitter-dscexpression/corpus/valid_expressions.txt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,121 @@ Quotes float input
114114
(functionName)
115115
(arguments
116116
(string)))))
117+
118+
=====
119+
Array index
120+
=====
121+
[myFunction('argString')[0]]
122+
---
123+
124+
(statement
125+
(expression
126+
(memberAccess
127+
(function
128+
(functionName)
129+
(arguments
130+
(string)))
131+
(index))))
132+
133+
=====
134+
Multiple array indexes
135+
=====
136+
[myFunction('argString')[0][1][2]]
137+
---
138+
139+
(statement
140+
(expression
141+
(memberAccess
142+
(memberAccess
143+
(memberAccess
144+
(function
145+
(functionName)
146+
(arguments
147+
(string)))
148+
(index))
149+
(index))
150+
(index))))
151+
152+
=====
153+
Array index with function
154+
=====
155+
[myFunction('argString')[myFunction2(1)]]
156+
---
157+
158+
(statement
159+
(expression
160+
(memberAccess
161+
(function
162+
(functionName)
163+
(arguments
164+
(string)))
165+
(function
166+
(functionName)
167+
(arguments
168+
(number))))))
169+
170+
=====
171+
Array index with function and dot-notation
172+
=====
173+
[myFunction('argString')[myFunction2(1)].prop1]
174+
---
175+
176+
(statement
177+
(expression
178+
(memberAccess
179+
(memberAccess
180+
(function
181+
(functionName)
182+
(arguments
183+
(string)))
184+
(function
185+
(functionName)
186+
(arguments
187+
(number))))
188+
(memberName)))
189+
190+
=====
191+
Array index with function and nested dot-notation
192+
=====
193+
[myFunction('argString')[myFunction2(1).prop1.prop2].prop3]
194+
---
195+
196+
(statement
197+
(expression
198+
(memberAccess
199+
(memberAccess
200+
(function
201+
(functionName)
202+
(arguments
203+
(string)))
204+
(memberAccess
205+
(function
206+
(functionName)
207+
(arguments
208+
(number)))
209+
(memberName)
210+
(memberName))
211+
(memberName))))
212+
213+
=====
214+
Array index with function and nested dot-notation with index
215+
=====
216+
[myFunction('argString')[myFunction2(1).prop1.prop2][0]]
217+
---
218+
219+
(statement
220+
(expression
221+
(memberAccess
222+
(memberAccess
223+
(function
224+
(functionName)
225+
(arguments
226+
(string)))
227+
(memberAccess
228+
(function
229+
(functionName)
230+
(arguments
231+
(number)))
232+
(memberName)
233+
(memberName))
234+
(index))))

tree-sitter-dscexpression/grammar.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = grammar({
1818
escapedStringLiteral: $ => token(prec(PREC.ESCAPEDSTRING, seq('[[', /.*?/))),
1919
bracketInStringLiteral: $ => token(prec(PREC.BRACKETINSTRING, seq('[', /.*?/, ']', /.+?/))),
2020
_expressionString: $ => prec(PREC.EXPRESSIONSTRING, seq('[', $.expression, ']')),
21-
expression: $ => seq(field('function', $.function), field('members', optional($.memberAccess))),
21+
expression: $ => seq(field('function', $.function), optional($.accessor)),
2222
stringLiteral: $ => token(prec(PREC.STRINGLITERAL, /[^\[].*?/)),
2323

2424
function: $ => seq(field('name', $.functionName), '(', field('args', optional($.arguments)), ')'),
@@ -27,13 +27,17 @@ module.exports = grammar({
2727
_argument: $ => choice($.expression, $._quotedString, $.number, $.boolean),
2828

2929
_quotedString: $ => seq('\'', $.string, '\''),
30-
// ARM strings do not allow to contain single-quote characters
30+
// ARM strings are allowed to contain single-quote characters
3131
string: $ => /[^']*/,
3232
number: $ => /-?\d+/,
3333
boolean: $ => choice('true', 'false'),
3434

35-
memberAccess: $ => seq('.', $.memberName, repeat(seq('.', $.memberName))),
35+
accessor: $ => choice(field('members', $.memberAccess), field('index', $.arrayIndex)),
36+
37+
memberAccess: $ => seq('.', $.memberName, optional($.accessor)),
3638
memberName: $ => /[a-zA-Z0-9_-]+/,
39+
40+
arrayIndex: $ => seq('[', $.expression, ']'),
3741
}
3842

3943
});

0 commit comments

Comments
 (0)