##// END OF EJS Templates
parser: add helper function that constructs parsed tree from template...
Yuya Nishihara -
r34045:90896b61 default
parent child Browse files
Show More
@@ -285,6 +285,33 b' def simplifyinfixops(tree, targetnodes):'
285 285 simplified.append(op)
286 286 return tuple(reversed(simplified))
287 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
314
288 315 def parseerrordetail(inst):
289 316 """Compose error message from specified ParseError object
290 317 """
General Comments 0
You need to be logged in to leave comments. Login now