aboutsummaryrefslogtreecommitdiff
path: root/src/parser/index.ts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-24 19:19:15 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-24 19:19:15 +0530
commitdb497bb9950648c056c079c591b239ad4932b5c9 (patch)
treea554e54f9a3b0d7d7373874c508831e52ee33416 /src/parser/index.ts
parenta4e2b5b44823571a693ad47250e40efca5f7fe10 (diff)
downloadelxr-db497bb9950648c056c079c591b239ad4932b5c9.tar.gz
elxr-db497bb9950648c056c079c591b239ad4932b5c9.zip
feat(parser): implements min-max quantifier parser
Diffstat (limited to 'src/parser/index.ts')
-rw-r--r--src/parser/index.ts21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/parser/index.ts b/src/parser/index.ts
index 2db91a4..a336b83 100644
--- a/src/parser/index.ts
+++ b/src/parser/index.ts
@@ -23,7 +23,7 @@ import {
tuple3,
whitespaces0,
} from './utils'
-import { Expr, ListExpr, Literal } from '../types'
+import { Expr, index, ListExpr, Literal } from '../types'
import * as Option from 'fp-ts/Option'
import { snd } from 'fp-ts/Tuple'
@@ -36,6 +36,13 @@ const anyBool = mapTo(symbol('\\b'), _ => Expr.AnyBool())
const truthy = mapTo(symbol('\\T'), _ => Expr.Truthy())
const falsey = mapTo(symbol('\\F'), _ => Expr.Falsey())
+const parseQuantifier = (s: Option.Option<string>, def: index): index =>
+ pipe(
+ s,
+ Option.map(s => parseInt(s, 10)),
+ Option.getOrElse(() => def),
+ )
+
const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> =
Either.chain(([expr, input]) =>
pipe(
@@ -44,6 +51,18 @@ const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> =
mapTo(symbol('*'), _ => Expr.ZeroOrMore({ expr })),
mapTo(symbol('+'), _ => Expr.OneOrMore({ expr })),
mapTo(symbol('?'), _ => Expr.Optional({ expr })),
+ mapTo(
+ pair(
+ prefixed(symbol('{'), optional(digits)),
+ delimited(symbol(','), optional(digits), symbol('}')),
+ ),
+ ([min, max]) =>
+ Expr.MinMax({
+ expr,
+ min: parseQuantifier(min, 0),
+ max: parseQuantifier(max, Infinity),
+ }),
+ ),
]),
Either.orElse(_ => Either.right([expr, input])),
),