aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-14 19:10:49 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-14 19:23:59 +0530
commitb5893d68ce4eacbc3ae431ca48f821657236c7aa (patch)
tree3bf215c9ff226fbe1363069fcc63ca22ea5417c7 /src/parser
parentde5d3de99afae3cb7f1dc56fe8781394f8614a50 (diff)
downloadelxr-b5893d68ce4eacbc3ae431ca48f821657236c7aa.tar.gz
elxr-b5893d68ce4eacbc3ae431ca48f821657236c7aa.zip
refactor: refactors imports + formatting
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/index.ts29
-rw-r--r--src/parser/utils.ts73
2 files changed, 48 insertions, 54 deletions
diff --git a/src/parser/index.ts b/src/parser/index.ts
index cbdc02b..d1da542 100644
--- a/src/parser/index.ts
+++ b/src/parser/index.ts
@@ -1,5 +1,5 @@
-import { flow, pipe } from 'fp-ts/function'
-import { chain, map as mapE, left, orElse, right } from 'fp-ts/lib/Either'
+import { pipe } from 'fp-ts/function'
+import * as Either from 'fp-ts/Either'
import {
delimited,
digits,
@@ -13,7 +13,6 @@ import {
pair,
Parser,
ParserResult,
- prefixed,
satifyChar,
sepBy1,
suffixed,
@@ -22,8 +21,8 @@ import {
whitespaces0,
} from './utils'
import { Expr, ListExpr, Literal } from '../types'
-import { getOrElse, map } from 'fp-ts/lib/Option'
-import { mapFst, snd } from 'fp-ts/lib/Tuple'
+import * as Option from 'fp-ts/Option'
+import { snd } from 'fp-ts/Tuple'
const start = mapTo(symbol('^'), _ => Expr.Start())
const end = mapTo(symbol('$'), _ => Expr.End())
@@ -34,8 +33,8 @@ const anyBool = mapTo(symbol('\\b'), _ => Expr.AnyBool())
const truthy = mapTo(symbol('\\T'), _ => Expr.Truthy())
const falsey = mapTo(symbol('\\F'), _ => Expr.Falsey())
-const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> = chain(
- ([expr, input]) =>
+const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> =
+ Either.chain(([expr, input]) =>
pipe(
input,
or([
@@ -43,9 +42,9 @@ const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> = chain(
mapTo(symbol('+'), _ => Expr.OneOrMore({ expr })),
mapTo(symbol('?'), _ => Expr.Optional({ expr })),
]),
- orElse(_ => right([expr, input])),
+ Either.orElse(_ => Either.right([expr, input])),
),
-)
+ )
const propRegex = /^[A-Za-z0-9_-]$/
@@ -75,8 +74,8 @@ const unsignedNum: Parser<number> = mapTo(
([int, decimal]) =>
pipe(
decimal,
- map(snd),
- getOrElse(() => '0'),
+ Option.map(snd),
+ Option.getOrElse(() => '0'),
n => parseFloat(`${int}.${n}`),
),
)
@@ -86,7 +85,7 @@ const numberLiteral: Parser<Literal> = mapTo(
([signO, n]) =>
pipe(
signO,
- getOrElse(() => '+'),
+ Option.getOrElse(() => '+'),
sign => (sign === '-' ? -1 : 1) * n,
Literal.Number,
),
@@ -108,10 +107,10 @@ const infixOp =
pipe(
input,
sepBy1(op, groupP),
- chain(([exprs, nextInput]) =>
+ Either.chain(([exprs, nextInput]) =>
exprs.length === 1
- ? left(['Infix operator parsing error', input])
- : right([exprs, nextInput]),
+ ? Either.left(['Infix operator parsing error', input])
+ : Either.right([exprs, nextInput]),
),
)
diff --git a/src/parser/utils.ts b/src/parser/utils.ts
index cee1de7..4b68aff 100644
--- a/src/parser/utils.ts
+++ b/src/parser/utils.ts
@@ -1,49 +1,44 @@
import { flow, pipe } from 'fp-ts/function'
-import {
- Either,
- left,
- right,
- map,
- chain,
- orElse,
- fold,
- orElseW,
-} from 'fp-ts/Either'
-import { none, some, Option, getOrElse } from 'fp-ts/Option'
+import * as Either from 'fp-ts/Either'
+import * as Option from 'fp-ts/Option'
import { fst, mapFst, mapSnd, snd } from 'fp-ts/Tuple'
import { eq } from '../utils'
-import { prepend } from 'fp-ts/lib/Array'
+import { prepend } from 'fp-ts/Array'
export type char = string
export type ParserState<T> = [T, string]
export type ParserError = [string, string]
-export type ParserResult<T> = Either<ParserError, ParserState<T>>
+export type ParserResult<T> = Either.Either<ParserError, ParserState<T>>
export type Parser<T> = (input: string) => ParserResult<T>
export const constP =
<T>(v: T): Parser<T> =>
(inp: string) =>
- right([v, inp])
+ Either.right([v, inp])
export const sepBy1 = <T>(sep: Parser<any>, parser: Parser<T>): Parser<T[]> =>
flow(
parser,
- chain(([val, nextInput]) =>
- pipe(nextInput, many0(prefixed(sep, parser)), map(mapFst(prepend(val)))),
+ Either.chain(([val, nextInput]) =>
+ pipe(
+ nextInput,
+ many0(prefixed(sep, parser)),
+ Either.map(mapFst(prepend(val))),
+ ),
),
)
export const many0 = <T>(parser: Parser<T>): Parser<Array<T>> =>
flow(
parser,
- chain(([a, nextInput]) =>
- pipe(nextInput, many0(parser), map(mapFst(prepend(a)))),
+ Either.chain(([a, nextInput]) =>
+ pipe(nextInput, many0(parser), Either.map(mapFst(prepend(a)))),
),
- orElse(
+ Either.orElse(
flow(
mapFst(_ => [] as T[]),
- right,
+ Either.right,
),
),
)
@@ -52,10 +47,10 @@ export const many1 = <T>(parser: Parser<T>): Parser<Array<T>> =>
recoverInput(
flow(
many0(parser),
- chain(([res, inp]) =>
+ Either.chain(([res, inp]) =>
res.length > 0
- ? right([res, inp])
- : left([`many1 failed to parse at '${inp}'`, inp]),
+ ? Either.right([res, inp])
+ : Either.left([`many1 failed to parse at '${inp}'`, inp]),
),
),
)
@@ -66,10 +61,10 @@ const recoverInput =
pipe(
input,
p,
- orElseW(
+ Either.orElseW(
flow(
mapSnd(_ => input),
- left,
+ Either.left,
),
),
)
@@ -90,27 +85,27 @@ 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])
+ if (f(c)) return Either.right([c, input.slice(1)])
+ return Either.left([`Expected to satisfy ${f}, got "${c}"`, input])
}
export const digit = satifyChar(c => /^[0-9]$/g.test(c))
export const digits: Parser<string> = flow(
many1(digit),
- map(mapFst(ds => ds.join(''))),
+ Either.map(mapFst(ds => ds.join(''))),
)
export const or = <T>(parsers: Parser<T>[]): Parser<T> => {
const run = ([p, ...ps]: Parser<T>[]) =>
flow(
p,
- orElse(([_, inp]) => or(ps)(inp)),
+ Either.orElse(([_, inp]) => or(ps)(inp)),
)
return parsers.length > 0
? run(parsers)
- : (inp: string) => left(['unable to match', inp])
+ : (inp: string) => Either.left(['unable to match', inp])
}
export const matchChar = (ch: char): Parser<char> => satifyChar(eq(ch))
@@ -119,8 +114,8 @@ 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])
+ ? Either.right([s, input.slice(s.length)])
+ : Either.left([`Expected ${s} but got ${input.slice(0, 1)}`, input])
export const oneOf = (xs: string[]): Parser<string> => or(xs.map(matchString))
@@ -131,25 +126,25 @@ 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(mapFst(f)))
+ flow(p, Either.map(mapFst(f)))
export const andThen =
<I, R>(f: (p: I) => Parser<R>) =>
(p: Parser<I>): Parser<R> =>
flow(
p,
- chain(([v, inp]) => f(v)(inp)),
+ Either.chain(([v, inp]) => f(v)(inp)),
)
-export const optional = <T>(p: Parser<T>): Parser<Option<T>> =>
+export const optional = <T>(p: Parser<T>): Parser<Option.Option<T>> =>
flow(
recoverInput(p),
- fold(
+ Either.fold(
flow(
- mapFst(_ => none),
- right,
+ mapFst(_ => Option.none),
+ Either.right,
),
- flow(mapFst(some), right),
+ flow(mapFst(Option.some), Either.right),
),
)