Brace expansion: Difference between revisions

Content deleted Content added
Tim-brown (talk | contribs)
Hout (talk | contribs)
Line 1,225: Line 1,225:
<lang JavaScript>(function () {
<lang JavaScript>(function () {
'use strict'
'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
// Index of any closing brace matching the opening brace at iPosn
Line 1,265: Line 1,232:


var t = tkns[iPosn],
var t = tkns[iPosn],
n = (t === '{') ? iNest + 1 : (
n = (t === '{') ? iNest + 1 : (t === '}' ? iNest - 1 : iNest),
t === '}' ? iNest - 1 : iNest
lst = (t === ',' && iNest === 1) ? lstCommas.concat(iPosn) : lstCommas;
),
lst = (t === ',' && iNest === 1) ?
lstCommas.concat(iPosn) : lstCommas;


return n ? bracePair(tkns, iPosn + 1, n, lst) : {
return n ? bracePair(tkns, iPosn + 1, n, lst) : {
Line 1,347: Line 1,311:
return lng ? (
return lng ? (
1 < lng ? lstHead.reduce(function (a, h) {
1 < lng ? lstHead.reduce(function (a, h) {
return a.concat(
return a.concat(and(args.slice(1)).map(function (t) {
and(args.slice(1)).map(function (t) {
return h + t;
return h + t;
}));
})
);
}, []) : lstHead
}, []) : lstHead
) : [];
) : [];
Line 1,366: Line 1,328:
// One list split into two (first sublist length n)
// One list split into two (first sublist length n)
function splitAt(n, lst) {
function splitAt(n, lst) {
return n < lst.length + 1 ? (
return n < lst.length + 1 ? [lst.slice(0, n), lst.slice(n)] : [lst, []];
[lst.slice(0, n), lst.slice(n)]
) : [lst, []];
}
}


Line 1,380: Line 1,340:
// Value of the parse tree
// Value of the parse tree
function evaluated(e) {
function evaluated(e) {
return typeof e === 'string' ? e : e.fn(
return typeof e === 'string' ? e :
e.args.map(evaluated)
e.fn(e.args.map(evaluated));
);
}
}


Line 1,394: Line 1,353:
}
}



return main();
// MAIN

// s -> [s]
function expansions(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 evaluated(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' + expansions(s).map(function (x) {
return ' ' + x;
}).join('\n');
}).join('\n\n');

})();</lang>
})();</lang>