Parse for loop identifier as a variable name

Also remove some duplication in expression and concatenation
This commit is contained in:
Max Brunsfeld 2018-02-27 10:54:40 -08:00
parent 7e73a575e9
commit 8a0a4a8501
4 changed files with 24636 additions and 24634 deletions

View File

@ -28,7 +28,7 @@ done
(program
(for_statement
(word)
(variable_name)
(word)
(word)
(command_substitution (command (command_name (word)) (word) (word)))

View File

@ -16,6 +16,7 @@ module.exports = grammar({
$._statement,
$._terminator,
$._expression,
$._primary_expression,
$._variable_name,
],
@ -27,7 +28,7 @@ module.exports = grammar({
$.file_descriptor,
$._empty_value,
$._concat,
$.variable_name,
$.variable_name, // Variable name followed by an operator like '=' or '+='
'\n',
],
@ -63,7 +64,7 @@ module.exports = grammar({
for_statement: $ => seq(
'for',
$.word,
$._variable_name,
'in',
repeat1($._expression),
$._terminator,
@ -242,44 +243,29 @@ module.exports = grammar({
// Expressions
_expression: $ => choice(
$.concatenation,
$._primary_expression
),
_primary_expression: $ => choice(
$.word,
$.string,
$.raw_string,
$.expansion,
$.simple_expansion,
$.command_substitution,
$.process_substitution,
$.concatenation
$.process_substitution
),
concatenation: $ => prec(-1, seq(
choice(
$.word,
$.string,
$.raw_string,
$.expansion,
$.simple_expansion,
$.command_substitution,
$.process_substitution
),
repeat1(seq(
$._concat,
choice(
$.word,
$.string,
$.raw_string,
$.expansion,
$.simple_expansion,
$.command_substitution,
$.process_substitution
)
))
$._primary_expression,
repeat1(seq($._concat, $._primary_expression))
)),
string: $ => seq(
'"',
repeat(choice(
/[^"`$]+/,
$._string_content,
$.expansion,
$.simple_expansion,
$.command_substitution
@ -287,6 +273,8 @@ module.exports = grammar({
'"'
),
_string_content: $ => /[^"`$]+/,
array: $ => seq(
'(',
repeat($._expression),

147
src/grammar.json vendored
View File

@ -83,7 +83,7 @@
},
{
"type": "SYMBOL",
"name": "word"
"name": "_variable_name"
},
{
"type": "STRING",
@ -321,24 +321,46 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "function"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "function"
},
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "STRING",
"value": "("
@ -348,9 +370,6 @@
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
},
@ -857,6 +876,19 @@
]
},
"_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "concatenation"
},
{
"type": "SYMBOL",
"name": "_primary_expression"
}
]
},
"_primary_expression": {
"type": "CHOICE",
"members": [
{
@ -886,10 +918,6 @@
{
"type": "SYMBOL",
"name": "process_substitution"
},
{
"type": "SYMBOL",
"name": "concatenation"
}
]
},
@ -900,37 +928,8 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "raw_string"
},
{
"type": "SYMBOL",
"name": "expansion"
},
{
"type": "SYMBOL",
"name": "simple_expansion"
},
{
"type": "SYMBOL",
"name": "command_substitution"
},
{
"type": "SYMBOL",
"name": "process_substitution"
}
]
"type": "SYMBOL",
"name": "_primary_expression"
},
{
"type": "REPEAT1",
@ -942,37 +941,8 @@
"name": "_concat"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "raw_string"
},
{
"type": "SYMBOL",
"name": "expansion"
},
{
"type": "SYMBOL",
"name": "simple_expansion"
},
{
"type": "SYMBOL",
"name": "command_substitution"
},
{
"type": "SYMBOL",
"name": "process_substitution"
}
]
"type": "SYMBOL",
"name": "_primary_expression"
}
]
}
@ -993,8 +963,8 @@
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^\"`$]+"
"type": "SYMBOL",
"name": "_string_content"
},
{
"type": "SYMBOL",
@ -1017,6 +987,10 @@
}
]
},
"_string_content": {
"type": "PATTERN",
"value": "[^\"`$]+"
},
"array": {
"type": "SEQ",
"members": [
@ -1443,6 +1417,7 @@
"_statement",
"_terminator",
"_expression",
"_primary_expression",
"_variable_name"
]
}

49081
src/parser.c vendored

File diff suppressed because it is too large Load Diff