-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_parser.py
211 lines (156 loc) · 4.78 KB
/
json_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
from typing import Iterable, Union, Optional
from lexit import Lexer, LexerError, Token
class JsonLexer(Lexer):
NUMBER = r"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?"
STRING = r'"(\\\"|\\\\|[^"\n])*?"i?'
L_BRACE = r"{"
R_BRACE = r"}"
L_BRACKET = r"\["
R_BRACKET = r"\]"
TRUE = r"true"
FALSE = r"false"
NULL = r"null"
COMMA = r","
COLON = r":"
ignore = r"\s+"
class ParserError(Exception):
pass
class ExpectedError(Exception):
def __init__(self, expected_types, token):
self.expected_types = expected_types
self.token: Optional[Token] = token
def maybe(fn) -> Optional:
try:
return fn()
except ExpectedError:
return
class JsonParser:
VALUE_TYPE = Union[int, float, str, list, dict, type(None)]
def __init__(self, tokens: Iterable[Token]):
self._tokens = list(tokens)
self._idx = 0 # current token index
def consume(self, *token_types):
self.expect(*token_types)
return self.advance()
def advance(self) -> Token:
"""
Returns current_token and increments the current token index
"""
token = self.token
self._idx += 1
if self._idx > len(self._tokens):
raise ParserError
return token
@property
def token(self) -> Optional[Token]:
"""
Returns current token or None if no such token
"""
try:
return self._tokens[self._idx]
except IndexError:
return None
def check_token_type(self, *token_types: str) -> bool:
return self.token and self.token.type in token_types
@property
def lookup(self) -> Optional[Token]:
"""
Returns next token or None if no such token
"""
try:
return self._tokens[self._idx + 1]
except IndexError:
return None
def parse(self) -> VALUE_TYPE:
return self.read_value()
def read_value(self) -> VALUE_TYPE:
token_type_to_reader = {
"NUMBER": self.read_number,
"STRING": self.read_string,
"L_BRACKET": self.read_list,
"L_BRACE": self.read_dict,
"NULL": self.read_null,
"TRUE": self.read_true,
"FALSE": self.read_false,
}
if not self.token:
raise ExpectedError("Expected value got nothing instead", None)
try:
return token_type_to_reader[self.token.type]()
except KeyError:
raise ExpectedError(
f"Expected value, got {self.token.type} instead", self.token
)
def expect(self, *token_types):
if not self.check_token_type(*token_types):
raise ExpectedError(token_types, self.token)
def read_number(self) -> Union[int, float]:
token = self.consume("NUMBER")
if token.value.isdigit():
return int(token.value)
return float(token.value)
def read_string(self) -> str:
return self.consume("STRING").value[1:-1]
def read_list(self) -> list:
self.consume("L_BRACKET")
rv = []
while not self.check_token_type("R_BRACKET"):
value = self.read_value()
rv.append(value)
if not maybe(self.read_comma):
break
if not self.check_token_type("R_BRACKET"):
raise ParserError(f'Expected "," or "]", got {self.token.type} instead')
self.advance()
return rv
def read_comma(self):
return self.consume("COMMA")
def read_null(self) -> None:
self.consume("NULL")
return None
def read_true(self) -> True:
self.consume("TRUE")
return True
def read_false(self) -> True:
self.consume("FALSE")
return False
def read_dict(self) -> dict:
self.consume("L_BRACE")
rv = {}
while not self.check_token_type("R_BRACE"):
key, value = self.read_pair()
rv[key] = value
maybe(self.read_comma)
if not self.check_token_type("R_BRACE"):
raise ParserError(f'Expected "," or "}}", got {self.token.type} instead')
self.advance()
return rv
def read_pair(self) -> tuple:
key = self.read_string()
self.read_colon()
value = self.read_value()
return key, value
def read_colon(self):
return self.consume("COLON")
json_text = r"""
[
1,
"two",
true,
false,
null,
{"hello": "world", "hi": "there"},
[]
]
"""
def main():
try:
tokens = list(JsonLexer.lex(json_text))
print(tokens)
except LexerError as e:
print(e.pretty())
return exit(1)
parser = JsonParser(tokens)
print(repr(parser.parse()))
if __name__ == "__main__":
main()