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 (program
(for_statement (for_statement
(word) (variable_name)
(word) (word)
(word) (word)
(command_substitution (command (command_name (word)) (word) (word))) (command_substitution (command (command_name (word)) (word) (word)))

View File

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

121
src/grammar.json vendored
View File

@ -83,7 +83,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "word" "name": "_variable_name"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -319,16 +319,14 @@
"members": [ "members": [
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "function" "value": "function"
}, },
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "word" "name": "word"
@ -353,6 +351,27 @@
"type": "BLANK" "type": "BLANK"
} }
] ]
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "word"
},
{
"type": "STRING",
"value": "("
},
{
"type": "STRING",
"value": ")"
}
]
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -859,47 +878,17 @@
"_expression": { "_expression": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "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", "type": "SYMBOL",
"name": "concatenation" "name": "concatenation"
},
{
"type": "SYMBOL",
"name": "_primary_expression"
} }
] ]
}, },
"concatenation": { "_primary_expression": {
"type": "PREC",
"value": -1,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -932,6 +921,16 @@
} }
] ]
}, },
"concatenation": {
"type": "PREC",
"value": -1,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_primary_expression"
},
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
@ -941,38 +940,9 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_concat" "name": "_concat"
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "word" "name": "_primary_expression"
},
{
"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"
}
]
} }
] ]
} }
@ -993,8 +963,8 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[^\"`$]+" "name": "_string_content"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1017,6 +987,10 @@
} }
] ]
}, },
"_string_content": {
"type": "PATTERN",
"value": "[^\"`$]+"
},
"array": { "array": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1443,6 +1417,7 @@
"_statement", "_statement",
"_terminator", "_terminator",
"_expression", "_expression",
"_primary_expression",
"_variable_name" "_variable_name"
] ]
} }

49081
src/parser.c vendored

File diff suppressed because it is too large Load Diff