variable-regex: fix 70-*.sh

This commit is contained in:
Rutger van Beusekom 2018-11-15 19:57:10 +01:00
parent df605a161a
commit d36ee815df
2 changed files with 35 additions and 7 deletions

View File

@ -87,8 +87,8 @@
'elif' / 'for' / 'done' / 'do' / 'until' / 'while') &ws
nonreserved <- !reserved word
word <-- test / substitution /
(number / variable / variable-subst / delim / literal)+
word <-- test / substitution /
(number / variable-subst / variable / delim / literal)+
function-def <-- name sp* lpar rpar# ws* function-body
name <-- !reserved identifier
@ -136,12 +136,16 @@
variable <-- dollar ('*' / '@' / [0-9] / name /
lbrace name (variable-literal / &rbrace) rbrace)
variable-subst <- dollar lbrace (variable-or / variable-and / variable-regex / &rbrace) rbrace
variable-subst <- dollar lbrace (variable-or / variable-and / variable-regex) rbrace
variable-or <-- name min variable-word
variable-and <-- name plus variable-word
variable-word <- (variable-regex / substitution / variable-subst / variable / variable-literal)+
variable-regex <-- name ('%%' / '%' / '##' / '#' / '^^' / '^' /',,' / ',' / '*' / '@' / '?')+ variable-word
variable-literal <- (!rbrace !min !plus .)+
variable-word <- variable-regex / substitution / variable-subst / variable / variable-literal !slash / variable-string
variable-regex <-- name &slash regex-sep variable-literal '/' variable-string &rbrace /
name regex-sep variable-string
slash < '/'
variable-string <- (!rbrace .)+
variable-literal <- (!rbrace !min !plus !slash .)+
regex-sep <-- ('/' / '%%' / '%' / '##' / '#' / '^^' / '^' /',,' / ',' / '*' / '@' / '?')
min < '-'
plus < '+'
lbrace < '{'
@ -152,7 +156,7 @@
sq < [']
dq < [\"]
singlequotes <- sq (!sq .)* sq#
doublequotes <- dq (substitution / variable / (!dq (escape '\"' / .)))* dq#
doublequotes <- dq (substitution / variable-subst / variable / (!dq (escape '\"' / .)))* dq#
case-keyword < 'case'
do-keyword < 'do'

View File

@ -177,6 +177,12 @@
(define-syntax-rule (substitution commands)
(string-trim-right (with-output-to-string (lambda _ commands))))
;; (define (substitution . command)
;; (if (string? (car command)) (warn (parse-string (string-join command)))
;; (pipeline->string (list command))
;; (warn 'substitution: command '=> ))
;; )
(define-syntax command
(lambda (x)
(syntax-case x ()
@ -351,6 +357,21 @@
(or (regexp-exec regexp (substring string start))
(loop (1- start)))))))
(define (variable-regex name sep pattern)
(match sep
("##" (variable-hash-hash name pattern))
("#" (variable-hash name pattern))
("%%" (variable-percent-percent name pattern))
("%" (variable-percent name pattern))
("/" (variable-replace name pattern))))
(define (variable-replace name pattern)
(let* ((value (variable name))
(at (string-index pattern #\/))
(regex (if at (substring pattern 0 at) pattern))
(subst (if at (substring pattern (1+ at)) "")))
(regexp-substitute/global #f regex value 'pre subst 'post)))
(define (variable-hash name pattern)
(let ((value (variable name))
(glob? (glob? pattern)))
@ -423,5 +444,8 @@
(define (name o)
o)
(define (regex-sep o)
o)
(define (test . o) ;; TODO replace with implementation in scheme
(command (cons "test" o)))