Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for interpolation in execution operators. Closes #135 #146

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module.exports = grammar({
$._automatic_semicolon,
$.encapsed_string_chars,
$.encapsed_string_chars_after_variable,
$.execution_string_chars,
$.execution_string_chars_after_variable,
$.encapsed_string_chars_heredoc,
$.encapsed_string_chars_after_variable_heredoc,
$._eof,
Expand Down Expand Up @@ -957,10 +959,6 @@ module.exports = grammar({
seq('--', $._variable)
)),

shell_command_expression: $ => token(seq(
'`', backtick_chars(), '`'
)),

cast_expression: $ => prec(PREC.CAST, seq(
'(', field('type', $.cast_type), ')',
field('value', choice($._unary_expression, $.include_expression, $.include_once_expression))
Expand Down Expand Up @@ -1244,6 +1242,7 @@ module.exports = grammar({
"\\",
/\$/,
'"',
'`',
/[0-7]{1,3}/,
/x[0-9A-Fa-f]{1,2}/,
/u{[0-9A-Fa-f]+}/,
Expand Down Expand Up @@ -1350,6 +1349,23 @@ module.exports = grammar({
),
),

_interpolated_execution_operator_body: $ => repeat1(
choice(
$.escape_sequence,
seq($.variable_name, alias($.execution_string_chars_after_variable, $.string_value)),
alias($.execution_string_chars, $.string_value),
$._simple_string_part,
$._complex_string_part,
alias('\\u', $.string_value),
),
),

shell_command_expression: $ => seq(
'`',
optional($._interpolated_execution_operator_body),
'`'
),

boolean: $ => /[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]/,

null: $ => keyword('null', false),
Expand Down Expand Up @@ -1505,19 +1521,3 @@ function pipeSep1(rule) {
function ampSep1(rule) {
return seq(rule, repeat(seq(token('&'), rule)));
}

function backtick_chars() {
const dq_simple_escapes = /\\"|\\\\|\\\$|\\e|\\f|\\n|\\r|\\t|\\v/
const octal_digit = /[0-7]/
const dq_octal_escapes = seq('\\', octal_digit, optional(octal_digit), optional(octal_digit))
const hex_digit = /\d|a-f|A-F/
const dq_hex_escapes = seq(
/\\[xX]/,
hex_digit,
optional(hex_digit)
)

const dq_unicode_escapes = seq('\\u{', repeat1(hex_digit), '}')
const dq_escapes = choice(dq_simple_escapes, dq_octal_escapes, dq_hex_escapes, dq_unicode_escapes)
return repeat(choice(dq_escapes, /[^`\\]|\\[^`\\$efnrtv0-7]/))
}
212 changes: 93 additions & 119 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -5089,125 +5089,6 @@
]
}
},
"shell_command_expression": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "`"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\\\\"|\\\\\\\\|\\\\\\$|\\\\e|\\\\f|\\\\n|\\\\r|\\\\t|\\\\v"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "PATTERN",
"value": "[0-7]"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[0-7]"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[0-7]"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "\\\\[xX]"
},
{
"type": "PATTERN",
"value": "\\d|a-f|A-F"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\d|a-f|A-F"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\u{"
},
{
"type": "REPEAT1",
"content": {
"type": "PATTERN",
"value": "\\d|a-f|A-F"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
]
},
{
"type": "PATTERN",
"value": "[^`\\\\]|\\\\[^`\\\\$efnrtv0-7]"
}
]
}
},
{
"type": "STRING",
"value": "`"
}
]
}
},
"cast_expression": {
"type": "PREC",
"value": -1,
Expand Down Expand Up @@ -6909,6 +6790,10 @@
"type": "STRING",
"value": "\""
},
{
"type": "STRING",
"value": "`"
},
{
"type": "PATTERN",
"value": "[0-7]{1,3}"
Expand Down Expand Up @@ -7439,6 +7324,87 @@
}
]
},
"_interpolated_execution_operator_body": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "escape_sequence"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "variable_name"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "execution_string_chars_after_variable"
},
"named": true,
"value": "string_value"
}
]
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "execution_string_chars"
},
"named": true,
"value": "string_value"
},
{
"type": "SYMBOL",
"name": "_simple_string_part"
},
{
"type": "SYMBOL",
"name": "_complex_string_part"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "\\u"
},
"named": true,
"value": "string_value"
}
]
}
},
"shell_command_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "`"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_interpolated_execution_operator_body"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "`"
}
]
},
"boolean": {
"type": "PATTERN",
"value": "[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]"
Expand Down Expand Up @@ -8885,6 +8851,14 @@
"type": "SYMBOL",
"name": "encapsed_string_chars_after_variable"
},
{
"type": "SYMBOL",
"name": "execution_string_chars"
},
{
"type": "SYMBOL",
"name": "execution_string_chars_after_variable"
},
{
"type": "SYMBOL",
"name": "encapsed_string_chars_heredoc"
Expand Down
Loading