-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.py
218 lines (176 loc) · 5.95 KB
/
parse.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
import re
from datetime import date
# debug options
debug_chunk = False
debug_line = False
# these variables are left capitalized
nocaps = ['L', 'T', 'R', 'C', 'C2', 'H', 'E', 'J',
'TR', 'T0', 'T1', 'R0', 'R1']
# states and their names
BLANKLINE = 1
HEADER = 2
TABLE = 3
LIST = 4
TEXT = 5
SEEALSO = 6
SUMMARY = 7
TABLESEP = 8
END = 9
statenames = ['BLANKLINE', 'HEADER', 'TABLE', 'LIST', 'TEXT',
'SEEALSO', 'SUMMARY', 'TABLESEP', 'END']
# regular expressions for code parsing
# \s is whitespace
# \S is not whitespace
re_table = re.compile(r'\s*(?P<col1>\S[\s\S]+?)\s\s\s+(?P<col2>.+)')
re_tablesep = re.compile(r'-+')
re_bullet = re.compile(r'\s+-\s*(?P<text>.*)')
re_text = re.compile(r'\s*(?P<text>.*)')
re_header = re.compile(r'\s*(?P<text>[A-Z][^:]+)::$')
re_seealso = re.compile(r'\s*See also\s*(?P<text>.*?)\.$')
# re_code = re.compile(r' {8}\s*(?P<text>.+)')
class MATLABLine(object):
def __init__(self, indent, type, text):
self.indent = indent # can be a list in case of table
self.type = type
self.textdata = text # is a list, 2 elements in case of table [col1 col2]
def __repr__(self):
return 'indent=' + str(self.indent) + ' type=' + stateName(self.type) + ' text=' + self.textdata[0]
def same(self, other):
if self.type == TEXT and other.type == TEXT and self.indent > 8 and other.indent > 8:
return True
return self.type == other.type and self.indent == other.indent
def text(self, indent=False):
if indent:
return ' ' * (self.indent - 6) + self.textdata[0].strip()
else:
return self.textdata[0].strip()
def col1(self):
return self.textdata[0].strip()
def col2(self):
return self.textdata[1].strip()
class MATLABLineEnd(Exception):
pass
# p = Parser(mfile)
# l = p.nextLine()
# returns an object with properties: type, text, indent
class Parser(object):
def __init__(self, doc):
self.linenum = 0
self.lines = doc.split('\n')
def nextLine(self):
# get comment line, classify it
(text, indent, typ) = self.readline()
if typ == TABLE:
z = self.peekline() # peek at next line
if z[2] == TEXT and z[1] == indent[1]:
# continuation line
text[0] += ' ' + z[0][0]
self.readline() # consume that line
elif typ == LIST:
z = self.peekline() # peek at next line
if z[2] == TEXT and z[1] == indent:
# continuation line
text[0] += ' ' + z[0][0]
self.readline() # consume that line
if debug_line:
self.showline(indent, typ, text)
if debug_chunk:
self.showchunk(indent, typ, text)
return MATLABLine(indent, typ, text)
def readline(self):
# return the next MATLAB comment line from the string
# returns on the first non-comment line found
try:
line = self.lines.pop(0)
except IndexError:
return ('', 0, END)
self.linenum += 1
if line and line[0] != '%':
return ('', 0, END)
line = line.lstrip('%')
line = line.rstrip()
if debug_line:
print '---|%s|' % line
if self.linenum == 1:
return ([line.strip()], 0, SUMMARY)
else:
return self.classify(line)
def peekline(self, i=0):
# return the next MATLAB comment line from the string
# returns on the first non-comment line found
try:
line = self.lines[i]
except IndexError:
return ('', 0, END)
if line and line[0] != '%':
return ('', 0, END)
line = line.lstrip('%')
line = line.rstrip()
return self.classify(line)
def classify(self, line):
if line == '':
return ([''], 0, BLANKLINE)
# TEXT::
m = re_header.match(line)
if m:
indent = m.start('text')
chunk = [m.group('text')]
return (chunk, indent, HEADER)
# OPT TEXT at least 3 spaces between
m = re_table.match(line)
if m:
# the two chunks of text are <opt>, <text>
# if indent >= 8:
# # if opt is indented >= 8 this signals verbatim mode
# # ie. PARA with indent 8
# m2 = re_para.match(line)
# if m2:
# state = PARA
# indent = m2.start('text')
# curIndent = indent
# chunk = [m2.group('text')]
# return (chunk, indent, state)
# otherwise a table line
chunk = [m.group('col1'), m.group('col2')]
if m.start('col1') < 8:
return (chunk, [m.start('col1'), m.start('col2')], TABLE)
# --
m = re_tablesep.match(line)
if m:
return (None, 0, TABLESEP)
# - TEXT
m = re_bullet.match(line)
if m:
indent = m.start('text')
chunk = [m.group('text')]
return (chunk, indent, LIST)
# See also TEXT.
m = re_seealso.match(line)
if m:
indent = m.start('text')
chunk = [m.group('text')]
return (chunk, indent, SEEALSO)
m = re_text.match(line)
if m:
indent = m.start('text')
chunk = [m.group(0)]
return (chunk, indent, TEXT)
def showchunk(self, indent, typ, text):
print '<<< getchunk:%s' % stateName(typ),
print ', indent=',
print indent,
print '{',
print text,
print '} >>>'
def showline(self, indent, typ, text):
print '<< getchunk:%s' % stateName(typ),
print ', indent=',
print indent,
print '{',
print text,
print '} >>'
def stateName(c):
if c:
return statenames[c - 1]
else:
return 'NIL'