From 18811ef276b6147c9a73d361e5b00bb9855827fa Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Fri, 4 May 2012 13:36:33 -0700 Subject: [PATCH] Adding 'putback', ' special case where the end of a match parameter, outside a specified regexp, shouldn't be consumed. --- lib/reparse.js | 13 ++++++++----- src/reparse.coffee | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/reparse.js b/lib/reparse.js index 55e8f27..1d9a2d7 100644 --- a/lib/reparse.js +++ b/lib/reparse.js @@ -101,17 +101,20 @@ throw new Error("Could not parse '" + this.input + "'."); }; - ReParse.prototype.match = function(pattern) { + ReParse.prototype.match = function(pattern, putback) { var probe; + if (putback == null) { + putback = false; + } probe = this.input.match(pattern); if (!probe) { return this.fail(); } - this.input = this.input.substr(probe[0].length); - if (probe[1] === undefined) { - return probe[0]; - } else { + this.input = this.input.substr(((probe[1] != null) && putback ? probe[1].length : probe[0].length)); + if (probe[1] != null) { return probe[1]; + } else { + return probe[0]; } }; diff --git a/src/reparse.coffee b/src/reparse.coffee index 2ac84ae..59c524e 100644 --- a/src/reparse.coffee +++ b/src/reparse.coffee @@ -75,11 +75,11 @@ exports.ReParse = class ReParse # # Note that the `return fail()` call eventually leads to a throw. - match: (pattern) => + match: (pattern, putback = false) => probe = @input.match pattern return @fail() unless probe - @input = @input.substr probe[0].length - if probe[1] is `undefined` then probe[0] else probe[1] + @input = @input.substr (if probe[1]? and putback then probe[1].length else probe[0].length) + if probe[1]? then probe[1] else probe[0] # Attempts to apply the method and produce a value. If it fails, # restores the input to the previous state.