Skip to content

Commit de690d8

Browse files
committed
Start adding fields
1 parent 2a3aec5 commit de690d8

File tree

5 files changed

+46326
-44307
lines changed

5 files changed

+46326
-44307
lines changed

corpus/statements.txt

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ cat foo | grep -v bar
99

1010
(program
1111
(pipeline
12-
(command (command_name (word)))
13-
(command (command_name (word))))
12+
(command
13+
name: (command_name (word)))
14+
(command
15+
name: (command_name (word))))
1416
(pipeline
15-
(command (command_name (word)) (word))
16-
(command (command_name (word)) (word) (word))))
17+
(command
18+
name: (command_name (word))
19+
argument: (word))
20+
(command
21+
name: (command_name (word))
22+
argument: (word)
23+
argument: (word))))
1724

1825
===================================
1926
Lists
@@ -48,10 +55,12 @@ done
4855

4956
(program
5057
(while_statement
51-
(command (command_name (word)) (word))
52-
(do_group
53-
(command (command_name (word)) (word))
54-
(command (command_name (word)) (word)))))
58+
condition: (command
59+
name: (command_name (word))
60+
argument: (word))
61+
body: (do_group
62+
(command name: (command_name (word)) argument: (word))
63+
(command name: (command_name (word)) argument: (word)))))
5564

5665
====================================
5766
While statements with IO redirects
@@ -65,12 +74,18 @@ done < <(cat file)
6574

6675
(program
6776
(redirected_statement
68-
(while_statement
69-
(command (command_name (word)) (word))
70-
(do_group
71-
(command (command_name (word)) (simple_expansion (variable_name)))))
72-
(file_redirect (process_substitution
73-
(command (command_name (word)) (word))))))
77+
body: (while_statement
78+
condition: (command
79+
name: (command_name (word))
80+
argument: (word))
81+
body: (do_group
82+
(command
83+
name: (command_name (word))
84+
argument: (simple_expansion (variable_name)))))
85+
redirect: (file_redirect
86+
destination: (process_substitution (command
87+
name: (command_name (word))
88+
argument: (word))))))
7489

7590
====================================
7691
For statements
@@ -89,17 +104,26 @@ done
89104

90105
(program
91106
(for_statement
92-
(variable_name)
93-
(word)
94-
(word)
95-
(command_substitution (command (command_name (word)) (word) (word)))
96-
(do_group
97-
(command (command_name (word)) (simple_expansion (variable_name)))))
107+
variable: (variable_name)
108+
value: (word)
109+
value: (word)
110+
value: (command_substitution (command
111+
name: (command_name (word))
112+
argument: (word)
113+
argument: (word)))
114+
body: (do_group
115+
(command
116+
name: (command_name (word))
117+
argument: (simple_expansion (variable_name)))))
98118
(for_statement
99-
(variable_name)
100-
(do_group
101-
(command (command_name (word)) (simple_expansion (variable_name)))
102-
(variable_assignment (variable_name) (raw_string)))))
119+
variable: (variable_name)
120+
body: (do_group
121+
(command
122+
name: (command_name (word))
123+
argument: (simple_expansion (variable_name)))
124+
(variable_assignment
125+
name: (variable_name)
126+
value: (raw_string)))))
103127

104128
====================================
105129
C-style for statements

