clarity-pattern-parser
Advanced tools
Comparing version 3.0.2 to 3.0.3
@@ -1518,2 +1518,189 @@ (function (global, factory) { | ||
class NodeVisitor { | ||
constructor(context, selectedNodes = []) { | ||
this.context = context; | ||
this.selectedNodes = selectedNodes; | ||
} | ||
flatten() { | ||
this.selectedNodes.forEach((node) => { | ||
if (node.isComposite) { | ||
const children = []; | ||
this.walkUp(node, (descendant) => { | ||
if (!descendant.isComposite) { | ||
children.push(descendant); | ||
} | ||
}); | ||
node.children = children; | ||
} | ||
}); | ||
return this; | ||
} | ||
remove() { | ||
this.recursiveRemove(this.context); | ||
return this; | ||
} | ||
recursiveRemove(node) { | ||
const nodesToRemove = this.selectedNodes; | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
for (let x = 0; x < node.children.length; x++) { | ||
if (nodesToRemove.indexOf(node.children[x]) > -1) { | ||
node.children.splice(x, 1); | ||
x--; | ||
} | ||
else { | ||
this.recursiveRemove(node.children[x]); | ||
} | ||
} | ||
} | ||
} | ||
wrap(callback) { | ||
const visitor = new NodeVisitor(this.context); | ||
visitor.selectRoot().transform((node) => { | ||
if (this.selectedNodes.includes(node)) { | ||
return callback(node); | ||
} | ||
return node; | ||
}); | ||
return this; | ||
} | ||
unwrap() { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
const grandParent = stack[stack.length - 2]; | ||
if (parent != null && grandParent != null) { | ||
const index = grandParent.children.indexOf(parent); | ||
if (index > -1) { | ||
grandParent.children.splice(index, 1, ...parent.children); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
prepend(callback) { | ||
this.walkUp(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
append(callback) { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index + 1, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
transform(callback) { | ||
this.selectedNodes.forEach((node) => { | ||
return this.recursiveTransform(node, callback); | ||
}); | ||
return this; | ||
} | ||
recursiveTransform(node, callback) { | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const length = node.children.length; | ||
for (let x = 0; x < length; x++) { | ||
node.children[x] = this.recursiveTransform(node.children[x], callback); | ||
} | ||
} | ||
return callback(node); | ||
} | ||
walkUp(node, callback, ancestors = []) { | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkUp(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
callback(node, ancestors); | ||
return this; | ||
} | ||
walkDown(node, callback, ancestors = []) { | ||
callback(node, ancestors); | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkDown(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
return this; | ||
} | ||
selectAll() { | ||
return this.select((n) => true); | ||
} | ||
selectNode(node) { | ||
return new NodeVisitor(this.context, [...this.selectedNodes, node]); | ||
} | ||
deselectNode(node) { | ||
const visitor = new NodeVisitor(this.context, this.selectedNodes.slice()); | ||
return visitor.filter((n) => n !== node); | ||
} | ||
select(callback) { | ||
const node = this.context; | ||
const selectedNodes = []; | ||
if (node.isComposite) { | ||
this.walkDown(node, (descendant) => { | ||
if (callback(descendant)) { | ||
selectedNodes.push(descendant); | ||
} | ||
}); | ||
} | ||
return new NodeVisitor(this.context, selectedNodes); | ||
} | ||
forEach(callback) { | ||
this.selectedNodes.forEach(callback); | ||
return this; | ||
} | ||
filter(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.filter(callback)); | ||
} | ||
map(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.map(callback)); | ||
} | ||
selectRoot() { | ||
return new NodeVisitor(this.context, [this.context]); | ||
} | ||
first() { | ||
return this.get(0); | ||
} | ||
last() { | ||
return this.get(this.selectedNodes.length - 1); | ||
} | ||
get(index) { | ||
const node = this.selectedNodes[index]; | ||
if (node == null) { | ||
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`); | ||
} | ||
return new NodeVisitor(node, []); | ||
} | ||
clear() { | ||
this.selectedNodes = []; | ||
return this; | ||
} | ||
static select(context, callback) { | ||
if (callback != null) { | ||
return new NodeVisitor(context).select(callback); | ||
} | ||
else { | ||
return new NodeVisitor(context); | ||
} | ||
} | ||
} | ||
exports.AndComposite = AndComposite; | ||
@@ -1527,2 +1714,3 @@ exports.AndValue = AndValue; | ||
exports.Node = Node; | ||
exports.NodeVisitor = NodeVisitor; | ||
exports.NotValue = NotValue; | ||
@@ -1529,0 +1717,0 @@ exports.OptionalComposite = OptionalComposite; |
@@ -23,2 +23,3 @@ import Node from "./ast/Node"; | ||
import TextSuggester from "./TextSuggester"; | ||
export { Node, CompositeNode, ValueNode, Cursor, RegexValue, AndValue, AnyOfThese, Literal, NotValue, OptionalValue, OrValue, RepeatValue, ValuePattern, AndComposite, CompositePattern, OptionalComposite, OrComposite, RepeatComposite, ParseError, Pattern, RecursivePattern, TextSuggester, }; | ||
import NodeVisitor from "./ast/NodeVisitor"; | ||
export { Node, CompositeNode, ValueNode, Cursor, RegexValue, AndValue, AnyOfThese, Literal, NotValue, OptionalValue, OrValue, RepeatValue, ValuePattern, AndComposite, CompositePattern, OptionalComposite, OrComposite, RepeatComposite, ParseError, Pattern, RecursivePattern, TextSuggester, NodeVisitor, }; |
@@ -1512,3 +1512,190 @@ class Node { | ||
export { AndComposite, AndValue, AnyOfThese, CompositeNode, CompositePattern, Cursor, Literal, Node, NotValue, OptionalComposite, OptionalValue, OrComposite, OrValue, ParseError, Pattern, RecursivePattern, RegexValue, RepeatComposite, RepeatValue, TextSuggester, ValueNode, ValuePattern }; | ||
class NodeVisitor { | ||
constructor(context, selectedNodes = []) { | ||
this.context = context; | ||
this.selectedNodes = selectedNodes; | ||
} | ||
flatten() { | ||
this.selectedNodes.forEach((node) => { | ||
if (node.isComposite) { | ||
const children = []; | ||
this.walkUp(node, (descendant) => { | ||
if (!descendant.isComposite) { | ||
children.push(descendant); | ||
} | ||
}); | ||
node.children = children; | ||
} | ||
}); | ||
return this; | ||
} | ||
remove() { | ||
this.recursiveRemove(this.context); | ||
return this; | ||
} | ||
recursiveRemove(node) { | ||
const nodesToRemove = this.selectedNodes; | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
for (let x = 0; x < node.children.length; x++) { | ||
if (nodesToRemove.indexOf(node.children[x]) > -1) { | ||
node.children.splice(x, 1); | ||
x--; | ||
} | ||
else { | ||
this.recursiveRemove(node.children[x]); | ||
} | ||
} | ||
} | ||
} | ||
wrap(callback) { | ||
const visitor = new NodeVisitor(this.context); | ||
visitor.selectRoot().transform((node) => { | ||
if (this.selectedNodes.includes(node)) { | ||
return callback(node); | ||
} | ||
return node; | ||
}); | ||
return this; | ||
} | ||
unwrap() { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
const grandParent = stack[stack.length - 2]; | ||
if (parent != null && grandParent != null) { | ||
const index = grandParent.children.indexOf(parent); | ||
if (index > -1) { | ||
grandParent.children.splice(index, 1, ...parent.children); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
prepend(callback) { | ||
this.walkUp(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
append(callback) { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index + 1, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
transform(callback) { | ||
this.selectedNodes.forEach((node) => { | ||
return this.recursiveTransform(node, callback); | ||
}); | ||
return this; | ||
} | ||
recursiveTransform(node, callback) { | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const length = node.children.length; | ||
for (let x = 0; x < length; x++) { | ||
node.children[x] = this.recursiveTransform(node.children[x], callback); | ||
} | ||
} | ||
return callback(node); | ||
} | ||
walkUp(node, callback, ancestors = []) { | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkUp(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
callback(node, ancestors); | ||
return this; | ||
} | ||
walkDown(node, callback, ancestors = []) { | ||
callback(node, ancestors); | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkDown(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
return this; | ||
} | ||
selectAll() { | ||
return this.select((n) => true); | ||
} | ||
selectNode(node) { | ||
return new NodeVisitor(this.context, [...this.selectedNodes, node]); | ||
} | ||
deselectNode(node) { | ||
const visitor = new NodeVisitor(this.context, this.selectedNodes.slice()); | ||
return visitor.filter((n) => n !== node); | ||
} | ||
select(callback) { | ||
const node = this.context; | ||
const selectedNodes = []; | ||
if (node.isComposite) { | ||
this.walkDown(node, (descendant) => { | ||
if (callback(descendant)) { | ||
selectedNodes.push(descendant); | ||
} | ||
}); | ||
} | ||
return new NodeVisitor(this.context, selectedNodes); | ||
} | ||
forEach(callback) { | ||
this.selectedNodes.forEach(callback); | ||
return this; | ||
} | ||
filter(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.filter(callback)); | ||
} | ||
map(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.map(callback)); | ||
} | ||
selectRoot() { | ||
return new NodeVisitor(this.context, [this.context]); | ||
} | ||
first() { | ||
return this.get(0); | ||
} | ||
last() { | ||
return this.get(this.selectedNodes.length - 1); | ||
} | ||
get(index) { | ||
const node = this.selectedNodes[index]; | ||
if (node == null) { | ||
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`); | ||
} | ||
return new NodeVisitor(node, []); | ||
} | ||
clear() { | ||
this.selectedNodes = []; | ||
return this; | ||
} | ||
static select(context, callback) { | ||
if (callback != null) { | ||
return new NodeVisitor(context).select(callback); | ||
} | ||
else { | ||
return new NodeVisitor(context); | ||
} | ||
} | ||
} | ||
export { AndComposite, AndValue, AnyOfThese, CompositeNode, CompositePattern, Cursor, Literal, Node, NodeVisitor, NotValue, OptionalComposite, OptionalValue, OrComposite, OrValue, ParseError, Pattern, RecursivePattern, RegexValue, RepeatComposite, RepeatValue, TextSuggester, ValueNode, ValuePattern }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1516,2 +1516,189 @@ 'use strict'; | ||
class NodeVisitor { | ||
constructor(context, selectedNodes = []) { | ||
this.context = context; | ||
this.selectedNodes = selectedNodes; | ||
} | ||
flatten() { | ||
this.selectedNodes.forEach((node) => { | ||
if (node.isComposite) { | ||
const children = []; | ||
this.walkUp(node, (descendant) => { | ||
if (!descendant.isComposite) { | ||
children.push(descendant); | ||
} | ||
}); | ||
node.children = children; | ||
} | ||
}); | ||
return this; | ||
} | ||
remove() { | ||
this.recursiveRemove(this.context); | ||
return this; | ||
} | ||
recursiveRemove(node) { | ||
const nodesToRemove = this.selectedNodes; | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
for (let x = 0; x < node.children.length; x++) { | ||
if (nodesToRemove.indexOf(node.children[x]) > -1) { | ||
node.children.splice(x, 1); | ||
x--; | ||
} | ||
else { | ||
this.recursiveRemove(node.children[x]); | ||
} | ||
} | ||
} | ||
} | ||
wrap(callback) { | ||
const visitor = new NodeVisitor(this.context); | ||
visitor.selectRoot().transform((node) => { | ||
if (this.selectedNodes.includes(node)) { | ||
return callback(node); | ||
} | ||
return node; | ||
}); | ||
return this; | ||
} | ||
unwrap() { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
const grandParent = stack[stack.length - 2]; | ||
if (parent != null && grandParent != null) { | ||
const index = grandParent.children.indexOf(parent); | ||
if (index > -1) { | ||
grandParent.children.splice(index, 1, ...parent.children); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
prepend(callback) { | ||
this.walkUp(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
append(callback) { | ||
this.walkDown(this.context, (node, stack) => { | ||
if (this.selectedNodes.includes(node)) { | ||
const parent = stack[stack.length - 1]; | ||
if (parent != null) { | ||
const index = parent.children.indexOf(node); | ||
if (index > -1) { | ||
parent.children.splice(index + 1, 0, callback(node)); | ||
} | ||
} | ||
} | ||
}); | ||
return this; | ||
} | ||
transform(callback) { | ||
this.selectedNodes.forEach((node) => { | ||
return this.recursiveTransform(node, callback); | ||
}); | ||
return this; | ||
} | ||
recursiveTransform(node, callback) { | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const length = node.children.length; | ||
for (let x = 0; x < length; x++) { | ||
node.children[x] = this.recursiveTransform(node.children[x], callback); | ||
} | ||
} | ||
return callback(node); | ||
} | ||
walkUp(node, callback, ancestors = []) { | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkUp(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
callback(node, ancestors); | ||
return this; | ||
} | ||
walkDown(node, callback, ancestors = []) { | ||
callback(node, ancestors); | ||
ancestors.push(node); | ||
if (node.isComposite && Array.isArray(node.children)) { | ||
const children = node.children.slice(); | ||
children.forEach((c) => this.walkDown(c, callback, ancestors)); | ||
} | ||
ancestors.pop(); | ||
return this; | ||
} | ||
selectAll() { | ||
return this.select((n) => true); | ||
} | ||
selectNode(node) { | ||
return new NodeVisitor(this.context, [...this.selectedNodes, node]); | ||
} | ||
deselectNode(node) { | ||
const visitor = new NodeVisitor(this.context, this.selectedNodes.slice()); | ||
return visitor.filter((n) => n !== node); | ||
} | ||
select(callback) { | ||
const node = this.context; | ||
const selectedNodes = []; | ||
if (node.isComposite) { | ||
this.walkDown(node, (descendant) => { | ||
if (callback(descendant)) { | ||
selectedNodes.push(descendant); | ||
} | ||
}); | ||
} | ||
return new NodeVisitor(this.context, selectedNodes); | ||
} | ||
forEach(callback) { | ||
this.selectedNodes.forEach(callback); | ||
return this; | ||
} | ||
filter(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.filter(callback)); | ||
} | ||
map(callback) { | ||
return new NodeVisitor(this.context, this.selectedNodes.map(callback)); | ||
} | ||
selectRoot() { | ||
return new NodeVisitor(this.context, [this.context]); | ||
} | ||
first() { | ||
return this.get(0); | ||
} | ||
last() { | ||
return this.get(this.selectedNodes.length - 1); | ||
} | ||
get(index) { | ||
const node = this.selectedNodes[index]; | ||
if (node == null) { | ||
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`); | ||
} | ||
return new NodeVisitor(node, []); | ||
} | ||
clear() { | ||
this.selectedNodes = []; | ||
return this; | ||
} | ||
static select(context, callback) { | ||
if (callback != null) { | ||
return new NodeVisitor(context).select(callback); | ||
} | ||
else { | ||
return new NodeVisitor(context); | ||
} | ||
} | ||
} | ||
exports.AndComposite = AndComposite; | ||
@@ -1525,2 +1712,3 @@ exports.AndValue = AndValue; | ||
exports.Node = Node; | ||
exports.NodeVisitor = NodeVisitor; | ||
exports.NotValue = NotValue; | ||
@@ -1527,0 +1715,0 @@ exports.OptionalComposite = OptionalComposite; |
{ | ||
"name": "clarity-pattern-parser", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "Parsing Library for Typescript and Javascript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -23,2 +23,3 @@ import Node from "./ast/Node"; | ||
import TextSuggester from "./TextSuggester"; | ||
import NodeVisitor from "./ast/NodeVisitor"; | ||
@@ -48,2 +49,3 @@ export { | ||
TextSuggester, | ||
NodeVisitor, | ||
}; |
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
687742
113
10312