New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

clarity-pattern-parser

Package Overview
Dependencies
Maintainers
1
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clarity-pattern-parser - npm Package Compare versions

Comparing version 2.0.10 to 2.0.11

lib/tests/CompositeNode.js

6

lib/Cursor.js

@@ -25,2 +25,3 @@ "use strict";

this.string = string;
this.assertValidity();
this.index = 0;

@@ -30,3 +31,2 @@ this.length = string.length;

this.isInErrorState = false;
this.assertValidity();
}

@@ -121,4 +121,4 @@

}, {
key: "moveToLast",
value: function moveToLast() {
key: "moveToEnd",
value: function moveToEnd() {
this.index = this.string.length - 1;

@@ -125,0 +125,0 @@ }

@@ -39,6 +39,2 @@ "use strict";

if (astNode == null) {
debugger;
}
if (this.furthestMatch.astNode == null || astNode.endIndex >= this.furthestMatch.astNode.endIndex) {

@@ -69,2 +65,3 @@ this.furthestMatch.pattern = pattern;

this.isRecording = false;
this.clear();
}

@@ -74,4 +71,5 @@ }, {

value: function clear() {
this.matches = [];
this.errors = [];
this.patterns.length = 0;
this.astNodes.length = 0;
this.errors.length = 0;
}

@@ -91,6 +89,10 @@ }, {

value: function getLastMatch() {
return {
pattern: this.patterns[this.patterns.length - 1] || null,
astNode: this.astNodes[this.astNodes.length - 1] || null
};
if (this.isRecording) {
return {
pattern: this.patterns[this.patterns.length - 1] || null,
astNode: this.astNodes[this.astNodes.length - 1] || null
};
} else {
return this.furthestMatch;
}
}

@@ -97,0 +99,0 @@ }, {

@@ -178,4 +178,4 @@ "use strict";

var pattern = this.patternMatch.pattern || this.rootPattern;
var parentPattern = pattern.parent || pattern;
var pattern = this.patternMatch.pattern;
var parentPattern = pattern.parent;
var index = parentPattern.children.indexOf(pattern);

@@ -186,3 +186,2 @@ var parentClone = parentPattern.clone();

var startIndex = this.matchedText.length;
startIndex = startIndex >= 0 ? startIndex : 0;

@@ -222,3 +221,3 @@ if (this.matchedText.length < this.text.length) {

value: function inspectParse(text, pattern) {
return new ResultGenerator().inspectParse(text, pattern);
return new ParseInspector().inspectParse(text, pattern);
}

@@ -225,0 +224,0 @@ }]);

@@ -49,5 +49,7 @@ "use strict";

function AndComposite(name, patterns) {
function AndComposite(name) {
var _this;
var patterns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
_classCallCheck(this, AndComposite);

@@ -190,2 +192,20 @@

}
}, {
key: "getTokens",
value: function getTokens() {
var tokens = [];
for (var x = 0; x < this._children.length; x++) {
var child = this._children[x];
if (child instanceof _OptionalValue.default || child instanceof _OptionalComposite.default) {
tokens = tokens.concat(child.getTokens());
} else {
tokens = tokens.concat(child.getTokens());
break;
}
}
return tokens;
}
}]);

@@ -192,0 +212,0 @@

@@ -44,17 +44,2 @@ "use strict";

_createClass(CompositePattern, [{
key: "_assertChildren",
value: function _assertChildren() {
if (!Array.isArray(this._children)) {
throw new Error("Invalid Arguments: The patterns argument need to be an array of Patterns.");
}
var areAllPatterns = this._children.every(function (pattern) {
return pattern instanceof _Pattern2.default;
});
if (!areAllPatterns) {
throw new Error("Invalid Argument: All patterns need to be an instance of Pattern.");
}
}
}, {
key: "clone",

@@ -61,0 +46,0 @@ value: function clone() {

@@ -38,21 +38,8 @@ "use strict";

function OptionalComposite(pattern) {
var _this;
_classCallCheck(this, OptionalComposite);
_this = _possibleConstructorReturn(this, _getPrototypeOf(OptionalComposite).call(this, "optional-composite", "optional-composite", [pattern]));
_this._assertArguments();
return _this;
return _possibleConstructorReturn(this, _getPrototypeOf(OptionalComposite).call(this, "optional-composite", "optional-composite", [pattern]));
}
_createClass(OptionalComposite, [{
key: "_assertArguments",
value: function _assertArguments() {
if (!(this.children[0] instanceof _Pattern.default)) {
throw new Error("Invalid Arguments: Expected a Pattern.");
}
}
}, {
key: "parse",

@@ -94,2 +81,7 @@ value: function parse(cursor) {

}
}, {
key: "getTokens",
value: function getTokens() {
return this._children[0].getTokens();
}
}]);

@@ -96,0 +88,0 @@

@@ -71,11 +71,7 @@ "use strict";

value: function _reset(cursor) {
this.cursor = null;
this.cursor = cursor;
this.mark = null;
this.index = 0;
this.node = null;
if (cursor != null) {
this.cursor = cursor;
this.mark = cursor.mark();
}
this.mark = cursor.mark();
}

@@ -139,2 +135,23 @@ }, {

}
}, {
key: "getTokens",
value: function getTokens() {
var tokens = this._children.map(function (c) {
return c.getTokens();
});
var hasPrimitiveTokens = tokens.every(function (t) {
return t.every(function (value) {
return typeof value === "string";
});
});
if (hasPrimitiveTokens && tokens.length > 0) {
return tokens.reduce(function (acc, t) {
return acc.concat(t);
}, []);
}
return this._children[0].getTokens();
}
}]);

@@ -141,0 +158,0 @@

@@ -165,2 +165,7 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
return this._pattern.getTokens();
}
}]);

@@ -167,0 +172,0 @@

@@ -23,3 +23,3 @@ "use strict";

var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var name = arguments.length > 1 ? arguments[1] : undefined;
var children = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

@@ -31,3 +31,5 @@

this._name = name;
this._children = [];
this._parent = null;
this.isSequence = false;

@@ -108,2 +110,72 @@ this._assertName();

}, {
key: "getTokens",
value: function getTokens() {
throw new Error("Method Not Implemented");
}
}, {
key: "getNextTokens",
value: function getNextTokens() {
var _this3 = this;
if (this._parent != null) {
var siblings = this._parent.children;
var index = siblings.findIndex(function (c) {
return c === _this3;
});
var nextSibling = siblings[index + 1]; // I don't like this, so I think we need to rethink this.
if (this._parent.type.indexOf("repeat") === 0) {
var tokens = this._parent.getNextTokens();
if (index === 0 && siblings.length > 1) {
return nextSibling.getTokens().concat(tokens);
} else if (index === 1) {
return siblings[0].getTokens().concat(tokens);
} else {
return this.getTokens().concat(tokens);
}
} // Another thing I don't like.
if (this._parent.type.indexOf("and") === 0 && nextSibling != null && nextSibling.type.indexOf("optional") === 0) {
var _tokens = [];
for (var x = index + 1; x < siblings.length; x++) {
var child = siblings[x];
if (child.type.indexOf("optional") === 0) {
_tokens = _tokens.concat(child.getTokens());
} else {
_tokens = _tokens.concat(child.getTokens());
break;
}
if (x === siblings.length - 1) {
_tokens = _tokens.concat(this._parent.getNextTokens());
}
}
return _tokens;
} // If you are an or you have already qualified.
if (this._parent.type.indexOf("or") === 0) {
return this._parent.getNextTokens();
}
if (nextSibling != null) {
return nextSibling.getTokens();
} else {
return this._parent.getNextTokens();
}
}
return [];
}
}, {
key: "getTokenValue",
value: function getTokenValue() {
return null;
}
}, {
key: "name",

@@ -110,0 +182,0 @@ get: function get() {

@@ -43,3 +43,3 @@ "use strict";

_this = _possibleConstructorReturn(this, _getPrototypeOf(RecursivePattern).call(this, "recursive", name));
_this.isGettingPossibilities = false;
_this.isRecursing = false;
return _this;

@@ -54,2 +54,6 @@ }

return this._climb(this.parent, function (pattern) {
if (pattern == null) {
return false;
}
return pattern.name === _this2.name;

@@ -64,3 +68,3 @@ });

} else {
if (pattern.parent != null) {
if (pattern && pattern.parent != null) {
return this._climb(pattern.parent, isMatch);

@@ -107,6 +111,6 @@ }

value: function getPossibilities() {
if (!this.isGettingPossibilities) {
this.isGettingPossibilities = true;
if (!this.isRecursing) {
this.isRecursing = true;
var possibilities = this.getPattern().getPossibilities();
this.isGettingPossibilities = false;
this.isRecursing = false;
return possibilities;

@@ -117,2 +121,17 @@ } else {

}
}, {
key: "getTokenValue",
value: function getTokenValue() {
return this.getPattern().getTokenValue();
}
}, {
key: "getTokens",
value: function getTokens() {
if (!this.isRecursing) {
this.isRecursing = true;
var tokens = this.getPattern().getTokens();
this.isRecursing = false;
return tokens;
}
}
}]);

@@ -119,0 +138,0 @@

@@ -188,2 +188,20 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
var tokens = [];
for (var x = 0; x < this._children.length; x++) {
var child = this._children[x];
if (child instanceof _OptionalValue.default) {
tokens = tokens.concat(child.getTokens());
} else {
tokens = tokens.concat(child.getTokens());
break;
}
}
return tokens;
}
}]);

