Brace expansion: Difference between revisions
→ES5 Functional
Line 1,225:
<lang JavaScript>(function () {
'use strict'
// 1. Return each expression with an indented list of its expansions, while▼
// 2. logging each parse tree to the console.log() stream▼
function main() {▼
// Sample expressions, double-escaped for quotation in source code.▼
var lstTests = [▼
'~/{Downloads,Pictures}/*.{jpg,gif,png}',▼
'It{{em,alic}iz,erat}e{d,}, please.',▼
'{,{,gotta have{ ,\\, again\\, }}more }cowbell!',▼
'{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}'▼
return lstTests.map(▼
var dctParse = andTree(▼
null,▼
console.log(▼
pp(dctParse)▼
return s + '\n\n' + evaluated(▼
}).join('\n');▼
).join('\n\n');▼
// Index of any closing brace matching the opening brace at iPosn
Line 1,265 ⟶ 1,232:
var t = tkns[iPosn],
n = (t === '{') ? iNest + 1 : (t === '}' ? iNest - 1 : iNest),
lst = (t === '
return n ? bracePair(tkns, iPosn + 1, n, lst) : {
Line 1,347 ⟶ 1,311:
return lng ? (
1 < lng ? lstHead.reduce(function (a, h) {
return a.concat(and(args.slice(1)).map(function (t) {
}, []) : lstHead
) : [];
Line 1,366 ⟶ 1,328:
// One list split into two (first sublist length n)
function splitAt(n, lst) {
return n < lst.length + 1 ? [lst.slice(0, n), lst.slice(n)] : [lst, []];
}
Line 1,380 ⟶ 1,340:
// Value of the parse tree
function evaluated(e) {
return typeof e === 'string' ? e :
e.fn(e.args.map(evaluated));
}
Line 1,394 ⟶ 1,353:
}
// MAIN
// s -> [s]
// BRACE EXPRESSION PARSED
// ABSTRACT SYNTAX TREE LOGGED
// AST EVALUATED TO LIST OF STRINGS
▲ }
▲ ];
▲ // 1. Return each expression with an indented list of its expansions, while
▲ // 2. logging each parse tree to the console.log() stream
})();</lang>
|