From d47c0a220e4912a30c59a7fd3c81b8376d74d720 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Sun, 19 Apr 2020 20:11:26 +0100 Subject: [PATCH] feat(parse): add ParseContext.reset(), update addChild() --- packages/parse/src/context.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/parse/src/context.ts b/packages/parse/src/context.ts index 1ffae5bcb4..dc254c909c 100644 --- a/packages/parse/src/context.ts +++ b/packages/parse/src/context.ts @@ -29,8 +29,8 @@ interface ContextOpts { } export class ParseContext { - protected _scopes: ParseScope[]; - protected _curr: ParseScope; + protected _scopes!: ParseScope[]; + protected _curr!: ParseScope; protected _maxDepth: number; protected _debug: boolean; @@ -41,6 +41,10 @@ export class ParseContext { this._maxDepth = opts.maxDepth!; this._debug = opts.debug!; this._retain = opts.retain!; + this.reset(); + } + + reset() { this._curr = { id: "root", state: { p: 0, l: 1, c: 1 }, @@ -48,7 +52,8 @@ export class ParseContext { result: null, }; this._scopes = [this._curr]; - reader.isDone(this._curr.state!); + this.reader.isDone(this._curr.state!); + return this; } start(id: string) { @@ -103,7 +108,11 @@ export class ParseContext { return true; } - addChild(id: string, result: any = null, consume = false) { + addChild( + id: string, + result: any = null, + newState: ParseState | boolean = false + ) { const curr = this._curr; const cstate = curr.state; const child: ParseScope = { @@ -116,8 +125,10 @@ export class ParseContext { }; const children = curr.children; children ? children.push(child) : (curr.children = [child]); - if (consume) { - this.reader.next(cstate!); + if (newState !== false) { + newState === true + ? this.reader.next(cstate!) + : (this._curr.state = newState); } return true; }