From 0b10aa225629c27e070497e84b81ad1fe253bd0f Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 28 Feb 2013 19:27:29 +0100 Subject: [PATCH] Add walk.findNodeBefore --- index.html | 3 ++- util/walk.js | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index fe54262cfa..e005f759d4 100644 --- a/index.html +++ b/index.html @@ -464,7 +464,8 @@ of the type given by its first argument.

} function readToken(forceRegexp) { - tokStart = tokPos; + if (!forceRegexp) tokStart = tokPos; + else tokPos = tokStart + 1; if (options.locations) tokStartLoc = new line_loc_t; if (forceRegexp) return readRegexp(); if (tokPos >= inputLen) return finishToken(_eof); diff --git a/util/walk.js b/util/walk.js index d6f207440b..afed78c0f3 100644 --- a/util/walk.js +++ b/util/walk.js @@ -103,7 +103,7 @@ var c = function(node, st, override) { if (node.end < pos) return; var type = override || node.type; - if (node.start >= pos && test(node, type)) throw new Found(node, st); + if (node.start >= pos && test(type, node)) throw new Found(node, st); base[type](node, st, c); }; c(node, state); @@ -113,6 +113,22 @@ } }; + // Find the outermost matching node before a given position. + exports.findNodeBefore = function(node, pos, test, base, state) { + test = makeTest(test); + if (!base) base = exports.base; + var max; + var c = function(node, st, override) { + if (node.start > pos) return; + var type = override || node.type; + if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) + max = new Found(node, st); + base[type](node, st, c); + }; + c(node, state); + return max; + }; + // Used to create a custom walker. Will fill in all missing node // type properties with the defaults. exports.make = function(funcs, base) {