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
|
import { flow, pipe } from 'fp-ts/function'
import { chain, left, orElse, right } from 'fp-ts/lib/Either'
import {
delimited,
many0,
many1,
mapTo,
oneOf,
optional,
or,
pair,
Parser,
ParserResult,
prefixed,
satifyChar,
suffixed,
symbol,
tuple3,
whitespaces0,
} from './utils'
import { Expr, ListExpr } from '../types'
const start = mapTo(symbol('^'), _ => Expr.Start(null))
const end = mapTo(symbol('$'), _ => Expr.End(null))
const anyItem = mapTo(symbol('.'), _ => Expr.AnyItem(null))
const nextItem = mapTo(symbol(','), _ => Expr.NextItem(null))
const anyString = mapTo(symbol('\\s'), _ => Expr.AnyString(null))
const anyNumber = mapTo(symbol('\\n'), _ => Expr.AnyNumber(null))
const anyBool = mapTo(symbol('\\b'), _ => Expr.AnyBool(null))
const truthy = mapTo(symbol('\\T'), _ => Expr.Truthy(null))
const falsey = mapTo(symbol('\\F'), _ => Expr.Falsey(null))
const wrapQuantifiers: (e: ParserResult<Expr>) => ParserResult<Expr> =
chain(([expr, input]) =>
pipe(
input,
or([
mapTo(symbol('*'), _ => Expr.ZeroOrMore({ expr })),
mapTo(symbol('+'), _ => Expr.OneOrMore({ expr })),
mapTo(symbol('?'), _ => Expr.Optional({ expr })),
]),
orElse(_ => right([expr, input])),
),
)
const wrapAlt: (e: ParserResult<Expr>) => ParserResult<Expr> = chain(
([expr, input]) =>
pipe(
input,
mapTo(prefixed(symbol('|'), many1(expressionP)), rest =>
Expr.Or({ left: expr, right: rest }),
),
orElse(_ => right([expr, input])),
),
)
const propRegex = /^[A-Za-z0-9_-]$/
export const propertyName: Parser<string> = pipe(
satifyChar(c => propRegex.test(c)),
many1,
p => mapTo(p, xs => xs.join('')),
p => suffixed(p, whitespaces0),
)
const objectProperty = (input: string) => pipe(
input,
mapTo(delimited(symbol('['), pair(propertyName, many0(expressionP)), symbol(']')), ([name, exprs]) =>
Expr.PropertyMatch({ name, exprs }),
),
)
const expressionP: Parser<Expr> = (input: string) =>
pipe(
input,
or([
mapTo(delimited(symbol('('), many1(expressionP), symbol(')')), exprs =>
Expr.Group({ exprs }),
),
objectProperty,
nextItem,
anyItem,
anyString,
anyNumber,
anyBool,
truthy,
falsey,
]),
wrapQuantifiers,
wrapAlt,
)
export const parser: Parser<ListExpr> = 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`
/x/ => match regular expression (string values in list)
*/
|