Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/parser.py @ 34060:90896b61fe26
parser: add helper function that constructs parsed tree from template
This function will be used as follows:
build('only(_, _)', x, y)
See the next patch for details.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 17 Feb 2016 21:30:04 +0900 |
parents | 0f41f1e3c75c |
children | 79681d8ee587 |
comparison
equal
deleted
inserted
replaced
34059:c0170d88ed2b | 34060:90896b61fe26 |
---|---|
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: |