From ad8983655dc35b5401e810a9d4b61e933d35b783 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 7 Jan 2022 14:26:00 +0530 Subject: feat: basic parsing for expressions --- src/parser.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/parser.ts') diff --git a/src/parser.ts b/src/parser.ts index 9baab8e..baa3e76 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -90,15 +90,12 @@ export const tab = matchChar('\t') export const whitespace = or([space, newline, tab]) export const whitespaces0 = many0(whitespace) -export const matchString = (s: string): Parser => - s === '' - ? constP('') - : flow( - matchChar(s.charAt(0)), - chain(([c, inp]) => - pipe(inp, matchString(s.slice(1)), map(mapFst((s) => c + s))) - ) - ) +export const matchString = + (s: string): Parser => + (input: string) => + input.slice(0, s.length) === s + ? right([s, input.slice(s.length)]) + : left([`Expected ${s} but got ${input.slice(0, 1)}`, input]) export const symbol = (s: string): Parser => delimited(whitespaces0, matchString(s), whitespaces0) @@ -131,3 +128,9 @@ export const pair = (a: Parser, b: Parser): Parser<[A, B]> => a, andThen((ra) => mapTo(b, (rb) => [ra, rb])) ) + +export const tuple3 = ( + a: Parser, + b: Parser, + c: Parser +): Parser<[A, B, C]> => mapTo(pair(pair(a, b), c), ([[a, b], c]) => [a, b, c]) -- cgit v1.3.1