-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
256 lines (211 loc) · 7.39 KB
/
parser.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from functools import partial
from types import NoneType
from typing import Any, Tuple, Union, cast, Callable, Optional, Iterable
class NothingType:
pass
Nothing = NothingType()
JsonValue = Union[NothingType, None, str, int, float, bool, list, dict]
ParserResult = Tuple[JsonValue, str]
def head_split(in_: Iterable) -> Tuple[Any, Any]:
if not isinstance(in_, (str, list, tuple)):
in_ = list(in_)
if len(in_) == 0:
return None, None
return in_[0], in_[1:]
hs = head_split
def parse_char(text: str, token: str) -> ParserResult:
if not text:
return (Nothing, "")
match hs(text):
case a, rest if a == token:
return (a, rest)
case _:
return (Nothing, text)
def parse_char_ignore(text: str, token: str):
match parse_char(text, token):
case NothingType(), _:
return (Nothing, text)
case _, rest:
return ("", rest)
def parse_literal(text: str, token: str) -> ParserResult:
match token:
case "":
return (Nothing, text)
case tkn if len(tkn) == 1:
return parse_char(text, tkn)
case _:
match parse_char(text, token[0]):
case NothingType(), _:
return (Nothing, text)
case tkn, rest:
match parse_literal(rest, token[1:]):
case NothingType(), _:
return (Nothing, text)
case string, rest:
return (cast(str, tkn) + cast(str, string), rest)
def parse_string(text: str) -> ParserResult:
return parse_all(text,
partial(parse_char_ignore, token="\""),
partial(parse_span, func=lambda t: t != "\""),
partial(parse_char_ignore, token="\""),
)
def parse_null(text: str) -> ParserResult:
match parse_literal(text, "null"):
case "null", rest:
return (None, rest)
case _:
return (Nothing, text)
TokenParseCallable = Callable[[str, str], ParserResult]
ParseCallable = Callable[[str], ParserResult]
def parse_any(text: str, *parser: TokenParseCallable) -> ParserResult:
"""return the first parser result tha ist not Nothing"""
match hs(parser):
case None, None:
return (Nothing, text)
case p, prest:
match p(text):
case NothingType(), _:
if prest:
return parse_any(text, *prest)
return (Nothing, text)
case value, rest:
return (value, rest)
parse_literal_true = partial(parse_literal, token="true")
parse_literal_false = partial(parse_literal, token="false")
def parse_boolean(text: str) -> ParserResult:
match parse_any(text, parse_literal_true, parse_literal_false):
case "true", rest:
return (True, rest)
case "false", rest:
return (False, rest)
case _:
return (Nothing, text)
def strip_char(text: str, token: str):
match parse_char(text, token):
case NothingType(), _:
return text
case t, rest if t == token:
return strip_char(rest, token)
def strip_span(text: str, func: Callable):
match parse_span(text, func):
case NothingType(), _:
return text
case t, rest:
return strip_span(rest, func)
strip_whitespace = partial(strip_span, func=str.isspace)
strip_comma = partial(strip_span, func=lambda t: t == ",")
def multiple(parser: Callable[[str], ParserResult]):
def inner(text: str) -> ParserResult:
match parser(text):
case NothingType(), _:
return (Nothing, text)
case _, rest:
return inner(rest)
return inner
def parse_all(text: str, *parser: Callable, prev: str = ""):
match hs(parser):
case None, None:
return (prev if prev else Nothing, text)
case p, prest:
match p(text):
case str() as rest:
if prest:
return parse_all(rest, *prest, prev=prev)
return (prev, rest)
case NothingType(), _:
return (Nothing, prev + text)
case v, rest:
if prest:
return parse_all(rest, *prest, prev=prev + v)
return (prev + v, rest)
def parse_int(text: str) -> ParserResult:
match parse_span(text, str.isdigit):
case NothingType(), _:
return (Nothing, text)
case num, rest:
return (int(num), rest)
def parse_float(text: str) -> ParserResult:
match parse_all(
text,
partial(parse_span, func=str.isdigit),
partial(parse_char, token="."),
partial(parse_span, func=str.isdigit),
):
case NothingType(), _:
return (Nothing, text)
case value, rest:
return float(value), rest
def parse_span(
text: str, func: Callable[[str], bool], prev: Optional[JsonValue] = ""
) -> ParserResult:
"""concat values to span while func applied to char is true"""
match hs(text):
case "" | None, _:
return (prev if prev else Nothing, text)
case token, rest:
if func(token):
return parse_span(rest, func, prev + token)
return (prev if prev else Nothing, text)
case _:
return (Nothing, text)
def apply_to_list(text: str, parser, sep, prev=None) -> ParserResult:
prev = prev or []
match parser(text):
case NothingType(), _:
return (prev, text)
case value, rest:
prev = prev + [value]
match sep(rest):
case False, _:
return (prev, rest)
case True, rest:
return apply_to_list(rest, parser, sep, prev)
def strip_list_seperator(text: str):
rest = strip_whitespace(text)
match parse_char(rest, ","):
case NothingType(), _:
return False, text
case _, rest:
rest = strip_whitespace(rest)
return True, rest
def parse_list(text: str) -> ParserResult:
match parse_char_ignore(text, "["):
case NothingType(), _:
return (Nothing, text)
case _, rest:
result, rest = apply_to_list(
rest,
parse_json_value,
strip_list_seperator,
[]
)
match parse_char_ignore(rest, "]"):
case NothingType(), _:
return (Nothing, text)
case _, rest:
return (result, rest)
def parse_object(text: str, prev = None) -> ParserResult: pass
# def parse_object(text: str, prev = None) -> ParserResult:
# prev = prev or {}
# match parse_char_ignore(text, "{"):
# case NothingType(), _:
# (Nothing, text)
# case _, rest:
# match parse_string(rest):
# case NothingType(), _:
# return (Nothing, text)
# case str() as key, rest:
# rest = strip_whitespace(rest)
#
def parse_json_value(text: str):
return parse_any(
text,
parse_null,
parse_boolean,
parse_float,
parse_int,
parse_string,
parse_float,
parse_int,
parse_list,
)