1 | from StarFile import * |
---|
2 | from types import * |
---|
3 | import copy |
---|
4 | # An alternative specification for the Cif Parser, based on Yapps2 |
---|
5 | # by Amit Patel (http://theory.stanford.edu/~amitp/Yapps) |
---|
6 | # |
---|
7 | # helper code: we define our match tokens |
---|
8 | lastval = '' |
---|
9 | def monitor(location,value): |
---|
10 | global lastval |
---|
11 | # print 'At %s: %s' % (location,`value`) |
---|
12 | lastval = `value` |
---|
13 | return value |
---|
14 | |
---|
15 | # Strip extras gets rid of leading and trailing whitespace, and |
---|
16 | # semicolons. |
---|
17 | def stripextras(value): |
---|
18 | # we get rid of semicolons and leading/trailing terminators etc. |
---|
19 | import re |
---|
20 | jj = re.compile("[\n\r\f \t\v]*") |
---|
21 | semis = re.compile("[\n\r\f \t\v]*[\n\r\f]\n*;") |
---|
22 | cut = semis.match(value) |
---|
23 | if cut: #we have a semicolon-delimited string |
---|
24 | nv = value[cut.end():len(value)-2] |
---|
25 | try: |
---|
26 | if nv[-1]=='\r': nv = nv[:-1] |
---|
27 | except IndexError: #empty data value |
---|
28 | pass |
---|
29 | return nv |
---|
30 | else: |
---|
31 | cut = jj.match(value) |
---|
32 | if cut: |
---|
33 | return stripstring(value[cut.end():]) |
---|
34 | return value |
---|
35 | |
---|
36 | # helper function to get rid of inverted commas etc. |
---|
37 | |
---|
38 | def stripstring(value): |
---|
39 | if value: |
---|
40 | if value[0]== '\'' and value[-1]=='\'': |
---|
41 | return value[1:-1] |
---|
42 | if value[0]=='"' and value[-1]=='"': |
---|
43 | return value[1:-1] |
---|
44 | return value |
---|
45 | |
---|
46 | # helper function to populate a nested LoopBlock structure given an |
---|
47 | # empty structure together with listed values. The values are |
---|
48 | # organised into a list of lists, where each time 'stop' was |
---|
49 | # encountered one list terminates and a new one starts. |
---|
50 | # For a correctly constructed loop, the final 'popout' will pop out |
---|
51 | # of the iteration completely and raise a StopIteration error. |
---|
52 | # |
---|
53 | # Note that there may be an empty list at the very end of our itemlists, |
---|
54 | # so we remove that if necessary. |
---|
55 | # |
---|
56 | # We optimise for CIF files by loading differently if we have a flat loop |
---|
57 | |
---|
58 | def makeloop(loopstructure,itemlists): |
---|
59 | if itemlists[-1] == []: itemlists.pop(-1) |
---|
60 | # print 'Making loop with %s' % `itemlists` |
---|
61 | if loopstructure.dimension == 1 and loopstructure.loops == []: |
---|
62 | storage_iter = loopstructure.fast_load_iter() |
---|
63 | else: |
---|
64 | storage_iter = loopstructure.load_iter() |
---|
65 | nowloop = loopstructure |
---|
66 | for datalist in itemlists: |
---|
67 | for datavalue in datalist: |
---|
68 | try: |
---|
69 | nowloop,target = storage_iter.next() |
---|
70 | except StopIteration: |
---|
71 | print "StopIter at %s/%s" % (datavalue,datalist) |
---|
72 | raise StopIteration |
---|
73 | # print 'Got %s %s ->' % (`nowloop`,`target`), |
---|
74 | target.append(datavalue) |
---|
75 | # print '%s' % `target` |
---|
76 | # the end of each list is the same as a stop_ token |
---|
77 | # print 'Saw end of list' |
---|
78 | nowloop.popout = True |
---|
79 | nowloop,blank = storage_iter.next() #execute the pop |
---|
80 | # print 'discarding %s/%s' % (`nowloop`,`blank`) |
---|
81 | # print 'Makeloop returning %s' % `loopstructure` |
---|
82 | return loopstructure |
---|
83 | |
---|
84 | # return an object with the appropriate amount of nesting |
---|
85 | def make_empty(nestlevel): |
---|
86 | gd = [] |
---|
87 | for i in range(1,nestlevel): |
---|
88 | gd = [gd] |
---|
89 | return gd |
---|
90 | |
---|
91 | # this function updates a dictionary first checking for name collisions, |
---|
92 | # which imply that the CIF is invalid. We need case insensitivity for |
---|
93 | # names. |
---|
94 | |
---|
95 | # Unfortunately we cannot check loop item contents against non-loop contents |
---|
96 | # in a non-messy way during parsing, as we may not have easy access to previous |
---|
97 | # key value pairs in the context of our call (unlike our built-in access to all |
---|
98 | # previous loops). |
---|
99 | # For this reason, we don't waste time checking looped items against non-looped |
---|
100 | # names during parsing of a data block. This would only match a subset of the |
---|
101 | # final items. We do check against ordinary items, however. |
---|
102 | # |
---|
103 | # Note the following situations: |
---|
104 | # (1) new_dict is empty -> we have just added a loop; do no checking |
---|
105 | # (2) new_dict is not empty -> we have some new key-value pairs |
---|
106 | # |
---|
107 | def cif_update(old_dict,new_dict,loops): |
---|
108 | old_keys = map(lambda a:a.lower(),old_dict.keys()) |
---|
109 | if new_dict != {}: # otherwise we have a new loop |
---|
110 | #print 'Comparing %s to %s' % (`old_keys`,`new_dict.keys()`) |
---|
111 | for new_key in new_dict.keys(): |
---|
112 | if new_key.lower() in old_keys: |
---|
113 | raise CifError, "Duplicate dataname or blockname %s in input file" % new_key |
---|
114 | old_dict[new_key] = new_dict[new_key] |
---|
115 | # |
---|
116 | # this takes two lines, so we couldn't fit it into a one line execution statement... |
---|
117 | def order_update(order_array,new_name): |
---|
118 | order_array.append(new_name) |
---|
119 | return new_name |
---|
120 | |
---|
121 | |
---|
122 | # Begin -- grammar generated by Yapps |
---|
123 | import sys, re |
---|
124 | import yapps3_compiled_rt as yappsrt |
---|
125 | |
---|
126 | class StarParserScanner(yappsrt.Scanner): |
---|
127 | patterns = [ |
---|
128 | ('","', re.compile(',')), |
---|
129 | ('"\\*"', re.compile('\\*')), |
---|
130 | ('([ \t\n\r](?!;))|[ \t]', re.compile('([ \t\n\r](?!;))|[ \t]')), |
---|
131 | ('(#.*[\n\r](?!;))|(#.*)', re.compile('(#.*[\n\r](?!;))|(#.*)')), |
---|
132 | ('LBLOCK', re.compile('(L|l)(O|o)(O|o)(P|p)_')), |
---|
133 | ('GLOBAL', re.compile('(G|g)(L|l)(O|o)(B|b)(A|a)(L|l)_')), |
---|
134 | ('STOP', re.compile('(S|s)(T|t)(O|o)(P|p)_')), |
---|
135 | ('save_heading', re.compile('(S|s)(A|a)(V|v)(E|e)_[][!%&\\(\\)*+,./:<=>?@0-9A-Za-z\\\\^`{}\\|~"#$\';_-]+')), |
---|
136 | ('save_end', re.compile('(S|s)(A|a)(V|v)(E|e)_')), |
---|
137 | ('data_name', re.compile('_[][!%&\\(\\)*+,./:<=>?@0-9A-Za-z\\\\^`{}\\|~"#$\';_-]+')), |
---|
138 | ('data_heading', re.compile('(D|d)(A|a)(T|t)(A|a)_[][!%&\\(\\)*+,./:<=>?@0-9A-Za-z\\\\^`{}\\|~"#$\';_-]+')), |
---|
139 | ('start_sc_line', re.compile('(\n|\r\n);([^\n\r])*(\r\n|\r|\n)+')), |
---|
140 | ('sc_line_of_text', re.compile('[^;\r\n]([^\r\n])*(\r\n|\r|\n)+')), |
---|
141 | ('end_sc_line', re.compile(';')), |
---|
142 | ('c_r_b', re.compile('\\)')), |
---|
143 | ('o_r_b', re.compile('\\(')), |
---|
144 | ('c_c_b', re.compile('\\}')), |
---|
145 | ('o_c_b', re.compile('\\{')), |
---|
146 | ('c_s_b', re.compile('\\]')), |
---|
147 | ('o_s_b', re.compile('\\[')), |
---|
148 | ('dat_val_nocomma_nosq', re.compile('([^\\s"#$,\'_\\(\\{\\[\\]][^\\s,\\[\\]]*)|\'((\'(?![\\s,]))|([^\n\r\x0c\']))*\'+|"(("(?![\\s,]))|([^\n\r"]))*"+')), |
---|
149 | ('dat_val_internal_sq', re.compile('\\[([^\\s\\[\\]]*)\\]')), |
---|
150 | ('dat_val_nocomma_nocurl', re.compile('([^\\s"#$,\'_\\(\\{\\[\\]][^\\s,}]*)|\'((\'(?![\\s,]))|([^\n\r\x0c\']))*\'+|"(("(?![\\s,]))|([^\n\r"]))*"+')), |
---|
151 | ('dat_val_nocomma_nornd', re.compile('([^\\s"#$,\'\\(\\{\\[\\]][^\\s,)]*)|\'((\'(?![\\s,]))|([^\n\r\x0c\']))*\'+|"(("(?![\\s,]))|([^\n\r"]))*"+')), |
---|
152 | ('data_value_1', re.compile('((?!(((S|s)(A|a)(V|v)(E|e)_[^\\s]*)|((G|g)(L|l)(O|o)(B|b)(A|a)(L|l)_[^\\s]*)|((S|s)(T|t)(O|o)(P|p)_[^\\s]*)|((D|d)(A|a)(T|t)(A|a)_[^\\s]*)))[^\\s"#$\'_\\(\\{\\[\\]][^\\s]*)|\'((\'(?=\\S))|([^\n\r\x0c\']))*\'+|"(("(?=\\S))|([^\n\r"]))*"+')), |
---|
153 | ('END', re.compile('$')), |
---|
154 | ] |
---|
155 | def __init__(self, str): |
---|
156 | yappsrt.Scanner.__init__(self,None,['([ \t\n\r](?!;))|[ \t]', '(#.*[\n\r](?!;))|(#.*)'],str) |
---|
157 | |
---|
158 | class StarParser(yappsrt.Parser): |
---|
159 | Context = yappsrt.Context |
---|
160 | def input(self, _parent=None): |
---|
161 | _context = self.Context(_parent, self._scanner, self._pos, 'input', []) |
---|
162 | _token = self._peek('END', 'data_heading') |
---|
163 | if _token == 'data_heading': |
---|
164 | dblock = self.dblock(_context) |
---|
165 | allblocks = StarFile(); allblocks.NewBlock(dblock[0],blockcontents=dblock[1],fix=False,replace=False) |
---|
166 | while self._peek('END', 'data_heading') == 'data_heading': |
---|
167 | dblock = self.dblock(_context) |
---|
168 | allblocks.NewBlock(dblock[0],blockcontents=monitor('input',dblock[1]),fix=False,replace=False) |
---|
169 | if self._peek() not in ['END', 'data_heading']: |
---|
170 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['END', 'data_heading'])) |
---|
171 | END = self._scan('END') |
---|
172 | else: # == 'END' |
---|
173 | END = self._scan('END') |
---|
174 | allblocks = StarFile() |
---|
175 | return allblocks |
---|
176 | |
---|
177 | def dblock(self, _parent=None): |
---|
178 | _context = self.Context(_parent, self._scanner, self._pos, 'dblock', []) |
---|
179 | data_heading = self._scan('data_heading') |
---|
180 | heading = data_heading[5:];thisblock=StarBlock(overwrite=False) |
---|
181 | while self._peek('save_heading', 'save_end', 'LBLOCK', 'data_name', 'END', 'data_heading') in ['save_heading', 'LBLOCK', 'data_name']: |
---|
182 | _token = self._peek('save_heading', 'LBLOCK', 'data_name') |
---|
183 | if _token != 'save_heading': |
---|
184 | dataseq = self.dataseq(thisblock, _context) |
---|
185 | else: # == 'save_heading' |
---|
186 | save_frame = self.save_frame(_context) |
---|
187 | thisblock["saves"].NewBlock(save_frame[0],save_frame[1],fix=False,replace=True) |
---|
188 | if self._peek() not in ['save_heading', 'save_end', 'LBLOCK', 'data_name', 'END', 'data_heading']: |
---|
189 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['save_heading', 'LBLOCK', 'data_name', 'save_end', 'END', 'data_heading'])) |
---|
190 | return (heading,monitor('dblock',thisblock)) |
---|
191 | |
---|
192 | def dataseq(self, starblock, _parent=None): |
---|
193 | _context = self.Context(_parent, self._scanner, self._pos, 'dataseq', [starblock]) |
---|
194 | data = self.data(starblock, _context) |
---|
195 | while self._peek('LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading') in ['LBLOCK', 'data_name']: |
---|
196 | data = self.data(starblock, _context) |
---|
197 | if self._peek() not in ['LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading']: |
---|
198 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading'])) |
---|
199 | |
---|
200 | def data(self, currentblock, _parent=None): |
---|
201 | _context = self.Context(_parent, self._scanner, self._pos, 'data', [currentblock]) |
---|
202 | _token = self._peek('LBLOCK', 'data_name') |
---|
203 | if _token == 'LBLOCK': |
---|
204 | top_loop = self.top_loop(_context) |
---|
205 | currentblock.insert_loop(top_loop,audit=False) |
---|
206 | else: # == 'data_name' |
---|
207 | datakvpair = self.datakvpair(_context) |
---|
208 | currentblock.AddLoopItem(datakvpair[:2],precheck=True) |
---|
209 | |
---|
210 | def datakvpair(self, _parent=None): |
---|
211 | _context = self.Context(_parent, self._scanner, self._pos, 'datakvpair', []) |
---|
212 | data_name = self._scan('data_name') |
---|
213 | data_value = self.data_value(_context) |
---|
214 | return [data_name,data_value] |
---|
215 | |
---|
216 | def data_value(self, _parent=None): |
---|
217 | _context = self.Context(_parent, self._scanner, self._pos, 'data_value', []) |
---|
218 | _token = self._peek('data_value_1', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
219 | if _token == 'data_value_1': |
---|
220 | data_value_1 = self._scan('data_value_1') |
---|
221 | thisval = stripstring(data_value_1) |
---|
222 | elif _token == 'start_sc_line': |
---|
223 | sc_lines_of_text = self.sc_lines_of_text(_context) |
---|
224 | thisval = stripextras(sc_lines_of_text) |
---|
225 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
226 | bracket_expression = self.bracket_expression(_context) |
---|
227 | thisval = bracket_expression |
---|
228 | return monitor('data_value',thisval) |
---|
229 | |
---|
230 | def sc_lines_of_text(self, _parent=None): |
---|
231 | _context = self.Context(_parent, self._scanner, self._pos, 'sc_lines_of_text', []) |
---|
232 | start_sc_line = self._scan('start_sc_line') |
---|
233 | lines = start_sc_line |
---|
234 | while self._peek('end_sc_line', 'sc_line_of_text') == 'sc_line_of_text': |
---|
235 | sc_line_of_text = self._scan('sc_line_of_text') |
---|
236 | lines = lines+sc_line_of_text |
---|
237 | if self._peek() not in ['end_sc_line', 'sc_line_of_text']: |
---|
238 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['sc_line_of_text', 'end_sc_line'])) |
---|
239 | end_sc_line = self._scan('end_sc_line') |
---|
240 | return monitor('sc_line_of_text',lines+end_sc_line) |
---|
241 | |
---|
242 | def bracket_expression(self, _parent=None): |
---|
243 | _context = self.Context(_parent, self._scanner, self._pos, 'bracket_expression', []) |
---|
244 | _token = self._peek('o_s_b', 'o_c_b', 'o_r_b') |
---|
245 | if _token == 'o_s_b': |
---|
246 | square_bracket_expr = self.square_bracket_expr(_context) |
---|
247 | return square_bracket_expr |
---|
248 | elif _token == 'o_c_b': |
---|
249 | curly_bracket_expr = self.curly_bracket_expr(_context) |
---|
250 | return 'curly brackets' |
---|
251 | else: # == 'o_r_b' |
---|
252 | round_bracket_expr = self.round_bracket_expr(_context) |
---|
253 | return round_bracket_expr |
---|
254 | |
---|
255 | def nested_bracket_expression(self, _parent=None): |
---|
256 | _context = self.Context(_parent, self._scanner, self._pos, 'nested_bracket_expression', []) |
---|
257 | _token = self._peek('o_s_b', 'o_c_b', 'o_r_b') |
---|
258 | if _token == 'o_s_b': |
---|
259 | nest_square_bracket_expr = self.nest_square_bracket_expr(_context) |
---|
260 | return nest_square_bracket_expr |
---|
261 | elif _token == 'o_c_b': |
---|
262 | nest_curly_bracket_expr = self.nest_curly_bracket_expr(_context) |
---|
263 | return 'curly brackets' |
---|
264 | else: # == 'o_r_b' |
---|
265 | nest_round_bracket_expr = self.nest_round_bracket_expr(_context) |
---|
266 | return nest_round_bracket_expr |
---|
267 | |
---|
268 | def square_bracket_expr(self, _parent=None): |
---|
269 | _context = self.Context(_parent, self._scanner, self._pos, 'square_bracket_expr', []) |
---|
270 | o_s_b = self._scan('o_s_b') |
---|
271 | _token = self._peek('dat_val_nocomma_nosq', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
272 | if _token == 'dat_val_nocomma_nosq': |
---|
273 | dat_val_nocomma_nosq = self._scan('dat_val_nocomma_nosq') |
---|
274 | this_list = [stripstring(dat_val_nocomma_nosq)] |
---|
275 | if self._peek('dat_val_internal_sq', '","', 'c_s_b') == 'dat_val_internal_sq': |
---|
276 | dat_val_internal_sq = self._scan('dat_val_internal_sq') |
---|
277 | this_list[0]+=dat_val_internal_sq |
---|
278 | if self._peek('"\\*"', '","', 'c_s_b') == '"\\*"': |
---|
279 | self._scan('"\\*"') |
---|
280 | this_list[0]+="*" |
---|
281 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
282 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
283 | this_list = [nested_bracket_expression] |
---|
284 | while self._peek('c_s_b', '","') == '","': |
---|
285 | self._scan('","') |
---|
286 | _token = self._peek('dat_val_nocomma_nosq', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
287 | if _token == 'dat_val_nocomma_nosq': |
---|
288 | dat_val_nocomma_nosq = self._scan('dat_val_nocomma_nosq') |
---|
289 | this_list.append(stripstring(dat_val_nocomma_nosq)) |
---|
290 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
291 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
292 | this_list.append(nested_bracket_expression) |
---|
293 | if self._peek() not in ['c_s_b', '","']: |
---|
294 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_s_b'])) |
---|
295 | c_s_b = self._scan('c_s_b') |
---|
296 | return StarList(this_list) |
---|
297 | |
---|
298 | def nest_square_bracket_expr(self, _parent=None): |
---|
299 | _context = self.Context(_parent, self._scanner, self._pos, 'nest_square_bracket_expr', []) |
---|
300 | o_s_b = self._scan('o_s_b') |
---|
301 | _token = self._peek('dat_val_nocomma_nosq', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
302 | if _token == 'dat_val_nocomma_nosq': |
---|
303 | dat_val_nocomma_nosq = self._scan('dat_val_nocomma_nosq') |
---|
304 | this_list = [stripstring(dat_val_nocomma_nosq)] |
---|
305 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
306 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
307 | this_list = [nested_bracket_expression] |
---|
308 | while self._peek('c_s_b', '","') == '","': |
---|
309 | self._scan('","') |
---|
310 | _token = self._peek('dat_val_nocomma_nosq', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
311 | if _token == 'dat_val_nocomma_nosq': |
---|
312 | dat_val_nocomma_nosq = self._scan('dat_val_nocomma_nosq') |
---|
313 | this_list.append(stripstring(dat_val_nocomma_nosq)) |
---|
314 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
315 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
316 | this_list.append(nested_bracket_expression) |
---|
317 | if self._peek() not in ['c_s_b', '","']: |
---|
318 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_s_b'])) |
---|
319 | c_s_b = self._scan('c_s_b') |
---|
320 | return StarList(this_list) |
---|
321 | |
---|
322 | def curly_bracket_expr(self, _parent=None): |
---|
323 | _context = self.Context(_parent, self._scanner, self._pos, 'curly_bracket_expr', []) |
---|
324 | o_c_b = self._scan('o_c_b') |
---|
325 | _token = self._peek('dat_val_nocomma_nocurl', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
326 | if _token == 'dat_val_nocomma_nocurl': |
---|
327 | dat_val_nocomma_nocurl = self._scan('dat_val_nocomma_nocurl') |
---|
328 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
329 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
330 | while self._peek('c_c_b', '","', 'c_s_b', 'c_r_b') == '","': |
---|
331 | self._scan('","') |
---|
332 | _token = self._peek('dat_val_nocomma_nocurl', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
333 | if _token == 'dat_val_nocomma_nocurl': |
---|
334 | dat_val_nocomma_nocurl = self._scan('dat_val_nocomma_nocurl') |
---|
335 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
336 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
337 | if self._peek() not in ['c_c_b', '","', 'c_s_b', 'c_r_b']: |
---|
338 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_c_b', 'c_s_b', 'c_r_b'])) |
---|
339 | c_c_b = self._scan('c_c_b') |
---|
340 | |
---|
341 | def nest_curly_bracket_expr(self, _parent=None): |
---|
342 | _context = self.Context(_parent, self._scanner, self._pos, 'nest_curly_bracket_expr', []) |
---|
343 | o_c_b = self._scan('o_c_b') |
---|
344 | _token = self._peek('dat_val_nocomma_nocurl', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
345 | if _token == 'dat_val_nocomma_nocurl': |
---|
346 | dat_val_nocomma_nocurl = self._scan('dat_val_nocomma_nocurl') |
---|
347 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
348 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
349 | while self._peek('c_c_b', '","', 'c_s_b', 'c_r_b') == '","': |
---|
350 | self._scan('","') |
---|
351 | _token = self._peek('dat_val_nocomma_nocurl', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
352 | if _token == 'dat_val_nocomma_nocurl': |
---|
353 | dat_val_nocomma_nocurl = self._scan('dat_val_nocomma_nocurl') |
---|
354 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
355 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
356 | if self._peek() not in ['c_c_b', '","', 'c_s_b', 'c_r_b']: |
---|
357 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_c_b', 'c_s_b', 'c_r_b'])) |
---|
358 | c_c_b = self._scan('c_c_b') |
---|
359 | |
---|
360 | def round_bracket_expr(self, _parent=None): |
---|
361 | _context = self.Context(_parent, self._scanner, self._pos, 'round_bracket_expr', []) |
---|
362 | o_r_b = self._scan('o_r_b') |
---|
363 | _token = self._peek('dat_val_nocomma_nornd', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
364 | if _token == 'dat_val_nocomma_nornd': |
---|
365 | dat_val_nocomma_nornd = self._scan('dat_val_nocomma_nornd') |
---|
366 | this_tuple = [stripstring(dat_val_nocomma_nornd)] |
---|
367 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
368 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
369 | this_tuple = [nested_bracket_expression] |
---|
370 | while self._peek('c_r_b', '","') == '","': |
---|
371 | self._scan('","') |
---|
372 | _token = self._peek('dat_val_nocomma_nornd', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
373 | if _token == 'dat_val_nocomma_nornd': |
---|
374 | dat_val_nocomma_nornd = self._scan('dat_val_nocomma_nornd') |
---|
375 | this_tuple.append(stripstring(dat_val_nocomma_nornd)) |
---|
376 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
377 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
378 | this_tuple.append(nested_bracket_expression) |
---|
379 | if self._peek() not in ['c_r_b', '","']: |
---|
380 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_r_b'])) |
---|
381 | c_r_b = self._scan('c_r_b') |
---|
382 | return StarTuple(this_tuple) |
---|
383 | |
---|
384 | def nest_round_bracket_expr(self, _parent=None): |
---|
385 | _context = self.Context(_parent, self._scanner, self._pos, 'nest_round_bracket_expr', []) |
---|
386 | o_r_b = self._scan('o_r_b') |
---|
387 | _token = self._peek('dat_val_nocomma_nornd', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
388 | if _token == 'dat_val_nocomma_nornd': |
---|
389 | dat_val_nocomma_nornd = self._scan('dat_val_nocomma_nornd') |
---|
390 | this_tuple = [stripstring(dat_val_nocomma_nornd)] |
---|
391 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
392 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
393 | this_tuple = [nested_bracket_expression] |
---|
394 | while self._peek('c_r_b', '","') == '","': |
---|
395 | self._scan('","') |
---|
396 | _token = self._peek('dat_val_nocomma_nornd', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
397 | if _token == 'dat_val_nocomma_nornd': |
---|
398 | dat_val_nocomma_nornd = self._scan('dat_val_nocomma_nornd') |
---|
399 | this_tuple.append(stripstring(dat_val_nocomma_nornd)) |
---|
400 | else: # in ['o_s_b', 'o_c_b', 'o_r_b'] |
---|
401 | nested_bracket_expression = self.nested_bracket_expression(_context) |
---|
402 | this_tuple.append(nested_bracket_expression) |
---|
403 | if self._peek() not in ['c_r_b', '","']: |
---|
404 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['","', 'c_r_b'])) |
---|
405 | c_r_b = self._scan('c_r_b') |
---|
406 | return tuple(this_tuple) |
---|
407 | |
---|
408 | def top_loop(self, _parent=None): |
---|
409 | _context = self.Context(_parent, self._scanner, self._pos, 'top_loop', []) |
---|
410 | LBLOCK = self._scan('LBLOCK') |
---|
411 | loopfield = self.loopfield(_context) |
---|
412 | loopvalues = self.loopvalues(_context) |
---|
413 | return makeloop(loopfield,loopvalues) |
---|
414 | |
---|
415 | def loopfield(self, _parent=None): |
---|
416 | _context = self.Context(_parent, self._scanner, self._pos, 'loopfield', []) |
---|
417 | toploop=LoopBlock(dimension=1,overwrite=False);curloop=toploop;poploop=None;dim=1 |
---|
418 | while self._peek('data_name', 'LBLOCK', 'STOP', 'data_value_1', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b') in ['data_name', 'LBLOCK', 'STOP']: |
---|
419 | _token = self._peek('data_name', 'LBLOCK', 'STOP') |
---|
420 | if _token == 'data_name': |
---|
421 | data_name = self._scan('data_name') |
---|
422 | curloop[data_name]=[] |
---|
423 | elif _token == 'LBLOCK': |
---|
424 | LBLOCK = self._scan('LBLOCK') |
---|
425 | dim=dim+1;newloop=LoopBlock(dimension=dim,overwrite=False);poploop=curloop;curloop.insert_loop(newloop,audit=False);curloop=newloop |
---|
426 | else: # == 'STOP' |
---|
427 | STOP = self._scan('STOP') |
---|
428 | curloop=poploop;dim=dim-1 |
---|
429 | if self._peek() not in ['data_name', 'LBLOCK', 'STOP', 'data_value_1', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b']: |
---|
430 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['data_name', 'LBLOCK', 'STOP', 'data_value_1', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b'])) |
---|
431 | return toploop |
---|
432 | |
---|
433 | def loopvalues(self, _parent=None): |
---|
434 | _context = self.Context(_parent, self._scanner, self._pos, 'loopvalues', []) |
---|
435 | data_value = self.data_value(_context) |
---|
436 | dataloop=[[data_value]] |
---|
437 | while self._peek('data_value_1', 'STOP', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b', 'LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading') in ['data_value_1', 'STOP', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b']: |
---|
438 | _token = self._peek('data_value_1', 'STOP', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b') |
---|
439 | if _token != 'STOP': |
---|
440 | data_value = self.data_value(_context) |
---|
441 | dataloop[-1].append(monitor('loopval',data_value)) |
---|
442 | else: # == 'STOP' |
---|
443 | STOP = self._scan('STOP') |
---|
444 | dataloop.append([]) |
---|
445 | if self._peek() not in ['data_value_1', 'STOP', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b', 'LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading']: |
---|
446 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['data_value_1', 'STOP', 'start_sc_line', 'o_s_b', 'o_c_b', 'o_r_b', 'LBLOCK', 'data_name', 'save_end', 'save_heading', 'END', 'data_heading'])) |
---|
447 | return dataloop |
---|
448 | |
---|
449 | def save_frame(self, _parent=None): |
---|
450 | _context = self.Context(_parent, self._scanner, self._pos, 'save_frame', []) |
---|
451 | save_heading = self._scan('save_heading') |
---|
452 | savehead = save_heading[5:];savebody = StarBlock(overwrite=False) |
---|
453 | while self._peek('save_end', 'LBLOCK', 'data_name', 'save_heading', 'END', 'data_heading') in ['LBLOCK', 'data_name']: |
---|
454 | dataseq = self.dataseq(savebody, _context) |
---|
455 | if self._peek() not in ['save_end', 'LBLOCK', 'data_name', 'save_heading', 'END', 'data_heading']: |
---|
456 | raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['save_end', 'LBLOCK', 'data_name', 'save_heading', 'END', 'data_heading'])) |
---|
457 | save_end = self._scan('save_end') |
---|
458 | return (savehead,monitor('save_frame',savebody)) |
---|
459 | |
---|
460 | |
---|
461 | def parse(rule, text): |
---|
462 | P = StarParser(StarParserScanner(text)) |
---|
463 | return yappsrt.wrap_error_reporter(P, rule) |
---|
464 | |
---|
465 | # End -- grammar generated by Yapps |
---|
466 | |
---|
467 | |
---|