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
|
import { flow, pipe } from 'fp-ts/function'
import { chain, map as mapE, left, orElse, right } from 'fp-ts/lib/Either'
import {
delimited,
digits,
many0,
many1,
mapTo,
matchChar,
oneOf,
optional,
or,
pair,
Parser,
ParserResult,
prefixed,
satifyChar,
sepBy1,
suffixed,
symbol,
tuple3,
whitespaces0,
} from './utils'
import { Expr, ListExpr, Literal } from '../types'
import {getOrElse, map} from 'fp-ts/lib/Option'
import {mapFst, snd} from 'fp-ts/lib/Tuple'
const start = mapTo(symbol('^'), _ => Expr.Start())
const end = mapTo(symbol('$'), _ => Expr.End())
const anyItem = mapTo(symbol('.'), _ => Expr.AnyItem())
const anyString = mapTo(symbol('\\s'), _ => Expr.AnyString())
const anyNumber = mapTo(symbol('\\n'), _ => Expr.AnyNumber())
const anyBool = mapTo(symbol('\\b'), _ => Expr.AnyBool())
const truthy = mapTo(symbol('\\T'), _ => Expr.Truthy())
const falsey = mapTo(symbol('\\F'), _ => Expr.Falsey())
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 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, expr: exprsToGroup(exprs) }),
),
)
const unsignedNum: Parser<number> = mapTo(pair(digits, optional(pair(matchChar('.'), digits))), ([int, decimal]) =>
pipe(
decimal,
map(snd),
getOrElse(() => '0'),
n => parseFloat(`${int}.${n}`),
)
)
const numberLiteral: Parser<Literal> = mapTo(
pair(optional(oneOf(['+', '-'])), unsignedNum),
([signO, n]) =>
pipe(
signO,
getOrElse(() => '+'),
sign => (sign === '-' ? -1 : 1) * n,
Literal.Number,
)
,
)
const booleanLiteral: Parser<Literal> = mapTo(oneOf(['true', 'false']), b =>
Literal.Boolean(b === 'true' ? true : false),
)
const literalP: Parser<Literal> = delimited(
whitespaces0,
or([booleanLiteral, numberLiteral]),
whitespaces0,
)
const infixOp = (op: Parser<any>): Parser<Expr[]> => (input: string) => pipe(
input,
sepBy1(op, groupP),
chain(([exprs, nextInput]) => exprs.length === 1
? left(['Infix operator parsing error', input])
: right([exprs, nextInput])),
)
const altP: Parser<Expr> = mapTo(infixOp(symbol('|')), exprs => Expr.Or({ exprs }))
const sequenceP: Parser<Expr> = mapTo(infixOp(symbol(',')), exprs => Expr.Sequence({ exprs }))
const expressionP: Parser<Expr> = (input: string) => or([ altP, sequenceP, groupP ])(input)
const atomP: Parser<Expr> = (input: string) =>
pipe(
input,
or([
mapTo(delimited(symbol('('), many1(expressionP), symbol(')')), exprsToGroup),
objectProperty,
anyItem,
anyString,
anyNumber,
anyBool,
truthy,
falsey,
mapTo(literalP, Expr.Literal),
]),
wrapQuantifiers,
)
const exprsToGroup = (exprs: Expr[]) => exprs.length === 1 ? exprs[0] : Expr.Group({ exprs })
const groupP: Parser<Expr> = mapTo(many1(atomP), exprsToGroup)
export const parser: Parser<ListExpr> = tuple3(
optional(start),
expressionP,
optional(end),
)
/*
{3,6} => 3 to 6 instances
(> 5) => number greater than
(< 5) => number less than
/x/ => match regular expression (string values in list)
*/
|