@@ -190,0 +208,0 @@

@@ -121,2 +121,7 @@ "use strict";

return this.getTokens();
}
}, {
key: "getTokens",
value: function getTokens() {
return this.characters.split("");

@@ -123,0 +128,0 @@ }

@@ -115,4 +115,14 @@ "use strict";

value: function getPossibilities() {
return [this.literal];
return [this.getTokenValue()];
}
}, {
key: "getTokenValue",
value: function getTokenValue() {
return this.literal;
}
}, {
key: "getTokens",
value: function getTokens() {
return [this.getTokenValue()];
}
}]);

@@ -119,0 +129,0 @@

@@ -121,2 +121,7 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
return [];
}
}]);

@@ -123,0 +128,0 @@

@@ -92,2 +92,7 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
return this._children[0].getTokens();
}
}]);

@@ -94,0 +99,0 @@

@@ -132,2 +132,23 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
var tokens = this._children.map(function (c) {
return c.getTokens();
});
var hasPrimitiveTokens = tokens.every(function (t) {
return t.every(function (value) {
return typeof value === "string";
});
});
if (hasPrimitiveTokens && tokens.length > 0) {
return tokens.reduce(function (acc, t) {
return acc.concat(t);
}, []);
}
return this._children[0].getTokens();
}
}]);

@@ -134,0 +155,0 @@

@@ -125,2 +125,12 @@ "use strict";

value: function getPossibilities() {
return [this.getTokenValue()];
}
}, {
key: "getTokenValue",
value: function getTokenValue() {
return this.name;
}
}, {
key: "getTokens",
value: function getTokens() {
return [this.name];

@@ -127,0 +137,0 @@ }

@@ -168,2 +168,7 @@ "use strict";

}
}, {
key: "getTokens",
value: function getTokens() {
return this._pattern.getTokens();
}
}]);

@@ -170,0 +175,0 @@

@@ -7,2 +7,4 @@ "use strict";

var _OptionalValue = _interopRequireDefault(require("../patterns/value/OptionalValue.js"));
var _assert = _interopRequireDefault(require("assert"));

@@ -47,2 +49,70 @@

};
exports["AndComposite: test."] = function () {
var john = new _Literal.default("john", "John");
var doe = new _Literal.default("doe", "Doe");
var name = new _AndComposite.default("name", [john, doe]);
var isMatch = name.test("JohnDoe");
_assert.default.equal(isMatch, true);
};
exports["AndComposite: no children."] = function () {
_assert.default.throws(function () {
new _AndComposite.default("name");
});
};
exports["AndComposite: string runs out before match is done."] = function () {
var first = new _Literal.default("first", "John");
var last = new _Literal.default("last", "Doe");
var name = new _AndComposite.default("name", [first, last]);
var cursor = new _index.Cursor("JohnDo");
var result = name.parse(cursor);
};
exports["AndComposite: last name is optional."] = function () {
var first = new _Literal.default("first", "John");
var last = new _OptionalValue.default(new _Literal.default("last", "Boe"));
var name = new _AndComposite.default("name", [first, last]);
var cursor = new _index.Cursor("JohnDo");
var result = name.parse(cursor);
_assert.default.equal(result.name, "name");
_assert.default.equal(result.type, "and-composite");
_assert.default.equal(result.children[0].value, "John");
_assert.default.equal(result.children[0].name, "first");
_assert.default.equal(result.children[0].type, "literal");
};
exports["AndComposite: three non-optional patterns."] = function () {
var first = new _Literal.default("first", "John");
var middle = new _Literal.default("middle", "Smith");
var last = new _Literal.default("last", "Doe");
var name = new _AndComposite.default("name", [first, middle, last]);
var cursor = new _index.Cursor("JohnDoe");
var result = name.parse(cursor);
};
exports["AndComposite: full name, middle optional, and last name isn't."] = function () {
var first = new _Literal.default("first", "John");
var middle = new _OptionalValue.default(new _Literal.default("middle", "Smith"));
var last = new _Literal.default("last", "Doe");
var name = new _AndComposite.default("name", [first, middle, last]);
var cursor = new _index.Cursor("JohnDoe");
var result = name.parse(cursor);
};
exports["AndComposite: clone."] = function () {
var john = new _Literal.default("john", "John");
var doe = new _Literal.default("doe", "Doe");
var name = new _AndComposite.default("name", [john, doe]);
var clone = name.clone("name2");
_assert.default.equal(clone.name, "name2");
};
//# sourceMappingURL=AndComposite.js.map

@@ -7,6 +7,8 @@ "use strict";

var _assert = _interopRequireDefault(require("assert"));
var _OptionalValue = _interopRequireDefault(require("../patterns/value/OptionalValue.js"));
var _Cursor = _interopRequireDefault(require("../Cursor.js"));
var _assert = _interopRequireDefault(require("assert"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -60,2 +62,48 @@

exports["AndValue: First Part Match with optional Second part."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(lastName)]);
var cursor = new _Cursor.default("John");
var node = fullName.parse(cursor);
_assert.default.equal(node.name, "full-name");
_assert.default.equal(node.value, "John");
_assert.default.equal(node.startIndex, 0);
_assert.default.equal(node.endIndex, 3);
};
exports["AndValue: First Part Match, but run out for second part."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var cursor = new _Cursor.default("John");
var node = fullName.parse(cursor);
_assert.default.equal(node, null);
};
exports["AndValue: No Match"] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var cursor = new _Cursor.default("JaneDoe");
var node = fullName.parse(cursor);
_assert.default.equal(node, null);
};
exports["AndValue: Partial Match without optional siblings."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var cursor = new _Cursor.default("JohnSmith");
var node = fullName.parse(cursor);
_assert.default.equal(node, null);
};
exports["AndValue: Success with more to parse"] = function () {

@@ -101,2 +149,114 @@ var firstName = new _Literal.default("first-name", "John");

};
exports["AndValue: Clone with custom name."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var clone = fullName.clone("full-name-2");
var fullNamePatterns = fullName.children;
var _cloneChildren = clone.children;
_assert.default.notEqual(fullNamePatterns[0], _cloneChildren[0]);
_assert.default.notEqual(fullNamePatterns[1], _cloneChildren[1]);
_assert.default.equal(clone.name, "full-name-2");
};
exports["AndValue: getPossibilities."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var possibilities = fullName.getPossibilities();
_assert.default.equal(possibilities.length, 1);
_assert.default.equal(possibilities[0], "JohnDoe");
};
exports["AndValue: getPossibilities with itself being the root pattern."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var possibilities = fullName.getPossibilities(fullName);
_assert.default.equal(possibilities.length, 1);
_assert.default.equal(possibilities[0], "JohnDoe");
};
exports["AndValue: getPossibilities with a invalid root."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var possibilities = fullName.getPossibilities("");
_assert.default.equal(possibilities.length, 1);
_assert.default.equal(possibilities[0], "JohnDoe");
};
exports["AndValue: Partial Match."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(lastName)]);
var result = fullName.parse(new _Cursor.default("JohnBo"));
_assert.default.equal(result.type, "and-value");
_assert.default.equal(result.name, "full-name");
_assert.default.equal(result.value, "John");
};
exports["AndValue: Partial Match with string running out, and optional last name."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(lastName)]);
var result = fullName.parse(new _Cursor.default("JohnDo"));
_assert.default.equal(result.type, "and-value");
_assert.default.equal(result.name, "full-name");
_assert.default.equal(result.value, "John");
};
exports["AndValue: Three parts first optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middle = new _Literal.default("middle", "Smith");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [new _OptionalValue.default(firstName), middle, lastName]);
var result = fullName.parse(new _Cursor.default("SmithDoe"));
_assert.default.equal(result.value, "SmithDoe");
_assert.default.equal(result.type, "and-value");
_assert.default.equal(result.name, "full-name");
};
exports["AndValue: Three parts middle optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middle = new _Literal.default("middle", "Smith");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(middle), lastName]);
var result = fullName.parse(new _Cursor.default("JohnDo"));
_assert.default.equal(result, null);
};
exports["AndValue: Three parts third optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middle = new _Literal.default("middle", "Smith");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, middle, new _OptionalValue.default(lastName)]);
var result = fullName.parse(new _Cursor.default("JohnSmith"));
_assert.default.equal(result.value, "JohnSmith");
_assert.default.equal(result.type, "and-value");
_assert.default.equal(result.name, "full-name");
};
//# sourceMappingURL=AndValue.js.map

