summaryrefslogtreecommitdiff
path: root/src/parser.ts
blob: a2bb577d74a1707656ec4dbe46c2f209b33f9d45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Enum, constructors, match, matchString } from './utils/adt'
import * as P from './utils/parser-comb'
import { Result } from './utils/result'

export type CSSUnit = '' | 's' | 'ms' | 'px' | '%' | 'rem' | 'em'

export type BinOp = '+' | '-' | '*' | '/'

export interface Selector {
  tag: string | undefined
  id: string
  selectors: Array<SelectorComp>
}

export type SelectorComp = Enum<{
  ClassName: string
  Attr: readonly [string, string]
}>
export const SelectorComp = constructors<SelectorComp>()

export type Expr = Enum<{
  Call: { name: string; args: Expr[] }
  Identifier: string
  VarIdentifier: string
  LiteralString: string
  LiteralNumber: { value: number; unit: CSSUnit }
  BinOp: { op: BinOp; left: Expr; right: Expr }
  Parens: { expr: Expr }

  Pair: { key: string; value: Expr }
  Selector: Selector
}>

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 =
  (fnParser?: P.Parser<string>, argParser?: P.Parser<Expr>) =>
  (input: string) =>
    P.map(
      P.zip2(
        consumeWhitespace(fnParser ?? identifierParser),
        parens(consumeWhitespace(P.sepBy(argParser ?? exprParser, comma))),
      ),
      ([name, args]) => Expr.Call({ name, args }),
    )(input)

const stringLiteralParser = P.or([
  P.between(singleQuote, P.regex(/^[^']*/), singleQuote),
  P.between(doubleQuote, P.regex(/^[^"]*/), doubleQuote),
])
const stringLiteralExprParser: P.Parser<Expr> = P.map(
  stringLiteralParser,
  Expr.LiteralString,
)

const unitParser = P.regex(/^(s|ms|%|px|rem|em)/i)
const numberParser = P.regex(/^[-+]?((\d*\.\d+)|\d+)/)
const numberExprParser: P.Parser<Expr> = P.map(
  P.zip2(numberParser, P.optional(unitParser)),
  ([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<Expr> = (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<Expr> = (input: string) =>
  P.map(
    P.zip2(
      P.suffixed(varIdentifierParser, consumeWhitespace(P.string(':'))),
      exprParser,
    ),
    ([key, value]) => Expr.Pair({ key, value }),
  )(input)

const precedence = (op: BinOp) =>
  matchString(op, {
    '+': () => 0,
    '-': () => 0,
    '*': () => 1,
    '/': () => 2,
    _: () => -1,
  })

const binOpWithFixitySwitchity = (op: BinOp, left: Expr, right: Expr) =>
  match(right, {
    BinOp: binOp => {
      if (precedence(op) > precedence(binOp.op)) {
        return Expr.BinOp({
          op: binOp.op,
          left: Expr.BinOp({
            op,
            left: left,
            right: binOp.left,
          }),
          right: binOp.right,
        })
      }
      return Expr.BinOp({ op, left, right })
    },
    Parens: ({ expr }) => Expr.BinOp({ op, left, right: expr }),
    _: () => Expr.BinOp({ op, left, right }),
  })

const allowParens = (p: P.Parser<Expr>): P.Parser<Expr> =>
  P.or([P.map(parens(p), expr => Expr.Parens({ expr })), p])

const binOpP = P.regex(/^[+\-*/]/)

const binOpExprParser: P.Parser<Expr> = allowParens((input: string) =>
  match(exprParser(input), {
    Ok: ({ value, input: rest }) =>
      P.map(
        P.optional(P.zip2(consumeWhitespace(binOpP), binOpExprParser)),
        res =>
          res
            ? binOpWithFixitySwitchity(res[0] as BinOp, value, res[1])
            : value,
      )(rest),
    Err: _ => Result.Ok({ value: [], input }),
  }),
)

const exprParser: P.Parser<Expr> = allowParens(
  P.or([
    stringLiteralExprParser,
    numberExprParser,
    callExprParser(P.string('calc'), binOpExprParser),
    callExprParser(),
    pairExprParser,
    varIdentifierExprParser,
    selectorExprParser,
    identifierExprParser,
  ]),
)

export const parseExpr = (input: string): Expr => {
  return match(exprParser(input), {
    Ok: ({ value, input }) => {
      if (input) throw new Error(`Aaaaaa. Input left: ${input}`)
      return value
    },
    Err: e => {
      throw e
    },
  })
}

const declarationParser = P.or([callExprParser(), selectorExprParser])

const multiDeclarationParser = P.sepBy(declarationParser, whitespace)

export const parseDeclarations = (input: string) =>
  match<Array<Expr>, P.ParseResult<Array<Expr>>>(
    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<Expr> => {
  const res = P.sepBy(exprParser, P.or([comma, whitespace]))(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)}...`)
    },
  })
}