diff options
Diffstat (limited to '')
| -rw-r--r-- | src/parser.ts | 135 |
1 files changed, 80 insertions, 55 deletions
diff --git a/src/parser.ts b/src/parser.ts index 076e0c5..f8dcd44 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -7,49 +7,67 @@ export type ParserResult<T> = [T, string] export type ParserError = [string, string] export type Parser<T> = (input: string) => Either<ParserError, ParserResult<T>> -export const constP = <T>(v: T): Parser<T> => (inp: string) => right([v, inp]) +export const constP = + <T>(v: T): Parser<T> => + (inp: string) => + right([v, inp]) -export const many0 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( - parser, - chain(([a, nextInput]) => - pipe( - nextInput, - many0(parser), - map(([ls, inp]): ParserResult<T[]> => [[a, ...ls], inp]), - ) - ), - orElse(([_, inp]) => right([[] as T[], inp])) -) +export const many0 = <T>(parser: Parser<T>): Parser<Array<T>> => + flow( + parser, + chain(([a, nextInput]) => + pipe( + nextInput, + many0(parser), + map(([ls, inp]): ParserResult<T[]> => [[a, ...ls], inp]) + ) + ), + orElse(([_, inp]) => right([[] as T[], inp])) + ) -export const many1 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( - many0(parser), - chain(([res, inp]) => - res.length > 0 ? right([res, inp]) : left([`many1 failed to parse at ${inp}`, inp])) -) +export const many1 = <T>(parser: Parser<T>): Parser<Array<T>> => + flow( + many0(parser), + chain(([res, inp]) => + res.length > 0 + ? right([res, inp]) + : left([`many1 failed to parse at ${inp}`, inp]) + ) + ) -export const prefixed = <T>(a: Parser<any>, b: Parser<T>): Parser<T> => flow( - a, - chain(([_, inp]) => b(inp)) -) +export const prefixed = <T>(a: Parser<any>, b: Parser<T>): Parser<T> => + flow( + a, + chain(([_, inp]) => b(inp)) + ) -export const suffixed = <T>(a: Parser<T>, b: Parser<any>): Parser<T> => flow( - a, - chain(([out, inp]) => pipe(inp, - b, - chain(([_, inp2]) => right([out, inp2])) - )) -) +export const suffixed = <T>(a: Parser<T>, b: Parser<any>): Parser<T> => + flow( + a, + chain(([out, inp]) => + pipe( + inp, + b, + chain(([_, inp2]) => right([out, inp2])) + ) + ) + ) -export const delimited = <T>(p: Parser<any>, a: Parser<T>, s: Parser<any>): Parser<T> => - suffixed(prefixed(p, a), s) +export const delimited = <T>( + p: Parser<any>, + a: Parser<T>, + s: Parser<any> +): Parser<T> => suffixed(prefixed(p, a), s) -export const satifyChar = (f: (c: char) => boolean): Parser<char> => (input: string) => { - const c = input.charAt(0) - if (f(c)) return right([c, input.slice(1)]) - return left([`Expected to satisfy ${f}, got "${c}"`, input]) -} +export const satifyChar = + (f: (c: char) => boolean): Parser<char> => + (input: string) => { + const c = input.charAt(0) + if (f(c)) return right([c, input.slice(1)]) + return left([`Expected to satisfy ${f}, got "${c}"`, input]) + } -export const digit = satifyChar(c => /^[0-9]$/g.test(c)) +export const digit = satifyChar((c) => /^[0-9]$/g.test(c)) export const integer: Parser<number> = flow( many1(digit), @@ -57,15 +75,18 @@ export const integer: Parser<number> = flow( ) export const or = <T>(parsers: Parser<T>[]): Parser<T> => { - const ppp = ([p, ...ps]: Parser<T>[]) => flow( - p, - orElse(([_, inp]) => or(ps)(inp)) - ) + const ppp = ([p, ...ps]: Parser<T>[]) => + flow( + p, + orElse(([_, inp]) => or(ps)(inp)) + ) - return parsers.length > 0 ? ppp(parsers) : (inp: string) => left(['unable to match', inp]) + return parsers.length > 0 + ? ppp(parsers) + : (inp: string) => left(['unable to match', inp]) } -export const matchChar = (ch: char): Parser<char> => satifyChar(c => c === ch) +export const matchChar = (ch: char): Parser<char> => satifyChar((c) => c === ch) export const space = matchChar(' ') export const newline = matchChar('\n') @@ -75,20 +96,24 @@ 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(([s, inp]) => [c + s, inp]), - )) - ) + s === '' + ? constP('') + : flow( + matchChar(s.charAt(0)), + chain(([c, inp]) => + pipe( + inp, + matchString(s.slice(1)), + map(([s, inp]) => [c + s, inp]) + ) + ) + ) export const symbol = (s: string): Parser<string> => delimited(whitespaces0, matchString(s), whitespaces0) -export const mapTo = <I, R>(p: Parser<I>, f: (p: I) => R): Parser<R> => flow( - p, - map(([v, inp]) => [f(v), inp]) -) - +export const mapTo = <I, R>(p: Parser<I>, f: (p: I) => R): Parser<R> => + flow( + p, + map(([v, inp]) => [f(v), inp]) + ) |
