# HG changeset patch # User Yuya Nishihara # Date 2016-04-03 07:55:23 # Node ID 475dad3432fd5826bfa674192ea62ea75744bd34 # Parent f8182ab688a5b86ca2a469ed345adb0bec1d9864 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. diff --git a/mercurial/parser.py b/mercurial/parser.py --- a/mercurial/parser.py +++ b/mercurial/parser.py @@ -228,3 +228,39 @@ def parseerrordetail(inst): 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 diff --git a/mercurial/revset.py b/mercurial/revset.py --- a/mercurial/revset.py +++ b/mercurial/revset.py @@ -2366,6 +2366,11 @@ def _parsealiasdefn(defn, args): 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`.