From 1e8135bf0e24e39f4dc3f8b0a13241ddf0cf475c Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 13 Aug 2023 12:05:41 +0530 Subject: feat: selector parser + pair parser implemented --- src/eval.ts | 1 + src/parser.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/eval.ts b/src/eval.ts index 9f053c2..3a4e4f9 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -113,6 +113,7 @@ const getFunctions = (name: string, args: Expr[], actions: EvalActions) => return actions.getAttribute(id as string | undefined, name) } }, + 'prevent-default': async () => actions.withEvent(e => e.preventDefault()), request: async () => { diff --git a/src/parser.ts b/src/parser.ts index 3279f57..a6c3928 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -3,12 +3,25 @@ import * as P from './utils/parser-comb' export type CSSUnit = '' | 's' | 'ms' +export type SelectorComp = Enum<{ + ClassName: string + Attr: readonly [string, string] +}> +export const SelectorComp = constructors() + export type Expr = Enum<{ Call: { name: string; args: Expr[] } Identifier: string VarIdentifier: string LiteralString: string LiteralNumber: { value: number; unit: CSSUnit } + + Pair: { key: string; value: Expr } + Selector: { + tag: string | undefined + id: string + selectors: Array + } }> export const Expr = constructors() @@ -36,28 +49,61 @@ const callExprParser = (input: string) => ([name, args]) => Expr.Call({ name, args }), )(input) -const stringLiteralParser: P.Parser = P.map( - P.or([ - P.between(singleQuote, P.regex(/^[^']*/), singleQuote), - P.between(doubleQuote, P.regex(/^[^"]*/), doubleQuote), - ]), +const stringLiteralParser = P.or([ + P.between(singleQuote, P.regex(/^[^']*/), singleQuote), + P.between(doubleQuote, P.regex(/^[^"]*/), doubleQuote), +]) +const stringLiteralExprParser: P.Parser = P.map( + stringLiteralParser, 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 CSSUnit }), ) +const tagP = identifierParser +const idP = P.prefixed(P.string('#'), identifierParser) +const classP = P.map( + P.prefixed(P.string('.'), identifierParser), + SelectorComp.ClassName, +) +const valueP = P.or([identifierParser, stringLiteralParser, numberParser]) +const attrP = P.map( + P.between( + P.string('['), + P.zip2(P.suffixed(identifierParser, P.string('=')), valueP), + P.string(']'), + ), + SelectorComp.Attr, +) + +const selectorExprParser: P.Parser = (input: string) => + P.map( + P.zip2(P.zip2(P.optional(tagP), idP), P.many0(P.or([classP, attrP]))), + ([[tag, id], selectors]) => Expr.Selector({ tag, id, selectors }), + )(input) + +const pairExprParser: P.Parser = (input: string) => + P.map( + P.zip2( + P.suffixed(varIdentifierParser, consumeWhitespace(P.string(':'))), + exprParser, + ), + ([key, value]) => Expr.Pair({ key, value }), + )(input) + const exprParser: P.Parser = P.or([ - stringLiteralParser, - varIdentifierExprParser, + stringLiteralExprParser, numberExprParser, callExprParser, + selectorExprParser, identifierExprParser, + pairExprParser, + varIdentifierExprParser, ]) const multiExprParser = P.many1(exprParser) -- cgit v1.3.1