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 {o[k]}
echo }}} echo }}}
echo ]]] === echo ]]] ===
[[ "35d8b" =~ ^[0-9a-fA-F] ]] || echo {nomatch}
--- ---
(program (program
(command (command_name (word)) (concatenation (word) (word))) (command (command_name (word)) (concatenation (word) (word)))
(command (command_name (word)) (word)) (command (command_name (word)) (word))
(command (command_name (word)) (word) (word)) (command (command_name (word)) (word) (word)))
(list
=============================
The regex operator
=============================
[[ "35d8b" =~ ^[0-9a-fA-F] ]]
[[ $CMD =~ (^|;)update_terminal_cwd($|;) ]]
[[ ! " ${completions[*]} " =~ " $alias_cmd " ]]
---
(program
(bracket_command (bracket_command
(string) (string)
(regex))
(bracket_command
(simple_expansion (variable_name))
(regex))
(bracket_command
(word) (word)
(concatenation (word) (word))) (string (expansion (subscript (variable_name) (word))))
(command (string (simple_expansion (variable_name)))))
(command_name (word))
(concatenation (word)))))
============================= =============================
Simple variable expansions Simple variable expansions

View File

@ -179,10 +179,20 @@ module.exports = grammar({
$._statement $._statement
)), )),
bracket_command: $ => choice( bracket_command: $ => {
seq('[', repeat1($._expression), ']'), const contents = repeat1(choice(
seq('[[', repeat1($._expression), ']]') $._expression,
), seq('=~', choice(
$.regex,
$._expression
))
))
return choice(
seq('[', contents, ']'),
seq('[[', contents, ']]')
)
},
// Commands // Commands
@ -383,6 +393,8 @@ module.exports = grammar({
seq('\\', noneOf('\\s')) seq('\\', noneOf('\\s'))
))), ))),
regex: $ => /\S+/,
_terminator: $ => choice(';', ';;', '\n', '&') _terminator: $ => choice(';', ';;', '\n', '&')
} }
}); });