Add special lexing for regexes after `=~`

This commit is contained in:
Max Brunsfeld 2018-03-01 09:53:10 -08:00
parent 0d9f854862
commit c7e42b8e96
2 changed files with 39 additions and 14 deletions

View File

@ -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
(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)
(concatenation (word) (word)))
(command
(command_name (word))
(concatenation (word)))))
(string (expansion (subscript (variable_name) (word))))
(string (simple_expansion (variable_name)))))
=============================
Simple variable expansions

View File

@ -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', '&')
}
});