From 4868ce43e235b77619bf15352f98ed8847f07262 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 21 Aug 2023 18:41:47 -0400 Subject: [PATCH] fix: single line function definitions, expose special characters --- grammar.js | 13 ++++----- test/corpus/literals.txt | 16 +++++++++-- test/corpus/statements.txt | 55 +++++++++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/grammar.js b/grammar.js index 24662fafe..de6c08e5d 100644 --- a/grammar.js +++ b/grammar.js @@ -28,6 +28,7 @@ module.exports = grammar({ conflicts: $ => [ [$._expression, $.command_name], [$.command, $.variable_assignments], + [$.compound_statement], ], inline: $ => [ @@ -306,8 +307,8 @@ module.exports = grammar({ compound_statement: $ => seq( '{', - optional($._statements2), - '}', + optional(choice($._statements2, seq($._statement, optional($._terminator)))), + alias(token(prec(-1, '}')), '}'), ), subshell: $ => seq( @@ -537,16 +538,16 @@ module.exports = grammar({ concatenation: $ => prec(-1, seq( choice( $._primary_expression, - $._special_character, + alias($._special_character, $.word), ), - repeat1(prec(-1, seq( + repeat1(seq( $._concat, choice( $._primary_expression, - $._special_character, + alias($._special_character, $.word), alias($._comment_word, $.word), ), - ))), + )), optional(seq($._concat, '$')), )), diff --git a/test/corpus/literals.txt b/test/corpus/literals.txt index 41c12da62..db070286e 100644 --- a/test/corpus/literals.txt +++ b/test/corpus/literals.txt @@ -33,16 +33,26 @@ echo ]]] === (command_name (word)) (concatenation + (word) + (word) + (word) + (word) (word) (word))) (command (command_name (word)) - (concatenation)) + (concatenation + (word) + (word) + (word))) (command (command_name (word)) - (concatenation) + (concatenation + (word) + (word) + (word)) (word))) ================================================================================ @@ -338,6 +348,8 @@ cat ${BAR} ${ABC=def} ${GHI:?jkl} (expansion (variable_name) (concatenation + (word) + (word) (word))))))) ================================================================================ diff --git a/test/corpus/statements.txt b/test/corpus/statements.txt index 24d7bd925..c0e53aee6 100644 --- a/test/corpus/statements.txt +++ b/test/corpus/statements.txt @@ -463,7 +463,8 @@ fi argument: (word) argument: (word))) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -484,7 +485,8 @@ fi argument: (word) argument: (word)) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -500,10 +502,15 @@ fi (program (if_statement condition: (variable_assignment - name: (variable_name) - value: (command_substitution (command name: (command_name (word)) argument: (raw_string)))) + name: (variable_name) + value: (command_substitution + (command + name: (command_name + (word)) + argument: (raw_string)))) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -518,7 +525,7 @@ fi (program (if_statement - condition: (negated_command + condition: (negated_command (variable_assignment name: (variable_name) value: (command_substitution @@ -527,7 +534,8 @@ fi (word)) argument: (raw_string))))) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -543,10 +551,11 @@ fi (program (if_statement condition: (variable_assignment - name: (variable_name) - value: (number)) + name: (variable_name) + value: (number)) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -566,7 +575,8 @@ fi name: (variable_name) value: (number))) (command - name: (command_name (word)) + name: (command_name + (word)) argument: (raw_string)))) ================================================================================ @@ -674,7 +684,9 @@ esac (case_item (concatenation (word) - (number)) + (word) + (number) + (word)) (command (command_name (word)) @@ -913,6 +925,8 @@ function do_yet_another_thing { echo ok } 2>&1 +do_nothing() { return 0 } + -------------------------------------------------------------------------------- (program @@ -946,9 +960,13 @@ function do_yet_another_thing { (command_name (word)) (concatenation + (word) + (word) (word)) (word) (concatenation + (word) + (word) (word)) (word) (word))))) @@ -962,7 +980,14 @@ function do_yet_another_thing { (word)))) (file_redirect (file_descriptor) - (number)))) + (number))) + (function_definition + (word) + (compound_statement + (command + (command_name + (word)) + (number))))) ================================================================================ Variable declaration: declare & typeset @@ -1124,8 +1149,12 @@ export "$(echo ${key} | tr [:lower:] [:upper:])=${p_key#*=}" (command_name (word)) (concatenation + (word) + (word) (word)) (concatenation + (word) + (word) (word))))) (expansion (variable_name)