Patchwork [1,of,7] parser: construct alias object by rule-set class

login
register
mail settings
Submitter Yuya Nishihara
Date April 13, 2016, 3:55 p.m.
Message ID <ed89253c74dde41715fb.1460562913@mimosa>
Download mbox | patch
Permalink /patch/14580/
State Accepted
Commit 0c135f37c6f8097d0a53295a25995abaab4d30cf
Headers show

Comments

Yuya Nishihara - April 13, 2016, 3:55 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1456738410 -32400
#      Mon Feb 29 18:33:30 2016 +0900
# Node ID ed89253c74dde41715fb40384e1212995be5a264
# Parent  4894c1c6205069573553df64c2c6b575d2625c69
parser: construct alias object by rule-set class

It was odd that the revsetalias did the whole parsing stuff in __init__().
Instead, this patch adds a factory function to the aliasrules class, and
makes the alias (= revsetalias) class a plain-old value object.

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -229,6 +229,19 @@  def parseerrordetail(inst):
     else:
         return inst.args[0]
 
+class alias(object):
+    """Parsed result of alias"""
+
+    def __init__(self, name, tree, args, err, replacement):
+        self.name = name
+        self.tree = tree
+        self.args = args
+        self.error = err
+        self.replacement = replacement
+        # whether own `error` information is already shown or not.
+        # this avoids showing same warning multiple times at each `findaliases`.
+        self.warned = False
+
 class basealiasrules(object):
     """Parsing and expansion rule set of aliases
 
@@ -430,3 +443,22 @@  class basealiasrules(object):
         else:
             args = set()
         return cls._relabelargs(tree, args)
+
+    @classmethod
+    def build(cls, decl, defn):
+        """Parse an alias declaration and definition into an alias object"""
+        repl = efmt = None
+        name, tree, args, err = cls._builddecl(decl)
+        if err:
+            efmt = _('failed to parse the declaration of %(section)s '
+                     '"%(name)s": %(error)s')
+        else:
+            try:
+                repl = cls._builddefn(defn, args)
+            except error.ParseError as inst:
+                err = parseerrordetail(inst)
+                efmt = _('failed to parse the definition of %(section)s '
+                         '"%(name)s": %(error)s')
+        if err:
+            err = efmt % {'section': cls._section, 'name': name, 'error': err}
+        return alias(name, tree, args, err, repl)
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2256,31 +2256,6 @@  class _aliasrules(parser.basealiasrules)
     _parse = staticmethod(_parsealias)
     _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`.
-    warned = False
-
-    def __init__(self, name, value):
-        '''Aliases like:
-
-        h = heads(default)
-        b($1) = ancestors($1) - ancestors(default)
-        '''
-        r = _aliasrules._builddecl(name)
-        self.name, self.tree, self.args, self.error = r
-        if self.error:
-            self.error = _('failed to parse the declaration of revset alias'
-                           ' "%s": %s') % (self.name, self.error)
-            return
-
-        try:
-            self.replacement = _aliasrules._builddefn(value, self.args)
-        except error.ParseError as inst:
-            self.error = _('failed to parse the definition of revset alias'
-                           ' "%s": %s') % (self.name,
-                                           parser.parseerrordetail(inst))
-
 def _getalias(aliases, tree):
     """If tree looks like an unexpanded alias, return it. Return None
     otherwise.
@@ -2314,7 +2289,7 @@  def _expandaliases(aliases, tree, expand
     """Expand aliases in tree, recursively.
 
     'aliases' is a dictionary mapping user defined aliases to
-    revsetalias objects.
+    alias objects.
     """
     if not isinstance(tree, tuple):
         # Do not expand raw strings
@@ -2347,7 +2322,7 @@  def _expandaliases(aliases, tree, expand
 def findaliases(ui, tree, showwarning=None):
     aliases = {}
     for k, v in ui.configitems('revsetalias'):
-        alias = revsetalias(k, v)
+        alias = _aliasrules.build(k, v)
         aliases[alias.name] = alias
     tree = _expandaliases(aliases, tree, [], {})
     if showwarning: