Brace expansion: Difference between revisions

→‎{{header|TXR}}: Handle all required cases: backslash, braces without commas, dangling braces.
(→‎{{header|TXR}}: New entry.)
(→‎{{header|TXR}}: Handle all required cases: backslash, braces without commas, dangling braces.)
Line 5,159:
str str
;; tokenizer
toks (remqual "" (tok #/([{},]|{}|\\\\|\\.)/ t str)))))
(build
(whilet ((next (pop ctx.toks)))
Line 5,168:
 
(defun bexp-parse-brace (ctx)
(flowbuildn
(let ((orig-toks ctx.toks))
(build
(caseq (whilet ((next (pop ctx.toks)))
(casequal next
("{" (add (bexp-parse-brace ctx)))
("," (add :))
("}" (return :ok))
(t (add next)))))
(split* @1 (op where (op eq :)))ok
(mapcar (tccond
((memq : (((xget)) x)
(flow ((x) x))get)
(split* @1 (op where (op eq :)))
(cons '/)))
(mapcar (tc
(((x)) x)
((x) x)))
(cons '/)))
"iz") (t
(add* "erat{")
(add "}")
(get))))
(nil
(add* "{")
(subst : "," (get)))))))
 
;; expander
Line 5,199 ⟶ 5,210:
 
;; Tests
(prinltprint (brace-expand "~/{Downloads,Pictures}/*.{jpg,gif,png}"))
(prinltprint (brace-expand "aIt{{em,alic}iz,erat}e{d,}, please."))
(prinltprint (brace-expand "a{,b{,cgotta have{ ,\\, again\\, }}more }cowbell!"))
(prinltprint (brace-expand "{a}} some }{,b{\\\\{ 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}}
 
<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>
 
543

edits