Skip to content

Commit

Permalink
feat(parse): add ParseContext.reset(), update addChild()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 19, 2020
1 parent 38e9c66 commit d47c0a2
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/parse/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface ContextOpts {
}

export class ParseContext<T> {
protected _scopes: ParseScope<T>[];
protected _curr: ParseScope<T>;
protected _scopes!: ParseScope<T>[];
protected _curr!: ParseScope<T>;

protected _maxDepth: number;
protected _debug: boolean;
Expand All @@ -41,14 +41,19 @@ export class ParseContext<T> {
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 },
children: null,
result: null,
};
this._scopes = [this._curr];
reader.isDone(this._curr.state!);
this.reader.isDone(this._curr.state!);
return this;
}

start(id: string) {
Expand Down Expand Up @@ -103,7 +108,11 @@ export class ParseContext<T> {
return true;
}

addChild(id: string, result: any = null, consume = false) {
addChild(
id: string,
result: any = null,
newState: ParseState<T> | boolean = false
) {
const curr = this._curr;
const cstate = curr.state;
const child: ParseScope<T> = {
Expand All @@ -116,8 +125,10 @@ export class ParseContext<T> {
};
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;
}
Expand Down

0 comments on commit d47c0a2

Please sign in to comment.