Created
May 3, 2009 04:05
-
-
Save satyr/105823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findArgs(input, delims, wordBased, leftBranch){ | |
var res = [{'': input}], source = '('+ delims.join('|') +')'; | |
if(wordBased) source = '\\b'+ source +'\\b'; | |
var parts = input.split(RegExp(source)); | |
if(parts.length < 2) return res; | |
// power sets of delimiter indices | |
var sets = [[]], {push} = sets; | |
for(let i = 1, l = parts.length; i < l; i += 2) | |
push.apply(sets, [a.concat(i) for each(a in sets)]); | |
sets.shift(); | |
for each(let idcs in sets){ | |
let args = {}, len = idcs.length; | |
for(let i = 0; i < len; ++i){ | |
let idx = idcs[i], dlm = parts[idx]; | |
(args[dlm] || (args[dlm] = [])).push( | |
(leftBranch | |
? parts.slice(idcs[i - 1] + 1 || 0, idcs[i]) | |
: parts.slice(idcs[i] + 1, idcs[i + 2] || parts.length)) | |
.join('')); | |
} | |
if(args){ | |
args[''] = ((leftBranch | |
? parts.slice(idcs[len - 1] + 1) | |
: parts.slice(0, idcs[0])) | |
.join('')); | |
res.push(args); | |
} | |
} | |
return res; | |
} | |
CmdUtils.CreateCommand({ | |
name: 'test-findargs', | |
takes: {'?': noun_arb_text}, | |
preview: function(pbl, {text}){ | |
var txt = ( | |
text | |
? [[text, 'を|に|の|で|から|として'.split('|'), false, true]] | |
: [['to be or not to be', ['to'], true, false], | |
['あれとこれはそこへ!', ['と', 'は', 'へ'], false, true],] | |
).map(function(arg)( | |
findArgs.apply(null, arg).map(function(a){ | |
var s = ''; | |
for(var d in a) s += (d || 'DO') +': '+ a[d] +'\n'; | |
return s; | |
}).join('\n'))).join('----------------\n'); | |
pbl.innerHTML = <pre>{txt}</pre>.toXMLString(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment