11import { assertGreater } from "@std/assert/greater" ;
22import { MemoizationCacheResult , memoize } from "@std/cache/memoize" ;
33import { IterableResult , ResultError } from "../compound.ts" ;
4- import { ErrorOption , lazy as lazyEval } from "../misc/misc.ts" ;
4+ import { lazy as lazyEval } from "../misc/misc.ts" ;
55
66type ValueLength < T > = Readonly < { value : T ; length : number } > ;
77type ParserResult < T > = IterableResult < ValueLength < T > > ;
@@ -101,16 +101,19 @@ export class Parser<T> {
101101 }
102102}
103103export type Position = Readonly < { position : number ; length : number } > ;
104+ export type PositionedErrorOption = Readonly < {
105+ position ?: Position ;
106+ cause ?: unknown ;
107+ } > ;
104108export class PositionedError extends ResultError {
105109 override name = "PositionedError" ;
106110 position : null | Position ;
107111 constructor (
108112 message : string ,
109- // TODO: it's better to separate these two instead
110- option ?: Position & ErrorOption ,
113+ option ?: PositionedErrorOption ,
111114 ) {
112115 super ( message , option ) ;
113- this . position = option ?? null ;
116+ this . position = option ?. position ?? null ;
114117 }
115118}
116119function withPositionedError < T > ( fn : ( ) => T , position : Position ) {
@@ -120,7 +123,7 @@ function withPositionedError<T>(fn: () => T, position: Position) {
120123 if ( error instanceof PositionedError && error . position != null ) {
121124 throw error ;
122125 } else if ( error instanceof ResultError ) {
123- throw new PositionedError ( error . message , { ... position , cause : error } ) ;
126+ throw new PositionedError ( error . message , { position, cause : error } ) ;
124127 } else {
125128 throw error ;
126129 }
@@ -131,7 +134,7 @@ export class UnexpectedError extends PositionedError {
131134 constructor (
132135 unexpected : string ,
133136 expected : string ,
134- option ?: Position & ErrorOption ,
137+ option ?: PositionedErrorOption ,
135138 ) {
136139 super (
137140 `unexpected ${ unexpected } . ${ expected } were expected instead` ,
@@ -141,7 +144,7 @@ export class UnexpectedError extends PositionedError {
141144}
142145export class UnrecognizedError extends PositionedError {
143146 override name = "UnrecognizedError" ;
144- constructor ( element : string , option ?: Position & ErrorOption ) {
147+ constructor ( element : string , option ?: PositionedErrorOption ) {
145148 super ( `${ element } is unrecognized` , option ) ;
146149 }
147150}
@@ -279,7 +282,11 @@ function generateError(position: number, expected: string) {
279282 }
280283 }
281284 return IterableResult . errors ( [
282- new UnexpectedError ( unexpected , expected , { position, length } ) ,
285+ new UnexpectedError (
286+ unexpected ,
287+ expected ,
288+ { position : { position, length } } ,
289+ ) ,
283290 ] ) ;
284291}
285292export function matchCapture (
@@ -338,7 +345,7 @@ export const notEnd: Parser<null> = new Parser((position) =>
338345 new UnexpectedError (
339346 "end of text" ,
340347 "not end of text" ,
341- { position, length : currentSource . length - position } ,
348+ { position : { position , length : currentSource . length - position } } ,
342349 ) ,
343350 ] )
344351) ;
0 commit comments