From 952256ae8bf5514a246db8f33bef207f5747b138 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 11 Aug 2023 17:13:57 +0530 Subject: feat: adds parser for literal numbers --- src/parser.ts | 11 +++++++++++ src/utils/parser-comb.ts | 8 ++++++++ 2 files changed, 19 insertions(+) (limited to 'src') diff --git a/src/parser.ts b/src/parser.ts index 1c5c08f..8230825 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,11 +1,14 @@ import { Enum, constructors, match } from './utils/adt' import * as P from './utils/parser-comb' +type Unit = '' | 's' | 'ms' + export type Expr = Enum<{ Call: { name: string; args: Expr[] } Identifier: string VarIdentifier: string LiteralString: string + LiteralNumber: { value: number, unit: Unit } }> export const Expr = constructors() @@ -41,9 +44,17 @@ const stringLiteralParser: P.Parser = P.map( Expr.LiteralString ) +const numberParser = P.regex(/^[-+]?((\d*\.\d+)|\d+)/) + +const numberExprParser: P.Parser = P.map( + P.zip2(numberParser, P.optional(P.regex(/^(s|ms)/i))), + ([value, unit]) => Expr.LiteralNumber({ value: Number(value), unit: (unit ?? '') as Unit }), +) + const exprParser: P.Parser = P.or([ stringLiteralParser, varIdentifierExprParser, + numberExprParser, callExprParser, identifierExprParser, ]) diff --git a/src/utils/parser-comb.ts b/src/utils/parser-comb.ts index e0665f6..f682068 100644 --- a/src/utils/parser-comb.ts +++ b/src/utils/parser-comb.ts @@ -73,3 +73,11 @@ export const sepBy = (parser: Parser, sepP: Parser): Parser> Err: _ => Result.Ok({ value: [], input: originalInput }), }) +export const optional = (parser: Parser): Parser => input => { + const result = parser(input) + return match(result, { + Ok: _ => result, + Err: _ => Result.Ok({ value: undefined, input }) + }) +} + -- cgit v1.3.1