diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-08-10 22:45:00 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-08-10 22:45:00 +0530 |
| commit | e01cb693bc5737792e2b37abfd98d2d8f81bac4d (patch) | |
| tree | 894d367e14f5a7edac5a80fb7caf9f9a84727d1e /src/parse-expr.ts | |
| parent | 2102e7608b1b3634a651cb40508d2f560f3eeb05 (diff) | |
| download | css-everything-e01cb693bc5737792e2b37abfd98d2d8f81bac4d.tar.gz css-everything-e01cb693bc5737792e2b37abfd98d2d8f81bac4d.zip | |
feat: adds simple evaluator
Diffstat (limited to 'src/parse-expr.ts')
| -rw-r--r-- | src/parse-expr.ts | 66 |
1 files changed, 0 insertions, 66 deletions
diff --git a/src/parse-expr.ts b/src/parse-expr.ts deleted file mode 100644 index 1c5c08f..0000000 --- a/src/parse-expr.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Enum, constructors, match } from './utils/adt' -import * as P from './utils/parser-comb' - -export type Expr = Enum<{ - Call: { name: string; args: Expr[] } - Identifier: string - VarIdentifier: string - LiteralString: string -}> - -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 comma = consumeWhitespace(P.string(',')) -const parens = <A>(p: P.Parser<A>): P.Parser<A> => - P.between(P.string('('), p, P.string(')')) -const identifierParser = P.regex(/^[a-z][a-z0-9_-]*/i) -const varIdentifierParser = P.regex(/^--[a-z][a-z0-9-]*/i) -const singleQuote = P.string("'") -const doubleQuote = P.string('"') - -const identifierExprParser = P.map(identifierParser, Expr.Identifier) -const varIdentifierExprParser = P.map(varIdentifierParser, Expr.VarIdentifier) - -const callExprParser = (input: string) => - P.map( - P.zip2( - consumeWhitespace(identifierParser), - parens(consumeWhitespace(P.sepBy(exprParser, comma))) - ), - ([name, args]) => Expr.Call({ name, args }) - )(input) - -const stringLiteralParser: P.Parser<Expr> = P.map( - P.or([ - P.between(singleQuote, P.regex(/^[^']*/), singleQuote), - P.between(doubleQuote, P.regex(/^[^"]*/), doubleQuote), - ]), - Expr.LiteralString -) - -const exprParser: P.Parser<Expr> = P.or([ - stringLiteralParser, - varIdentifierExprParser, - callExprParser, - identifierExprParser, -]) - -const multiExprParser = P.many1(exprParser) - -export const parse = (input: string): Array<Expr> => { - const res = multiExprParser(input.trim()) - return match(res, { - Ok: ({ value, input }) => { - if (input) { - throw new Error(`Input not consumed completely here brosky: "${input}"`) - } - return value - }, - Err: ({ error, input }) => { - throw new Error(`${error}.\n Left input: ${input.slice(0, 20)}...`) - }, - }) -} |
