From 3d88de49d99d86c70204d6870296b20177c2af21 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 6 Jan 2022 21:59:34 +0530 Subject: chore: adds prettier --- src/index.d.ts | 7 --- src/index.ts | 8 ++-- src/parser.ts | 137 ++++++++++++++++++++++++++++++++++----------------------- 3 files changed, 86 insertions(+), 66 deletions(-) delete mode 100644 src/index.d.ts (limited to 'src') diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index fdaab41..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Either } from 'fp-ts/Either'; -declare type ParserResult = [T, string]; -declare type ParserError = [string, string]; -declare type Parser = (input: string) => Either>; -export declare const p_digit: Parser; -export declare const many0: (p: Parser) => Parser; -export {}; diff --git a/src/index.ts b/src/index.ts index 3dc572b..d236473 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ import { constant, flow, identity, pipe } from 'fp-ts/function' -import {mapTo, symbol} from './parser' +import { mapTo, symbol } from './parser' export const start = mapTo(symbol('^'), constant({ tag: 'Start' } as Expr)) export const end = mapTo(symbol('$'), constant({ tag: 'End' } as Expr)) export const anyItem = mapTo(symbol(','), constant({ tag: 'AnyItem' } as Expr)) -export const nextItem = mapTo(symbol(','), constant({ tag: 'NextItem' } as Expr)) +export const nextItem = mapTo( + symbol(','), + constant({ tag: 'NextItem' } as Expr) +) // export const optional = mapTo(symbol('?'), constant({ tag: 'Optional' } as Expr)) // export const zeroOrMore = mapTo(symbol('*'), constant({ tag: 'ZeroOrMore' } as Expr)) // export const oneOrMore = mapTo(symbol('+'), constant({ tag: 'OneOrMore' } as Expr)) @@ -45,4 +48,3 @@ type Expr = /x/ => match regular expression (string values in list) */ - 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, string] export type ParserError = [string, string] export type Parser = (input: string) => Either> -export const constP = (v: T): Parser => (inp: string) => right([v, inp]) - -export const many0 = (parser: Parser): Parser> => flow( - parser, - chain(([a, nextInput]) => - pipe( - nextInput, - many0(parser), - map(([ls, inp]): ParserResult => [[a, ...ls], inp]), - ) - ), - orElse(([_, inp]) => right([[] as T[], inp])) -) +export const constP = + (v: T): Parser => + (inp: string) => + right([v, inp]) + +export const many0 = (parser: Parser): Parser> => + flow( + parser, + chain(([a, nextInput]) => + pipe( + nextInput, + many0(parser), + map(([ls, inp]): ParserResult => [[a, ...ls], inp]) + ) + ), + orElse(([_, inp]) => right([[] as T[], inp])) + ) -export const many1 = (parser: Parser): Parser> => flow( - many0(parser), - chain(([res, inp]) => - res.length > 0 ? right([res, inp]) : left([`many1 failed to parse at ${inp}`, inp])) -) +export const many1 = (parser: Parser): Parser> => + flow( + many0(parser), + chain(([res, inp]) => + res.length > 0 + ? right([res, inp]) + : left([`many1 failed to parse at ${inp}`, inp]) + ) + ) -export const prefixed = (a: Parser, b: Parser): Parser => flow( - a, - chain(([_, inp]) => b(inp)) -) +export const prefixed = (a: Parser, b: Parser): Parser => + flow( + a, + chain(([_, inp]) => b(inp)) + ) -export const suffixed = (a: Parser, b: Parser): Parser => flow( - a, - chain(([out, inp]) => pipe(inp, - b, - chain(([_, inp2]) => right([out, inp2])) - )) -) +export const suffixed = (a: Parser, b: Parser): Parser => + flow( + a, + chain(([out, inp]) => + pipe( + inp, + b, + chain(([_, inp2]) => right([out, inp2])) + ) + ) + ) -export const delimited = (p: Parser, a: Parser, s: Parser): Parser => - suffixed(prefixed(p, a), s) +export const delimited = ( + p: Parser, + a: Parser, + s: Parser +): Parser => suffixed(prefixed(p, a), s) -export const satifyChar = (f: (c: char) => boolean): Parser => (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 => + (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 = flow( many1(digit), @@ -57,15 +75,18 @@ export const integer: Parser = flow( ) export const or = (parsers: Parser[]): Parser => { - const ppp = ([p, ...ps]: Parser[]) => flow( - p, - orElse(([_, inp]) => or(ps)(inp)) - ) + const ppp = ([p, ...ps]: Parser[]) => + 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 => satifyChar(c => c === ch) +export const matchChar = (ch: char): Parser => 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 => - 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 => delimited(whitespaces0, matchString(s), whitespaces0) -export const mapTo = (p: Parser, f: (p: I) => R): Parser => flow( - p, - map(([v, inp]) => [f(v), inp]) -) - +export const mapTo = (p: Parser, f: (p: I) => R): Parser => + flow( + p, + map(([v, inp]) => [f(v), inp]) + ) -- cgit v1.3.1