clarity-pattern-parser
Advanced tools
Comparing version
@@ -13,3 +13,5 @@ import { Node } from "../ast/Node"; | ||
private _patterns; | ||
private _unaryPatterns; | ||
private _atomPatterns; | ||
private _unaryPrefixPatterns; | ||
private _unaryPrefixNames; | ||
private _binaryPatterns; | ||
@@ -40,2 +42,5 @@ private _recursivePatterns; | ||
private _extractName; | ||
private _isUnary; | ||
private _isUnaryPattern; | ||
private _extractUnaryPrefixPattern; | ||
private _isRecursive; | ||
@@ -42,0 +47,0 @@ private _isRecursivePattern; |
{ | ||
"name": "clarity-pattern-parser", | ||
"version": "10.3.2", | ||
"version": "10.3.3", | ||
"description": "Parsing Library for Typescript and Javascript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -27,3 +27,5 @@ import { Node } from "../ast/Node"; | ||
private _patterns: Pattern[]; | ||
private _unaryPatterns: Pattern[]; | ||
private _atomPatterns: Pattern[]; | ||
private _unaryPrefixPatterns: Pattern[]; | ||
private _unaryPrefixNames: string[]; | ||
private _binaryPatterns: Pattern[]; | ||
@@ -65,3 +67,3 @@ private _recursivePatterns: Pattern[]; | ||
get unaryPatterns(): readonly Pattern[] { | ||
return this._unaryPatterns; | ||
return this._atomPatterns; | ||
} | ||
@@ -91,3 +93,5 @@ | ||
this._firstIndex = -1; | ||
this._unaryPatterns = []; | ||
this._atomPatterns = []; | ||
this._unaryPrefixPatterns = []; | ||
this._unaryPrefixNames = []; | ||
this._binaryPatterns = []; | ||
@@ -104,3 +108,3 @@ this._recursivePatterns = []; | ||
if (this._unaryPatterns.length === 0) { | ||
if (this._atomPatterns.length === 0) { | ||
throw new Error("Need at least one operand pattern with an 'expression' pattern."); | ||
@@ -115,3 +119,9 @@ } | ||
if (this._isBinary(pattern)) { | ||
if (this._isUnary(pattern)) { | ||
const unaryPrefix = this._extractUnaryPrefixPattern(pattern).clone(); | ||
this._unaryPrefixPatterns.push(pattern); | ||
this._unaryPrefixNames.push(pattern.name); | ||
finalPatterns.push(unaryPrefix); | ||
} else if (this._isBinary(pattern)) { | ||
const binaryName = this._extractName(pattern); | ||
@@ -146,3 +156,3 @@ const clone = this._extractDelimiter(pattern).clone(); | ||
this._unaryPatterns.push(clone); | ||
this._atomPatterns.push(clone); | ||
finalPatterns.push(clone); | ||
@@ -187,2 +197,27 @@ } | ||
private _isUnary(pattern: Pattern) { | ||
if (pattern.type === "right-associated" && this._isUnaryPattern(pattern.children[0])) { | ||
return true; | ||
} | ||
return this._isUnaryPattern(pattern); | ||
} | ||
private _isUnaryPattern(pattern: Pattern) { | ||
return pattern.type === "sequence" && | ||
pattern.children[0].type !== "reference" && | ||
pattern.children[0].name !== this.name && | ||
pattern.children[1].type === "reference" && | ||
pattern.children[1].name === this.name && | ||
pattern.children.length === 2; | ||
} | ||
private _extractUnaryPrefixPattern(pattern: Pattern) { | ||
if (pattern.type === "right-associated") { | ||
return pattern.children[0].children[0]; | ||
} | ||
return pattern.children[0]; | ||
} | ||
private _isRecursive(pattern: Pattern) { | ||
@@ -200,3 +235,3 @@ if (pattern.type === "right-associated" && this._isRecursivePattern(pattern.children[0])) { | ||
pattern.children[0].name === this.name && | ||
pattern.children.length > 1; | ||
pattern.children.length > 2; | ||
} | ||
@@ -266,3 +301,3 @@ | ||
let lastUnaryNode: Node | null = null; | ||
let lastAtomNode: Node | null = null; | ||
let lastBinaryNode: Node | null = null; | ||
@@ -275,14 +310,37 @@ let onIndex = cursor.index; | ||
for (let i = 0; i < this._unaryPatterns.length; i++) { | ||
let prefix: Node | null = null; | ||
let prefixName = ""; | ||
for (let i = 0; i < this._unaryPrefixPatterns.length; i++) { | ||
cursor.moveTo(onIndex); | ||
const pattern = this._unaryPrefixPatterns[i]; | ||
const node = pattern.parse(cursor); | ||
const pattern = this._unaryPatterns[i]; | ||
if (node != null) { | ||
prefix = node; | ||
prefixName = this._unaryPrefixNames[i]; | ||
if (cursor.hasNext()) { | ||
cursor.next(); | ||
} else { | ||
break outer; | ||
} | ||
break; | ||
} else { | ||
cursor.resolveError(); | ||
} | ||
} | ||
onIndex = cursor.index; | ||
for (let i = 0; i < this._atomPatterns.length; i++) { | ||
cursor.moveTo(onIndex); | ||
const pattern = this._atomPatterns[i]; | ||
const node = pattern.parse(cursor); | ||
if (node != null) { | ||
lastUnaryNode = node; | ||
lastAtomNode = node; | ||
break; | ||
} else { | ||
lastUnaryNode = null; | ||
lastAtomNode = null; | ||
cursor.resolveError(); | ||
@@ -292,3 +350,3 @@ } | ||
if (lastUnaryNode == null) { | ||
if (lastAtomNode == null) { | ||
break; | ||
@@ -300,4 +358,7 @@ } | ||
} else { | ||
if (lastBinaryNode != null && lastUnaryNode != null) { | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
if (lastBinaryNode != null && lastAtomNode != null) { | ||
if (prefix != null) { | ||
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]); | ||
} | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
} | ||
@@ -317,7 +378,10 @@ break; | ||
if (this._endsInRecursion[i]) { | ||
if (lastBinaryNode != null && lastUnaryNode != null) { | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
if (lastBinaryNode != null && lastAtomNode != null) { | ||
if (prefix != null) { | ||
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]); | ||
} | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
} | ||
const frontExpression = lastBinaryNode == null ? lastUnaryNode as Node : lastBinaryNode.findRoot(); | ||
const frontExpression = lastBinaryNode == null ? lastAtomNode as Node : lastBinaryNode.findRoot(); | ||
const recursiveNode = createNode(name, [frontExpression, ...node.children]); | ||
@@ -329,11 +393,15 @@ | ||
} else { | ||
const recursiveNode = createNode(name, [lastUnaryNode, ...node.children]); | ||
recursiveNode.normalize(lastUnaryNode.startIndex); | ||
lastUnaryNode = recursiveNode; | ||
if (prefix != null) { | ||
lastAtomNode = createNode(prefixName, [prefix, lastAtomNode]); | ||
} | ||
const recursiveNode = createNode(name, [lastAtomNode, ...node.children]); | ||
recursiveNode.normalize(lastAtomNode.startIndex); | ||
lastAtomNode = recursiveNode; | ||
if (cursor.hasNext()) { | ||
cursor.next(); | ||
} else { | ||
if (lastBinaryNode != null) { | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
if (lastBinaryNode != null && lastAtomNode != null) { | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
} | ||
@@ -364,5 +432,5 @@ break outer; | ||
if (lastBinaryNode == null) { | ||
return lastUnaryNode; | ||
} else if (lastUnaryNode != null) { | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
return lastAtomNode; | ||
} else if (lastAtomNode != null) { | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
} | ||
@@ -373,6 +441,6 @@ } | ||
if (lastBinaryNode == null && lastUnaryNode != null && delimiterNode != null) { | ||
const node = createNode(name, [lastUnaryNode, delimiterNode]); | ||
if (lastBinaryNode == null && lastAtomNode != null && delimiterNode != null) { | ||
const node = createNode(name, [lastAtomNode, delimiterNode]); | ||
lastBinaryNode = node; | ||
} else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) { | ||
} else if (lastBinaryNode != null && lastAtomNode != null && delimiterNode != null) { | ||
const precedence = this._precedenceMap[name]; | ||
@@ -383,3 +451,3 @@ const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] == null ? -1 : this._precedenceMap[lastBinaryNode.name]; | ||
if (precedence === lastPrecendece && association === Association.right) { | ||
const node = createNode(name, [lastUnaryNode, delimiterNode]); | ||
const node = createNode(name, [lastAtomNode, delimiterNode]); | ||
lastBinaryNode.appendChild(node); | ||
@@ -392,3 +460,3 @@ | ||
lastBinaryNode.replaceWith(node); | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
@@ -411,3 +479,3 @@ node.append(lastBinaryNode, delimiterNode); | ||
lastBinaryNode.appendChild(lastUnaryNode); | ||
lastBinaryNode.appendChild(lastAtomNode); | ||
@@ -421,3 +489,3 @@ if (root != null) { | ||
} else { | ||
const node = createNode(name, [lastUnaryNode, delimiterNode]); | ||
const node = createNode(name, [lastAtomNode, delimiterNode]); | ||
lastBinaryNode = node; | ||
@@ -427,3 +495,3 @@ } | ||
} else { | ||
const node = createNode(name, [lastUnaryNode, delimiterNode]); | ||
const node = createNode(name, [lastAtomNode, delimiterNode]); | ||
lastBinaryNode.appendChild(node); | ||
@@ -451,3 +519,3 @@ | ||
if (lastBinaryNode == null) { | ||
return lastUnaryNode; | ||
return lastAtomNode; | ||
} else { | ||
@@ -459,3 +527,3 @@ const root = lastBinaryNode.findAncestor(n => n.parent == null) as Node || lastBinaryNode; | ||
if (lastBinaryNode === root) { | ||
return lastUnaryNode; | ||
return lastAtomNode; | ||
} | ||
@@ -523,3 +591,3 @@ } | ||
if (this.binaryPatterns.indexOf(childReference)) { | ||
const unaryTokens = this._unaryPatterns.map(p => p.getTokens()).flat(); | ||
const unaryTokens = this._atomPatterns.map(p => p.getTokens()).flat(); | ||
@@ -562,3 +630,3 @@ if (this._parent != null) { | ||
if (this.binaryPatterns.indexOf(childReference)) { | ||
const unaryPatterns = this._unaryPatterns.map(p => p.getPatterns()).flat(); | ||
const unaryPatterns = this._atomPatterns.map(p => p.getPatterns()).flat(); | ||
@@ -565,0 +633,0 @@ if (this._parent != 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
1285165
1.34%21964
1.1%