Skip to content

Commit

Permalink
Support for do and while loops named fields (#324)
Browse files Browse the repository at this point in the history
* Support for do and while loops named fields

- both `body` and `condition` fields
- updated parser

* Update tests

* Update file sizes
  • Loading branch information
GeekMasher authored Nov 29, 2023
1 parent 1648e21 commit dd5e597
Show file tree
Hide file tree
Showing 6 changed files with 1,214 additions and 1,162 deletions.
12 changes: 6 additions & 6 deletions corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class A {
parameters: (parameter_list)
body: (block
(while_statement
(boolean_literal)
(break_statement)))))))
condition: (boolean_literal)
body: (break_statement)))))))

================================================================================
Continue statement
Expand All @@ -92,8 +92,8 @@ class A {
parameters: (parameter_list)
body: (block
(while_statement
(boolean_literal)
(continue_statement)))))))
condition: (boolean_literal)
body: (continue_statement)))))))

================================================================================
Throw nothing
Expand Down Expand Up @@ -167,8 +167,8 @@ class A {
name: (identifier)))
body: (block
(do_statement
(block)
(identifier)))))))
body: (block)
condition: (identifier)))))))

================================================================================
Goto statement and label
Expand Down
18 changes: 16 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,15 @@ module.exports = grammar({

continue_statement: $ => seq('continue', ';'),

do_statement: $ => seq('do', $._statement, 'while', '(', $._expression, ')', ';'),
do_statement: $ => seq(
'do',
field('body', $._statement),
'while',
'(',
field('condition', $._expression),
')',
';'
),

empty_statement: $ => ';',

Expand Down Expand Up @@ -1161,7 +1169,13 @@ module.exports = grammar({
field('body', $._statement)
),

while_statement: $ => seq('while', '(', $._expression, ')', $._statement),
while_statement: $ => seq(
'while',
'(',
field('condition', $._expression),
')',
field('body', $._statement)
),

yield_statement: $ => seq(
'yield',
Expand Down
8 changes: 4 additions & 4 deletions script/file_sizes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
src/grammar.json 0.2MB 11140
src/node-types.json 0.1MB 7902
src/parser.c 41.8MB 1311926
src/grammar.json 0.2MB 11156
src/node-types.json 0.1MB 7916
src/parser.c 41.8MB 1311934
src/scanner.c 0.0MB 37
total 42.2MB 1331005
total 42.2MB 1331043
32 changes: 24 additions & 8 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -4420,8 +4420,12 @@
"value": "do"
},
{
"type": "SYMBOL",
"name": "_statement"
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
},
{
"type": "STRING",
Expand All @@ -4432,8 +4436,12 @@
"value": "("
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
Expand Down Expand Up @@ -6134,16 +6142,24 @@
"value": "("
},
{
"type": "SYMBOL",
"name": "_expression"
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": ")"
},
{
"type": "SYMBOL",
"name": "_statement"
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
}
]
},
Expand Down
70 changes: 42 additions & 28 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -2271,20 +2271,27 @@
{
"type": "do_statement",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "_statement",
"named": true
}
]
"fields": {
"body": {
"multiple": false,
"required": true,
"types": [
{
"type": "_statement",
"named": true
}
]
},
"condition": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
}
}
},
{
Expand Down Expand Up @@ -7003,20 +7010,27 @@
{
"type": "while_statement",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "_statement",
"named": true
}
]
"fields": {
"body": {
"multiple": false,
"required": true,
"types": [
{
"type": "_statement",
"named": true
}
]
},
"condition": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
}
}
},
{
Expand Down
Loading

0 comments on commit dd5e597

Please sign in to comment.