Allow $ in strings

This commit is contained in:
Max Brunsfeld 2018-03-01 09:54:08 -08:00
parent c7e42b8e96
commit 384d920af5
4 changed files with 61556 additions and 60091 deletions

View File

@ -252,6 +252,19 @@ echo "\"The great escape\`\${var}"
(program (command (command_name (word)) (string))) (program (command (command_name (word)) (string)))
======================================
Strings containing special characters
======================================
echo "s/$/'/"
echo "#"
---
(program
(command (command_name (word)) (string))
(command (command_name (word)) (string)))
========================================= =========================================
Variable declaration: declare & typeset Variable declaration: declare & typeset
========================================= =========================================

View File

@ -293,6 +293,7 @@ module.exports = grammar({
$.raw_string, $.raw_string,
$.expansion, $.expansion,
$.simple_expansion, $.simple_expansion,
$.string_expansion,
$.command_substitution, $.command_substitution,
$.process_substitution $.process_substitution
), ),
@ -317,7 +318,7 @@ module.exports = grammar({
'"', '"',
repeat(seq( repeat(seq(
choice( choice(
$._string_content, seq(optional('$'), $._string_content),
$.expansion, $.expansion,
$.simple_expansion, $.simple_expansion,
$.command_substitution $.command_substitution
@ -327,7 +328,7 @@ module.exports = grammar({
'"' '"'
), ),
_string_content: $ => /([^"`$]|\\.)+/, _string_content: $ => token(prec(-1, /([^"`$]|\\.)+/)),
array: $ => seq( array: $ => seq(
'(', '(',
@ -342,11 +343,12 @@ module.exports = grammar({
choice( choice(
$._simple_variable_name, $._simple_variable_name,
$._special_variable_name, $._special_variable_name,
alias('#', $.special_variable_name), alias('#', $.special_variable_name)
$.string
) )
), ),
string_expansion: $ => seq('$', $.string),
expansion: $ => seq( expansion: $ => seq(
'${', '${',
optional('#'), optional('#'),
@ -382,7 +384,7 @@ module.exports = grammar({
')' ')'
), ),
comment: $ => token(prec(-1, /#.*/)), comment: $ => token(prec(-10, /#.*/)),
_simple_variable_name: $ => alias(/\w+/, $.variable_name), _simple_variable_name: $ => alias(/\w+/, $.variable_name),

121
src/grammar.json vendored
View File

@ -610,8 +610,35 @@
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_expression" "members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "=~"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "regex"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
]
}
]
} }
}, },
{ {
@ -630,8 +657,35 @@
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_expression" "members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "=~"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "regex"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
]
}
]
} }
}, },
{ {
@ -1042,6 +1096,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "simple_expansion" "name": "simple_expansion"
}, },
{
"type": "SYMBOL",
"name": "string_expansion"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "command_substitution" "name": "command_substitution"
@ -1150,8 +1208,25 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SEQ",
"name": "_string_content" "members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "$"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_string_content"
}
]
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1189,8 +1264,15 @@
] ]
}, },
"_string_content": { "_string_content": {
"type": "PATTERN", "type": "TOKEN",
"value": "([^\"`$]|\\\\.)+" "content": {
"type": "PREC",
"value": -1,
"content": {
"type": "PATTERN",
"value": "([^\"`$]|\\\\.)+"
}
}
}, },
"array": { "array": {
"type": "SEQ", "type": "SEQ",
@ -1242,15 +1324,24 @@
}, },
"named": true, "named": true,
"value": "special_variable_name" "value": "special_variable_name"
},
{
"type": "SYMBOL",
"name": "string"
} }
] ]
} }
] ]
}, },
"string_expansion": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "$"
},
{
"type": "SYMBOL",
"name": "string"
}
]
},
"expansion": { "expansion": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1445,7 +1536,7 @@
"type": "TOKEN", "type": "TOKEN",
"content": { "content": {
"type": "PREC", "type": "PREC",
"value": -1, "value": -10,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "#.*" "value": "#.*"
@ -1527,6 +1618,10 @@
} }
} }
}, },
"regex": {
"type": "PATTERN",
"value": "\\S+"
},
"_terminator": { "_terminator": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

121501
src/parser.c vendored

File diff suppressed because it is too large Load Diff