Brace expansion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|TXR}}: More notes.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 users not shown)
Line 4,989:
 
templates collateSequence
data part <[]|''> local
@: [''];
$... -> #
$@!
when <´part´ '.*'> do
def part: $;
@: [$@... -> '$;$part;'];
Line 5,039 ⟶ 5,040:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
Line 5,160 ⟶ 5,162:
This notation is processed by the <code>bexp-expand</code> function to produce the list of strings which it denotes. The <code>bexp-parse</code> function parses a string containing brace expansion into the above notation.
 
The backslashes and edge cases are handled between the tokenizing and parsing. Backslashed characters are represented as tokens which include the backslash. Thus the <code>\{</code> token compares unequal to <code>{</code> and isn't mistaken for it. These backslashed tokens just look like any other text that has no special meaning. Adding code to the parser to recognize these tokens and strip the backslash would be very easy. Right after the <code>("," (add :))</code> case, adding a case which looks like <code>(`\\@x` (add x))</code> should do the trick.
 
The empty <code>{}</code> is handled as a token, but other cases of braces containing no commas are handled in the parser.
 
TheWhen the parser forhas thescanned a complete, valid brace notationthat replacescontains no comma, instead of generating a <code>,(/ ...)</code> (comma)tree tokensnode withfrom the content, it generates <code>:("{" ... "}")</code>, (colon)rendering keywordthe symbol.braces Thatas isliteral thenstrings. usedThe for<code>...</code> splittingcontent themay items to form the arguments ofcontain <code>/</code> operators, as required.
 
When the parser has scanned aan complete, validincomplete brace that contains no comma, insteadit ofputs generatingout a <code>(/"{" ...)</code>: treethe nodedangling frombrace theis represented contentliterally, itfollowed generatesby <code>("{"the items that have ..been parsed out. "}")</code>,The renderingcomma theelements bracesare aspreserved literalin strings.this Thecase; <code>...</code>the contentlack mayof containa <code>/</code>closing operators,brace turns off astheir requiredmeaning.
 
In the main case of a balanced brace with commas, the parsed out elements are split on the commas, which are removed, and that forms the arguments of <code>/</code> node.
When the parser has scanned an incomplete brace, which may contain commas that have been replaced by <code>:</code>, it reverts those symbols back to commas and then puts out <code>("{" ...)</code>: the dangling brace is represented literally, followed by the items that have been parsed out.
 
<syntaxhighlight lang="txrlisp">;; API
Line 5,197 ⟶ 5,199:
(casequal next
("{" (add (bexp-parse-brace ctx)))
("," (add :))
("}" (return :ok))
(t (add next))))
(:ok
(cond
((memqmemqual :"," (get))
(flow (get)
(split* @1 (op where (op eqequal :",")))
(mapcar (tc
(((x)) x)
((x) x)))
(cons '/)))
(t
Line 5,215 ⟶ 5,213:
(nil
(add* "{")
(subst : "," (get)))))))
 
;; expander
Line 5,237 ⟶ 5,235:
(tprint (brace-expand "It{{em,alic}iz,erat}e{d,}, please."))
(tprint (brace-expand "{,{,gotta have{ ,\\, again\\, }}more }cowbell!"))
(tprint (brace-expand "{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"))</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 5,365 ⟶ 5,362:
=={{header|Wren}}==
{{trans|Python}}
<syntaxhighlight lang="ecmascriptwren">var getGroup // forward declaration
 
var getItem = Fn.new { |s, depth|
Line 5,460 ⟶ 5,457:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|zkl}}==
This is a two pass algorithm (2*length(string)), one pass to find valid {} pairs, the next pass to expand them.
9,485

edits