diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-08-10 18:18:46 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-08-10 18:18:46 +0530 |
| commit | db97b1a9c42fe945ca37256f5ee18d52c6aa32b4 (patch) | |
| tree | 1da3da4a99192be8ad10b2931d447c2cc3f45036 /src/parse-expr.ts | |
| parent | 6a04167058b59b54b183104a1cf1b1b2cf8d7e9a (diff) | |
| download | css-everything-db97b1a9c42fe945ca37256f5ee18d52c6aa32b4.tar.gz css-everything-db97b1a9c42fe945ca37256f5ee18d52c6aa32b4.zip | |
feat: adds parser for simple expressions
Diffstat (limited to 'src/parse-expr.ts')
| -rw-r--r-- | src/parse-expr.ts | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/parse-expr.ts b/src/parse-expr.ts index 53de485..dbad1a1 100644 --- a/src/parse-expr.ts +++ b/src/parse-expr.ts @@ -1,11 +1,38 @@ -import { Enum, constructors } from "./utils/adt"; +import { Enum, constructors, match } from './utils/adt'; +import * as P from './utils/parser-comb' export type Expr = Enum<{ Call: { name: string, args: Expr[] } Var: { name: string, defaultValue: Expr } Identifier: string LiteralString: string -}>; +}> -export const Expr = constructors<Expr>(); +export const Expr = constructors<Expr>() + +const whitespace = P.regex(/\s*/) +const consumeWhitespace = <A>(p: P.Parser<A>): P.Parser<A> => P.between(whitespace, p, whitespace) + +const identifierParser = P.regex(/^[a-z][a-z0-9]+/i) +const identifierExprParser = P.map(identifierParser, ident => Expr.Identifier(ident)) + +const callParser = P.map(P.zip2( + consumeWhitespace(identifierParser), + P.between(P.string('('), consumeWhitespace(identifierParser), P.string(')')), +), ([name, rest]) => Expr.Call({ name, args: [Expr.Identifier(rest)] })) + +const exprParser: P.Parser<Expr> = P.or([ + callParser, + identifierExprParser, +]) + +export const parse = (input: string): Expr => { + const res = exprParser(input.trim()); + return match(res, { + Ok: ({ value }) => value, + Err: ({ error, input }) => { + throw new Error(`${error}.\n Left input: ${input.slice(0, 20)}...`) + }, + }) +} |
