Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 14, 2023
1 parent 4e21ee4 commit a580e68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
19 changes: 12 additions & 7 deletions src/string-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export class StringParser<U> {
this.tag = opts.tag ?? '';
if (opts.handler != null) {
this.ctx = {
kind: 'strict',
handler: wrapByTraceHandler(opts.handler, this.tag),
};
} else {
this.ctx = opts.lazy;
this.ctx = { kind: 'lazy', handler: opts.lazy };
}
}

Expand All @@ -38,10 +39,11 @@ export class StringParser<U> {
* @internal
*/
_evalContext(): ParserContext<U> {
if (typeof this.ctx === 'function') {
const parser = this.ctx();
if (this.ctx.kind === 'lazy') {
const parser = this.ctx.handler();
const ctx = parser._evalContext();
this.ctx = {
kind: 'strict',
handler: wrapByTraceHandler(ctx.handler, this.tag),
};
}
Expand Down Expand Up @@ -202,7 +204,7 @@ export type StrictParserOpts<U> = {
* @internal
*/
export type LazyParserOpts<U> = {
lazy: LazyContext<U>,
lazy: () => StringParser<U>,
tag?: string,
handler?: undefined,
};
Expand All @@ -218,14 +220,17 @@ export type ParserHandler<U> = (input: string, index: number, state: any) => Res
* @internal
*/
export type ParserContext<U> = {
handler: ParserHandler<U>;
kind: 'strict',
handler: ParserHandler<U>,
};

/**
* @internal
*/
export type LazyContext<U> =
() => StringParser<U>;
export type LazyContext<U> = {
kind: 'lazy',
handler: () => StringParser<U>,
};

/**
* Get result type of Parser.
Expand Down
19 changes: 12 additions & 7 deletions src/token-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export class TokenParser<U> {
this.tag = opts.tag ?? '';
if (opts.handler != null) {
this.ctx = {
kind: 'strict',
handler: wrapByTraceHandler(opts.handler, this.tag),
};
} else {
this.ctx = opts.lazy;
this.ctx = { kind: 'lazy', handler: opts.lazy };
}
}

Expand All @@ -38,10 +39,11 @@ export class TokenParser<U> {
* @internal
*/
_evalContext(): ParserContext<U> {
if (typeof this.ctx === 'function') {
const parser = this.ctx();
if (this.ctx.kind === 'lazy') {
const parser = this.ctx.handler();
const ctx = parser._evalContext();
this.ctx = {
kind: 'strict',
handler: wrapByTraceHandler(ctx.handler, this.tag),
};
}
Expand Down Expand Up @@ -185,7 +187,7 @@ export type StrictParserOpts<U> = {
* @internal
*/
export type LazyParserOpts<U> = {
lazy: LazyContext<U>,
lazy: () => TokenParser<U>,
tag?: string,
handler?: undefined,
};
Expand All @@ -201,14 +203,17 @@ export type ParserHandler<U> = (input: string[], index: number, state: any) => R
* @internal
*/
export type ParserContext<U> = {
handler: ParserHandler<U>;
kind: 'strict',
handler: ParserHandler<U>,
};

/**
* @internal
*/
export type LazyContext<U> =
() => TokenParser<U>;
export type LazyContext<U> = {
kind: 'lazy',
handler: () => TokenParser<U>,
};

/**
* Get result type of Parser.
Expand Down

0 comments on commit a580e68

Please sign in to comment.