clarity-pattern-parser
Advanced tools
Comparing version 11.0.3 to 11.0.4
@@ -14,5 +14,5 @@ import { Node } from "../ast/Node"; | ||
private _atomNode; | ||
private _orphanedAtom; | ||
private _precedenceMap; | ||
private _associationMap; | ||
private _revertBinary; | ||
constructor(precedenceMap?: Record<string, number>, associationMap?: Record<string, Association>); | ||
@@ -19,0 +19,0 @@ addPrefix(name: string, ...prefix: Node[]): void; |
{ | ||
"name": "clarity-pattern-parser", | ||
"version": "11.0.3", | ||
"version": "11.0.4", | ||
"description": "Parsing Library for Typescript and Javascript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -1,2 +0,2 @@ | ||
import { PrecedenceTree } from "./PrecedenceTree"; | ||
import { Association, PrecedenceTree } from "./PrecedenceTree"; | ||
import { Node } from "../ast/Node"; | ||
@@ -162,2 +162,114 @@ | ||
}); | ||
test("add Partial Binary With Lower Precedence", () => { | ||
const tree = new PrecedenceTree({ | ||
mul: 0, | ||
add: 1, | ||
bool: 2 | ||
}, {}); | ||
tree.addAtom(Node.createValueNode("literal", "a", "a")); | ||
tree.addBinary("add", Node.createValueNode("literal", "+", "+")); | ||
tree.addAtom(Node.createValueNode("literal", "b", "b")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
tree.addAtom(Node.createValueNode("literal", "c", "c")); | ||
tree.addBinary("bool", Node.createValueNode("literal", "||", "||")); | ||
const result = tree.commit(); | ||
const expected = Node.createNode("expression", "add", [ | ||
Node.createValueNode("literal", "a", "a"), | ||
Node.createValueNode("literal", "+", "+"), | ||
Node.createNode("expression", "mul", [ | ||
Node.createValueNode("literal", "b", "b"), | ||
Node.createValueNode("literal", "*", "*"), | ||
Node.createValueNode("literal", "c", "c"), | ||
]), | ||
]); | ||
expect(result?.toString()).toBe("a+b*c"); | ||
expect(result?.toCycleFreeObject()).toEqual(expected.toCycleFreeObject()); | ||
}); | ||
test("add Partial Binary With Equal Precedence", () => { | ||
const tree = new PrecedenceTree({ | ||
mul: 0, | ||
add: 1, | ||
bool: 2 | ||
}, {}); | ||
tree.addAtom(Node.createValueNode("literal", "a", "a")); | ||
tree.addBinary("add", Node.createValueNode("literal", "+", "+")); | ||
tree.addAtom(Node.createValueNode("literal", "b", "b")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
tree.addAtom(Node.createValueNode("literal", "c", "c")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
const result = tree.commit(); | ||
const expected = Node.createNode("expression", "add", [ | ||
Node.createValueNode("literal", "a", "a"), | ||
Node.createValueNode("literal", "+", "+"), | ||
Node.createNode("expression", "mul", [ | ||
Node.createValueNode("literal", "b", "b"), | ||
Node.createValueNode("literal", "*", "*"), | ||
Node.createValueNode("literal", "c", "c"), | ||
]), | ||
]); | ||
expect(result?.toString()).toBe("a+b*c"); | ||
expect(result?.toCycleFreeObject()).toEqual(expected.toCycleFreeObject()); | ||
}); | ||
test("add Partial Binary With Equal Precedence And Right Associated", () => { | ||
const tree = new PrecedenceTree({ | ||
mul: 0, | ||
add: 1, | ||
bool: 2 | ||
}, { mul: Association.right }); | ||
tree.addAtom(Node.createValueNode("literal", "a", "a")); | ||
tree.addBinary("add", Node.createValueNode("literal", "+", "+")); | ||
tree.addAtom(Node.createValueNode("literal", "b", "b")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
tree.addAtom(Node.createValueNode("literal", "c", "c")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
const result = tree.commit(); | ||
const expected = Node.createNode("expression", "add", [ | ||
Node.createValueNode("literal", "a", "a"), | ||
Node.createValueNode("literal", "+", "+"), | ||
Node.createNode("expression", "mul", [ | ||
Node.createValueNode("literal", "b", "b"), | ||
Node.createValueNode("literal", "*", "*"), | ||
Node.createValueNode("literal", "c", "c"), | ||
]), | ||
]); | ||
expect(result?.toString()).toBe("a+b*c"); | ||
expect(result?.toCycleFreeObject()).toEqual(expected.toCycleFreeObject()); | ||
}); | ||
test("add Partial Binary With Greater Precedence", () => { | ||
const tree = new PrecedenceTree({ | ||
mul: 0, | ||
add: 1, | ||
bool: 2 | ||
}, {}); | ||
tree.addAtom(Node.createValueNode("literal", "a", "a")); | ||
tree.addBinary("add", Node.createValueNode("literal", "+", "+")); | ||
tree.addAtom(Node.createValueNode("literal", "b", "b")); | ||
tree.addBinary("mul", Node.createValueNode("literal", "*", "*")); | ||
const result = tree.commit(); | ||
const expected = Node.createNode("expression", "add", [ | ||
Node.createValueNode("literal", "a", "a"), | ||
Node.createValueNode("literal", "+", "+"), | ||
Node.createValueNode("literal", "b", "b"), | ||
]); | ||
expect(result?.toString()).toBe("a+b"); | ||
expect(result?.toCycleFreeObject()).toEqual(expected.toCycleFreeObject()); | ||
}); | ||
}); |
@@ -16,5 +16,5 @@ import { Node } from "../ast/Node"; | ||
private _atomNode: Node | null; | ||
private _orphanedAtom: Node | null; | ||
private _precedenceMap: Record<string, number>; | ||
private _associationMap: Record<string, Association>; | ||
private _revertBinary: () => void; | ||
@@ -29,5 +29,5 @@ constructor(precedenceMap: Record<string, number> = {}, associationMap: Record<string, Association> = {}) { | ||
this._binaryNode = null; | ||
this._orphanedAtom = null; | ||
this._precedenceMap = precedenceMap; | ||
this._associationMap = associationMap; | ||
this._revertBinary = () => { }; | ||
} | ||
@@ -77,3 +77,2 @@ | ||
this._binaryPlaceholder.remove(); | ||
this._orphanedAtom = lastAtomNode; | ||
@@ -84,2 +83,8 @@ if (lastBinaryNode == null) { | ||
this._binaryNode = node; | ||
this._revertBinary = () => { | ||
lastAtomNode.remove(); | ||
this._binaryNode = lastAtomNode; | ||
}; | ||
return; | ||
@@ -93,2 +98,7 @@ } | ||
this._revertBinary = () => { | ||
node.replaceWith(lastAtomNode); | ||
this._binaryNode = lastBinaryNode; | ||
}; | ||
this._binaryNode = node; | ||
@@ -102,2 +112,9 @@ } else if (precedence === lastPrecendece) { | ||
node.append(lastBinaryNode, ...delimiterNode, this._binaryPlaceholder); | ||
this._revertBinary = () => { | ||
lastBinaryNode.remove(); | ||
node.replaceWith(lastBinaryNode); | ||
this._binaryNode = lastBinaryNode; | ||
}; | ||
this._binaryNode = node; | ||
@@ -124,2 +141,8 @@ } else if (precedence > lastPrecendece) { | ||
this._revertBinary = () => { | ||
root.remove(); | ||
node.replaceWith(root); | ||
this._binaryNode = root; | ||
}; | ||
this._binaryNode = node; | ||
@@ -132,2 +155,8 @@ | ||
this._revertBinary = () => { | ||
lastAtomNode.remove(); | ||
node.replaceWith(lastAtomNode); | ||
this._binaryNode = lastBinaryNode; | ||
}; | ||
this._binaryNode = node; | ||
@@ -182,3 +211,3 @@ } | ||
hasAtom(){ | ||
hasAtom() { | ||
return this._atomNode != null; | ||
@@ -195,4 +224,4 @@ } | ||
if (atomNode == null) { | ||
let root = this._binaryPlaceholder.findRoot(); | ||
this._binaryPlaceholder.parent?.replaceWith(this._orphanedAtom as Node); | ||
this._revertBinary(); | ||
let root = this._binaryNode.findRoot(); | ||
this.reset(); | ||
@@ -212,3 +241,2 @@ return root; | ||
this._atomNode = null; | ||
this._orphanedAtom = null; | ||
this._postfixNode = null; | ||
@@ -215,0 +243,0 @@ this._binaryNode = null; |
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 too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1291645
22357