From 48455e6caaa7ad98316bbb4896b59f4487ad8650 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 13 Aug 2023 13:56:15 +0530 Subject: feat: adds declaration parser --- src/parser.ts | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/parser.ts b/src/parser.ts index a6c3928..732e32a 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -3,6 +3,12 @@ import * as P from './utils/parser-comb' export type CSSUnit = '' | 's' | 'ms' +export interface Selector { + tag: string | undefined + id: string + selectors: Array +} + export type SelectorComp = Enum<{ ClassName: string Attr: readonly [string, string] @@ -17,11 +23,7 @@ export type Expr = Enum<{ LiteralNumber: { value: number; unit: CSSUnit } Pair: { key: string; value: Expr } - Selector: { - tag: string | undefined - id: string - selectors: Array - } + Selector: Selector }> export const Expr = constructors() @@ -100,16 +102,35 @@ const exprParser: P.Parser = P.or([ stringLiteralExprParser, numberExprParser, callExprParser, - selectorExprParser, - identifierExprParser, pairExprParser, varIdentifierExprParser, + selectorExprParser, + identifierExprParser, ]) -const multiExprParser = P.many1(exprParser) +const declarationParser = P.or([callExprParser, selectorExprParser]) + +const multiDeclarationParser = P.sepBy(declarationParser, whitespace) + +export const parseDeclarations = (input: string) => + match, P.ParseResult>>( + multiDeclarationParser(input), + { + Ok: ({ value, input }) => { + if (input) { + console.error(`Declaration stopped parsing at: "${input}"`) + } + return value + }, + Err: ({ error }) => { + console.error(error) + return [] + }, + }, + ) export const parse = (input: string): Array => { - const res = multiExprParser(input.trim()) + const res = P.many1(exprParser)(input.trim()) return match(res, { Ok: ({ value, input }) => { if (input) { -- cgit v1.3.1