@@ -69,3 +69,3 @@ "use strict";

exports["Css: spaced values"] = function () {
exports["Css: spaced values in a method"] = function () {
var cursor = new _index.Cursor("method(arg1 0%, arg2, 10%) 0%");

@@ -72,0 +72,0 @@

@@ -67,2 +67,12 @@ "use strict";

var result = _cssSelector.default.parse(cursor);
_assert.default.equal(result.value, "*");
_assert.default.equal(result.name, "element-name");
_assert.default.equal(result.type, "regex-value");
_assert.default.equal(result.startIndex, 0);
_assert.default.equal(result.endIndex, 0);
};

@@ -74,5 +84,19 @@

var result = _cssSelector.default.parse(cursor);
_assert.default.equal(result.type, "and-composite");
_assert.default.equal(result.name, "attribute-selector");
_assert.default.equal(result.children[0].name, "element-name");
_assert.default.equal(result.children[0].value, "*");
_assert.default.equal(result.children[1].name, "attribute");
_assert.default.equal(result.startIndex, 0);
_assert.default.equal(result.endIndex, 14);
};
exports["cssSelector: element with attribute"] = function () {
exports["cssSelector: element with attribute and a child selector"] = function () {
var cursor = new _Cursor.default("element-name > *[name='value']");

@@ -83,3 +107,3 @@

exports["cssSelector: element with attribute"] = function () {
exports["cssSelector: element with attribute and two deep child selector"] = function () {
var cursor = new _Cursor.default("element-name > * > *[name='value']");

@@ -86,0 +110,0 @@

@@ -35,8 +35,2 @@ "use strict";

exports["Html: element with attributes"] = function () {
var cursor = new _index.Cursor('<div prop="value"></div>');
var result = _element.default.parse(cursor);
};
exports["Html: multiple children"] = function () {

@@ -43,0 +37,0 @@ var cursor = new _index.Cursor("<div prop=\"value\">\n Text\n <span>Hello</span>\n <span>World!</span>\n </div>");

@@ -11,9 +11,21 @@ "use strict";

_assert.default.throws(function () {
new _Node.default("type", 0, 1).clone();
new _Node.default("type", "name", 0, 1).clone();
});
};
exports["Node: fitler."] = function () {
_assert.default.throws(function () {
new _Node.default("type", "name", 0, 1).filter();
});
};
exports["Node: toString."] = function () {
_assert.default.throws(function () {
new _Node.default("type", "name", 0, 1).toString();
});
};
exports["Node: With no startIndex."] = function () {
_assert.default.throws(function () {
new _Node.default("type", undefined, 1).clone();
new _Node.default("type", "name", undefined, 1);
});

@@ -24,5 +36,9 @@ };

_assert.default.throws(function () {
new _Node.default("type", 0).clone();
new _Node.default("type", "name", 0);
});
};
exports["Node: With all arguments."] = function () {
new _Node.default("type", "name", 0, 0);
};
//# sourceMappingURL=Node.js.map

@@ -7,2 +7,4 @@ "use strict";

var _OptionalValue = _interopRequireDefault(require("../patterns/value/OptionalValue.js"));
var _assert = _interopRequireDefault(require("assert"));

@@ -39,2 +41,59 @@

};
exports["OrComposite: Supplied only one option."] = function () {
var john = new _Literal.default("john", "John");
_assert.default.throws(function () {
new _OrComposite.default("name", [john]);
});
};
exports["OrComposite: Optional Children."] = function () {
var john = new _Literal.default("john", "John");
var jane = new _Literal.default("jane", "Jane");
_assert.default.throws(function () {
new _OrComposite.default("name", [new _OptionalValue.default(john), new _OptionalValue.default(jane)]);
});
};
exports["OrComposite: getPossibilities."] = function () {
var john = new _Literal.default("john", "John");
var jane = new _Literal.default("jane", "Jane");
var name = new _OrComposite.default("name", [john, jane]);
var possibilities = name.getPossibilities();
_assert.default.equal(possibilities.length, 2);
_assert.default.equal(possibilities[0], "John");
_assert.default.equal(possibilities[1], "Jane");
};
exports["OrComposite: parse with null cursor."] = function () {
var john = new _Literal.default("john", "John");
var jane = new _Literal.default("jane", "Jane");
var cursor = new _index.Cursor("John");
var name = new _OrComposite.default("name", [john, jane]);
var node = name.parse(cursor);
_assert.default.equal(node.name, "john");
_assert.default.equal(node.value, "John");
};
exports["OrComposite: clone."] = function () {
var john = new _Literal.default("john", "John");
var jane = new _Literal.default("jane", "Jane");
var name = new _OrComposite.default("name", [john, jane]);
var clone = name.clone("name2");
_assert.default.equal(clone.name, "name2");
_assert.default.equal(clone.children.length, 2);
_assert.default.equal(clone.children[0].name, "john");
_assert.default.equal(clone.children[1].name, "jane");
};
//# sourceMappingURL=OrComposite.js.map

@@ -152,2 +152,20 @@ "use strict";

};
exports["ParseInspector: static inspectParse."] = function () {
var text = "Pat went to a big store";
var inspection = _ParseInspector.default.inspectParse(text, _sentence.default);
_assert.default.equal(inspection.match.text, "Pat went to a big store");
_assert.default.equal(inspection.isComplete, true);
_assert.default.equal(inspection.match.startIndex, 0);
_assert.default.equal(inspection.match.endIndex, 22);
_assert.default.equal(inspection.error, null);
_assert.default.equal(inspection.possibilities, null);
};
//# sourceMappingURL=ParseInspector.js.map

@@ -5,2 +5,12 @@ "use strict";

var _AndValue = _interopRequireDefault(require("../patterns/value/AndValue.js"));
var _OrValue = _interopRequireDefault(require("../patterns/value/OrValue.js"));
var _Literal = _interopRequireDefault(require("../patterns/value/Literal.js"));
var _RepeatValue = _interopRequireDefault(require("../patterns/value/RepeatValue.js"));
var _OptionalValue = _interopRequireDefault(require("../patterns/value/OptionalValue.js"));
var _assert = _interopRequireDefault(require("assert"));

@@ -25,2 +35,368 @@

};
exports["Pattern: getPossibilities."] = function () {
var valuePattern = new _Pattern.default("pattern-type", "pattern");
_assert.default.throws(function () {
valuePattern.getPossibilities();
});
};
exports["Pattern: limited arguments."] = function () {
new _Pattern.default(undefined, "name");
};
exports["Pattern: no arguments."] = function () {
_assert.default.throws(function () {
new _Pattern.default();
});
};
exports["Pattern: set parent."] = function () {
var parent = new _Pattern.default("pattern-type", "pattern");
var child = new _Pattern.default("pattern-type", "pattern");
child.parent = parent;
};
exports["Pattern: set invalid parent."] = function () {
var child = new _Pattern.default("pattern-type", "pattern");
child.parent = "";
_assert.default.equal(child.parent, null);
};
exports["Pattern: getTokens"] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var tokens = firstName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "John");
tokens = lastName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "John");
};
exports["Pattern: getNextToken one deep."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, lastName]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
};
exports["Pattern: getNextToken two deep."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var otherLastName = new _Literal.default("other-last-name", "Smith");
var lastNames = new _OrValue.default("last-names", [lastName, otherLastName]);
var fullName = new _AndValue.default("full-name", [firstName, lastNames]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Doe");
_assert.default.equal(tokens[1], "Smith");
};
exports["Pattern: getNextToken three deep."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var middleName = new _Literal.default("middle-name", "Moses");
var otherMiddleName = new _Literal.default("other-middle-name", "Joshua");
var middleNames = new _OrValue.default("middle-names", [middleName, otherMiddleName]);
var otherLastName = new _Literal.default("other-last-name", "Smith");
var lastNames = new _OrValue.default("last-names", [lastName, otherLastName]);
var fullName = new _AndValue.default("full-name", [firstName, middleNames, lastNames]);
var tokens = fullName.children[1].children[1].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Doe");
_assert.default.equal(tokens[1], "Smith");
};
exports["Pattern: getNextToken end of patterns."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var middleName = new _Literal.default("middle-name", "Moses");
var otherMiddleName = new _Literal.default("other-middle-name", "Joshua");
var middleNames = new _OrValue.default("middle-names", [middleName, otherMiddleName]);
var otherLastName = new _Literal.default("other-last-name", "Smith");
var lastNames = new _OrValue.default("last-names", [lastName, otherLastName]);
var fullName = new _AndValue.default("full-name", [firstName, middleNames, lastNames]);
var tokens = fullName.children[2].children[1].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
exports["Pattern: getNextToken end of patterns."] = function () {
var firstName = new _Literal.default("first-name", "John");
var lastName = new _Literal.default("last-name", "Doe");
var moses = new _Literal.default("moses", "Moses");
var joshua = new _Literal.default("other-middle-name", "Joshua");
var moreLastNames = new _OrValue.default("more-last-names", [moses, joshua]);
var otherLastName = new _Literal.default("other-last-name", "Smith");
var lastNames = new _OrValue.default("last-names", [moreLastNames, lastName, otherLastName]);
var fullName = new _AndValue.default("full-name", [firstName, lastNames]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 4);
_assert.default.equal(tokens[0], "Moses");
_assert.default.equal(tokens[1], "Joshua");
_assert.default.equal(tokens[2], "Doe");
_assert.default.equal(tokens[3], "Smith");
};
exports["Pattern: getNextTokens, with repeat."] = function () {
var firstName = new _Literal.default("first-name", "John");
var edward = new _Literal.default("edward", "Edward");
var middleName = new _RepeatValue.default("middle-names", edward);
var lastName = new _Literal.default("lastName", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, middleName, lastName]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Edward");
tokens = fullName.children[1].children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
};
exports["Pattern: getNextTokens, with repeat and divider."] = function () {
var firstName = new _Literal.default("first-name", "John");
var edward = new _Literal.default("edward", "Edward");
var stewart = new _Literal.default("stewart", "Stewart");
var middleName = new _RepeatValue.default("middle-names", edward, stewart);
var lastName = new _Literal.default("lastName", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, middleName, lastName]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Edward");
tokens = fullName.children[1].children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Stewart");
_assert.default.equal(tokens[1], "Doe");
tokens = fullName.children[1].children[1].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
};
exports["Pattern: getNextTokens, has child and at the beginning."] = function () {
var firstName = new _Literal.default("first-name", "John");
var edward = new _Literal.default("edward", "Edward");
var stewart = new _Literal.default("stewart", "Stewart");
var middleName = new _RepeatValue.default("middle-names", edward, stewart);
var lastName = new _Literal.default("lastName", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, middleName, lastName]);
var tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "John");
};
exports["Pattern: getNextTokens, has no child and is at the beginning."] = function () {
var firstName = new _Literal.default("first-name", "John");
var tokens = firstName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "John");
};
exports["Pattern: getNextTokens, and with optional start."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [new _OptionalValue.default(firstName), middleName, lastName]);
var tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "John");
_assert.default.equal(tokens[1], "Edward");
};
exports["Pattern: getNextTokens, and with optional middle."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(middleName), lastName]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, and with optional last."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, middleName, new _OptionalValue.default(lastName)]);
var tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Edward");
tokens = fullName.children[1].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, first two optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [new _OptionalValue.default(firstName), new _OptionalValue.default(middleName), lastName]);
var tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 3);
_assert.default.equal(tokens[0], "John");
_assert.default.equal(tokens[1], "Edward");
_assert.default.equal(tokens[2], "Doe");
tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, last two optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [firstName, new _OptionalValue.default(middleName), new _OptionalValue.default(lastName)]);
var tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "John");
tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, all three optional."] = function () {
var firstName = new _Literal.default("first-name", "John");
var middleName = new _Literal.default("middle-name", "Edward");
var lastName = new _Literal.default("last-name", "Doe");
var fullName = new _AndValue.default("full-name", [new _OptionalValue.default(firstName), new _OptionalValue.default(middleName), new _OptionalValue.default(lastName)]);
var tokens = fullName.getTokens();
_assert.default.equal(tokens.length, 3);
_assert.default.equal(tokens[0], "John");
_assert.default.equal(tokens[1], "Edward");
_assert.default.equal(tokens[2], "Doe");
tokens = fullName.children[0].getNextTokens();
_assert.default.equal(tokens.length, 2);
_assert.default.equal(tokens[0], "Edward");
_assert.default.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
_assert.default.equal(tokens.length, 1);
_assert.default.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
_assert.default.equal(tokens.length, 0);
};
//# sourceMappingURL=Pattern.js.map

