@@ -2,7 +2,6 @@ import { assert } from "@std/assert/assert";
22import { MemoizationCacheResult , memoize } from "@std/cache/memoize" ;
33import { ArrayResult , ArrayResultError } from "../array_result.ts" ;
44import { Clearable , ClearableCacheSet , Lazy } from "../cache.ts" ;
5- import { throwError } from "../../misc/misc.ts" ;
65
76export type ValueRest < T > = Readonly < { rest : string ; value : T } > ;
87export type ParserResult < T > = ArrayResult < ValueRest < T > > ;
@@ -213,60 +212,54 @@ export function matchCapture(
213212 description : string ,
214213) : Parser < RegExpMatchArray > {
215214 const newRegex = new RegExp ( `^${ regex . source } ` , regex . flags ) ;
216- return new Parser ( ( src ) =>
217- ArrayResult . from ( ( ) => {
218- const match = src . match ( newRegex ) ;
219- if ( match != null ) {
220- return new ArrayResult ( [ {
221- value : match ,
222- rest : src . slice ( match [ 0 ] . length ) ,
223- } ] ) ;
224- } else {
225- throw new UnexpectedError ( describeSource ( src ) , description ) ;
226- }
227- } )
228- ) ;
215+ return new Parser ( ( src ) => {
216+ const match = src . match ( newRegex ) ;
217+ if ( match != null ) {
218+ return new ArrayResult ( [ {
219+ value : match ,
220+ rest : src . slice ( match [ 0 ] . length ) ,
221+ } ] ) ;
222+ } else {
223+ return new ArrayResult (
224+ new UnexpectedError ( describeSource ( src ) , description ) ,
225+ ) ;
226+ }
227+ } ) ;
229228}
230229export function match ( regex : RegExp , description : string ) : Parser < string > {
231230 return matchCapture ( regex , description ) . map ( ( [ matched ] ) => matched ) ;
232231}
233232export function slice ( length : number , description : string ) : Parser < string > {
234233 return new Parser ( ( src ) =>
235- ArrayResult . from ( ( ) =>
236- src . length >= length
237- ? new ArrayResult ( [ {
238- rest : src . slice ( length ) ,
239- value : src . slice ( 0 , length ) ,
240- } ] )
241- : throwError ( new UnexpectedError ( describeSource ( src ) , description ) )
242- )
234+ src . length >= length
235+ ? new ArrayResult ( [ {
236+ rest : src . slice ( length ) ,
237+ value : src . slice ( 0 , length ) ,
238+ } ] )
239+ : new ArrayResult ( new UnexpectedError ( describeSource ( src ) , description ) )
243240 ) ;
244241}
245242export function matchString (
246243 match : string ,
247244 description = `"${ match } "` ,
248245) : Parser < string > {
249246 return new Parser ( ( src ) =>
250- ArrayResult . from ( ( ) =>
251- src . length >= match . length && src . slice ( 0 , match . length ) === match
252- ? new ArrayResult ( [ {
253- rest : src . slice ( match . length ) ,
254- value : match ,
255- } ] )
256- : throwError ( new UnexpectedError ( describeSource ( src ) , description ) )
257- )
247+ src . length >= match . length && src . slice ( 0 , match . length ) === match
248+ ? new ArrayResult ( [ {
249+ rest : src . slice ( match . length ) ,
250+ value : match ,
251+ } ] )
252+ : new ArrayResult ( new UnexpectedError ( describeSource ( src ) , description ) )
258253 ) ;
259254}
260255export const everything = new Parser ( ( src ) =>
261256 new ArrayResult ( [ { value : src , rest : "" } ] )
262257) ;
263258export const character = match ( / ./ us, "character" ) ;
264259export const end = new Parser ( ( src ) =>
265- ArrayResult . from ( ( ) =>
266- src === ""
267- ? new ArrayResult ( [ { value : null , rest : "" } ] )
268- : throwError ( new UnexpectedError ( describeSource ( src ) , "end of text" ) )
269- )
260+ src === ""
261+ ? new ArrayResult ( [ { value : null , rest : "" } ] )
262+ : new ArrayResult ( new UnexpectedError ( describeSource ( src ) , "end of text" ) )
270263) ;
271264export function withSource < T > (
272265 parser : Parser < T > ,
0 commit comments