1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 12:37:58 +01:00

Add inordinate number of quoting styles, none of which, save one, are acually accessible

This commit is contained in:
Kris Maglione
2008-12-12 21:48:13 -05:00
parent 5489a0b2de
commit d94d8b6517

View File

@@ -164,6 +164,8 @@ function Commands() //{{{
return NaN; return NaN;
} }
const QUOTE_STYLE = "vimperator";
const quoteMap = { const quoteMap = {
"\n": "n", "\n": "n",
"\t": "t" "\t": "t"
@@ -298,92 +300,63 @@ function Commands() //{{{
}, },
// returns [count, parsed_argument] // returns [count, parsed_argument]
parseArg: function getNextArg(str) parseArg: function parseArg(str)
{ {
var stringDelimiter = null; let arg = "";
var escapeNext = false; let quote = null;
let len = str.length;
var arg = ""; while (str.length && !/^\s/.test(str))
{
let res;
outer: switch (QUOTE_STYLE)
for (let i = 0; i < str.length; i++)
{ {
inner: case "vim-sucks":
switch (str[i]) if (res = str.match = str.match(/^()((?:[^\\\s]|\\.)+)((?:\\$)?)/))
{ arg += res[2].replace(/\\(.)/g, "$1");
case '"':
case "'":
if (escapeNext)
{
escapeNext = false;
break;
}
switch (stringDelimiter)
{
case str[i]:
stringDelimiter = null;
continue outer;
case null:
stringDelimiter = str[i];
continue outer;
}
break; break;
// \ is an escape key for non quoted or "-quoted strings case "vimperator":
// for '-quoted strings it is taken literally, apart from \' and \\ if (res = str.match(/^()((?:[^\\\s"']|\\.)+)((?:\\$)?)/))
case "\\": arg += res[2].replace(/\\(.)/g, "$1");
if (escapeNext) else if (res = str.match(/^(")((?:[^\\"]|\\.)*)("?)/))
{ arg += eval(res[0] + (res[3] ? "" : '"'));
escapeNext = false; else if (res = str.match(/^(')((?:[^\\']|\\.)*)('?)/))
break; arg += res[2].replace(/\\(.)/g, function (n0, n1) /[\\']/.test(n1) ? n1 : n0);
}
else
{
// in non-quoted strings, only escape "\\" and "\ ", otherwise drop "\\"
if (!stringDelimiter && str[i + 1] != "\\" && str[i + 1] != " ")
continue outer;
// in single quoted strings, only escape "\\" and "\'", otherwise keep "\\"
if (stringDelimiter == "'" && str[i + 1] != "\\" && str[i + 1] != "'")
break;
escapeNext = true;
continue outer;
}
break; break;
default: case "rc-ish":
if (stringDelimiter == "'") if (res = str.match = str.match(/^()((?:[^\\\s"']|\\.)+)((?:\\$)?)/))
{ arg += res[2].replace(/\\(.)/g, "$1");
escapeNext = false; else if (res = str.match(/^(")((?:[^\\"]|\\.)*)("?)/))
arg += eval(res[0] + (res[3] ? "" : '"'));
else if (res = str.match(/^(')((?:[^']|'')*)('?)/))
arg += res[2].replace("''", "'", "g");
break; break;
}
if (escapeNext) case "pythonesque":
{ if (res = str.match = str.match(/^()((?:[^\\\s"']|\\.)+)((?:\\$)?)/))
escapeNext = false; arg += res[2].replace(/\\(.)/g, "$1");
switch (str[i]) else if (res = str.match(/^(""")((?:.?.?[^"])*)((?:""")?)/))
{ arg += res[2];
case "n": arg += "\n"; break; else if (res = str.match(/^(")((?:[^\\"]|\\.)*)("?)/))
case "t": arg += "\t"; break; arg += eval(res[0] + (res[3] ? "" : '"'));
default: else if (res = str.match(/^(')((?:[^\\']|\\.)*)('?)/))
break inner; // this makes "a\fb" -> afb; wanted or should we return ab? --mst arg += res[2].replace(/\\(.)/g, function (n0, n1) /[\\']/.test(n1) ? n1 : n0);
}
continue outer;
}
else if (stringDelimiter != '"' && /\s/.test(str[i]))
{
return [i, arg];
}
break; break;
} }
arg += str[i];
}
// TODO: add parsing of a " comment here: if (!res)
if (stringDelimiter) break;
return [str.length, arg, stringDelimiter]; if (!res[3])
if (escapeNext) quote = res[1];
return [str.length, arg, "\\"]; if (!res[1])
else quote = res[3];
return [str.length, arg]; str = str.substr(res[0].length);
}
return [len - str.length, arg, quote];
}, },
// in '-quoted strings, only ' and \ itself are escaped // in '-quoted strings, only ' and \ itself are escaped
@@ -506,6 +479,8 @@ function Commands() //{{{
if (sep == "=" || /\s/.test(sep) && opt[1] != this.OPTION_NOARG) if (sep == "=" || /\s/.test(sep) && opt[1] != this.OPTION_NOARG)
{ {
[count, arg, quote] = getNextArg(sub.substr(optname.length + 1)); [count, arg, quote] = getNextArg(sub.substr(optname.length + 1));
if (quote == "\\" && !complete)
return liberator.echoerr("Trailing \\");
// if we add the argument to an option after a space, it MUST not be empty // if we add the argument to an option after a space, it MUST not be empty
if (sep != "=" && !quote && arg.length == 0) if (sep != "=" && !quote && arg.length == 0)
@@ -595,6 +570,9 @@ function Commands() //{{{
// if not an option, treat this token as an argument // if not an option, treat this token as an argument
var [count, arg, quote] = getNextArg(sub); var [count, arg, quote] = getNextArg(sub);
if (quote == "\\" && !complete)
return liberator.echoerr("Trailing \\");
if (complete) if (complete)
{ {
args.quote = complQuote[quote] || complQuote[""]; args.quote = complQuote[quote] || complQuote[""];