Brace expansion: Difference between revisions

Content added Content deleted
(→‎{{header|TXR}}: New entry.)
(→‎{{header|TXR}}: Handle all required cases: backslash, braces without commas, dangling braces.)
Line 5,159: Line 5,159:
str str
str str
;; tokenizer
;; tokenizer
toks (remqual "" (tok #/[{},]/ t str)))))
toks (remqual "" (tok #/([{},]|{}|\\\\|\\.)/ t str)))))
(build
(build
(whilet ((next (pop ctx.toks)))
(whilet ((next (pop ctx.toks)))
Line 5,168: Line 5,168:


(defun bexp-parse-brace (ctx)
(defun bexp-parse-brace (ctx)
(flow
(buildn
(let ((orig-toks ctx.toks))
(build
(whilet ((next (pop ctx.toks)))
(caseq (whilet ((next (pop ctx.toks)))
(casequal next
(casequal next
("{" (add (bexp-parse-brace ctx)))
("{" (add (bexp-parse-brace ctx)))
("," (add :))
("," (add :))
("}" (return))
("}" (return :ok))
(t (add next)))))
(t (add next))))
(split* @1 (op where (op eq :)))
(:ok
(mapcar (tc
(cond
(((x)) x)
((memq : (get))
((x) x)))
(flow (get)
(split* @1 (op where (op eq :)))
(cons '/)))
(mapcar (tc
(((x)) x)
((x) x)))
(cons '/)))
(t
(add* "{")
(add "}")
(get))))
(nil
(add* "{")
(subst : "," (get)))))))


;; expander
;; expander
Line 5,199: Line 5,210:


;; Tests
;; Tests
(prinl (brace-expand ""))
(tprint (brace-expand "~/{Downloads,Pictures}/*.{jpg,gif,png}"))
(prinl (brace-expand "a"))
(tprint (brace-expand "It{{em,alic}iz,erat}e{d,}, please."))
(prinl (brace-expand "a,b,c"))
(tprint (brace-expand "{,{,gotta have{ ,\\, again\\, }}more }cowbell!"))
(prinl (brace-expand "{a,b}"))
(tprint (brace-expand "{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"))
</syntaxhighlight>
(prinl (brace-expand "{a,b,c}"))
(prinl (brace-expand "x{a,b,c}y"))
(prinl (brace-expand "x{a,b}y{c,d}z"))
(prinl (brace-expand "x{a{b,c}d,e{f,g}h}y"))
(prinl (bexp-parse "It{{em,alic}iz,erat}e{d,}"))
(prinl (brace-expand "It{{em,alic}iz,erat}e{d,}"))</syntaxhighlight>


{{out}}
{{out}}


<pre>("")
<pre>~/Downloads/*.jpg
~/Downloads/*.gif
("a")
~/Downloads/*.png
("a,b,c")
~/Pictures/*.jpg
("a" "b")
~/Pictures/*.gif
("a" "b" "c")
~/Pictures/*.png
("xay" "xby" "xcy")
Itemized, please.
("xaycz" "xaydz" "xbycz" "xbydz")
Itemize, please.
("xabdy" "xacdy" "xefhy" "xeghy")
Italicized, please.
("It" (/ ((/ "em" "alic")
Italicize, please.
"iz")
Iterated, please.
"erat")
Iterate, please.
"e" (/ "d" ()))
cowbell!
("Itemized" "Itemize" "Italicized" "Italicize" "Iterated" "Iterate")
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
</pre>