Add support for C-style for statements (#23)

This commit is contained in:
Kenneth Skovhus 2018-08-06 04:25:19 +02:00 committed by Max Brunsfeld
parent 886a957d1e
commit 9bbd80dee1
4 changed files with 71866 additions and 67172 deletions

View File

@ -100,6 +100,43 @@ done
(command (command_name (word)) (simple_expansion (variable_name))) (command (command_name (word)) (simple_expansion (variable_name)))
(variable_assignment (variable_name) (raw_string))))) (variable_assignment (variable_name) (raw_string)))))
====================================
C-style for statements
====================================
for (( c=1; c<=5; c++ ))
do
echo $c
done
for (( c=1; c<=5; c++ )) {
echo $c
}
for (( ; ; ))
do
echo 'forever'
done
---
(program
(c_style_for_statement
(word)
(binary_expression (word) (word))
(word)
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
(c_style_for_statement
(word)
(binary_expression (word) (word))
(word)
(compound_statement
(command (command_name (word)) (simple_expansion (variable_name)))))
(c_style_for_statement
(do_group
(command (command_name (word)) (raw_string)))))
==================================== ====================================
If statements If statements
==================================== ====================================

View File

@ -63,6 +63,7 @@ module.exports = grammar({
$.test_command, $.test_command,
$.negated_command, $.negated_command,
$.for_statement, $.for_statement,
$.c_style_for_statement,
$.while_statement, $.while_statement,
$.if_statement, $.if_statement,
$.case_statement, $.case_statement,
@ -83,6 +84,21 @@ module.exports = grammar({
$.do_group $.do_group
), ),
c_style_for_statement: $ => seq(
'for',
'((',
optional($._expression),
$._terminator,
optional($._expression),
$._terminator,
optional($._expression),
'))',
choice(
$.do_group,
$.compound_statement
)
),
while_statement: $ => seq( while_statement: $ => seq(
'while', 'while',
$._terminated_statement, $._terminated_statement,

78
src/grammar.json vendored
View File

@ -53,6 +53,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "for_statement" "name": "for_statement"
}, },
{
"type": "SYMBOL",
"name": "c_style_for_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "while_statement" "name": "while_statement"
@ -128,6 +132,80 @@
} }
] ]
}, },
"c_style_for_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "for"
},
{
"type": "STRING",
"value": "(("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_terminator"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_terminator"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "))"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "do_group"
},
{
"type": "SYMBOL",
"name": "compound_statement"
}
]
}
]
},
"while_statement": { "while_statement": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [

138907
src/parser.c vendored

File diff suppressed because it is too large Load Diff