revset: factor out common parsing function
authorYuya Nishihara <yuya@tcha.org>
Sun, 17 Apr 2016 13:03:23 +0900
changeset 29073 81bac118f9e2
parent 29072 f86fa7168059
child 29074 e7c679738503
revset: factor out common parsing function
mercurial/revset.py
--- a/mercurial/revset.py	Sun Apr 17 12:57:27 2016 +0900
+++ b/mercurial/revset.py	Sun Apr 17 13:03:23 2016 +0900
@@ -2217,25 +2217,35 @@
 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)]
                            if c.isalnum() or c in '._@$' or ord(c) > 127)
 
+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'))
+    >>> _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, 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.
-
-    >>> _parsealias('foo($1)')
-    ('func', ('symbol', 'foo'), ('symbol', '$1'))
-    >>> _parsealias('foo bar')
-    Traceback (most recent call last):
-      ...
-    ParseError: ('invalid token', 4)
     """
-    p = parser.parser(elements)
-    tree, pos = p.parse(tokenize(spec, syminitletters=_aliassyminitletters))
-    if pos != len(spec):
-        raise error.ParseError(_('invalid token'), pos)
-    return parser.simplifyinfixops(tree, ('list', 'or'))
+    return _parsewith(spec, syminitletters=_aliassyminitletters)
 
 class _aliasrules(parser.basealiasrules):
     """Parsing and expansion rule set of revset aliases"""
@@ -2280,11 +2290,7 @@
         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