From 559518acc877d9617f35bb2c9fde19c8d1cf9a1f Mon Sep 17 00:00:00 2001 From: Paul Harper Date: Fri, 15 Mar 2013 23:02:44 -0600 Subject: [PATCH] .parse_dammit() now recognizes the locations option, and will add a loc object to the ast nodes as .parse() does --- acorn_loose.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/acorn_loose.js b/acorn_loose.js index 16742f8d0b..4c97323a74 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -42,6 +42,7 @@ options = opts; if (!opts.tabSize) opts.tabSize = 4; fetchToken = acorn.tokenize(inpt, opts); + sourceFile = options.sourceFile || null; context = []; nextLineStart = 0; ahead.length = 0; @@ -50,14 +51,18 @@ }; var lastEnd, token = {start: 0, end: 0}, ahead = []; - var curLineStart, nextLineStart, curIndent; + var curLineStart, nextLineStart, curIndent, lastEndLoc, sourceFile; function next() { lastEnd = token.end; + if (options.locations) + lastEndLoc = token.endLoc; + if (ahead.length) token = ahead.shift(); else token = readToken(); + if (token.start >= nextLineStart) { while (token.start >= nextLineStart) { curLineStart = nextLineStart; @@ -181,8 +186,17 @@ this.end = null; } + function node_loc_t() { + this.start = token.startLoc; + this.end = null; + if (sourceFile !== null) this.source = sourceFile; + } + function startNode() { - return new node_t(token.start); + var node = new node_t(token.start); + if (options.locations) + node.loc = new node_loc_t(); + return node } function startNodeFrom(other) { return new node_t(other.start); @@ -190,6 +204,8 @@ function finishNode(node, type) { node.type = type; node.end = lastEnd; + if (options.locations) + node.loc.end = lastEndLoc; return node; }