diff --git a/acorn.js b/acorn.js index 1c7ec8f662..bf16e18dbb 100644 --- a/acorn.js +++ b/acorn.js @@ -887,15 +887,15 @@ // `commentsBefore` property to it. var node_t = function(s) { - this.type=null; - this.start=tokStart; - this.end=null; + this.type = null; + this.start = tokStart; + this.end = null; }; var node_loc_t = function(s) { - this.start=tokStartLoc; - this.end=null; - if (sourceFile !== null) this.source=sourceFile; + this.start = tokStartLoc; + this.end = null; + if (sourceFile !== null) this.source = sourceFile; }; function startNode() { diff --git a/index.html b/index.html index 55b77fd519..fb8466b278 100644 --- a/index.html +++ b/index.html @@ -221,13 +221,18 @@ the current line number and start of line offset, in order to set return match ? match.index + match[0].length : input.length + 1; } + var line_loc_t = function() { + this.line = tokCurLine; + this.column = tokPos - tokLineStart; + } + function curLineLoc() { while (tokLineStartNext <= tokPos) { ++tokCurLine; tokLineStart = tokLineStartNext; tokLineStartNext = nextLineStart(); } - return {line: tokCurLine, column: tokPos - tokLineStart}; + return new line_loc_t(); }
Reset the token state. Used at the start of a parse.
function initTokenState() {
tokCurLine = 1;
tokPos = tokLineStart = 0;
@@ -496,15 +501,17 @@ will return null unless the integer has exactly len di
else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
else val = parseInt(str, 8);
return finishToken(_num, val);
- }Read a string value, interpreting backslash-escapes.
function readString(quote) {
+ }Read a string value, interpreting backslash-escapes.
var rs_str = [];
+
+ function readString(quote) {
tokPos++;
- var str = [];
+ rs_str.length = 0;
for (;;) {
if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
var ch = input.charCodeAt(tokPos);
if (ch === quote) {
++tokPos;
- return finishToken(_string, String.fromCharCode.apply(null, str));
+ return finishToken(_string, String.fromCharCode.apply(null, rs_str));
}
if (ch === 92) { // '\'
ch = input.charCodeAt(++tokPos);
@@ -515,28 +522,28 @@ will return null unless the integer has exactly len di
++tokPos;
if (octal) {
if (strict) raise(tokPos - 2, "Octal literal in strict mode");
- str.push(parseInt(octal, 8));
+ rs_str.push(parseInt(octal, 8));
tokPos += octal.length - 1;
} else {
switch (ch) {
- case 110: str.push(10); break; // 'n' -> '\n'
- case 114: str.push(13); break; // 'r' -> '\r'
- case 120: str.push(readHexChar(2)); break; // 'x'
- case 117: str.push(readHexChar(4)); break; // 'u'
- case 85: str.push(readHexChar(8)); break; // 'U'
- case 116: str.push(9); break; // 't' -> '\t'
- case 98: str.push(8); break; // 'b' -> '\b'
- case 118: str.push(11); break; // 'v' -> '\u000b'
- case 102: str.push(12); break; // 'f' -> '\f'
- case 48: str.push(0); break; // 0 -> '\0'
+ case 110: rs_str.push(10); break; // 'n' -> '\n'
+ case 114: rs_str.push(13); break; // 'r' -> '\r'
+ case 120: rs_str.push(readHexChar(2)); break; // 'x'
+ case 117: rs_str.push(readHexChar(4)); break; // 'u'
+ case 85: rs_str.push(readHexChar(8)); break; // 'U'
+ case 116: rs_str.push(9); break; // 't' -> '\t'
+ case 98: rs_str.push(8); break; // 'b' -> '\b'
+ case 118: rs_str.push(11); break; // 'v' -> '\u000b'
+ case 102: rs_str.push(12); break; // 'f' -> '\f'
+ case 48: rs_str.push(0); break; // 0 -> '\0'
case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
case 10: break; // ' \n'
- default: str.push(ch); break;
+ default: rs_str.push(ch); break;
}
}
} else {
if (ch === 13 || ch === 10 || ch === 8232 || ch === 8329) raise(tokStart, "Unterminated string constant");
- if (ch !== 92) str.push(ch); // '\'
+ if (ch !== 92) rs_str.push(ch); // '\'
++tokPos;
}
}
@@ -615,14 +622,26 @@ tests ("use strict"; 010; -- should fail). Start an AST node, attaching a start offset and optionally a
-commentsBefore property to it.
function startNode() {
- var node = {type: null, start: tokStart, end: null};
+commentsBefore property to it. var node_t = function(s) {
+ this.type = null;
+ this.start = tokStart;
+ this.end = null;
+ };
+
+ var node_loc_t = function(s) {
+ this.start = tokStartLoc;
+ this.end = null;
+ if (sourceFile !== null) this.source = sourceFile;
+ };
+
+ function startNode() {
+ var node = new node_t();
if (options.trackComments && tokCommentsBefore) {
node.commentsBefore = tokCommentsBefore;
tokCommentsBefore = null;
}
if (options.locations)
- node.loc = {start: tokStartLoc, end: null, source: sourceFile};
+ node.loc = new node_loc_t();
if (options.ranges)
node.range = [tokStart, 0];
return node;
@@ -630,13 +649,16 @@ tests ("use strict"; 010; -- should fail). function startNodeFrom(other) {
- var node = {type: null, start: other.start};
+ var node = new node_t();
+ node.start = other.start;
if (other.commentsBefore) {
node.commentsBefore = other.commentsBefore;
other.commentsBefore = null;
}
- if (options.locations)
- node.loc = {start: other.loc.start, end: null, source: other.loc.source};
+ if (options.locations) {
+ node.loc = new node_loc_t();
+ node.loc.start = other.loc.start;
+ }
if (options.ranges)
node.range = [other.range[0], 0];
diff --git a/test/tests.js b/test/tests.js
index 247decceab..0c97a6ffc6 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -26068,90 +26068,90 @@ test("foo: 10; foo: 20;", {
// option tests
test("var a = 1;", {
- "type": "Program",
- "start": 0,
- "end": 10,
- "loc": {
- "start": {
- "line": 1,
- "column": 0
+ type: "Program",
+ start: 0,
+ end: 10,
+ loc: {
+ start: {
+ line: 1,
+ column: 0
},
- "end": {
- "line": 1,
- "column": 10
+ end: {
+ line: 1,
+ column: 10
},
- "source": "test.js"
+ source: "test.js"
},
- "body": [
+ body: [
{
- "type": "VariableDeclaration",
- "start": 0,
- "end": 9,
- "loc": {
- "start": {
- "line": 1,
- "column": 0
+ type: "VariableDeclaration",
+ start: 0,
+ end: 9,
+ loc: {
+ start: {
+ line: 1,
+ column: 0
},
- "end": {
- "line": 1,
- "column": 9
+ end: {
+ line: 1,
+ column: 9
},
- "source": "test.js"
+ source: "test.js"
},
- "declarations": [
+ declarations: [
{
- "type": "VariableDeclarator",
- "start": 4,
- "end": 9,
- "loc": {
- "start": {
- "line": 1,
- "column": 4
+ type: "VariableDeclarator",
+ start: 4,
+ end: 9,
+ loc: {
+ start: {
+ line: 1,
+ column: 4
},
- "end": {
- "line": 1,
- "column": 9
+ end: {
+ line: 1,
+ column: 9
},
- "source": "test.js"
+ source: "test.js"
},
- "id": {
- "type": "Identifier",
- "start": 4,
- "end": 5,
- "loc": {
- "start": {
- "line": 1,
- "column": 4
+ id: {
+ type: "Identifier",
+ start: 4,
+ end: 5,
+ loc: {
+ start: {
+ line: 1,
+ column: 4
},
- "end": {
- "line": 1,
- "column": 5
+ end: {
+ line: 1,
+ column: 5
},
- "source": "test.js"
+ source: "test.js"
},
- "name": "a"
+ name: "a"
},
- "init": {
- "type": "Literal",
- "start": 8,
- "end": 9,
- "loc": {
- "start": {
- "line": 1,
- "column": 8
+ init: {
+ type: "Literal",
+ start: 8,
+ end: 9,
+ loc: {
+ start: {
+ line: 1,
+ column: 8
},
- "end": {
- "line": 1,
- "column": 9
+ end: {
+ line: 1,
+ column: 9
},
- "source": "test.js"
+ source: "test.js"
},
- "value": 1,
- "raw": "1"
+ value: 1,
+ raw: "1"
}
}
],
- "kind": "var"
+ kind: "var"
}
]
}, {