parser: add helper function that constructs parsed tree from template
authorYuya Nishihara <yuya@tcha.org>
Wed, 17 Feb 2016 21:30:04 +0900
changeset 34043 90896b61fe26
parent 34042 c0170d88ed2b
child 34044 b862e6fca7ac
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.
mercurial/parser.py
--- 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
     """