Skip to content

Commit

Permalink
Adding context support to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Jun 5, 2011
1 parent 24d5433 commit b4127a8
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/jsonselect.js
Expand Up @@ -14,7 +14,7 @@
} }
return (new Function("return " + str))(); return (new Function("return " + str))();
} catch(e) { } catch(e) {
te("ijs"); te("ijs", e.message);
} }
} }


Expand All @@ -40,8 +40,8 @@
}; };


// throw an error message // throw an error message
function te(ec) { function te(ec, context) {
throw new Error(errorCodes[ec]); throw new Error(errorCodes[ec] + " in '" + context + "'");
} }


// THE LEXER // THE LEXER
Expand Down Expand Up @@ -90,9 +90,9 @@
else if (m[3]) a = [off, toks.typ, m[0]]; else if (m[3]) a = [off, toks.typ, m[0]];
else if (m[4]) a = [off, toks.psc, m[0]]; else if (m[4]) a = [off, toks.psc, m[0]];
else if (m[5]) a = [off, toks.psf, m[0]]; else if (m[5]) a = [off, toks.psf, m[0]];
else if (m[6]) te("upc"); else if (m[6]) te("upc", str);
else if (m[8]) a = [off, m[7] ? toks.ide : toks.str, jsonParse(m[8])]; else if (m[8]) a = [off, m[7] ? toks.ide : toks.str, jsonParse(m[8])];
else if (m[9]) te("ujs"); else if (m[9]) te("ujs", str);
else if (m[10]) a = [off, toks.ide, m[10].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")]; else if (m[10]) a = [off, toks.ide, m[10].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")];
return a; return a;
} }
Expand Down Expand Up @@ -156,11 +156,11 @@
if (l && l[1] === '(') { if (l && l[1] === '(') {
lhs = exprParse2(str, l[0]); lhs = exprParse2(str, l[0]);
var p = exprLex(str, lhs[0]); var p = exprLex(str, lhs[0]);
if (!p || p[1] !== ')') te('epex'); if (!p || p[1] !== ')') te('epex', str);
off = p[0]; off = p[0];
lhs = [ '(', lhs[1] ]; lhs = [ '(', lhs[1] ];
} else if (!l || (l[1] && l[1] != 'x')) { } else if (!l || (l[1] && l[1] != 'x')) {
te("ee"); te("ee", str + " - " + ( l[1] && l[1] ));
} else { } else {
lhs = ((l[1] === 'x') ? undefined : l[2]); lhs = ((l[1] === 'x') ? undefined : l[2]);
off = l[0]; off = l[0];
Expand All @@ -170,7 +170,7 @@
var op = exprLex(str, off); var op = exprLex(str, off);
if (!op || op[1] == ')') return [off, lhs]; if (!op || op[1] == ')') return [off, lhs];
else if (op[1] == 'x' || !op[1]) { else if (op[1] == 'x' || !op[1]) {
te('bop'); te('bop', str + " - " + ( op[1] && op[1] ));
} }


// tail recursion to fetch the rhs expression // tail recursion to fetch the rhs expression
Expand Down Expand Up @@ -238,13 +238,13 @@
a = []; a = [];
off = s[0]; off = s[0];
} else if (s[1] === ")") { } else if (s[1] === ")") {
if (!nested) te("ucp"); if (!nested) te("ucp", s[1]);
readParen = 1; readParen = 1;
off = s[0]; off = s[0];
break; break;
} }
} }
if (nested && !readParen) te("mcp"); if (nested && !readParen) te("mcp", str);
if (am) am.push(a); if (am) am.push(a);
var rv; var rv;
if (!nested && hints.usesSiblingOp) { if (!nested && hints.usesSiblingOp) {
Expand Down Expand Up @@ -322,10 +322,10 @@
if (l === undefined) { if (l === undefined) {
break; break;
} else if (l[1] === toks.ide) { } else if (l[1] === toks.ide) {
if (s.id) te("nmi"); if (s.id) te("nmi", l[1]);
s.id = l[2]; s.id = l[2];
} else if (l[1] === toks.psc) { } else if (l[1] === toks.psc) {
if (s.pc || s.pf) te("mpc"); if (s.pc || s.pf) te("mpc", l[1]);
// collapse first-child and last-child into nth-child expressions // collapse first-child and last-child into nth-child expressions
if (l[2] === ":first-child") { if (l[2] === ":first-child") {
s.pf = ":nth-child"; s.pf = ":nth-child";
Expand All @@ -344,33 +344,33 @@
// any amount of whitespace, followed by paren, string, paren // any amount of whitespace, followed by paren, string, paren
l = lex(str, (off = l[0])); l = lex(str, (off = l[0]));
if (l && l[1] === " ") l = lex(str, off = l[0]); if (l && l[1] === " ") l = lex(str, off = l[0]);
if (!l || l[1] !== "(") te("pex"); if (!l || l[1] !== "(") te("pex", str);
l = lex(str, (off = l[0])); l = lex(str, (off = l[0]));
if (l && l[1] === " ") l = lex(str, off = l[0]); if (l && l[1] === " ") l = lex(str, off = l[0]);
if (!l || l[1] !== toks.str) te("sex"); if (!l || l[1] !== toks.str) te("sex", str);
s.expr[2] = l[2]; s.expr[2] = l[2];
l = lex(str, (off = l[0])); l = lex(str, (off = l[0]));
if (l && l[1] === " ") l = lex(str, off = l[0]); if (l && l[1] === " ") l = lex(str, off = l[0]);
if (!l || l[1] !== ")") te("epex"); if (!l || l[1] !== ")") te("epex", str);
} else if (l[2] === ":has") { } else if (l[2] === ":has") {
// any amount of whitespace, followed by paren // any amount of whitespace, followed by paren
l = lex(str, (off = l[0])); l = lex(str, (off = l[0]));
if (l && l[1] === " ") l = lex(str, off = l[0]); if (l && l[1] === " ") l = lex(str, off = l[0]);
if (!l || l[1] !== "(") te("pex"); if (!l || l[1] !== "(") te("pex", str);
var h = parse(str, l[0], true); var h = parse(str, l[0], true);
l[0] = h[0]; l[0] = h[0];
if (!s.has) s.has = []; if (!s.has) s.has = [];
s.has.push(h[1]); s.has.push(h[1]);
} else if (l[2] === ":expr") { } else if (l[2] === ":expr") {
if (s.expr) te("mexp"); if (s.expr) te("mexp", str);
var e = exprParse(str, l[0]); var e = exprParse(str, l[0]);
l[0] = e[0]; l[0] = e[0];
s.expr = e[1]; s.expr = e[1];
} else { } else {
if (s.pc || s.pf ) te("mpc"); if (s.pc || s.pf ) te("mpc", str);
s.pf = l[2]; s.pf = l[2];
var m = nthPat.exec(str.substr(l[0])); var m = nthPat.exec(str.substr(l[0]));
if (!m) te("mepf"); if (!m) te("mepf", str);
if (m[5]) { if (m[5]) {
s.a = 2; s.a = 2;
s.b = (m[5] === "odd") ? 1 : 0; s.b = (m[5] === "odd") ? 1 : 0;
Expand All @@ -390,7 +390,7 @@
} }


// now if we didn't actually parse anything it's an error // now if we didn't actually parse anything it's an error
if (soff === off) te("se"); if (soff === off) te("se", str);


return [off, s]; return [off, s];
} }
Expand Down

0 comments on commit b4127a8

Please sign in to comment.