[PATCH 1 of 3 RFC] revset: add parsing rule for key=value pair

Yuya Nishihara yuya at tcha.org
Mon Jun 29 09:51:29 CDT 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1435392328 -32400
#      Sat Jun 27 17:05:28 2015 +0900
# Node ID 164e1587bcf1164dad21f7f01b2ba160a011898d
# Parent  ff5172c830022b64cc5bd1bae36b2276e9dc6e5d
revset: add parsing rule for key=value pair

It will be used as an keyword argument.

Note that our "=" operator is left-associative. In general, the assignment
operator is right-associative, but we don't care because it isn't allowed to
chain "=" operations.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -133,6 +133,7 @@ elements = {
     "or": (4, None, ("or", 4)),
     "|": (4, None, ("or", 4)),
     "+": (4, None, ("or", 4)),
+    "=": (3, None, ("keyvalue", 3)),
     ",": (2, None, ("list", 2)),
     ")": (0, None, None),
     "symbol": (0, ("symbol",), None),
@@ -190,7 +191,7 @@ def tokenize(program, lookup=None, symin
         elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
             yield ('##', None, pos)
             pos += 1 # skip ahead
-        elif c in "():,-|&+!~^%": # handle simple operators
+        elif c in "():=,-|&+!~^%": # handle simple operators
             yield (c, None, pos)
         elif (c in '"\'' or c == 'r' and
               program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
@@ -388,6 +389,9 @@ def notset(repo, subset, x):
 def listset(repo, subset, a, b):
     raise error.ParseError(_("can't use a list in this context"))
 
+def keyvaluepair(repo, subset, k, v):
+    raise error.ParseError(_("can't use a key-value pair in this context"))
+
 def func(repo, subset, a, b):
     if a[0] == 'symbol' and a[1] in symbols:
         return symbols[a[1]](repo, subset, b)
@@ -2177,6 +2181,7 @@ methods = {
     "or": orset,
     "not": notset,
     "list": listset,
+    "keyvalue": keyvaluepair,
     "func": func,
     "ancestor": ancestorspec,
     "parent": parentspec,
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -322,6 +322,17 @@ quoting needed
   4
   $ hg book -d date
 
+keyword arguments
+
+  $ try 'foo=bar|baz'
+  (keyvalue
+    ('symbol', 'foo')
+    (or
+      ('symbol', 'bar')
+      ('symbol', 'baz')))
+  hg: parse error: can't use a key-value pair in this context
+  [255]
+
 Test that symbols only get parsed as functions if there's an opening
 parenthesis.
 


More information about the Mercurial-devel mailing list