Adding 'putback', ' special case where the end of a match parameter, outside a specified regexp, shouldn't be consumed.

This commit is contained in:
Elf M. Sternberg 2012-05-04 13:36:33 -07:00
parent b43f34afa8
commit 18811ef276
2 changed files with 11 additions and 8 deletions

View File

@ -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];
}
};

View File

@ -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.