-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.ts
144 lines (135 loc) · 3.06 KB
/
parsers.ts
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
import { pipe } from 'fp-ts/lib/function'
import { char as C, parser as P, string as S } from 'parser-ts'
import {
JSON,
JSONArray,
JSONBoolean,
JSONNull,
JSONNumber,
JSONObject,
JSONString,
} from './model'
import { JSONStringEscapesParser } from './utils'
const CTRL_CHAR_SET = [
'\x00',
'\x01',
'\x02',
'\x03',
'\x04',
'\x05',
'\x06',
'\x07',
'\x08',
'\x09',
'\x0A',
'\x0B',
'\x0C',
'\x0D',
'\x0E',
'\x0F',
'\x10',
'\x11',
'\x12',
'\x13',
'\x14',
'\x15',
'\x16',
'\x17',
'\x18',
'\x19',
'\x1A',
'\x1B',
'\x1C',
'\x1D',
'\x1E',
'\x1F',
]
export const JSONStringParser = pipe(
C.char('"'),
P.chain(() =>
S.many(
pipe(
C.notOneOf(['"', '\\', ...CTRL_CHAR_SET].join('')),
P.alt(() => JSONStringEscapesParser),
),
),
),
P.apFirst(C.char('"')),
P.map((value): JSONString => ({ _tag: 'string', value })),
)
export const JSONNumberParser = pipe(
S.fold([
S.maybe(C.char('-')),
pipe(
C.char('0'),
P.alt(() => S.fold([C.oneOf('123456789'), C.many(C.digit)])),
),
S.maybe(S.fold([C.char('.'), C.many1(C.digit)])),
S.maybe(S.fold([C.oneOf('Ee'), S.maybe(C.oneOf('+-')), C.many1(C.digit)])),
]),
P.map(s => +s),
P.map((value): JSONNumber => ({ _tag: 'number', value })),
)
export const JSONNullParser = pipe(
S.string('null'),
P.map((): JSONNull => ({ _tag: 'null' })),
)
export const JSONBooleanParser = pipe(
S.string('true'),
P.map(() => true),
P.alt(() =>
pipe(
S.string('false'),
P.map(() => false),
),
),
P.map((value): JSONBoolean => ({ _tag: 'boolean', value })),
)
const SpaceParser = C.many(C.oneOf(' \n'))
export const JSONValueParser = pipe(
JSONStringParser as P.Parser<string, JSON>,
P.alt((): P.Parser<string, JSON> => JSONNumberParser),
P.alt((): P.Parser<string, JSON> => JSONBooleanParser),
P.alt((): P.Parser<string, JSON> => JSONNullParser),
P.alt((): P.Parser<string, JSON> => JSONObjectParser),
P.alt((): P.Parser<string, JSON> => JSONArrayParser),
)
export const JSONObjectParser = pipe(
SpaceParser,
P.apFirst(C.char('{')),
P.apFirst(SpaceParser),
P.apSecond(
P.sepBy(
pipe(SpaceParser, P.apFirst(C.char(',')), P.apFirst(SpaceParser)),
pipe(
JSONStringParser,
P.map(a => a.value),
P.bindTo('key'),
P.apFirst(SpaceParser),
P.apFirst(C.char(':')),
P.apFirst(SpaceParser),
P.bind('value', () => JSONValueParser),
),
),
),
P.apFirst(SpaceParser),
P.apFirst(C.char('}')),
P.apFirst(SpaceParser),
P.map((data): JSONObject => ({ _tag: 'object', value: data })),
)
export const JSONArrayParser = pipe(
SpaceParser,
P.apFirst(C.char('[')),
P.apFirst(SpaceParser),
P.apSecond(
P.sepBy(
pipe(SpaceParser, P.apFirst(C.char(',')), P.apFirst(SpaceParser)),
JSONValueParser,
),
),
P.apFirst(SpaceParser),
P.apFirst(C.char(']')),
P.apFirst(SpaceParser),
P.map((value): JSONArray => ({ _tag: 'array', value })),
)
export const JSONParser = pipe(JSONValueParser, P.apFirst(P.eof()))