fix: move heredoc bodies inside heredoc_redirect

pull/504/merge
Amaan Qureshi 2023-08-23 03:46:31 +07:00
parent 13b23426dc
commit a1fead6eb7
3 changed files with 86 additions and 81 deletions

@ -88,17 +88,14 @@ module.exports = grammar({
_statements: $ => prec(1, seq( _statements: $ => prec(1, seq(
repeat(seq( repeat(seq(
$._statement, $._statement,
optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))),
$._terminator, $._terminator,
)), )),
$._statement, $._statement,
optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))),
optional($._terminator), optional($._terminator),
)), )),
_statements2: $ => repeat1(seq( _statements2: $ => repeat1(seq(
$._statement, $._statement,
optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))),
$._terminator, $._terminator,
)), )),
@ -436,6 +433,16 @@ module.exports = grammar({
field('descriptor', optional($.file_descriptor)), field('descriptor', optional($.file_descriptor)),
choice('<<', '<<-'), choice('<<', '<<-'),
$.heredoc_start, $.heredoc_start,
optional(seq(
choice(alias($._heredoc_pipeline, $.pipeline), $.file_redirect),
)),
'\n',
choice($._heredoc_body, $._simple_heredoc_body),
),
_heredoc_pipeline: $ => seq(
choice('|', '|&'),
$._statement,
), ),
_heredoc_body: $ => seq( _heredoc_body: $ => seq(

28
src/scanner.c vendored

@ -224,6 +224,12 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer,
scanner->started_heredoc = true; scanner->started_heredoc = true;
return true; return true;
} }
if (middle_type == HEREDOC_BODY_BEGINNING &&
lexer->get_column(lexer) == 0) {
lexer->result_symbol = middle_type;
scanner->started_heredoc = true;
return true;
}
return false; return false;
} }
@ -239,19 +245,12 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer,
advance(lexer); advance(lexer);
} }
} }
if (end_type != SIMPLE_HEREDOC_BODY && lexer->result_symbol =
scan_heredoc_end_identifier(scanner, lexer)) { scanner->started_heredoc ? middle_type : end_type;
reset(scanner);
lexer->result_symbol = end_type;
return true;
}
if (end_type == SIMPLE_HEREDOC_BODY) {
lexer->result_symbol = end_type;
lexer->mark_end(lexer); lexer->mark_end(lexer);
if (scan_heredoc_end_identifier(scanner, lexer)) { if (scan_heredoc_end_identifier(scanner, lexer)) {
return true; return true;
} }
}
break; break;
} }
@ -262,12 +261,12 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer,
while (iswspace(lexer->lookahead)) { while (iswspace(lexer->lookahead)) {
skip(lexer); skip(lexer);
} }
if (end_type != SIMPLE_HEREDOC_BODY && if (end_type != SIMPLE_HEREDOC_BODY) {
scan_heredoc_end_identifier(scanner, lexer)) { lexer->result_symbol = middle_type;
reset(scanner); if (scan_heredoc_end_identifier(scanner, lexer)) {
lexer->result_symbol = end_type;
return true; return true;
} }
}
if (end_type == SIMPLE_HEREDOC_BODY) { if (end_type == SIMPLE_HEREDOC_BODY) {
lexer->result_symbol = end_type; lexer->result_symbol = end_type;
lexer->mark_end(lexer); lexer->mark_end(lexer);
@ -333,7 +332,8 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
} }
} }
if (valid_symbols[HEREDOC_BODY_BEGINNING] && if ((valid_symbols[HEREDOC_BODY_BEGINNING] ||
valid_symbols[SIMPLE_HEREDOC_BODY]) &&
scanner->heredoc_delimiter.len > 0 && !scanner->started_heredoc && scanner->heredoc_delimiter.len > 0 && !scanner->started_heredoc &&
!in_error_recovery(valid_symbols)) { !in_error_recovery(valid_symbols)) {
return scan_heredoc_content(scanner, lexer, HEREDOC_BODY_BEGINNING, return scan_heredoc_content(scanner, lexer, HEREDOC_BODY_BEGINNING,

@ -161,18 +161,18 @@ JS
(command_name (command_name
(word))) (word)))
(heredoc_redirect (heredoc_redirect
(heredoc_start))) (heredoc_start)
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end) (heredoc_end)))
(redirected_statement (redirected_statement
(command (command
(command_name (command_name
(word)) (word))
(word)) (word))
(heredoc_redirect (heredoc_redirect
(heredoc_start))) (heredoc_start)
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end)) (heredoc_end))))
=============================== ===============================
Heredocs with variables Heredocs with variables
@ -192,13 +192,13 @@ exit
(command_name (command_name
(word))) (word)))
(heredoc_redirect (heredoc_redirect
(heredoc_start))) (heredoc_start)
(heredoc_body (heredoc_body
(simple_expansion (simple_expansion
(variable_name)) (variable_name))
(expansion (expansion
(variable_name))) (variable_name)))
(heredoc_end) (heredoc_end)))
(command (command
(command_name (command_name
(word)))) (word))))
@ -221,16 +221,16 @@ wc -l $tmpfile
(command_name (command_name
(word))) (word)))
(heredoc_redirect (heredoc_redirect
(heredoc_start)) (heredoc_start)
(file_redirect (file_redirect
(simple_expansion (simple_expansion
(variable_name)))) (variable_name)))
(heredoc_body (heredoc_body
(simple_expansion (simple_expansion
(variable_name)) (variable_name))
(expansion (expansion
(variable_name))) (variable_name)))
(heredoc_end) (heredoc_end)))
(command (command
(command_name (command_name
(word)) (word))
@ -249,19 +249,19 @@ EOF
--- ---
(program (program
(pipeline
(redirected_statement (redirected_statement
(command (command
(command_name (command_name
(word))) (word)))
(heredoc_redirect (heredoc_redirect
(heredoc_start))) (heredoc_start)
(pipeline
(command (command
(command_name (command_name
(word)) (word))
(word))) (word)))
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end)) (heredoc_end))))
====================================== ======================================
Heredocs with escaped expansions Heredocs with escaped expansions
@ -273,7 +273,7 @@ EOF
--- ---
(program (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end)) (program (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end))))
====================================== ======================================
Quoted Heredocs Quoted Heredocs
@ -298,10 +298,10 @@ EOF
--- ---
(program (program
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end)) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end))))
========================================== ==========================================
Heredocs with indented closing delimiters Heredocs with indented closing delimiters
@ -321,9 +321,7 @@ usage() {
(compound_statement (compound_statement
(redirected_statement (redirected_statement
(command (command_name (word))) (command (command_name (word)))
(heredoc_redirect (heredoc_start))) (heredoc_redirect (heredoc_start) (heredoc_body (expansion (special_variable_name) (word))) (heredoc_end))))))
(heredoc_body (expansion (special_variable_name) (word)))
(heredoc_end))))
========================================== ==========================================
Heredocs with empty bodies Heredocs with empty bodies
@ -351,17 +349,17 @@ EOF
name: (command_name name: (command_name
(word))) (word)))
redirect: (heredoc_redirect redirect: (heredoc_redirect
(heredoc_start))) (heredoc_start)
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end) (heredoc_end)))
(redirected_statement (redirected_statement
body: (command body: (command
name: (command_name name: (command_name
(word))) (word)))
redirect: (heredoc_redirect redirect: (heredoc_redirect
(heredoc_start))) (heredoc_start)
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end) (heredoc_end)))
(function_definition (function_definition
name: (word) name: (word)
body: (compound_statement body: (compound_statement
@ -370,19 +368,19 @@ EOF
name: (command_name name: (command_name
(word))) (word)))
redirect: (heredoc_redirect redirect: (heredoc_redirect
(heredoc_start))) (heredoc_start)
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end))) (heredoc_end)))))
(redirected_statement (redirected_statement
body: (command body: (command
name: (command_name name: (command_name
(word))) (word)))
redirect: (heredoc_redirect redirect: (heredoc_redirect
(heredoc_start)) (heredoc_start)
redirect: (file_redirect (file_redirect
destination: (word))) destination: (word))
(simple_heredoc_body) (simple_heredoc_body)
(heredoc_end)) (heredoc_end))))
========================================== ==========================================
Heredocs with weird characters Heredocs with weird characters
@ -411,11 +409,11 @@ Hello.
--- ---
(program (program
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end)))
(redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start))) (simple_heredoc_body) (heredoc_end)) (redirected_statement (command (command_name (word))) (heredoc_redirect (heredoc_start) (simple_heredoc_body) (heredoc_end))))
========================================== ==========================================
Herestrings Herestrings