@@ -63,2 +63,31 @@ "use strict";

};
exports["RecursivePattern: No pattern"] = function () {
var node = new _RecursivePattern.default("nothing");
var result = node.exec("Some string.");
_assert.default.equal(result, null);
};
exports["RecursivePattern: clone."] = function () {
var node = new _RecursivePattern.default("nothing");
var clone = node.clone();
_assert.default.equal(node.name, clone.name);
var otherClone = node.clone("nothing2");
_assert.default.equal(otherClone.name, "nothing2");
};
exports["RecursivePattern: getNextTokens."] = function () {
var tokens = literals.getTokens();
tokens = literals.children[0].getNextTokens();
tokens = literals.children[4].getTokens();
tokens = literals.children[4].children[1].getNextTokens();
tokens = literals.children[4].children[2].getNextTokens();
tokens = literals.children[4].children[2].children[0].children[0].children[0].children[0].getTokens();
tokens = literals.children[4].children[2].children[0].children[0].getNextTokens();
tokens = literals.children[4].children[3].getNextTokens();
};
//# sourceMappingURL=RecursivePattern.js.map

@@ -21,2 +21,14 @@ "use strict";

};
exports["ValueNode: Without indexes."] = function () {
var node = new _ValueNode.default("type", "name", "value");
_assert.default.equal(node.name, "name");
_assert.default.equal(node.value, "value");
_assert.default.equal(node.startIndex, 0);
_assert.default.equal(node.endIndex, 0);
};
//# sourceMappingURL=ValueNode.js.map
{
"name": "clarity-pattern-parser",
"type": "module",
"version": "2.0.10",
"version": "2.0.11",
"description": "",

@@ -6,0 +6,0 @@ "main": "./lib/index.js",

@@ -6,2 +6,5 @@ import CursorHistory from "./CursorHistory.js";

this.string = string;
this.assertValidity();
this.index = 0;

@@ -11,4 +14,2 @@ this.length = string.length;

this.isInErrorState = false;
this.assertValidity();
}

@@ -97,3 +98,3 @@

moveToLast() {
moveToEnd() {
this.index = this.string.length - 1;

@@ -100,0 +101,0 @@ }

@@ -7,3 +7,3 @@ export default class CursorHistory {

pattern: null,
astNode: null
astNode: null,
};

@@ -23,5 +23,3 @@

}
if (astNode == null) {
debugger;
}
if (

@@ -52,7 +50,9 @@ this.furthestMatch.astNode == null ||

this.isRecording = false;
this.clear();
}
clear() {
this.matches = [];
this.errors = [];
this.patterns.length = 0;
this.astNodes.length = 0;
this.errors.length = 0;
}

@@ -69,6 +69,10 @@

getLastMatch() {
return {
pattern: this.patterns[this.patterns.length - 1] || null,
astNode: this.astNodes[this.astNodes.length - 1] || null
};
if (this.isRecording) {
return {
pattern: this.patterns[this.patterns.length - 1] || null,
astNode: this.astNodes[this.astNodes.length - 1] || null,
};
} else {
return this.furthestMatch;
}
}

@@ -75,0 +79,0 @@

@@ -31,5 +31,5 @@ import { Cursor } from "./index.js";

startIndex: 0,
options: pattern.getPossibilities()
options: pattern.getPossibilities(),
},
isComplete: false
isComplete: false,
};

@@ -50,3 +50,3 @@ }

possibilities: this.possibilities,
isComplete: this.cursor.didSuccessfullyParse()
isComplete: this.cursor.didSuccessfullyParse(),
};

@@ -96,3 +96,3 @@ }

startIndex: 0,
endIndex: endIndex
endIndex: endIndex,
};

@@ -106,3 +106,3 @@ }

