aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 2ea40eb1ad3fca345c58ba42a248c87a249a5ace (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
import { constant, pipe } from 'fp-ts/function'
import { chain, fold, right } from 'fp-ts/lib/Either'
import { match } from './utils'
import {
  delimited,
  many1,
  mapTo,
  optional,
  or,
  Parser,
  ParserResult,
  symbol,
  tuple3,
} from './parser'

export const start = mapTo(symbol('^'), constant({ tag: 'Start' } as Expr))
export const end = mapTo(symbol('$'), constant({ tag: 'End' } as Expr))
export const anyItem = mapTo(symbol('.'), constant({ tag: 'AnyItem' } as Expr))
export const nextItem = mapTo(
  symbol(','),
  constant({ tag: 'NextItem' } as Expr)
)
export const anyString = mapTo(
  symbol('\\s'),
  constant({ tag: 'AnyString' } as Expr)
)
export const anyNumber = mapTo(
  symbol('\\n'),
  constant({ tag: 'AnyNumber' } as Expr)
)
export const anyBool = mapTo(
  symbol('\\b'),
  constant({ tag: 'AnyBool' } as Expr)
)
export const truthy = mapTo(symbol('\\T'), constant({ tag: 'Truthy' } as Expr))
export const falsey = mapTo(symbol('\\F'), constant({ tag: 'Falsey' } as Expr))

type Expr =
  | { tag: 'Start' }
  | { tag: 'End' }
  | { tag: 'Optional'; expr: Expr }
  | { tag: 'OneOrMore'; expr: Expr }
  | { tag: 'ZeroOrMore'; expr: Expr }
  | { tag: 'NextItem' }
  | { tag: 'AnyItem' }
  | { tag: 'Or' }
  | { tag: 'AnyString' }
  | { tag: 'AnyNumber' }
  | { tag: 'AnyBool' }
  | { tag: 'Truthy' }
  | { tag: 'Falsey' }
  | { tag: 'Group'; exprs: Expr[] }

export const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> =
  chain(([expr, input]) =>
    pipe(
      input,
      or([symbol('*'), symbol('+'), symbol('?')]),
      fold(
        () => right([expr, input]),
        ([c, inp]) =>
          pipe(
            c,
            match<Expr, string>({
              '*': () => ({ tag: 'ZeroOrMore', expr }),
              '+': () => ({ tag: 'OneOrMore', expr }),
              '?': () => ({ tag: 'Optional', expr }),
              _: () => expr,
            }),
            (ex) => right([ex, inp])
          )
      )
    )
  )

export const expressionP: Parser<Expr> = (input: string) =>
  pipe(
    input,
    or([
      mapTo(
        delimited(symbol('('), many1(expressionP), symbol(')')),
        (exprs) => ({ tag: 'Group', exprs } as Expr)
      ),
      nextItem,
      anyItem,
      anyString,
      anyNumber,
      anyBool,
      truthy,
      falsey,
    ]),
    wrapQuantifiers
  )

export const parser = tuple3(optional(start), many1(expressionP), optional(end))

/*

{3,6} => 3 to 6 instances
(> 5) => number greater than
(< 5) => number less than
[name x] => apply x on property `name`
| => or
/x/ => match regular expression (string values in list)

*/