-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinators_json.py
278 lines (225 loc) · 7.46 KB
/
combinators_json.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from collections import deque
from typing import Callable, List, Any, Union
class Parser:
def __init__(self,to_parse):
self.to_parse=to_parse
self.idx=0
self.size=len(self.to_parse)
self.stack=[]
def isEnd(self) :
return(self.idx>self.size)
def charAt_(self):
return(self.to_parse[self.idx])
def charAt(self):
self.skipWhitespace()
return(self.to_parse[self.idx])
def skipWhitespace(self):
while (self.idx<self.size and (self.charAt_() == " " or self.charAt_()=="\n")):
self.idx+=1
def parse_string_lit(self):
#breakpoint()
if self.idx>=self.size: return(False)
if self.charAt() != '"': return(False)
if '"' in self.to_parse[self.idx+1:]:
last=self.to_parse[self.idx+1:].index('"')
self.stack.append([self.to_parse[self.idx+1:self.idx+1+last]])
else:
return(False)
self.idx=self.idx+last+2
# self.skipWhitespace()
return(True)
def parseNumber(self):
#breakpoint()
is_num=False
num=""
if self.idx>=self.size: return(False)
while self.idx<self.size and self.charAt().isnumeric(): #last element idx=size-1
num+=self.charAt()
is_num=True
self.idx+=1
if is_num:
self.stack.append([num])
# self.skipWhitespace()
return(True)
def parseChar(self, char):
if self.idx>=self.size:
return(False)
if self.charAt()==char:
self.idx+=1
# self.skipWhitespace()
return(True)
else:
return(False)
def parse(self) -> bool:
raise NotImplementedError("Method 'parse' must be implemented by subclasses.")
class Combinators():
"""
Grammar :
VALUE ::= STRINGLIT / NUMBER / OBJECT / ARRAY
OBJECT ::= "{" (PAIR ("," PAIR)* )? "}"
PAIR ::= STRINGLIT ":" VALUE
ARRAY ::= "[" (VALUE ("," VALUE)* )? "]"
This parser implements PEG semantics for the grammar.
"""
class StringLitParser(Parser):
def parse(self) -> bool:
return parse_string_lit()
class NumberParser(Parser):
def parse(self) -> bool:
return parseNumber()
class CharParser(Parser):
def __init__(self, c: str):
self.c = c
def parse(self) -> bool:
return parseChar(self.c)
class Sequence(Parser):
def __init__(self, *children: Parser):
self.children = children
def parse(self) -> bool:
self.idx0 = self.idx
for child in self.children:
if not child.parse():
self.idx = self.idx0
return False
return True
class ForwardReference(Parser):
def __init__(self, supplier: Callable[[], Parser]):
self.supplier = supplier
def parse(self) -> bool:
return self.supplier().parse()
class Repetition(Parser):
def __init__(self, child: Parser):
self.child = child
def parse(self) -> bool:
while self.child.parse():
pass
return True
class Optional(Parser):
def __init__(self, child: Parser):
self.child = child
def parse(self) -> bool:
self.child.parse()
return True
class Choice(Parser):
def __init__(self, *children: Parser):
self.children = children
def parse(self) -> bool:
for child in self.children:
if child.parse():
return True
return False
class ComposeObject(Parser):
def __init__(self, child: Parser):
self.child = child
def parse(self) -> bool:
stack0 = len(self.stack)
success = self.child.parse()
if not success:
return False
obj = {}
while len(self.stack) > stack0:
value = self.stack.pop()
string = self.stack.pop()
obj[string] = value
self.stack.append(obj)
return True
class ComposeArray(Parser):
def __init__(self, child: Parser):
self.child = child
def parse(self) -> bool:
stack0 = len(self.stack)
success = self.child.parse()
if not success:
return False
array = []
while len(self.stack) > stack0:
array.append(self.stack.pop())
array.reverse()
self.stack.append(array)
return True
###pair, pair_tails
class Json_parser(Combinators):
def __init__(self,to_parse):
super().__init__(to_parse)
def parsePair(self):
# PAIR ::= STRINGLIT ":" VALUE
# return(Sequence(StringLitParser(), CharParser(':'), ForwardReference(lambda: value)))
pair = self.Sequence(self.StringLitParser(), self.CharParser(':'), self.ForwardReference(lambda: self.parseValue()))
return(pair)
def parsePairTails(self):
# ("," PAIR)*
pair_tails = self.Repetition(self.Sequence(self.CharParser(','), self.parsePair()))
return(pair_tails)
def parsePairs(self):
# (PAIR ("," PAIR)* )?
pairs = self.Optional(self.Sequence(self.parsePair(), self.parsePair()))
return(pairs)
def parseObject(self):
# OBJECT ::= "{" (PAIR ("," PAIR)* )? "}"
object_parser = self.ComposeObject(self.Sequence(self.CharParser('{'), self.parsePairs(), CharParser('}')))
return(object_parser)
def parseTails(self):
# ("," VALUE)*
value_tails = self.Repetition(self.Sequence(self.CharParser(','), self.ForwardReference(lambda: self.parseValue())))
return(value_tails)
def parseValues(self):
# (VALUE ("," VALUE)* )?
values = self.Optional(self.Sequence(self.ForwardReference(lambda: self.parseValue()), self.parseTails()))
return(values)
def parseArray(self):
# ARRAY ::= "[" (VALUE ("," VALUE)* )? "]"
array_parser = self.ComposeArray(self.Sequence(self.CharParser('['), self.parseValues, self.CharParser(']')))
return(array_parser)
def parseValue(self):
# VALUE ::= STRINGLIT / NUMBER / OBJECT / ARRAY
value = self.Choice(self.StringLitParser(), self.NumberParser(), self.parseObject(), self.parseArray)
return(value)
##########################################################
##########################################################
EXAMPLE_INPUT="""
{
"version": 17,
"bundles" : [
{ "name" : "org.gci.Bundle" },
{ "name" : "org.gci.commands.Bundle" },
{ "name" : "org.gci.installer.remote.Bundle" },
{ "name" : "org.gci.installer.os.Bundle" }
]
}
"""
INPUT2="""
{
"key": "String",
"Number": 1,
"array": [1,2,3],
"nested": {
"literals": "true"
}
}
"""
INPUT3="""
{
"obj1" : { "a" : "A", "b" : "B"}
}
"""
#INPUT2="""
#<?xml version="1.0" encoding="UTF-8" ?>
#<hello>world</hello>
#"""
#INPUT2="""
#
#"""
#mp = Parser(EXAMPLE_INPUT)
#mp = Parser(INPUT2)
input_str = EXAMPLE_INPUT
print(input_str)
#mc = Combinators(input_str)
#mc = Combinators(input_str)
mc = Json_parser(input_str)
mp = mc
if mp.parseValue():
print("OK")
print("input = ", mp.to_parse)
print("stack = ", mp.stack)
else:
print("NO OK")