From c7e42b8e9624d193be7687d8fb8ce33a946ba336 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 1 Mar 2018 09:53:10 -0800 Subject: [PATCH] Add special lexing for regexes after `=~` --- corpus/expressions.txt | 33 +++++++++++++++++++++++---------- grammar.js | 20 ++++++++++++++++---- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/corpus/expressions.txt b/corpus/expressions.txt index 008d787..9cec1aa 100644 --- a/corpus/expressions.txt +++ b/corpus/expressions.txt @@ -18,22 +18,35 @@ Words with special characters echo {o[k]} echo }}} echo ]]] === -[[ "35d8b" =~ ^[0-9a-fA-F] ]] || echo {nomatch} --- (program (command (command_name (word)) (concatenation (word) (word))) (command (command_name (word)) (word)) - (command (command_name (word)) (word) (word)) - (list - (bracket_command - (string) - (word) - (concatenation (word) (word))) - (command - (command_name (word)) - (concatenation (word))))) + (command (command_name (word)) (word) (word))) + +============================= +The regex operator +============================= + +[[ "35d8b" =~ ^[0-9a-fA-F] ]] +[[ $CMD =~ (^|;)update_terminal_cwd($|;) ]] +[[ ! " ${completions[*]} " =~ " $alias_cmd " ]] + +--- + +(program + (bracket_command + (string) + (regex)) + (bracket_command + (simple_expansion (variable_name)) + (regex)) + (bracket_command + (word) + (string (expansion (subscript (variable_name) (word)))) + (string (simple_expansion (variable_name))))) ============================= Simple variable expansions diff --git a/grammar.js b/grammar.js index 2d89990..784713a 100644 --- a/grammar.js +++ b/grammar.js @@ -179,10 +179,20 @@ module.exports = grammar({ $._statement )), - bracket_command: $ => choice( - seq('[', repeat1($._expression), ']'), - seq('[[', repeat1($._expression), ']]') - ), + bracket_command: $ => { + const contents = repeat1(choice( + $._expression, + seq('=~', choice( + $.regex, + $._expression + )) + )) + + return choice( + seq('[', contents, ']'), + seq('[[', contents, ']]') + ) + }, // Commands @@ -383,6 +393,8 @@ module.exports = grammar({ seq('\\', noneOf('\\s')) ))), + regex: $ => /\S+/, + _terminator: $ => choice(';', ';;', '\n', '&') } });