diff options
| -rw-r--r-- | src/eval/index.ts | 41 | ||||
| -rw-r--r-- | src/parser/index.ts | 7 | ||||
| -rw-r--r-- | src/parser/utils.ts | 29 |
3 files changed, 49 insertions, 28 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts index f8d14d3..b7f4de1 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -52,15 +52,17 @@ const checkExpr = <T>( pipe(typeof item === 'boolean' ? [group(item, index)] : [], skip(1)), Truthy: _ => pipe(!!item ? [group(item, index)] : [], skip(1)), Falsey: _ => pipe(!item ? [group(item, index)] : [], skip(1)), - Literal: literal => pipe( - literal, - match<boolean, Literal>({ - RegExp: regex => regex && typeof item === 'string' && regex.test(item), - _: () => (literal.value as any) === item, - }), - passed => passed ? [group(item, index)] : [], - skip(1), - ), + Literal: literal => + pipe( + literal, + match<boolean, Literal>({ + RegExp: regex => + regex && typeof item === 'string' && regex.test(item), + _: () => (literal.value as any) === item, + }), + passed => (passed ? [group(item, index)] : []), + skip(1), + ), Group: ({ exprs }) => { const [head, ...tail] = exprs @@ -105,7 +107,13 @@ const checkExpr = <T>( OneOrMore: ({ expr }) => { const { localSkip, getSkips } = accumulateSkip() - const result = checkExpr(Expr.ZeroOrMore({ expr }), item, list, index, localSkip) + const result = checkExpr( + Expr.ZeroOrMore({ expr }), + item, + list, + index, + localSkip, + ) return pipe( result[0].value.length > 0 ? result : [], skip(getSkips().reduce((a, b) => a + b, 0)), @@ -114,7 +122,13 @@ const checkExpr = <T>( MinMax: ({ expr, min, max }) => { const { localSkip, getSkips } = accumulateSkip() - const result = checkExpr(Expr.ZeroOrMore({ expr }), item, list, index, localSkip) + const result = checkExpr( + Expr.ZeroOrMore({ expr }), + item, + list, + index, + localSkip, + ) // TODO: Use nested skips const matches = result[0].value.length @@ -135,10 +149,7 @@ const checkExpr = <T>( list, takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0), ) - return pipe( - [group(matches, index)], - skip(matches.length || 1), - ) + return pipe([group(matches, index)], skip(matches.length || 1)) }, Sequence: ({ exprs }) => { diff --git a/src/parser/index.ts b/src/parser/index.ts index 7f0e945..ac5c8ed 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -135,8 +135,11 @@ const regexLiteral: Parser<Literal> = mapTo( prefixed( regexDelimiter, pair( - manyTill(satisfyChar(_ => true), regexDelimiter), - many0(satisfyChar(c => REGEX_FLAGS.includes(c))) + manyTill( + satisfyChar(_ => true), + regexDelimiter, + ), + many0(satisfyChar(c => REGEX_FLAGS.includes(c))), ), ), ([rs, flags]) => Literal.RegExp(new RegExp(rs.join(''), flags.join(''))), diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 564d97c..c2c6300 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -29,17 +29,23 @@ export const sepBy1 = <T>(sep: Parser<any>, parser: Parser<T>): Parser<T[]> => ), ) -export const not = (parser: Parser<any>): Parser<never> => (input: string) => - pipe( - input, - parser, - Either.fold( - ([_left, inp]) => Either.right([null as never, inp]), - ([_right, inp]) => Either.left(['`not` parser failed. Match found', inp]) - ), - ) +export const not = + (parser: Parser<any>): Parser<never> => + (input: string) => + pipe( + input, + parser, + Either.fold( + ([_left, inp]) => Either.right([null as never, inp]), + ([_right, inp]) => + Either.left(['`not` parser failed. Match found', inp]), + ), + ) -export const manyTill = <T>(parser: Parser<T>, end: Parser<any>): Parser<T[]> => { +export const manyTill = <T>( + parser: Parser<T>, + end: Parser<any>, +): Parser<T[]> => { const p = pair(many0(suffixed(parser, not(end))), suffixed(parser, end)) return mapTo(p, ([xs, l]) => xs.concat([l])) } @@ -99,7 +105,8 @@ export const delimited = <T>( export const satisfyChar = (f: (c: char) => boolean): Parser<char> => (input: string) => { - if (input.length === 0) return Either.left(['Unexpected end of input', input]) + if (input.length === 0) + return Either.left(['Unexpected end of input', input]) const c = input.charAt(0) if (f(c)) return Either.right([c, input.slice(1)]) return Either.left([`Expected to satisfy ${f}, got "${c}"`, input]) |