endIndex: this.text.length - 1,
text: this.text
text: this.text,
};

@@ -120,3 +120,3 @@ return this;

endIndex: endIndex,
text: this.text.substring(startIndex, endIndex + 1)
text: this.text.substring(startIndex, endIndex + 1),
};

@@ -142,8 +142,8 @@

let options = this.rootPattern.getPossibilities();
const parts = this.text.split(" ").filter(part => {
const parts = this.text.split(" ").filter((part) => {
return part.length > 0;
});
options = options.filter(option => {
return parts.some(part => {
options = options.filter((option) => {
return parts.some((part) => {
return option.indexOf(part) > -1;

@@ -160,3 +160,3 @@ });

startIndex: 0,
options
options,
};

@@ -167,4 +167,4 @@

const pattern = this.patternMatch.pattern || this.rootPattern;
const parentPattern = pattern.parent || pattern;
const pattern = this.patternMatch.pattern;
const parentPattern = pattern.parent;
const index = parentPattern.children.indexOf(pattern);

@@ -177,3 +177,2 @@ const parentClone = parentPattern.clone();

let startIndex = this.matchedText.length;
startIndex = startIndex >= 0 ? startIndex : 0;

@@ -183,6 +182,6 @@ if (this.matchedText.length < this.text.length) {

const partialMatchOptions = options
.filter(option => {
.filter((option) => {
return option.indexOf(leftOver) === 0;
})
.map(option => {
.map((option) => {
return option.substring(leftOver.length);

@@ -198,3 +197,3 @@ });

text: this.match.text + leftOver,
endIndex: this.match.endIndex + leftOver.length
endIndex: this.match.endIndex + leftOver.length,
};

@@ -206,3 +205,3 @@

startIndex: this.match.endIndex + 1,
options: partialMatchOptions
options: partialMatchOptions,
};

@@ -216,3 +215,3 @@

startIndex,
options
options,
};

@@ -222,4 +221,4 @@ }

static inspectParse(text, pattern) {
return new ResultGenerator().inspectParse(text, pattern);
return new ParseInspector().inspectParse(text, pattern);
}
}

@@ -12,3 +12,3 @@ import CompositePattern from "./CompositePattern.js";

export default class AndComposite extends CompositePattern {
constructor(name, patterns) {
constructor(name, patterns = []) {
super("and-composite", name, patterns);

@@ -107,3 +107,3 @@ this._assertArguments();

if (!this.cursor.hasUnresolvedError()) {
this.nodes = this.nodes.filter(node => node != null);
this.nodes = this.nodes.filter((node) => node != null);

@@ -138,9 +138,31 @@ const lastNode = this.nodes[this.nodes.length - 1];

getPossibilities(rootPattern) {
if (rootPattern == null || !(rootPattern instanceof Pattern)){
if (rootPattern == null || !(rootPattern instanceof Pattern)) {
rootPattern = this;
}
const possibilities = this.children.map(child => child.getPossibilities(rootPattern));
const possibilities = this.children.map((child) =>
child.getPossibilities(rootPattern)
);
return permutor.permute(possibilities);
}
getTokens() {
let tokens = [];
for (let x = 0; x < this._children.length; x++) {
const child = this._children[x];
if (
child instanceof OptionalValue ||
child instanceof OptionalComposite
) {
tokens = tokens.concat(child.getTokens());
} else {
tokens = tokens.concat(child.getTokens());
break;
}
}
return tokens;
}
}

@@ -8,21 +8,2 @@ import Pattern from "../Pattern.js";

_assertChildren() {
if (!Array.isArray(this._children)) {
throw new Error(
"Invalid Arguments: The patterns argument need to be an array of Patterns."
);
}
const areAllPatterns = this._children.every(
pattern => pattern instanceof Pattern
);
if (!areAllPatterns) {
throw new Error(
"Invalid Argument: All patterns need to be an instance of Pattern."
);
}
}
clone() {

@@ -29,0 +10,0 @@ throw new Error("Not Yet Implemented");

@@ -7,11 +7,4 @@ import CompositePattern from "./CompositePattern.js";

super("optional-composite", "optional-composite", [pattern]);
this._assertArguments();
}
_assertArguments() {
if (!(this.children[0] instanceof Pattern)) {
throw new Error("Invalid Arguments: Expected a Pattern.");
}
}
parse(cursor) {

@@ -52,2 +45,6 @@ const mark = cursor.mark();

}
getTokens() {
return this._children[0].getTokens();
}
}

@@ -20,3 +20,3 @@ import CompositePattern from "./CompositePattern.js";

const hasOptionalChildren = this._children.some(
pattern =>
(pattern) =>
pattern instanceof OptionalValue || pattern instanceof OptionalComposite

@@ -31,11 +31,7 @@ );

_reset(cursor) {
this.cursor = null;
this.cursor = cursor;
this.mark = null;
this.index = 0;
this.node = null;
if (cursor != null) {
this.cursor = cursor;
this.mark = cursor.mark();
}
this.mark = cursor.mark();
}

@@ -84,3 +80,3 @@

getPossibilities(rootPattern) {
if (rootPattern == null || !(rootPattern instanceof Pattern)){
if (rootPattern == null || !(rootPattern instanceof Pattern)) {
rootPattern = this;

@@ -90,3 +86,3 @@ }

return this.children
.map(child => {
.map((child) => {
return child.getPossibilities(rootPattern);

@@ -98,2 +94,16 @@ })

}
getTokens() {
const tokens = this._children.map((c) => c.getTokens());
const hasPrimitiveTokens = tokens.every((t) =>
t.every((value) => typeof value === "string")
);
if (hasPrimitiveTokens && tokens.length > 0) {
return tokens.reduce((acc, t) => acc.concat(t), []);
}
return this._children[0].getTokens();
}
}

@@ -130,2 +130,6 @@ import CompositePattern from "./CompositePattern.js";

}
getTokens() {
return this._pattern.getTokens();
}
}
import Cursor from "../Cursor.js";
export default class Pattern {
constructor(type = null, name = null, children = []) {
constructor(type = null, name, children = []) {
this._type = type;
this._name = name;
this._children = [];
this._parent = null;
this.isSequence = false;

@@ -75,3 +77,3 @@ this._assertName();

// We need to clone the patterns so nested patterns can be parsed.
this._children = this._children.map(pattern => {
this._children = this._children.map((pattern) => {
if (!(pattern instanceof Pattern)) {

@@ -90,3 +92,3 @@ throw new Error(

_assignAsParent() {
this._children.forEach(child => (child.parent = this));
this._children.forEach((child) => (child.parent = this));
}

@@ -101,2 +103,69 @@

}
getTokens() {
throw new Error("Method Not Implemented");
}
getNextTokens() {
if (this._parent != null) {
const siblings = this._parent.children;
const index = siblings.findIndex((c) => c === this);
const nextSibling = siblings[index + 1];
// I don't like this, so I think we need to rethink this.
if (this._parent.type.indexOf("repeat") === 0) {
const tokens = this._parent.getNextTokens();
if (index === 0 && siblings.length > 1) {
return nextSibling.getTokens().concat(tokens);
} else if (index === 1) {
return siblings[0].getTokens().concat(tokens);
} else {
return this.getTokens().concat(tokens);
}
}
// Another thing I don't like.
if (
this._parent.type.indexOf("and") === 0 &&
nextSibling != null &&
nextSibling.type.indexOf("optional") === 0
) {
let tokens = [];
for (let x = index + 1; x < siblings.length; x++) {
const child = siblings[x];
if (child.type.indexOf("optional") === 0) {
tokens = tokens.concat(child.getTokens());
} else {
tokens = tokens.concat(child.getTokens());
break;
}
if (x === siblings.length - 1) {
tokens = tokens.concat(this._parent.getNextTokens());
}
}
return tokens;
}
// If you are an or you have already qualified.
if (this._parent.type.indexOf("or") === 0) {
return this._parent.getNextTokens();
}
if (nextSibling != null) {
return nextSibling.getTokens();
} else {
return this._parent.getNextTokens();
}
}
return [];
}
getTokenValue() {
return null;
}
}

@@ -7,7 +7,10 @@ import Pattern from "./Pattern.js";

super("recursive", name);
this.isGettingPossibilities = false;
this.isRecursing = false;
}
getPattern() {
return this._climb(this.parent, pattern => {
return this._climb(this.parent, (pattern) => {
if (pattern == null) {
return false;
}
return pattern.name === this.name;

@@ -21,3 +24,3 @@ });

} else {
if (pattern.parent != null) {
if (pattern && pattern.parent != null) {
return this._climb(pattern.parent, isMatch);

@@ -65,6 +68,6 @@ }

getPossibilities() {
if (!this.isGettingPossibilities) {
this.isGettingPossibilities = true;
if (!this.isRecursing) {
this.isRecursing = true;
const possibilities = this.getPattern().getPossibilities();
this.isGettingPossibilities = false;
this.isRecursing = false;

@@ -76,2 +79,16 @@ return possibilities;

}
getTokenValue() {
return this.getPattern().getTokenValue();
}
getTokens() {
if (!this.isRecursing) {
this.isRecursing = true;
const tokens = this.getPattern().getTokens();
this.isRecursing = false;
return tokens;
}
}
}

@@ -103,3 +103,3 @@ import ValuePattern from "./ValuePattern.js";

} else {
this.nodes = this.nodes.filter(node => node != null);
this.nodes = this.nodes.filter((node) => node != null);

@@ -109,3 +109,3 @@ const lastNode = this.nodes[this.nodes.length - 1];

const endIndex = lastNode.endIndex;
const value = this.nodes.map(node => node.value).join("");
const value = this.nodes.map((node) => node.value).join("");

@@ -137,3 +137,3 @@ this.node = new ValueNode(

const possibilities = this.children.map(child =>
const possibilities = this.children.map((child) =>
child.getPossibilities(rootPattern)

@@ -143,2 +143,19 @@ );

}
getTokens() {
let tokens = [];
for (let x = 0; x < this._children.length; x++) {
const child = this._children[x];
if (child instanceof OptionalValue) {
tokens = tokens.concat(child.getTokens());
} else {
tokens = tokens.concat(child.getTokens());
break;
}
}
return tokens;
}
}

@@ -77,4 +77,9 @@ import ValuePattern from "./ValuePattern.js";

return this.getTokens();
}
getTokens() {
return this.characters.split("");
}
}

@@ -79,4 +79,12 @@ import ParseError from "../ParseError.js";

getPossibilities() {
return [this.literal];
return [this.getTokenValue()];
}
getTokenValue() {
return this.literal;
}
getTokens() {
return [this.getTokenValue()];
}
}

@@ -88,2 +88,6 @@ import ValuePattern from "./ValuePattern.js";

}
getTokens() {
return [];
}
}

@@ -50,2 +50,7 @@ import ValuePattern from "./ValuePattern.js";

}
getTokens() {
return this._children[0].getTokens();
}
}

@@ -20,3 +20,3 @@ import ValuePattern from "./ValuePattern.js";

const hasOptionalChildren = this._children.some(
pattern => pattern instanceof OptionalValue
(pattern) => pattern instanceof OptionalValue
);

@@ -88,3 +88,3 @@

return this.children
.map(child => {
.map((child) => {
return child.getPossibilities(rootPattern);

@@ -96,2 +96,17 @@ })

}
getTokens() {
const tokens = this._children.map((c) => c.getTokens());
const hasPrimitiveTokens = tokens.every((t) =>
t.every((value) => typeof value === "string")
);
if (hasPrimitiveTokens && tokens.length > 0) {
return tokens.reduce((acc, t) => acc.concat(t), []);
}
return this._children[0].getTokens();
}
}

@@ -91,4 +91,12 @@ import ParseError from "../ParseError.js";

getPossibilities() {
return [this.getTokenValue()];
}
getTokenValue() {
return this.name;
}
getTokens() {
return [this.name];
}
}

@@ -88,3 +88,3 @@ import ValuePattern from "./ValuePattern.js";

} else {
const value = this.nodes.map(node => node.value).join("");
const value = this.nodes.map((node) => node.value).join("");

@@ -121,4 +121,4 @@ this.node = new ValueNode(

.getPossibilities(rootPattern)
.map(possibility => {
return dividerPossibilities.map(divider => {
.map((possibility) => {
return dividerPossibilities.map((divider) => {
return `${possibility}${divider}`;

@@ -134,2 +134,7 @@ });

}
getTokens() {
return this._pattern.getTokens();
}
}
import AndComposite from "../patterns/composite/AndComposite.js";
import Literal from "../patterns/value/Literal.js";
import OptionalValue from "../patterns/value/OptionalValue.js";
import assert from "assert";

@@ -22,13 +23,83 @@ import { Cursor } from "../index.js";

exports["AndComposite: No Match"] = () => {
const john = new Literal("john", "John");
const doe = new Literal("doe", "Doe");
const cursor = new Cursor("JohnSmith");
const name = new AndComposite("name", [john, doe]);
const node = name.parse(cursor);
assert.equal(node, null);
assert.equal(cursor.getIndex(), 0);
assert.equal(cursor.hasUnresolvedError(), true);
assert.equal(cursor.parseError.message, "ParseError: Expected 'Doe' but found 'Smi'.");
};
const john = new Literal("john", "John");
const doe = new Literal("doe", "Doe");
const cursor = new Cursor("JohnSmith");
const name = new AndComposite("name", [john, doe]);
const node = name.parse(cursor);
assert.equal(node, null);
assert.equal(cursor.getIndex(), 0);
assert.equal(cursor.hasUnresolvedError(), true);
assert.equal(
cursor.parseError.message,
"ParseError: Expected 'Doe' but found 'Smi'."
);
};
exports["AndComposite: test."] = () => {
const john = new Literal("john", "John");
const doe = new Literal("doe", "Doe");
const name = new AndComposite("name", [john, doe]);
const isMatch = name.test("JohnDoe");
assert.equal(isMatch, true);
};
exports["AndComposite: no children."] = () => {
assert.throws(() => {
new AndComposite("name");
});
};
exports["AndComposite: string runs out before match is done."] = () => {
const first = new Literal("first", "John");
const last = new Literal("last", "Doe");
const name = new AndComposite("name", [first, last]);
const cursor = new Cursor("JohnDo");
const result = name.parse(cursor);
};
exports["AndComposite: last name is optional."] = () => {
const first = new Literal("first", "John");
const last = new OptionalValue(new Literal("last", "Boe"));
const name = new AndComposite("name", [first, last]);
const cursor = new Cursor("JohnDo");
const result = name.parse(cursor);
assert.equal(result.name, "name");
assert.equal(result.type, "and-composite");
assert.equal(result.children[0].value, "John");
assert.equal(result.children[0].name, "first");
assert.equal(result.children[0].type, "literal");
};
exports["AndComposite: three non-optional patterns."] = () => {
const first = new Literal("first", "John");
const middle = new Literal("middle", "Smith");
const last = new Literal("last", "Doe");
const name = new AndComposite("name", [first, middle, last]);
const cursor = new Cursor("JohnDoe");
const result = name.parse(cursor);
};
exports[
"AndComposite: full name, middle optional, and last name isn't."
] = () => {
const first = new Literal("first", "John");
const middle = new OptionalValue(new Literal("middle", "Smith"));
const last = new Literal("last", "Doe");
const name = new AndComposite("name", [first, middle, last]);
const cursor = new Cursor("JohnDoe");
const result = name.parse(cursor);
};
exports["AndComposite: clone."] = () => {
const john = new Literal("john", "John");
const doe = new Literal("doe", "Doe");
const name = new AndComposite("name", [john, doe]);
const clone = name.clone("name2");
assert.equal(clone.name, "name2");
};
import AndValue from "../patterns/value/AndValue.js";
import Literal from "../patterns/value/Literal.js";
import OptionalValue from "../patterns/value/OptionalValue.js";
import Cursor from "../Cursor.js";
import assert from "assert";
import Cursor from "../Cursor.js";

@@ -49,2 +50,48 @@ exports["AndValue: Empty Constructor."] = () => {

exports["AndValue: First Part Match with optional Second part."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(lastName),
]);
const cursor = new Cursor("John");
const node = fullName.parse(cursor);
assert.equal(node.name, "full-name");
assert.equal(node.value, "John");
assert.equal(node.startIndex, 0);
assert.equal(node.endIndex, 3);
};
exports["AndValue: First Part Match, but run out for second part."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const cursor = new Cursor("John");
const node = fullName.parse(cursor);
assert.equal(node, null);
};
exports["AndValue: No Match"] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const cursor = new Cursor("JaneDoe");
const node = fullName.parse(cursor);
assert.equal(node, null);
};
exports["AndValue: Partial Match without optional siblings."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const cursor = new Cursor("JohnSmith");
const node = fullName.parse(cursor);
assert.equal(node, null);
};
exports["AndValue: Success with more to parse"] = () => {

@@ -86,1 +133,129 @@ const firstName = new Literal("first-name", "John");

};
exports["AndValue: Clone with custom name."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const clone = fullName.clone("full-name-2");
const fullNamePatterns = fullName.children;
const _cloneChildren = clone.children;
assert.notEqual(fullNamePatterns[0], _cloneChildren[0]);
assert.notEqual(fullNamePatterns[1], _cloneChildren[1]);
assert.equal(clone.name, "full-name-2");
};
exports["AndValue: getPossibilities."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const possibilities = fullName.getPossibilities();
assert.equal(possibilities.length, 1);
assert.equal(possibilities[0], "JohnDoe");
};
exports[
"AndValue: getPossibilities with itself being the root pattern."
] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const possibilities = fullName.getPossibilities(fullName);
assert.equal(possibilities.length, 1);
assert.equal(possibilities[0], "JohnDoe");
};
exports["AndValue: getPossibilities with a invalid root."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const possibilities = fullName.getPossibilities("");
assert.equal(possibilities.length, 1);
assert.equal(possibilities[0], "JohnDoe");
};
exports["AndValue: Partial Match."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(lastName),
]);
const result = fullName.parse(new Cursor("JohnBo"));
assert.equal(result.type, "and-value");
assert.equal(result.name, "full-name");
assert.equal(result.value, "John");
};
exports[
"AndValue: Partial Match with string running out, and optional last name."
] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(lastName),
]);
const result = fullName.parse(new Cursor("JohnDo"));
assert.equal(result.type, "and-value");
assert.equal(result.name, "full-name");
assert.equal(result.value, "John");
};
exports["AndValue: Three parts first optional."] = () => {
const firstName = new Literal("first-name", "John");
const middle = new Literal("middle", "Smith");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
new OptionalValue(firstName),
middle,
lastName,
]);
const result = fullName.parse(new Cursor("SmithDoe"));
assert.equal(result.value, "SmithDoe");
assert.equal(result.type, "and-value");
assert.equal(result.name, "full-name");
};
exports["AndValue: Three parts middle optional."] = () => {
const firstName = new Literal("first-name", "John");
const middle = new Literal("middle", "Smith");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(middle),
lastName,
]);
const result = fullName.parse(new Cursor("JohnDo"));
assert.equal(result, null);
};
exports["AndValue: Three parts third optional."] = () => {
const firstName = new Literal("first-name", "John");
const middle = new Literal("middle", "Smith");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
middle,
new OptionalValue(lastName),
]);
const result = fullName.parse(new Cursor("JohnSmith"));
assert.equal(result.value, "JohnSmith");
assert.equal(result.type, "and-value");
assert.equal(result.name, "full-name");
};

@@ -52,3 +52,3 @@ import cssValue from "./cssPatterns/cssValue.js";

exports["Css: spaced values"] = () => {
exports["Css: spaced values in a method"] = () => {
const cursor = new Cursor("method(arg1 0%, arg2, 10%) 0%");

@@ -55,0 +55,0 @@ const result = cssValue.parse(cursor);

@@ -44,2 +44,8 @@ import attribute from "../queryPatterns/attribute.js";

const result = cssSelector.parse(cursor);
assert.equal(result.value, "*");
assert.equal(result.name, "element-name");
assert.equal(result.type, "regex-value");
assert.equal(result.startIndex, 0);
assert.equal(result.endIndex, 0);
};

@@ -50,5 +56,13 @@

const result = cssSelector.parse(cursor);
assert.equal(result.type, "and-composite");
assert.equal(result.name, "attribute-selector");
assert.equal(result.children[0].name, "element-name");
assert.equal(result.children[0].value, "*");
assert.equal(result.children[1].name, "attribute");
assert.equal(result.startIndex, 0);
assert.equal(result.endIndex, 14);
};
exports["cssSelector: element with attribute"] = ()=>{
exports["cssSelector: element with attribute and a child selector"] = ()=>{
const cursor = new Cursor("element-name > *[name='value']");

@@ -58,5 +72,5 @@ const result = cssSelector.parse(cursor);

exports["cssSelector: element with attribute"] = ()=>{
exports["cssSelector: element with attribute and two deep child selector"] = ()=>{
const cursor = new Cursor("element-name > * > *[name='value']");
const result = cssSelector.parse(cursor);
};

@@ -20,7 +20,2 @@ import element, { attribute } from "./htmlPatterns/element";

exports["Html: element with attributes"] = () => {
const cursor = new Cursor('<div prop="value"></div>');
const result = element.parse(cursor);
};
exports["Html: multiple children"] = () => {

@@ -27,0 +22,0 @@ const cursor = new Cursor(`<div prop="value">

@@ -6,9 +6,21 @@ import Node from "../ast/Node.js";

assert.throws(() => {
new Node("type", 0, 1).clone();
new Node("type", "name", 0, 1).clone();
});
};
exports["Node: fitler."] = () => {
assert.throws(() => {
new Node("type", "name", 0, 1).filter();
});
};
exports["Node: toString."] = () => {
assert.throws(() => {
new Node("type", "name", 0, 1).toString();
});
};
exports["Node: With no startIndex."] = () => {
assert.throws(() => {
new Node("type", undefined, 1).clone();
new Node("type", "name", undefined, 1);
});

@@ -19,4 +31,8 @@ };

assert.throws(() => {
new Node("type", 0).clone();
new Node("type", "name", 0);
});
};
exports["Node: With all arguments."] = () => {
new Node("type", "name", 0, 0);
};
import OrComposite from "../patterns/composite/OrComposite.js";
import Literal from "../patterns/value/Literal.js";
import OptionalValue from "../patterns/value/OptionalValue.js";
import assert from "assert";

@@ -30,1 +31,55 @@ import { Cursor } from "../index.js";

};
exports["OrComposite: Supplied only one option."] = () => {
const john = new Literal("john", "John");
assert.throws(() => {
new OrComposite("name", [john]);
});
};
exports["OrComposite: Optional Children."] = () => {
const john = new Literal("john", "John");
const jane = new Literal("jane", "Jane");
assert.throws(() => {
new OrComposite("name", [new OptionalValue(john), new OptionalValue(jane)]);
});
};
exports["OrComposite: getPossibilities."] = () => {
const john = new Literal("john", "John");
const jane = new Literal("jane", "Jane");
const name = new OrComposite("name", [john, jane]);
const possibilities = name.getPossibilities();
assert.equal(possibilities.length, 2);
assert.equal(possibilities[0], "John");
assert.equal(possibilities[1], "Jane");
};
exports["OrComposite: parse with null cursor."] = () => {
const john = new Literal("john", "John");
const jane = new Literal("jane", "Jane");
const cursor = new Cursor("John");
const name = new OrComposite("name", [john, jane]);
const node = name.parse(cursor);
assert.equal(node.name, "john");
assert.equal(node.value, "John");
};
exports["OrComposite: clone."] = () => {
const john = new Literal("john", "John");
const jane = new Literal("jane", "Jane");
const name = new OrComposite("name", [john, jane]);
const clone = name.clone("name2");
assert.equal(clone.name, "name2");
assert.equal(clone.children.length, 2);
assert.equal(clone.children[0].name, "john");
assert.equal(clone.children[1].name, "jane");
};

@@ -115,1 +115,13 @@ import ParseInspector from "../ParseInspector.js";

};
exports["ParseInspector: static inspectParse."] = () => {
const text = "Pat went to a big store";
const inspection = ParseInspector.inspectParse(text, sentence);
assert.equal(inspection.match.text, "Pat went to a big store");
assert.equal(inspection.isComplete, true);
assert.equal(inspection.match.startIndex, 0);
assert.equal(inspection.match.endIndex, 22);
assert.equal(inspection.error, null);
assert.equal(inspection.possibilities, null);
};
import Pattern from "../patterns/Pattern.js";
import AndValue from "../patterns/value/AndValue.js";
import OrValue from "../patterns/value/OrValue.js";
import Literal from "../patterns/value/Literal.js";
import RepeatValue from "../patterns/value/RepeatValue.js";
import OptionalValue from "../patterns/value/OptionalValue.js";
import assert from "assert";

@@ -17,1 +22,348 @@

};
exports["Pattern: getPossibilities."] = () => {
const valuePattern = new Pattern("pattern-type", "pattern");
assert.throws(() => {
valuePattern.getPossibilities();
});
};
exports["Pattern: limited arguments."] = () => {
new Pattern(undefined, "name");
};
exports["Pattern: no arguments."] = () => {
assert.throws(() => {
new Pattern();
});
};
exports["Pattern: set parent."] = () => {
const parent = new Pattern("pattern-type", "pattern");
const child = new Pattern("pattern-type", "pattern");
child.parent = parent;
};
exports["Pattern: set invalid parent."] = () => {
const child = new Pattern("pattern-type", "pattern");
child.parent = "";
assert.equal(child.parent, null);
};
exports["Pattern: getTokens"] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
let tokens = firstName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "John");
tokens = lastName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "John");
};
exports["Pattern: getNextToken one deep."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [firstName, lastName]);
const tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
};
exports["Pattern: getNextToken two deep."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const otherLastName = new Literal("other-last-name", "Smith");
const lastNames = new OrValue("last-names", [lastName, otherLastName]);
const fullName = new AndValue("full-name", [firstName, lastNames]);
const tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Doe");
assert.equal(tokens[1], "Smith");
};
exports["Pattern: getNextToken three deep."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const middleName = new Literal("middle-name", "Moses");
const otherMiddleName = new Literal("other-middle-name", "Joshua");
const middleNames = new OrValue("middle-names", [
middleName,
otherMiddleName,
]);
const otherLastName = new Literal("other-last-name", "Smith");
const lastNames = new OrValue("last-names", [lastName, otherLastName]);
const fullName = new AndValue("full-name", [
firstName,
middleNames,
lastNames,
]);
const tokens = fullName.children[1].children[1].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Doe");
assert.equal(tokens[1], "Smith");
};
exports["Pattern: getNextToken end of patterns."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const middleName = new Literal("middle-name", "Moses");
const otherMiddleName = new Literal("other-middle-name", "Joshua");
const middleNames = new OrValue("middle-names", [
middleName,
otherMiddleName,
]);
const otherLastName = new Literal("other-last-name", "Smith");
const lastNames = new OrValue("last-names", [lastName, otherLastName]);
const fullName = new AndValue("full-name", [
firstName,
middleNames,
lastNames,
]);
const tokens = fullName.children[2].children[1].getNextTokens();
assert.equal(tokens.length, 0);
};
exports["Pattern: getNextToken end of patterns."] = () => {
const firstName = new Literal("first-name", "John");
const lastName = new Literal("last-name", "Doe");
const moses = new Literal("moses", "Moses");
const joshua = new Literal("other-middle-name", "Joshua");
const moreLastNames = new OrValue("more-last-names", [moses, joshua]);
const otherLastName = new Literal("other-last-name", "Smith");
const lastNames = new OrValue("last-names", [
moreLastNames,
lastName,
otherLastName,
]);
const fullName = new AndValue("full-name", [firstName, lastNames]);
const tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 4);
assert.equal(tokens[0], "Moses");
assert.equal(tokens[1], "Joshua");
assert.equal(tokens[2], "Doe");
assert.equal(tokens[3], "Smith");
};
exports["Pattern: getNextTokens, with repeat."] = () => {
const firstName = new Literal("first-name", "John");
const edward = new Literal("edward", "Edward");
const middleName = new RepeatValue("middle-names", edward);
const lastName = new Literal("lastName", "Doe");
const fullName = new AndValue("full-name", [firstName, middleName, lastName]);
let tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Edward");
tokens = fullName.children[1].children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
};
exports["Pattern: getNextTokens, with repeat and divider."] = () => {
const firstName = new Literal("first-name", "John");
const edward = new Literal("edward", "Edward");
const stewart = new Literal("stewart", "Stewart");
const middleName = new RepeatValue("middle-names", edward, stewart);
const lastName = new Literal("lastName", "Doe");
const fullName = new AndValue("full-name", [firstName, middleName, lastName]);
let tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Edward");
tokens = fullName.children[1].children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Stewart");
assert.equal(tokens[1], "Doe");
tokens = fullName.children[1].children[1].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
};
exports["Pattern: getNextTokens, has child and at the beginning."] = () => {
const firstName = new Literal("first-name", "John");
const edward = new Literal("edward", "Edward");
const stewart = new Literal("stewart", "Stewart");
const middleName = new RepeatValue("middle-names", edward, stewart);
const lastName = new Literal("lastName", "Doe");
const fullName = new AndValue("full-name", [firstName, middleName, lastName]);
let tokens = fullName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "John");
};
exports[
"Pattern: getNextTokens, has no child and is at the beginning."
] = () => {
const firstName = new Literal("first-name", "John");
let tokens = firstName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "John");
};
exports["Pattern: getNextTokens, and with optional start."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
new OptionalValue(firstName),
middleName,
lastName,
]);
let tokens = fullName.getTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "John");
assert.equal(tokens[1], "Edward");
};
exports["Pattern: getNextTokens, and with optional middle."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(middleName),
lastName,
]);
let tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
assert.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, and with optional last."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
middleName,
new OptionalValue(lastName),
]);
let tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Edward");
tokens = fullName.children[1].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
assert.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, first two optional."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
new OptionalValue(firstName),
new OptionalValue(middleName),
lastName,
]);
let tokens = fullName.getTokens();
assert.equal(tokens.length, 3);
assert.equal(tokens[0], "John");
assert.equal(tokens[1], "Edward");
assert.equal(tokens[2], "Doe");
tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
assert.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, last two optional."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
firstName,
new OptionalValue(middleName),
new OptionalValue(lastName),
]);
let tokens = fullName.getTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "John");
tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
assert.equal(tokens.length, 0);
};
exports["Pattern: getNextTokens, all three optional."] = () => {
const firstName = new Literal("first-name", "John");
const middleName = new Literal("middle-name", "Edward");
const lastName = new Literal("last-name", "Doe");
const fullName = new AndValue("full-name", [
new OptionalValue(firstName),
new OptionalValue(middleName),
new OptionalValue(lastName),
]);
let tokens = fullName.getTokens();
assert.equal(tokens.length, 3);
assert.equal(tokens[0], "John");
assert.equal(tokens[1], "Edward");
assert.equal(tokens[2], "Doe");
tokens = fullName.children[0].getNextTokens();
assert.equal(tokens.length, 2);
assert.equal(tokens[0], "Edward");
assert.equal(tokens[1], "Doe");
tokens = fullName.children[1].getNextTokens();
assert.equal(tokens.length, 1);
assert.equal(tokens[0], "Doe");
tokens = fullName.children[2].getNextTokens();
assert.equal(tokens.length, 0);
};

@@ -12,3 +12,3 @@ import RecursivePattern from "../patterns/RecursivePattern.js";

Cursor,
AndValue
AndValue,
} from "../index.js";

@@ -33,3 +33,3 @@ import string from "./javascriptPatterns/string.js";

comma,
optionalSpaces
optionalSpaces,
]);

@@ -49,3 +49,3 @@

optionalSpaces,
closeSquareBracket
closeSquareBracket,
]);

@@ -58,3 +58,3 @@

optionalSpaces,
new RecursivePattern("literals")
new RecursivePattern("literals"),
]);

@@ -70,3 +70,3 @@

optionalSpaces,
closeCurlyBracket
closeCurlyBracket,
]);

@@ -80,3 +80,3 @@

objectLiteral,
arrayLiteral
arrayLiteral,
]);

@@ -90,6 +90,6 @@

json: {
string: "This is a nested string."
string: "This is a nested string.",
},
null: null,
array: [1, "Blah", { prop1: true }]
array: [1, "Blah", { prop1: true }],
});

@@ -107,1 +107,34 @@

};
exports["RecursivePattern: No pattern"] = () => {
const node = new RecursivePattern("nothing");
const result = node.exec("Some string.");
assert.equal(result, null);
};
exports["RecursivePattern: clone."] = () => {
const node = new RecursivePattern("nothing");
const clone = node.clone();
assert.equal(node.name, clone.name);
const otherClone = node.clone("nothing2");
assert.equal(otherClone.name, "nothing2");
};
exports["RecursivePattern: getNextTokens."] = () => {
let tokens = literals.getTokens();
tokens = literals.children[0].getNextTokens();
tokens = literals.children[4].getTokens();
tokens = literals.children[4].children[1].getNextTokens();
tokens = literals.children[4].children[2].getNextTokens();
tokens = literals.children[4].children[2].children[0].children[0].children[0].children[0].getTokens();
tokens = literals.children[4].children[2].children[0].children[0].getNextTokens();
tokens = literals.children[4].children[3].getNextTokens();
};

@@ -13,1 +13,10 @@ import ValueNode from "../ast/ValueNode.js";

};
exports["ValueNode: Without indexes."] = () => {
const node = new ValueNode("type", "name", "value");
assert.equal(node.name, "name");
assert.equal(node.value, "value");
assert.equal(node.startIndex, 0);
assert.equal(node.endIndex, 0);
};

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc