From e95c4cce50cfcb63813eb0ef1923cdf1ed827ea2 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 28 Jan 2013 13:00:08 +0100 Subject: [PATCH] Add walk.findNodeAt utility --- acorn_loose.js | 1 - index.html | 2 +- util/walk.js | 25 +++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/acorn_loose.js b/acorn_loose.js index 771b20508f..f781bfaf2f 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -65,7 +65,6 @@ // Try to skip some text, based on the error message, and then continue var msg = e.message, pos = e.raisedAt, replace = true; - console.log(msg); if (/unterminated/i.test(msg)) { pos = lineEnd(e.pos); if (/string/.test(msg)) { diff --git a/index.html b/index.html index d2a46e16c1..0a3e94590d 100644 --- a/index.html +++ b/index.html @@ -312,7 +312,7 @@ the next one's tokStart will point at the right position.

ch = input.charCodeAt(tokPos); } if (options.onComment) - options.onComment(false, input.slice(start + 2, tokPos - 1), start, tokPos, + options.onComment(false, input.slice(start + 2, tokPos), start, tokPos, startLoc, options.locations && curLineLoc()); }

Called at the start of the parse and after every token. Skips whitespace and comments, and.

  function skipSpace() {
diff --git a/util/walk.js b/util/walk.js
index c47132f705..28659e41c5 100644
--- a/util/walk.js
+++ b/util/walk.js
@@ -41,6 +41,31 @@
     c(node, state);
   };
 
+  function Found(node, state) { this.node = node; this.state = state; }
+
+  // Find a node with a given start, end, and type (all are optional,
+  // null can be used as wildcard). Returns a {node, state} object, or
+  // undefined when it doesn't find a matching node.
+  exports.findNodeAt = function(node, start, end, targetType, base, state) {
+    try {
+      if (!base) base = exports;
+      var c = function(node, st, override) {
+        var type = override || node.type;
+        if ((start == null || node.start <= start) &&
+            (end == null || node.end >= end))
+          base[type](node, st, c);
+        if ((targetType == null || type == targetType) &&
+            (start == null || node.start == start) &&
+            (end == null || node.end == end))
+          throw new Found(node, st);
+      };
+      c(node, state);
+    } catch (e) {
+      if (e instanceof Found) return e;
+      throw e;
+    }
+  };
+
   // Used to create a custom walker. Will fill in all missing node
   // type properties with the defaults.
   exports.make = function(funcs, base) {