changeset 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 c0170d88ed2b
children b862e6fca7ac
files mercurial/parser.py
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/parser.py	Wed Aug 30 18:19:14 2017 +0530
+++ b/mercurial/parser.py	Wed Feb 17 21:30:04 2016 +0900
@@ -285,6 +285,33 @@
     simplified.append(op)
     return tuple(reversed(simplified))
 
+def _buildtree(template, placeholder, replstack):
+    if template == placeholder:
+        return replstack.pop()
+    if not isinstance(template, tuple):
+        return template
+    return tuple(_buildtree(x, placeholder, replstack) for x in template)
+
+def buildtree(template, placeholder, *repls):
+    """Create new tree by substituting placeholders by replacements
+
+    >>> _ = ('symbol', '_')
+    >>> def f(template, *repls):
+    ...     return buildtree(template, _, *repls)
+    >>> f(('func', ('symbol', 'only'), ('list', _, _)),
+    ...   ('symbol', '1'), ('symbol', '2'))
+    ('func', ('symbol', 'only'), ('list', ('symbol', '1'), ('symbol', '2')))
+    >>> f(('and', _, ('not', _)), ('symbol', '1'), ('symbol', '2'))
+    ('and', ('symbol', '1'), ('not', ('symbol', '2')))
+    """
+    if not isinstance(placeholder, tuple):
+        raise error.ProgrammingError('placeholder must be a node tuple')
+    replstack = list(reversed(repls))
+    r = _buildtree(template, placeholder, replstack)
+    if replstack:
+        raise error.ProgrammingError('too many replacements')
+    return r
+
 def parseerrordetail(inst):
     """Compose error message from specified ParseError object
     """