Support ${ / / }, ${ / }.

This commit is contained in:
Jan Nieuwenhuizen 2018-11-10 10:16:50 +01:00
parent e8f90ba6a8
commit 726ca052a9
9 changed files with 43 additions and 9 deletions

View File

@ -0,0 +1 @@
xxbar/xx

View File

@ -12,8 +12,8 @@ tests="
01-script-newline
01-script-semi
01-script-backslash
01-script-backslash-space.sh
01-script-backslash-twice.sh
01-script-backslash-space
01-script-backslash-twice
03-echo
03-echo-doublequotes
@ -62,18 +62,21 @@ tests="
40-eval
40-eval-echo-variable
40-assignment-eval-echo.sh
40-assignment-eval-echo
50-iohere
60-function
60-subst
70-hash.sh
70-hash-hash.sh
70-percent.sh
70-percent-percent.sh
70-percent-space.sh
70-hash
70-hash-hash
70-percent
70-percent-percent
70-percent-space
70-slash
70-slash-string
70-slash-string-slash
100-sed
100-sed-once

View File

@ -225,13 +225,16 @@
dollar < '$'
literal <-- backslash? (!ws !amp !tick !dollar !pipe !semi !par !nl !sp !rbrace !io-op !dq !sq .)+
variable <-- dollar ('$' / '#' / '*' / '?' / '@' / [0-9] / identifier / lbrace identifier rbrace)
variable-and-or <- dollar lbrace (variable-or / variable-and / variable-hash-hash / variable-hash / variable-percent-percent / variable-percent ) rbrace
variable-and-or <- dollar lbrace (variable-or / variable-and / variable-hash-hash / variable-hash / variable-percent-percent / variable-percent / variable-slash ) rbrace
variable-and <-- identifier plus rhs
variable-or <-- identifier minus rhs
variable-hash <-- identifier hash rhs
variable-hash-hash <-- identifier hash hash rhs
variable-percent <-- identifier percent rhs
variable-percent-percent <-- identifier percent percent rhs
variable-slash <-- (identifier slash pat slash str) / (identifier slash pat slash) / (identifier slash pat)
pat <-- (!dollar !rbrace !slash .)+
str <-- (!rbrace .)+
delim <- singlequotes / doublequotes / substitution
sq < [']
dq < [\"]
@ -254,6 +257,7 @@
minus < '-'
hash < '#'
percent < '%'
slash < '/'
par < lpar / rpar
nl < '\n'
sp < '\t' / ' ' / (escaped-nl sp*)

View File

@ -341,3 +341,21 @@
value))
(if (string-suffix? pattern value) (substring value 0 (string-length pattern))
value))))
(define (pat o)
o)
(define (str o)
o)
(define* (variable-slash name pattern #:optional (replace ""))
(let ((value (variable name))
(glob? (glob? pattern)))
(let ((match (if glob? (let ((regexp (glob->regex pattern #:begin "" #:end "")))
(regexp-exec regexp value))
(string-match pattern value))))
(if match (string-append
(substring value 0 (match:start match))
replace
(substring value (match:end match)))
value))))

View File

@ -0,0 +1,2 @@
var='xxfooxx'
echo "${var/foo/bar/}"

2
test/70-slash-string.sh Normal file
View File

@ -0,0 +1,2 @@
var='xxfooxx'
echo "${var/foo/bar}"

View File

@ -0,0 +1 @@
xxbarxx

2
test/70-slash.sh Normal file
View File

@ -0,0 +1,2 @@
var='xxfooxx'
echo "${var/foo}"

1
test/70-slash.stdout Normal file
View File

@ -0,0 +1 @@
xxxx