From c2c6e8d1258158f61fba026ded1e48ffc5b36cf6 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 14 Jan 2022 19:43:45 +0530 Subject: feat(eval): implements 0 or more quantifier --- src/eval/index.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/eval/index.ts b/src/eval/index.ts index f56ff92..8e162b4 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -20,6 +20,8 @@ const group = (value: T, index: number): MatchGroupIndexed => ({ type index = number +const indexed = (ls: T[]): Array<[number, T]> => ls.map((x, i) => [i, x]) + const accumulateSkip = () => { const skipIndexes = [] as index[] return { @@ -100,13 +102,22 @@ const checkExpr = ( ), OneOrMore: ({ expr }) => { + const { localSkip, getSkips } = accumulateSkip() + 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)), + ) + }, + + ZeroOrMore: ({ expr }) => { // TODO: Nested quantified expressions? const matches = pipe( list, takeLeftWhile(a => checkExpr(expr, a, list, index).length > 0), ) return pipe( - matches.length > 0 ? [group(matches, index)] : [], + [group(matches, index)], skip(matches.length || 1), ) }, @@ -115,8 +126,6 @@ const checkExpr = ( const { getSkips, localSkip } = accumulateSkip() const getGroups = () => { if (exprs.length > list.length) return [] - const indexed = (ls: T[]): Array<[number, T]> => - ls.map((x, i) => [i, x]) const result = pipe( zipWith(exprs, indexed(list), (expr, [i, val]) => checkExpr(expr, val, list.slice(i), index + i, localSkip), -- cgit v1.3.1