clarity-pattern-parser
Advanced tools
Comparing version 11.0.6 to 11.0.7
@@ -19,2 +19,3 @@ export interface CycleFreeNode { | ||
get name(): string; | ||
get value(): string; | ||
get firstIndex(): number; | ||
@@ -28,3 +29,2 @@ get lastIndex(): number; | ||
get isLeaf(): boolean; | ||
get value(): string; | ||
constructor(type: string, name: string, firstIndex: number, lastIndex: number, children?: Node[], value?: string); | ||
@@ -43,4 +43,4 @@ removeChild(node: Node): void; | ||
find(predicate: (node: Node) => boolean): Node | null; | ||
findAll(predicate: (node: Node) => boolean): Node[]; | ||
findRoot(): Node; | ||
findAll(predicate: (node: Node) => boolean): Node[]; | ||
findAncestor(predicate: (node: Node) => boolean): Node | null; | ||
@@ -52,7 +52,6 @@ walkUp(callback: (node: Node) => void): void; | ||
flatten(): Node[]; | ||
reduce(): void; | ||
compact(): void; | ||
remove(): void; | ||
clone(): Node; | ||
normalize(startIndex?: number): number; | ||
compact(): void; | ||
toString(): string; | ||
@@ -59,0 +58,0 @@ toCycleFreeObject(): CycleFreeNode; |
@@ -20,4 +20,4 @@ import { Pattern } from "../patterns/Pattern"; | ||
parse(expression: string): Promise<Record<string, Pattern>>; | ||
parseString(expression: string): Record<string, Pattern>; | ||
private _buildPatternRecord; | ||
parseString(expression: string): Record<string, Pattern>; | ||
private _tryToParse; | ||
@@ -24,0 +24,0 @@ private _hasImports; |
@@ -24,3 +24,4 @@ import { Node } from "./ast/Node"; | ||
import { Context } from "./patterns/Context"; | ||
import { ExpressionPattern } from "./patterns/ExpressionPattern"; | ||
export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Context, ExpressionPattern, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, patterns, compact, remove }; | ||
import { Expression } from "./patterns/Expression"; | ||
import { RightAssociated } from "./patterns/RightAssociated"; | ||
export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Context, Expression, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, RightAssociated, Regex, Repeat, grammar, patterns, compact, remove }; |
@@ -34,3 +34,3 @@ import { Node } from "../ast/Node"; | ||
recordMatch(pattern: Pattern, node: Node): void; | ||
recordErrorAt(startIndex: number, endIndex: number, pattern: Pattern): void; | ||
recordErrorAt(startIndex: number, lastIndex: number, pattern: Pattern): void; | ||
startRecording(): void; | ||
@@ -37,0 +37,0 @@ stopRecording(): void; |
@@ -8,5 +8,5 @@ import { Cursor } from "./Cursor"; | ||
name: string; | ||
startedOnIndex: number; | ||
parent: Pattern | null; | ||
children: Pattern[]; | ||
startedOnIndex: number; | ||
parse(cursor: Cursor): Node | null; | ||
@@ -13,0 +13,0 @@ exec(text: string, record?: boolean): ParseResult; |
{ | ||
"name": "clarity-pattern-parser", | ||
"version": "11.0.6", | ||
"version": "11.0.7", | ||
"description": "Parsing Library for Typescript and Javascript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -332,3 +332,3 @@ import { Node } from "./Node"; | ||
test("Reduce", () => { | ||
test("Compact", () => { | ||
const parent = new Node("parent", "parent", 0, 6, [ | ||
@@ -338,3 +338,3 @@ new Node("child", "child", 0, 6, undefined, "Content") | ||
parent.reduce(); | ||
parent.compact(); | ||
@@ -341,0 +341,0 @@ expect(parent.hasChildren).toBeFalsy(); |
@@ -31,2 +31,6 @@ function defaultVisitor(node: Node) { | ||
get value() { | ||
return this.toString(); | ||
} | ||
get firstIndex() { | ||
@@ -64,6 +68,2 @@ return this._firstIndex; | ||
get value() { | ||
return this.toString(); | ||
} | ||
constructor( | ||
@@ -190,2 +190,12 @@ type: string, | ||
findAll(predicate: (node: Node) => boolean): Node[] { | ||
const matches: Node[] = []; | ||
this.walkUp(n => { | ||
if (predicate(n)) { matches.push(n); } | ||
}); | ||
return matches; | ||
} | ||
findRoot() { | ||
@@ -202,12 +212,2 @@ let pattern: Node | null = this; | ||
findAll(predicate: (node: Node) => boolean): Node[] { | ||
const matches: Node[] = []; | ||
this.walkUp(n => { | ||
if (predicate(n)) { matches.push(n); } | ||
}); | ||
return matches; | ||
} | ||
findAncestor(predicate: (node: Node) => boolean) { | ||
@@ -274,3 +274,3 @@ let parent = this._parent; | ||
reduce() { | ||
compact() { | ||
const value = this.toString(); | ||
@@ -318,7 +318,2 @@ this.removeAllChildren(); | ||
compact() { | ||
this._value = this.toString(); | ||
this._children.length = 0; | ||
} | ||
toString(): string { | ||
@@ -325,0 +320,0 @@ if (this._children.length === 0) { |
@@ -14,4 +14,4 @@ import { Node } from "../ast/Node"; | ||
import { Context } from "../patterns/Context"; | ||
import { ExpressionPattern } from "../patterns/ExpressionPattern"; | ||
import { RightAssociatedPattern } from "../patterns/RightAssociatedPattern"; | ||
import { Expression } from "../patterns/Expression"; | ||
import { RightAssociated } from "../patterns/RightAssociated"; | ||
@@ -92,13 +92,2 @@ let anonymousIndexId = 0; | ||
private _buildPatternRecord() { | ||
const patterns: Record<string, Pattern> = {}; | ||
const allPatterns = Array.from(this._parseContext.patternsByName.values()); | ||
allPatterns.forEach(p => { | ||
patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p)); | ||
}); | ||
return patterns; | ||
} | ||
parseString(expression: string) { | ||
@@ -117,2 +106,13 @@ this._parseContext = new ParseContext(this._params); | ||
private _buildPatternRecord() { | ||
const patterns: Record<string, Pattern> = {}; | ||
const allPatterns = Array.from(this._parseContext.patternsByName.values()); | ||
allPatterns.forEach(p => { | ||
patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p)); | ||
}); | ||
return patterns; | ||
} | ||
private _tryToParse(expression: string): Node { | ||
@@ -258,3 +258,3 @@ const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression); | ||
if (rightAssociated != null) { | ||
return new RightAssociatedPattern(this._buildPattern(n.children[0])); | ||
return new RightAssociated(this._buildPattern(n.children[0])); | ||
} else { | ||
@@ -265,7 +265,7 @@ return this._buildPattern(n.children[0]); | ||
}); | ||
const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p)); | ||
if (hasRecursivePattern && !isGreedy) { | ||
try { | ||
const expression = new ExpressionPattern(name, patterns); | ||
const expression = new Expression(name, patterns); | ||
return expression; | ||
@@ -275,4 +275,4 @@ } catch { } | ||
const or = new Options(name, patterns, isGreedy); | ||
return or; | ||
const options = new Options(name, patterns, isGreedy); | ||
return options; | ||
} | ||
@@ -289,3 +289,3 @@ | ||
private _isRecursivePattern(name: string, pattern: Pattern) { | ||
if (pattern.children.length === 0){ | ||
if (pattern.children.length === 0) { | ||
return false; | ||
@@ -343,3 +343,3 @@ } | ||
private _buildSequence(name: string, node: Node) { | ||
const patternNodes = node.children.filter(n => n.name !== "and-divider"); | ||
const patternNodes = node.children.filter(n => n.name !== "sequence-divider"); | ||
@@ -346,0 +346,0 @@ const patterns = patternNodes.map(n => { |
@@ -13,5 +13,5 @@ import { Repeat } from "../../patterns/Repeat"; | ||
const patternName = name.clone("pattern-name"); | ||
const patterns = new Options("and-patterns", [patternName, anonymousPattern]); | ||
const patterns = new Options("sequence-patterns", [patternName, anonymousPattern]); | ||
export const pattern = new Sequence("and-child-pattern", [ | ||
export const pattern = new Sequence("sequence-child-pattern", [ | ||
optionalNot, | ||
@@ -22,5 +22,5 @@ patterns, | ||
const divider = new Regex("and-divider", "\\s*[+]\\s*"); | ||
const divider = new Regex("sequence-divider", "\\s*[+]\\s*"); | ||
divider.setTokens([" + "]); | ||
export const sequenceLiteral = new Repeat("sequence-literal", pattern, { divider, min: 2, trimDivider: true }); |
@@ -24,3 +24,4 @@ import { Node } from "./ast/Node"; | ||
import { Context } from "./patterns/Context"; | ||
import { ExpressionPattern } from "./patterns/ExpressionPattern"; | ||
import { Expression } from "./patterns/Expression"; | ||
import { RightAssociated } from "./patterns/RightAssociated"; | ||
@@ -39,3 +40,3 @@ export { | ||
Context, | ||
ExpressionPattern, | ||
Expression, | ||
Literal, | ||
@@ -49,2 +50,3 @@ Not, | ||
Reference, | ||
RightAssociated, | ||
Regex, | ||
@@ -51,0 +53,0 @@ Repeat, |
@@ -115,7 +115,7 @@ import { Node } from "../ast/Node"; | ||
recordErrorAt(startIndex: number, endIndex: number, pattern: Pattern): void { | ||
const error = new ParseError(startIndex, endIndex, pattern); | ||
recordErrorAt(startIndex: number, lastIndex: number, pattern: Pattern): void { | ||
const error = new ParseError(startIndex, lastIndex, pattern); | ||
this._currentError = error; | ||
if (this._furthestError === null || endIndex > this._furthestError.lastIndex) { | ||
if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) { | ||
this._furthestError = error; | ||
@@ -122,0 +122,0 @@ } |
@@ -96,3 +96,2 @@ import { Node } from "../ast/Node"; | ||
const startIndex = cursor.index; | ||
const nodes: Node[] = []; | ||
@@ -138,4 +137,4 @@ const modulo = this._hasDivider ? 2 : 1; | ||
const lastIndex = cursor.index; | ||
cursor.moveTo(startIndex); | ||
cursor.recordErrorAt(startIndex, lastIndex, this); | ||
cursor.moveTo(this._firstIndex); | ||
cursor.recordErrorAt(this._firstIndex, lastIndex, this); | ||
return null; | ||
@@ -145,3 +144,3 @@ } | ||
if (nodes.length === 0 && !cursor.hasError) { | ||
cursor.moveTo(startIndex); | ||
cursor.moveTo(this._firstIndex); | ||
return null; | ||
@@ -156,3 +155,9 @@ } | ||
const node = new Node(this._type, this.name, firstIndex, lastIndex, nodes); | ||
const node = new Node( | ||
this._type, | ||
this.name, | ||
firstIndex, | ||
lastIndex, | ||
nodes | ||
); | ||
@@ -159,0 +164,0 @@ return node; |
@@ -83,3 +83,3 @@ import { Node } from "../ast/Node"; | ||
this._divider = children[1]; | ||
this._firstIndex = -1; | ||
this._firstIndex = 0; | ||
this._nodes = []; | ||
@@ -95,3 +95,2 @@ this._trimDivider = options.trimDivider == null ? false : options.trimDivider; | ||
test(text: string, record = false): boolean { | ||
@@ -118,3 +117,2 @@ return testPattern(this, text, record); | ||
cursor.recordMatch(this, node); | ||
} | ||
@@ -320,3 +318,3 @@ | ||
// If there is no divider then suggest the repeating pattern and the next pattern after. | ||
if (index === 0 && !this._divider && this._parent) { | ||
if (index === 0 && this._divider == null && this._parent) { | ||
patterns.push(this._children[0]); | ||
@@ -355,3 +353,2 @@ patterns.push(...this._parent.getPatternsAfter(this)); | ||
clone._id = this._id; | ||
return clone; | ||
@@ -358,0 +355,0 @@ } |
@@ -9,5 +9,5 @@ import { Cursor } from "./Cursor"; | ||
name: string; | ||
startedOnIndex: number; | ||
parent: Pattern | null; | ||
children: Pattern[]; | ||
startedOnIndex: number; | ||
@@ -14,0 +14,0 @@ parse(cursor: Cursor): Node | null; |
@@ -1,8 +0,4 @@ | ||
* Add a Context Pattern | ||
This will allow you to store a list of patterns and the pattern you want to start a parse from. This will hold the context of many patterns | ||
that perhaps are references but are by way of the context. This will be helpful with cpat files. | ||
* Fix infinite repeat recursion with a reference | ||
* Optimizations We can add a map to the cursor where we can search through previous matches and if the match for this pattern with this id has already matched in other bracnhes of the parse it can just use the node and clone it and skip the full parse and speed up things tremendously | ||
* Generate typescript files from cpat | ||
@@ -9,0 +5,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 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
1310459
183
22596