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),

147
src/grammar.json vendored
View File

@ -83,7 +83,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "word" "name": "_variable_name"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -321,24 +321,46 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "STRING", "type": "SEQ",
"value": "function" "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", "type": "SEQ",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "word"
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -348,9 +370,6 @@
"value": ")" "value": ")"
} }
] ]
},
{
"type": "BLANK"
} }
] ]
}, },
@ -857,6 +876,19 @@
] ]
}, },
"_expression": { "_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "concatenation"
},
{
"type": "SYMBOL",
"name": "_primary_expression"
}
]
},
"_primary_expression": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -886,10 +918,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "process_substitution" "name": "process_substitution"
},
{
"type": "SYMBOL",
"name": "concatenation"
} }
] ]
}, },
@ -900,37 +928,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "_primary_expression"
{
"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": "REPEAT1", "type": "REPEAT1",
@ -942,37 +941,8 @@
"name": "_concat" "name": "_concat"
}, },
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "_primary_expression"
{
"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"
}
]
} }
] ]
} }
@ -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