forked from jpbarraca/mail-trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringscanner.py
98 lines (81 loc) · 2.72 KB
/
stringscanner.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
#Modified by Joao Paulo Barraca <[email protected]>
class StringScanner(object):
def str(self):
return self.__data
def __init__(self, string_chunks):
# TODO(mihaip) switch to reading from the chunks array directly to avoid
# extra string copies
def flatten(chunks):
if type(chunks) == str:
return chunks
else:
chunk_strings = []
for chunk in chunks:
chunk_strings.append(flatten(chunk))
return "".join(chunk_strings)
self.__data = flatten(string_chunks)
self.__index = 0
self.__length = len(self.__data)
def Peek(self):
if self.__index >= self.__length:
return None
return self.__data[self.__index]
def ReadChar(self):
if self.__index >= self.__length:
return None
c = self.__data[self.__index]
self.__index += 1
return c
def ReadUntil(self, c):
start = self.__index
end = start
dlen = len(self.__data)
while dlen > end:
if self.__data[end] != c:
end = end + 1
else:
break
self.__index = end
return self.__data[start:end]
def ConsumeAll(self, c):
while self.__index < self.__length and self.__data[self.__index] == c:
self.__index += 1
def ConsumeChar(self, c):
assert c == self.__data[self.__index]
self.__index += 1
def ReadUntilLength(self, length):
ret = self.__data[self.__index:self.__index + length]
self.__index += length
return ret
def ConsumeValue(self):
value = None
# Literal string
if self.Peek() == "{":
self.ConsumeChar("{")
literal_length = int(self.ReadUntil("}"))
self.ConsumeChar("}")
value = self.ReadUntilLength(literal_length)
# Quoted string
elif self.Peek() == "\"":
# TODO(mihaip): can quotes be escaped inside?
self.ConsumeChar("\"")
value = self.ReadUntil("\"")
self.ConsumeChar("\"")
# Parenthesized list
elif self.Peek() == "(":
self.ConsumeChar("(")
value = []
parenthesis_depth = 1
while parenthesis_depth > 0:
c = self.ReadChar()
if c == "(":
parenthesis_depth += 1
if c == ")":
parenthesis_depth -= 1
if parenthesis_depth > 0:
value.append(c)
value = "".join(value).split()
# Numbers
else:
value = self.ReadUntil(" ")
return value