changeset 28870:475dad3432fd

parser: add stub class that will host alias parsing and expansion This class will keep syntax rules that are necessary to parse and expand aliases. The implementations will be extracted from the revset module. In order to make the porting easier, this class keeps parsedecl and parsedefn separately, which will be unified later. Also, getlist and funcnode will be refactored by future patches for better handling of the template aliases. The following public functions will be added: aliasrules.build(decl, defn) -> aliasobj parse decl and defn into an object that keeps alias name, arguments and replacement tree. aliasrules.buildmap(aliasitems) -> aliasdict helper to build() a dict of alias objects from a list of (decl, defn) aliasrules.expand(aliasdict, tree) -> tree expand aliases in tree recursively Because these functions aren't introduced by this series, there would remain a few wrapper functions in the revset module. These ugly wrappers should be eliminated by the next series. This class is considered an inheritable namespace, which will host only class/static methods. That's because it won't have no object-scope variables. I'm not a big fan of using class as a syntax sugar, but I admit it can improve code readability at some level. So let's give it a try.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Apr 2016 16:55:23 +0900
parents f8182ab688a5
children 6d6201fc5aae
files mercurial/parser.py mercurial/revset.py
diffstat 2 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/parser.py	Mon Feb 29 17:43:39 2016 +0900
+++ b/mercurial/parser.py	Sun Apr 03 16:55:23 2016 +0900
@@ -228,3 +228,39 @@
         return _('at %s: %s') % (inst.args[1], inst.args[0])
     else:
         return inst.args[0]
+
+class basealiasrules(object):
+    """Parsing and expansion rule set of aliases
+
+    This is a helper for fileset/revset/template aliases. A concrete rule set
+    should be made by sub-classing this and implementing class/static methods.
+
+    It supports alias expansion of symbol and funciton-call styles::
+
+        # decl = defn
+        h = heads(default)
+        b($1) = ancestors($1) - ancestors(default)
+    """
+    # typically a config section, which will be included in error messages
+    _section = None
+    # tags of symbol and function nodes
+    _symbolnode = 'symbol'
+    _funcnode = 'func'
+
+    def __new__(cls):
+        raise TypeError("'%s' is not instantiatable" % cls.__name__)
+
+    @staticmethod
+    def _parsedecl(spec):
+        """Parse an alias name and arguments"""
+        raise NotImplementedError
+
+    @staticmethod
+    def _parsedefn(spec):
+        """Parse an alias definition"""
+        raise NotImplementedError
+
+    @staticmethod
+    def _getlist(tree):
+        """Extract a list of arguments from parsed tree"""
+        raise NotImplementedError
--- a/mercurial/revset.py	Mon Feb 29 17:43:39 2016 +0900
+++ b/mercurial/revset.py	Sun Apr 03 16:55:23 2016 +0900
@@ -2366,6 +2366,11 @@
     tree = parser.simplifyinfixops(tree, ('list', 'or'))
     return _relabelaliasargs(tree, args)
 
+class _aliasrules(parser.basealiasrules):
+    """Parsing and expansion rule set of revset aliases"""
+    _section = _('revset alias')
+    _getlist = staticmethod(getlist)
+
 class revsetalias(object):
     # whether own `error` information is already shown or not.
     # this avoids showing same warning multiple times at each `findaliases`.