Add regex syntax inside of expansions

This commit is contained in:
Max Brunsfeld 2018-03-01 10:41:16 -08:00
parent f33bf06de2
commit 0791027596
2 changed files with 33 additions and 5 deletions

View File

@ -33,6 +33,7 @@ The regex operator
[[ "35d8b" =~ ^[0-9a-fA-F] ]] [[ "35d8b" =~ ^[0-9a-fA-F] ]]
[[ $CMD =~ (^|;)update_terminal_cwd($|;) ]] [[ $CMD =~ (^|;)update_terminal_cwd($|;) ]]
[[ ! " ${completions[*]} " =~ " $alias_cmd " ]] [[ ! " ${completions[*]} " =~ " $alias_cmd " ]]
! [[ "$a" =~ ^a|b\ *c|d$ ]]
--- ---
@ -46,7 +47,13 @@ The regex operator
(bracket_command (bracket_command
(word) (word)
(string (expansion (subscript (variable_name) (word)))) (string (expansion (subscript (variable_name) (word))))
(string (simple_expansion (variable_name))))) (string (simple_expansion (variable_name))))
(command
(command_name (word))
(word)
(string (simple_expansion (variable_name)))
(regex)
(word)))
============================= =============================
Simple variable expansions Simple variable expansions
@ -108,7 +115,7 @@ F="${G%% *}"
(string (expansion (subscript (variable_name) (word))))) (string (expansion (subscript (variable_name) (word)))))
(variable_assignment (variable_assignment
(variable_name) (variable_name)
(string (expansion (variable_name) (word) (word)))) (string (expansion (variable_name) (regex) (word) (word))))
(variable_assignment (variable_assignment
(variable_name) (variable_name)
(string (expansion (variable_name) (word) (word))))) (string (expansion (variable_name) (word) (word)))))
@ -118,7 +125,7 @@ Variable expansions in strings
=================================== ===================================
A="${A:-$B/c}" A="${A:-$B/c}"
A="${b/$c/$d}" A="${b=$c/$d}"
--- ---
@ -139,6 +146,22 @@ A="${b/$c/$d}"
(word) (word)
(simple_expansion (variable_name))))))) (simple_expansion (variable_name)))))))
===================================
Variable expansions with regexes
===================================
A=${B//:;;/$'\n'}
# escaped space
C=${D/;\ *;|}
---
(program
(variable_assignment (variable_name) (expansion (variable_name) (regex)))
(comment)
(variable_assignment (variable_name) (expansion (variable_name) (regex))))
=================================== ===================================
Other variable expansion operators Other variable expansion operators
=================================== ===================================

View File

@ -373,9 +373,13 @@ module.exports = grammar({
$._simple_variable_name, $._simple_variable_name,
$._special_variable_name $._special_variable_name
), ),
optional(seq(
token(prec(1, '/')),
alias($.regex_without_right_brace, $.regex)
)),
repeat(choice( repeat(choice(
$._expression, $._expression,
':', ':?', '=', ':-', '%', '/', '-', '#' ':', ':?', '=', ':-', '%', '-', '#'
)) ))
), ),
), ),
@ -404,7 +408,8 @@ module.exports = grammar({
seq('\\', noneOf('\\s')) seq('\\', noneOf('\\s'))
))), ))),
regex: $ => /\S+/, regex: $ => /([^\s]|\\.)+/,
regex_without_right_brace: $ => /([^\s}]|\\.)+/,
_terminator: $ => choice(';', ';;', '\n', '&') _terminator: $ => choice(';', ';;', '\n', '&')
} }