Brace expansion: Difference between revisions

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(
function (s) {
var dctParse = andTree(
null,
tokens(s)
)[0];
 
console.log(
pp(dctParse)
);
 
return s + '\n\n' + evaluated(
dctParse
).map(function (x) {
return ' ' + x;
}).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 === '},' ?&& iNest -=== 1) ? lstCommas.concat(iPosn) : iNestlstCommas;
),
lst = (t === ',' && iNest === 1) ?
lstCommas.concat(iPosn) : lstCommas;
 
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) {
and(args.slice(1)).map(functionreturn (t)h {+ t;
return h + 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, []];
[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.fn(e.args.map(evaluated));
);
}
 
Line 1,394 ⟶ 1,353:
}
 
 
return main();
// MAIN
 
// s -> [s]
function mainexpansions(s) {
// BRACE EXPRESSION PARSED
var dctParse = andTree(null, tokens(s))[0];
 
// ABSTRACT SYNTAX TREE LOGGED
console.log(pp(dctParse));
 
// AST EVALUATED TO LIST OF STRINGS
return ppevaluated(dctParse);
}
 
 
// 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} \\\\\\\\\\}'
];
 
 
// 1. Return each expression with an indented list of its expansions, while
// 2. logging each parse tree to the console.log() stream
 
return lstTests.map(function (s) {
return s + '\n\n' + evaluatedexpansions(s).map(function (x) {
return ' null,' + x;
}).join('\n');
}).join('\n\n');
 
})();</lang>
 
9,659

edits