templater: add parsing rule for key-value pair
authorYuya Nishihara <yuya@tcha.org>
Mon, 03 Apr 2017 20:55:55 +0900
changeset 31885 d18b624c1c06
parent 31884 0926ca37a990
child 31886 bdda942f4b9c
templater: add parsing rule for key-value pair Based on the revset implementation, 70a2082f855a. This patch also adjusts the test as '=' is now a valid token.
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templater.py	Mon Apr 03 20:44:05 2017 +0900
+++ b/mercurial/templater.py	Mon Apr 03 20:55:55 2017 +0900
@@ -39,6 +39,7 @@
     "/": (5, None, None, ("/", 5), None),
     "+": (4, None, None, ("+", 4), None),
     "-": (4, None, ("negate", 19), ("-", 4), None),
+    "=": (3, None, None, ("keyvalue", 3), None),
     ",": (2, None, None, ("list", 2), None),
     ")": (0, None, None, None, None),
     "integer": (0, "integer", None, None, None),
@@ -56,7 +57,7 @@
         c = program[pos]
         if c.isspace(): # skip inter-token whitespace
             pass
-        elif c in "(,)%|+-*/": # handle simple operators
+        elif c in "(=,)%|+-*/": # handle simple operators
             yield (c, None, pos)
         elif c in '"\'': # handle quoted templates
             s = pos + 1
@@ -462,6 +463,9 @@
         return (runfilter, (args[0], f))
     raise error.ParseError(_("unknown function '%s'") % n)
 
+def buildkeyvaluepair(exp, content):
+    raise error.ParseError(_("can't use a key-value pair in this context"))
+
 # dict of template built-in functions
 funcs = {}
 
@@ -984,6 +988,7 @@
     "|": buildfilter,
     "%": buildmap,
     "func": buildfunc,
+    "keyvalue": buildkeyvaluepair,
     "+": lambda e, c: buildarithmetic(e, c, lambda a, b: a + b),
     "-": lambda e, c: buildarithmetic(e, c, lambda a, b: a - b),
     "negate": buildnegate,
--- a/tests/test-command-template.t	Mon Apr 03 20:44:05 2017 +0900
+++ b/tests/test-command-template.t	Mon Apr 03 20:55:55 2017 +0900
@@ -134,6 +134,18 @@
     ('string', '\n'))
   -3
 
+Keyword arguments:
+
+  $ hg debugtemplate -r0 -v '{foo=bar|baz}'
+  (template
+    (keyvalue
+      ('symbol', 'foo')
+      (|
+        ('symbol', 'bar')
+        ('symbol', 'baz'))))
+  hg: parse error: can't use a key-value pair in this context
+  [255]
+
 Second branch starting at nullrev:
 
   $ hg update null
@@ -2668,7 +2680,7 @@
   hg: parse error at 2: unterminated string
   [255]
 
-  $ hg log -T '{"foo{date|=}"}'
+  $ hg log -T '{"foo{date|?}"}'
   hg: parse error at 11: syntax error
   [255]