diff --git a/acorn.js b/acorn.js index 6046858e55..e9374e008b 100644 --- a/acorn.js +++ b/acorn.js @@ -164,15 +164,17 @@ var inFunction, labels, strict; // This function is used to raise exceptions on parse errors. It - // takes either a `{line, column}` object or an offset integer (into - // the current `input`) as `pos` argument. It attaches the position - // to the end of the error message, and then raises a `SyntaxError` - // with that message. + // takes an offset integer (into the current `input`) to indicate + // the location of the error, attaches the position to the end + // of the error message, and then raises a `SyntaxError` with that + // message. function raise(pos, message) { - if (typeof pos == "number") pos = getLineInfo(input, pos); - message += " (" + pos.line + ":" + pos.column + ")"; - throw new SyntaxError(message); + var loc = getLineInfo(input, pos); + message += " (" + loc.line + ":" + loc.column + ")"; + var err = new SyntaxError(message); + err.pos = pos; err.loc = loc; + throw err; } // ## Token types @@ -626,7 +628,7 @@ var tok = getTokenFromCode(code); - if(tok === false) { + if (tok === false) { // If we are here, we either found a non-ASCII identifier // character, or something that's entirely disallowed. var ch = String.fromCharCode(code); diff --git a/index.html b/index.html index 69d3e1c7c4..af26d2a798 100644 --- a/index.html +++ b/index.html @@ -91,13 +91,15 @@ when finishing a node and assigning its end position.

return statements outside of functions, labels to verify that break and continue have somewhere to jump to, and strict indicates whether strict mode is on.

  var inFunction, labels, strict;

This function is used to raise exceptions on parse errors. It -takes either a {line, column} object or an offset integer (into -the current input) as pos argument. It attaches the position -to the end of the error message, and then raises a SyntaxError -with that message.

  function raise(pos, message) {
-    if (typeof pos == "number") pos = getLineInfo(input, pos);
-    message += " (" + pos.line + ":" + pos.column + ")";
-    throw new SyntaxError(message);
+takes an offset integer (into the current input) to indicate
+the location of the error, attaches the position to the end
+of the error message, and then raises a SyntaxError with that
+message.

  function raise(pos, message) {
+    var loc = getLineInfo(input, pos);
+    message += " (" + loc.line + ":" + loc.column + ")";
+    var err = new SyntaxError(message);
+    err.pos = pos; err.loc = loc;
+    throw err;
   }

Token types

The assignment of fine-grained, information-carrying type objects allows the tokenizer to store the information it has about a token in a way that is very cheap for the parser to look up.

All token type variables start with an underscore, to make them @@ -420,7 +422,7 @@ identifiers, so '\' also dispatches to that.

< var tok = getTokenFromCode(code); - if(tok === false) {

If we are here, we either found a non-ASCII identifier + if (tok === false) {

If we are here, we either found a non-ASCII identifier character, or something that's entirely disallowed.

      var ch = String.fromCharCode(code);
       if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
       raise(tokPos, "Unexpected character '" + ch + "'");