Skip to content

Commit

Permalink
feat(parse): add ParseContext.peakDepth, update recursion limit
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 19, 2023
1 parent 52390e9 commit 0a2b7db
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/parse/src/api.ts
Original file line number Diff line number Diff line change
@@ -69,9 +69,10 @@ export interface Language {

export interface ContextOpts {
/**
* Max recursion depth failsafe.
* Max recursion depth failsafe. Parsing will terminate once this limit is
* reached.
*
* @defaultVal 32
* @defaultVal 64
*/
maxDepth: number;
/**
13 changes: 12 additions & 1 deletion packages/parse/src/context.ts
Original file line number Diff line number Diff line change
@@ -12,11 +12,12 @@ export class ParseContext<T> {
protected _curr!: ParseScope<T>;

protected _maxDepth: number;
protected _peakDepth!: number;
protected _debug: boolean;
protected _retain: boolean;

constructor(public reader: IReader<T>, opts?: Partial<ContextOpts>) {
this.opts = { maxDepth: 32, debug: false, retain: false, ...opts };
this.opts = { maxDepth: 64, debug: false, retain: false, ...opts };
this._maxDepth = this.opts.maxDepth!;
this._debug = this.opts.debug!;
this._retain = this.opts.retain!;
@@ -31,6 +32,7 @@ export class ParseContext<T> {
result: null,
};
this._scopes = [this._curr];
this._peakDepth = 1;
this.reader.isDone(this._curr.state!);
return this;
}
@@ -47,6 +49,7 @@ export class ParseContext<T> {
result: null,
};
scopes.push(scope);
this._peakDepth = Math.max(this._peakDepth, scopes.length);
this._debug &&
console.log(
`${indent(scopes.length)}start: ${id} (${scope.state!.p})`
@@ -153,6 +156,14 @@ export class ParseContext<T> {
const children = this.root.children;
return children ? children[0].children : undefined;
}

/**
* Returns max. recursion depth which was actually reached. Will always be
* less or equal configured {@link ContextOpts.maxDepth}.
*/
get peakDepth() {
return this._peakDepth;
}
}

/**

0 comments on commit 0a2b7db

Please sign in to comment.