Patchwork [6,of,6] parser: move alias definition parser to common rule-set class

login
register
mail settings
Submitter Yuya Nishihara
Date April 1, 2016, 3:37 p.m.
Message ID <34cd03642b11f43c3513.1459525032@mimosa>
Download mbox | patch
Permalink /patch/14227/
State Superseded
Commit 2ca3b7c563f3bbb8a2cc250f3ce96dba2c76f0a4
Headers show

Comments

Yuya Nishihara - April 1, 2016, 3:37 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1456737007 -32400
#      Mon Feb 29 18:10:07 2016 +0900
# Node ID 34cd03642b11f43c35131c1906ac1449493ab598
# Parent  d5414d3c08bbffc3ef6ad3fa3268d3d43b25afad
parser: move alias definition parser to common rule-set class

The original _parsealiasdefn() function is split into common _builddefn()
and revset-specific _parsealiasdefn(). revset._relabelaliasargs() is removed
as it is no longer used.

The doctest is kept in revset._parsealiasdefn() because it requires concrete
syntax rules.

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -305,3 +305,18 @@  class aliasrules(object):
         elif sym.startswith('$'):
             raise error.ParseError(_("'$' not for alias arguments"))
         return (op, sym)
+
+    def _builddefn(self, defn, args):
+        """Parse an alias definition into a tree and marks substitutions
+
+        This function marks alias argument references as ``_aliasarg``.
+
+        ``args`` is a list of alias argument names, or None if the alias
+        is declared as a symbol.
+        """
+        tree = self._parsedefn(defn)
+        if args:
+            args = set(args)
+        else:
+            args = set()
+        return self._relabelargs(tree, args)
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2277,52 +2277,37 @@  def _parsealiasdecl(decl):
         raise error.ParseError(_('invalid token'), pos)
     return parser.simplifyinfixops(tree, ('list',))
 
-def _relabelaliasargs(tree, args):
-    return parser.aliasrules('', None, None, None)._relabelargs(tree, args)
-
-def _parsealiasdefn(defn, args):
+def _parsealiasdefn(defn):
     """Parse alias definition ``defn``
 
-    This function marks alias argument references as ``_aliasarg``.
-
-    ``args`` is a list of alias argument names, or None if the alias
-    is declared as a symbol.
-
-    This returns "tree" as parsing result.
-
     >>> def prettyformat(tree):
     ...     return parser.prettyformat(tree, ('_aliasarg', 'string', 'symbol'))
+    >>> f = parser.aliasrules('', None, _parsealiasdefn, getlist)._builddefn
     >>> args = ['$1', '$2', 'foo']
-    >>> print prettyformat(_parsealiasdefn('$1 or foo', args))
+    >>> print prettyformat(f('$1 or foo', args))
     (or
       ('_aliasarg', '$1')
       ('_aliasarg', 'foo'))
     >>> try:
-    ...     _parsealiasdefn('$1 or $bar', args)
+    ...     f('$1 or $bar', args)
     ... except error.ParseError, inst:
     ...     print parser.parseerrordetail(inst)
     '$' not for alias arguments
     >>> args = ['$1', '$10', 'foo']
-    >>> print prettyformat(_parsealiasdefn('$10 or foobar', args))
+    >>> print prettyformat(f('$10 or foobar', args))
     (or
       ('_aliasarg', '$10')
       ('symbol', 'foobar'))
-    >>> print prettyformat(_parsealiasdefn('"$1" or "foo"', args))
+    >>> print prettyformat(f('"$1" or "foo"', args))
     (or
       ('string', '$1')
       ('string', 'foo'))
     """
-    if args:
-        args = set(args)
-    else:
-        args = set()
-
     p = parser.parser(elements)
     tree, pos = p.parse(_tokenizealias(defn))
     if pos != len(defn):
         raise error.ParseError(_('invalid token'), pos)
-    tree = parser.simplifyinfixops(tree, ('list', 'or'))
-    return _relabelaliasargs(tree, args)
+    return parser.simplifyinfixops(tree, ('list', 'or'))
 
 class revsetalias(object):
     # whether own `error` information is already shown or not.
@@ -2336,7 +2321,7 @@  class revsetalias(object):
         b($1) = ancestors($1) - ancestors(default)
         '''
         rules = parser.aliasrules(_('revset alias'),
-                                  _parsealiasdecl, None, getlist)
+                                  _parsealiasdecl, _parsealiasdefn, getlist)
         self.name, self.tree, self.args, self.error = rules._builddecl(name)
         if self.error:
             self.error = _('failed to parse the declaration of revset alias'
@@ -2344,7 +2329,7 @@  class revsetalias(object):
             return
 
         try:
-            self.replacement = _parsealiasdefn(value, self.args)
+            self.replacement = rules._builddefn(value, self.args)
         except error.ParseError as inst:
             self.error = _('failed to parse the definition of revset alias'
                            ' "%s": %s') % (self.name,