From a1fead6eb763a1cb901feb9ddb104dc65b8825cc Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Wed, 23 Aug 2023 03:46:31 -0400 Subject: [PATCH] fix: move heredoc bodies inside heredoc_redirect --- grammar.js | 13 ++++- src/scanner.c | 34 +++++------ test/corpus/commands.txt | 120 +++++++++++++++++++-------------------- 3 files changed, 86 insertions(+), 81 deletions(-) diff --git a/grammar.js b/grammar.js index 666aae22e..810df6aab 100644 --- a/grammar.js +++ b/grammar.js @@ -88,17 +88,14 @@ module.exports = grammar({ _statements: $ => prec(1, seq( repeat(seq( $._statement, - optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))), $._terminator, )), $._statement, - optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))), optional($._terminator), )), _statements2: $ => repeat1(seq( $._statement, - optional(seq('\n', choice($._heredoc_body, $._simple_heredoc_body))), $._terminator, )), @@ -436,6 +433,16 @@ module.exports = grammar({ field('descriptor', optional($.file_descriptor)), choice('<<', '<<-'), $.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( diff --git a/src/scanner.c b/src/scanner.c index 94b4ce016..a5ef953cc 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -224,6 +224,12 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, scanner->started_heredoc = 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; } @@ -239,19 +245,12 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, advance(lexer); } } - if (end_type != SIMPLE_HEREDOC_BODY && - scan_heredoc_end_identifier(scanner, lexer)) { - reset(scanner); - lexer->result_symbol = end_type; + lexer->result_symbol = + scanner->started_heredoc ? middle_type : end_type; + lexer->mark_end(lexer); + if (scan_heredoc_end_identifier(scanner, lexer)) { return true; } - if (end_type == SIMPLE_HEREDOC_BODY) { - lexer->result_symbol = end_type; - lexer->mark_end(lexer); - if (scan_heredoc_end_identifier(scanner, lexer)) { - return true; - } - } break; } @@ -262,11 +261,11 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, while (iswspace(lexer->lookahead)) { skip(lexer); } - if (end_type != SIMPLE_HEREDOC_BODY && - scan_heredoc_end_identifier(scanner, lexer)) { - reset(scanner); - lexer->result_symbol = end_type; - return true; + if (end_type != SIMPLE_HEREDOC_BODY) { + lexer->result_symbol = middle_type; + if (scan_heredoc_end_identifier(scanner, lexer)) { + return true; + } } if (end_type == SIMPLE_HEREDOC_BODY) { lexer->result_symbol = end_type; @@ -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 && !in_error_recovery(valid_symbols)) { return scan_heredoc_content(scanner, lexer, HEREDOC_BODY_BEGINNING, diff --git a/test/corpus/commands.txt b/test/corpus/commands.txt index bcf8076a9..62822431d 100644 --- a/test/corpus/commands.txt +++ b/test/corpus/commands.txt @@ -161,18 +161,18 @@ JS (command_name (word))) (heredoc_redirect - (heredoc_start))) - (simple_heredoc_body) - (heredoc_end) + (heredoc_start) + (simple_heredoc_body) + (heredoc_end))) (redirected_statement (command (command_name (word)) (word)) (heredoc_redirect - (heredoc_start))) - (simple_heredoc_body) - (heredoc_end)) + (heredoc_start) + (simple_heredoc_body) + (heredoc_end)))) =============================== Heredocs with variables @@ -192,13 +192,13 @@ exit (command_name (word))) (heredoc_redirect - (heredoc_start))) - (heredoc_body - (simple_expansion - (variable_name)) - (expansion - (variable_name))) - (heredoc_end) + (heredoc_start) + (heredoc_body + (simple_expansion + (variable_name)) + (expansion + (variable_name))) + (heredoc_end))) (command (command_name (word)))) @@ -221,16 +221,16 @@ wc -l $tmpfile (command_name (word))) (heredoc_redirect - (heredoc_start)) - (file_redirect - (simple_expansion - (variable_name)))) - (heredoc_body - (simple_expansion - (variable_name)) - (expansion - (variable_name))) - (heredoc_end) + (heredoc_start) + (file_redirect + (simple_expansion + (variable_name))) + (heredoc_body + (simple_expansion + (variable_name)) + (expansion + (variable_name))) + (heredoc_end))) (command (command_name (word)) @@ -249,19 +249,19 @@ EOF --- (program - (pipeline - (redirected_statement - (command - (command_name - (word))) - (heredoc_redirect - (heredoc_start))) + (redirected_statement (command (command_name - (word)) - (word))) - (simple_heredoc_body) - (heredoc_end)) + (word))) + (heredoc_redirect + (heredoc_start) + (pipeline + (command + (command_name + (word)) + (word))) + (simple_heredoc_body) + (heredoc_end)))) ====================================== 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 @@ -298,10 +298,10 @@ EOF --- (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 @@ -321,9 +321,7 @@ usage() { (compound_statement (redirected_statement (command (command_name (word))) - (heredoc_redirect (heredoc_start))) - (heredoc_body (expansion (special_variable_name) (word))) - (heredoc_end)))) + (heredoc_redirect (heredoc_start) (heredoc_body (expansion (special_variable_name) (word))) (heredoc_end)))))) ========================================== Heredocs with empty bodies @@ -351,17 +349,17 @@ EOF name: (command_name (word))) redirect: (heredoc_redirect - (heredoc_start))) - (simple_heredoc_body) - (heredoc_end) + (heredoc_start) + (simple_heredoc_body) + (heredoc_end))) (redirected_statement body: (command name: (command_name (word))) redirect: (heredoc_redirect - (heredoc_start))) - (simple_heredoc_body) - (heredoc_end) + (heredoc_start) + (simple_heredoc_body) + (heredoc_end))) (function_definition name: (word) body: (compound_statement @@ -370,19 +368,19 @@ EOF name: (command_name (word))) redirect: (heredoc_redirect - (heredoc_start))) - (simple_heredoc_body) - (heredoc_end))) + (heredoc_start) + (simple_heredoc_body) + (heredoc_end))))) (redirected_statement body: (command name: (command_name (word))) redirect: (heredoc_redirect - (heredoc_start)) - redirect: (file_redirect - destination: (word))) - (simple_heredoc_body) - (heredoc_end)) + (heredoc_start) + (file_redirect + destination: (word)) + (simple_heredoc_body) + (heredoc_end)))) ========================================== Heredocs with weird characters @@ -411,11 +409,11 @@ Hello. --- (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