Patchwork [2,of,3] revset: factor out common parsing function

login
register
mail settings
Submitter Yuya Nishihara
Date May 4, 2016, 12:11 p.m.
Message ID <478eb6b0fcd9b91fdc34.1462363902@mimosa>
Download mbox | patch
Permalink /patch/14880/
State Accepted
Delegated to: Augie Fackler
Headers show

Comments

Yuya Nishihara - May 4, 2016, 12:11 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1460865803 -32400
#      Sun Apr 17 13:03:23 2016 +0900
# Node ID 478eb6b0fcd9b91fdc34d9ee763ad093bc1a06e7
# Parent  57e38af84899a6fbb0a63cf84f60c17f3a6cf782
revset: factor out common parsing function

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2217,26 +2217,36 @@  def optimize(x, small):
 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)]
                            if c.isalnum() or c in '._@$' or ord(c) > 127)
 
-def _parsealias(spec):
-    """Parse alias declaration/definition ``spec``
-
-    This allows symbol names to use also ``$`` as an initial letter
-    (for backward compatibility), and callers of this function should
-    examine whether ``$`` is used also for unexpected symbols or not.
-
-    >>> _parsealias('foo($1)')
+def _parsewith(spec, lookup=None, syminitletters=None):
+    """Generate a parse tree of given spec with given tokenizing options
+
+    >>> _parsewith('foo($1)', syminitletters=_aliassyminitletters)
     ('func', ('symbol', 'foo'), ('symbol', '$1'))
-    >>> _parsealias('foo bar')
+    >>> _parsewith('$1')
+    Traceback (most recent call last):
+      ...
+    ParseError: ("syntax error in revset '$1'", 0)
+    >>> _parsewith('foo bar')
     Traceback (most recent call last):
       ...
     ParseError: ('invalid token', 4)
     """
     p = parser.parser(elements)
-    tree, pos = p.parse(tokenize(spec, syminitletters=_aliassyminitletters))
+    tree, pos = p.parse(tokenize(spec, lookup=lookup,
+                                 syminitletters=syminitletters))
     if pos != len(spec):
         raise error.ParseError(_('invalid token'), pos)
     return parser.simplifyinfixops(tree, ('list', 'or'))
 
+def _parsealias(spec):
+    """Parse alias declaration/definition ``spec``
+
+    This allows symbol names to use also ``$`` as an initial letter
+    (for backward compatibility), and callers of this function should
+    examine whether ``$`` is used also for unexpected symbols or not.
+    """
+    return _parsewith(spec, syminitletters=_aliassyminitletters)
+
 class _aliasrules(parser.basealiasrules):
     """Parsing and expansion rule set of revset aliases"""
     _section = _('revset alias')
@@ -2280,11 +2290,7 @@  def foldconcat(tree):
         return tuple(foldconcat(t) for t in tree)
 
 def parse(spec, lookup=None):
-    p = parser.parser(elements)
-    tree, pos = p.parse(tokenize(spec, lookup=lookup))
-    if pos != len(spec):
-        raise error.ParseError(_("invalid token"), pos)
-    return parser.simplifyinfixops(tree, ('list', 'or'))
+    return _parsewith(spec, lookup=lookup)
 
 def posttreebuilthook(tree, repo):
     # hook for extensions to execute code on the optimized tree