diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-07 14:26:00 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-07 14:26:00 +0530 |
| commit | ad8983655dc35b5401e810a9d4b61e933d35b783 (patch) | |
| tree | 62f494ff001d3ed55cb1eb55118e4e9d8cd2ae8d /src/parser.ts | |
| parent | 20480cd42bbbe1685b9470063248ed5fc1f2506b (diff) | |
| download | elxr-ad8983655dc35b5401e810a9d4b61e933d35b783.tar.gz elxr-ad8983655dc35b5401e810a9d4b61e933d35b783.zip | |
feat: basic parsing for expressions
Diffstat (limited to '')
| -rw-r--r-- | src/parser.ts | 21 |
1 files changed, 12 insertions, 9 deletions
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<string> => - 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<string> => + (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<string> => delimited(whitespaces0, matchString(s), whitespaces0) @@ -131,3 +128,9 @@ export const pair = <A, B>(a: Parser<A>, b: Parser<B>): Parser<[A, B]> => a, andThen((ra) => mapTo(b, (rb) => [ra, rb])) ) + +export const tuple3 = <A, B, C>( + a: Parser<A>, + b: Parser<B>, + c: Parser<C> +): Parser<[A, B, C]> => mapTo(pair(pair(a, b), c), ([[a, b], c]) => [a, b, c]) |
