-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntaq_experiment.py
277 lines (203 loc) · 6.29 KB
/
syntaq_experiment.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
from unicodedata import category
### START OF EXPERIMENT ###
NEWLINE_CHARS = u"\u000A\u000B\u000C\u000D\u0085\u2028\u2029"
class Token(object):
def __init__(self, source):
self.source = source
def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, self.source)
def __str__(self):
return self.source
def __len__(self):
return len(self.source)
class WhitespaceToken(Token):
pass
class NewlineToken(Token):
pass
class WordToken(Token):
pass
class SymbolToken(Token):
pass
def tokens(source):
p = 0
while p < len(source):
ch = source[p]
cat = category(ch)
if ch in NEWLINE_CHARS:
yield NewlineToken(source[p])
p += 1
elif cat[0] in "CZ":
q = p + 1
while q < len(source) and category(source[q])[0] in "CZ":
q += 1
yield WhitespaceToken(source[p:q])
p = q
elif cat[0] in "LN":
q = p + 1
while q < len(source) and category(source[q])[0] in "LN":
q += 1
yield WordToken(source[p:q])
p = q
else:
q = p + 1
while q < len(source) and source[q] == ch:
q += 1
yield SymbolToken(source[p:q])
p = q
class Line(object):
def __init__(self):
self.tokens = []
def __repr__(self):
return "<Line %s>" % "/".join(repr(token.source) for token in self.tokens)
def __bool__(self):
num_tokens = len(self.tokens)
if num_tokens == 0:
return False
elif num_tokens == 1 and self.ends_with_newline():
return False
else:
return True
def __nonzero__(self):
return self.__bool__()
def __getitem__(self, index):
return self.tokens[index]
def __len__(self):
return len(self.tokens)
def append(self, token):
self.tokens.append(token)
def pop(self, index):
return self.tokens.pop(index)
def dedent(self):
t = list(self.tokens)
while t and isinstance(t[0], WhitespaceToken):
t.pop(0)
line = Line()
line.tokens = t
return line
def ends_with_newline(self):
return isinstance(self.tokens[-1], NewlineToken)
def first_token_starts_with(self, s):
return self.tokens and self.tokens[0].source.startswith(s)
def is_list_item(self, context):
num_tokens = len(self.tokens) - 1
i = 0
while i < num_tokens and isinstance(self.tokens[i], WhitespaceToken):
i += 1
if i >= num_tokens:
return False
first_non_whitespace_token = self.tokens[i]
if isinstance(context, List) or not first_non_whitespace_token.startswith("**"):
return first_non_whitespace_token[0] in "#*"
else:
return False
def lines(tokens):
line = Line()
for token in tokens:
line.append(token)
if line.ends_with_newline():
yield line
line = Line()
if line:
yield line
class Block2(object):
pass
class HeadingBlock(Block2):
def __init__(self, line):
assert line.first_token_starts_with("=")
self.level = len(line.pop(0))
while line and (line.ends_with_newline() or line[-1].startswith("=")):
line.pop(-1)
self.line = line
class HorizontalRuleBlock(Block2):
pass
class List(Block2):
@classmethod
def signature(cls, line):
line = line.dedent()
s = []
while True:
t = line.pop(0)
if t.source[0] in "#*":
s.append(t.source)
else:
break
return "".join(s)
pass
class LiteralBlock(Block2):
def __init__(self, first_line):
assert first_line.first_token_starts_with('```')
self.metadata = "".join(token.source for token in first_line[1:])
self.lines = []
def append(self, line):
self.lines.append(line)
def text(self):
# TODO: escaped chars
s = []
for line in self.lines:
for token in line:
s.append(token.source)
return "".join(s)
class Paragraph(Block2):
def __init__(self):
self.lines = []
def append(self, line):
self.lines.append(line)
class Quotation(Block2):
def __init__(self, first_line):
assert first_line.first_token_starts_with('"""')
self.metadata = "".join(token.source for token in first_line[1:])
self.lines = []
def append(self, line):
self.lines.append(line)
class Table(Block2):
pass
def blocks(lines):
block = Paragraph()
for line in lines:
if isinstance(block, LiteralBlock):
if line.first_token_starts_with('```'):
yield block
block = Paragraph()
else:
block.append(line)
elif isinstance(block, Quotation):
if line.first_token_starts_with('"""'):
yield block
block = Paragraph()
else:
block.append(line)
elif line.first_token_starts_with('='):
# TODO: title capture
yield block
yield HeadingBlock(line)
block = Paragraph()
elif line.first_token_starts_with('----'):
yield block
yield HorizontalRuleBlock()
block = Paragraph()
elif line.first_token_starts_with('```'):
yield block
block = LiteralBlock(line)
elif line.first_token_starts_with('"""'):
yield block
block = Quotation(line)
elif line.first_token_starts_with('|'):
yield block
block = Table(line)
elif line.is_list_item(block):
if isinstance(block, List) and block.is_compatible(line):
block.append(line)
else:
yield block
block = List(line)
else:
if not isinstance(block, Paragraph):
yield block
block = Paragraph()
if line:
block.append(line)
else:
yield block
block = Paragraph()
yield block
### END OF EXPERIMENT ###