282 simplified.append(simplifyinfixops(r, targetnodes)) |
282 simplified.append(simplifyinfixops(r, targetnodes)) |
283 x = l |
283 x = l |
284 simplified.append(simplifyinfixops(x, targetnodes)) |
284 simplified.append(simplifyinfixops(x, targetnodes)) |
285 simplified.append(op) |
285 simplified.append(op) |
286 return tuple(reversed(simplified)) |
286 return tuple(reversed(simplified)) |
|
287 |
|
288 def _buildtree(template, placeholder, replstack): |
|
289 if template == placeholder: |
|
290 return replstack.pop() |
|
291 if not isinstance(template, tuple): |
|
292 return template |
|
293 return tuple(_buildtree(x, placeholder, replstack) for x in template) |
|
294 |
|
295 def buildtree(template, placeholder, *repls): |
|
296 """Create new tree by substituting placeholders by replacements |
|
297 |
|
298 >>> _ = ('symbol', '_') |
|
299 >>> def f(template, *repls): |
|
300 ... return buildtree(template, _, *repls) |
|
301 >>> f(('func', ('symbol', 'only'), ('list', _, _)), |
|
302 ... ('symbol', '1'), ('symbol', '2')) |
|
303 ('func', ('symbol', 'only'), ('list', ('symbol', '1'), ('symbol', '2'))) |
|
304 >>> f(('and', _, ('not', _)), ('symbol', '1'), ('symbol', '2')) |
|
305 ('and', ('symbol', '1'), ('not', ('symbol', '2'))) |
|
306 """ |
|
307 if not isinstance(placeholder, tuple): |
|
308 raise error.ProgrammingError('placeholder must be a node tuple') |
|
309 replstack = list(reversed(repls)) |
|
310 r = _buildtree(template, placeholder, replstack) |
|
311 if replstack: |
|
312 raise error.ProgrammingError('too many replacements') |
|
313 return r |
287 |
314 |
288 def parseerrordetail(inst): |
315 def parseerrordetail(inst): |
289 """Compose error message from specified ParseError object |
316 """Compose error message from specified ParseError object |
290 """ |
317 """ |
291 if len(inst.args) > 1: |
318 if len(inst.args) > 1: |