Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow CharSet inversion.

  • Loading branch information...
commit 6b93d6b2be4ae5a492b960c4cf79eda1962d18ea 1 parent 8c53e4a
@cdleary authored
Showing with 17 additions and 8 deletions.
  1. +16 −8 lib/matcher.js
  2. +1 −0  test/cdlre_test.js
View
24 lib/matcher.js
@@ -15,7 +15,6 @@ var cdlre = (function(cdlre) {
Assertion = cdlre.parser.Assertion,
ClassRanges = cdlre.parser.ClassRanges,
Set = cdlre.Set,
- SetUnion = cdlre.SetUnion,
assert = cdlre.assert;
// Matcher(State, Continuation) -> MatchResult
@@ -284,7 +283,7 @@ var cdlre = (function(cdlre) {
case 'Digit':
return this.CharSet(stringRange(10));
case 'NotDigit':
- throw new Error("NYI");
+ return this.CharSet(stringRange(10), {inverted: true});
case 'Space':
return this.CharSet([
// whitespace characters
@@ -573,8 +572,10 @@ var cdlre = (function(cdlre) {
};
};
- ProcedureBuilder.prototype._CharSet = function _CharSet(pb, items) {
+ ProcedureBuilder.prototype._CharSet = function _CharSet(pb, items, options) {
var self = this;
+
+ self._inverted = options.inverted || false;
self._set = new Set(items);
// Check it has characters in it.
@@ -583,25 +584,32 @@ var cdlre = (function(cdlre) {
});
self.hasCanonicalized = function CharSet__hasCanonicalized(tcc) {
- return self._set.hasWithMap(tcc, pb.canonicalize.bind(pb));
+ var result = self._set.hasWithMap(tcc, pb.canonicalize.bind(pb));
+ return self._inverted ? !result : result;
};
self.length = function CharSet__length() {
+ if (self._inverted)
+ throw new Error("Inverted sets have no length");
return self._set.length();
};
self.getOnly = function CharSet__getOnly() {
+ if (self._inverted)
+ throw new Error("Inverted sets do not have a solitary member");
return self._set.getOnly();
};
};
- ProcedureBuilder.prototype.CharSet = function CharSet(items) {
+ ProcedureBuilder.prototype.CharSet = function CharSet(items, options) {
assert(!(this instanceof CharSet), 'Do not use CharSet as a constructor');
- assert(arguments.length === 1, 'Pass CharSet a single items argument');
+ assert(arguments.length <= 2,
+ 'Pass CharSet a single items argument and an optional options argument');
+ var options = options || {};
var pb = this;
- return new this._CharSet(pb, items);
+ return new this._CharSet(pb, items, options);
};
ProcedureBuilder.prototype.CharSetUnion = function(cs1, cs2) {
- var su = SetUnion(cs1, cs2);
+ var su = cdlre.SetUnion(cs1, cs2);
su.hasCanonicalized = function(tcc) {
return cs1.hasCanonicalized(tcc) || cs2.hasCanonicalized(tcc);
};
View
1  test/cdlre_test.js
@@ -447,6 +447,7 @@ function testCDLRE(resultCallback) {
[/(?:first (\d) |second (\d) |third (\d) ){3}/, "first 1 second 2 third 3 "], /* Mozilla bug 692441 */
[/\w+\s+\w+/, "a_asdf \tasdkflj"],
+ [/\D+/, "asdf1234"],
/* FIXME: also permit an object literal that has an expected value. */
];
Please sign in to comment.
Something went wrong with that request. Please try again.