grammar.js

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -97,45 +97,45 @@ module.exports = grammar({
9797
),
9898

9999
redirected_statement: $ => prec(-1, seq(
100-
$._statement,
101-
repeat1(choice(
100+
field('body', $._statement),
101+
field('redirect', repeat1(choice(
102102
$.file_redirect,
103103
$.heredoc_redirect,
104104
$.herestring_redirect
105-
))
105+
)))
106106
)),
107107

108108
for_statement: $ => seq(
109109
'for',
110-
$._simple_variable_name,
110+
field('variable', $._simple_variable_name),
111111
optional(seq(
112112
'in',
113-
repeat1($._literal)
113+
field('value', repeat1($._literal))
114114
)),
115115
$._terminator,
116-
$.do_group
116+
field('body', $.do_group)
117117
),
118118

119119
c_style_for_statement: $ => seq(
120120
'for',
121121
'((',
122-
optional($._expression),
122+
field('initializer', optional($._expression)),
123123
$._terminator,
124-
optional($._expression),
124+
field('condition', optional($._expression)),
125125
$._terminator,
126-
optional($._expression),
126+
field('update', optional($._expression)),
127127
'))',
128128
optional(';'),
129-
choice(
129+
field('body', choice(
130130
$.do_group,
131131
$.compound_statement
132-
)
132+
))
133133
),
134134

135135
while_statement: $ => seq(
136136
'while',
137-
$._terminated_statement,
138-
$.do_group
137+
field('condition', $._terminated_statement),
138+
field('body', $.do_group)
139139
),
140140

141141
do_group: $ => seq(
@@ -146,7 +146,7 @@ module.exports = grammar({
146146

147147
if_statement: $ => seq(
148148
'if',
149-
$._terminated_statement,
149+
field('condition', $._terminated_statement),
150150
'then',
151151
optional($._statements2),
152152
repeat($.elif_clause),
@@ -168,7 +168,7 @@ module.exports = grammar({
168168

169169
case_statement: $ => seq(
170170
'case',
171-
$._literal,
171+
field('value', $._literal),
172172
optional($._terminator),
173173
'in',
174174
$._terminator,
@@ -180,36 +180,39 @@ module.exports = grammar({
180180
),
181181

182182
case_item: $ => seq(
183-
$._literal,
184-
repeat(seq('|', $._literal)),
183+
field('value', $._literal),
184+
repeat(seq('|', field('value', $._literal))),
185185
')',
186186
optional($._statements),
187187
prec(1, ';;')
188188
),
189189

190190
last_case_item: $ => seq(
191-
$._literal,
192-
repeat(seq('|', $._literal)),
191+
field('value', $._literal),
192+
repeat(seq('|', field('value', $._literal))),
193193
')',
194194
optional($._statements),
195195
optional(prec(1, ';;'))
196196
),
197197

198198
function_definition: $ => seq(
199199
choice(
200-
seq('function', $.word, optional(seq('(', ')'))),
201-
seq($.word, '(', ')')
200+
seq(
201+
'function',
202+
field('name', $.word),
203+
optional(seq('(', ')'))
204+
),
205+
seq(
206+
field('name', $.word),
207+
'(', ')'
208+
)
202209
),
203-
$.compound_statement
210+
field('body', $.compound_statement)
204211
),
205212

206213
compound_statement: $ => seq(
207214
'{',
208-
repeat(seq(
209-
$._statement,
210-
optional(seq('\n', $.heredoc_body)),
211-
$._terminator
212-
)),
215+
optional($._statements2),
213216
'}'
214217
),
215218

@@ -272,47 +275,47 @@ module.exports = grammar({
272275
$.variable_assignment,
273276
$.file_redirect
274277
)),
275-
$.command_name,
276-
repeat(choice(
278+
field('name', $.command_name),
279+
repeat(field('argument', choice(
277280
$._literal,
278281
seq(
279282
choice('=~', '=='),
280283
choice($._literal, $.regex)
281284
)
282-
))
285+
)))
283286
)),
284287

285288
command_name: $ => $._literal,
286289

287290
variable_assignment: $ => seq(
288-
choice(
291+
field('name', choice(
289292
$.variable_name,
290293
$.subscript
291-
),
294+
)),
292295
choice(
293296
'=',
294297
'+='
295298
),
296-
choice(
299+
field('value', choice(
297300
$._literal,
298301
$.array,
299302
$._empty_value
300-
)
303+
))
301304
),
302305

303306
subscript: $ => seq(
304-
$.variable_name,
307+
field('name', $.variable_name),
305308
'[',
306-
$._literal,
309+
field('index', $._literal),
307310
optional($._concat),
308311
']',
309312
optional($._concat)
310313
),
311314

312315
file_redirect: $ => prec.left(seq(
313-
optional($.file_descriptor),
316+
field('descriptor', optional($.file_descriptor)),
314317
choice('<', '>', '>>', '&>', '&>>', '<&', '>&'),
315-
$._literal
318+
field('destination', $._literal)
316319
)),
317320

318321
heredoc_redirect: $ => seq(
@@ -351,20 +354,20 @@ module.exports = grammar({
351354

352355
binary_expression: $ => prec.left(choice(
353356
seq(
354-
$._expression,
355-
choice(
357+
field('left', $._expression),
358+
field('operator', choice(
356359
'=', '==', '=~', '!=',
357360
'+', '-', '+=', '-=',
358361
'<', '>', '<=', '>=',
359362
'||', '&&',
360363
$.test_operator
361-
),
362-
$._expression
364+
)),
365+
field('right', $._expression)
363366
),
364367
seq(
365-
$._expression,
366-
choice('==', '=~'),
367-
$.regex
368+
field('left', $._expression),
369+
field('operator', choice('==', '=~')),
370+
field('right', $.regex)
368371
)
369372
)),
370373

0 commit comments

Comments
 (0)