templater: fix position of terminator character in error message
authorYuya Nishihara <yuya@tcha.org>
Sun, 04 Mar 2018 07:03:50 -0500
changeset 36691 1b179d151578
parent 36690 b529e640015d
child 36692 aeaf9c7f7528
templater: fix position of terminator character in error message Since a template expression starts after the '{' character, the expression should be considered to end immediately before the terminator '{'.
mercurial/revsetlang.py
mercurial/templater.py
tests/test-command-template.t
tests/test-export.t
tests/test-log.t
--- a/mercurial/revsetlang.py	Sun Mar 04 10:42:51 2018 -0500
+++ b/mercurial/revsetlang.py	Sun Mar 04 07:03:50 2018 -0500
@@ -543,16 +543,14 @@
         return _parsewith(spec, lookup=lookup)
     except error.ParseError as inst:
         if len(inst.args) > 1:  # has location
-            # Add 1 to location because unlike templates, revset parse errors
-            # point to the char where the error happened, not the char after.
-            loc = inst.args[1] + 1
+            loc = inst.args[1]
             # Remove newlines -- spaces are equivalent whitespace.
             spec = spec.replace('\n', ' ')
             # We want the caret to point to the place in the template that
             # failed to parse, but in a hint we get a open paren at the
             # start. Therefore, we print "loc + 1" spaces (instead of "loc")
             # to line up the caret with the location of the error.
-            inst.hint = spec + '\n' + ' ' * loc + '^ ' + _('here')
+            inst.hint = spec + '\n' + ' ' * (loc + 1) + '^ ' + _('here')
         raise
 
 def _quote(s):
--- a/mercurial/templater.py	Sun Mar 04 10:42:51 2018 -0500
+++ b/mercurial/templater.py	Sun Mar 04 07:03:50 2018 -0500
@@ -145,7 +145,7 @@
             yield ('symbol', sym, s)
             pos -= 1
         elif c == term:
-            yield ('end', None, pos + 1)
+            yield ('end', None, pos)
             return
         else:
             raise error.ParseError(_("syntax error"), pos)
@@ -237,9 +237,10 @@
                 return
 
             parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
-            if not tmpl.endswith('}', n + 1, pos):
+            if not tmpl.startswith('}', pos):
                 raise error.ParseError(_("invalid token"), pos)
             yield ('template', parseres, n)
+            pos += 1
 
         if quote:
             raise error.ParseError(_("unterminated string"), start)
@@ -253,9 +254,10 @@
             tmpl = tmpl.replace('\n', br'\n')
             # We want the caret to point to the place in the template that
             # failed to parse, but in a hint we get a open paren at the
-            # start. Therefore, we print "loc" spaces (instead of "loc - 1")
+            # start. Therefore, we print "loc + 1" spaces (instead of "loc")
             # to line up the caret with the location of the error.
-            inst.hint = tmpl + '\n' + ' ' * (loc + offset) + '^ ' + _('here')
+            inst.hint = (tmpl + '\n'
+                         + ' ' * (loc + 1 + offset) + '^ ' + _('here'))
         raise
     yield ('end', None, pos)
 
--- a/tests/test-command-template.t	Sun Mar 04 10:42:51 2018 -0500
+++ b/tests/test-command-template.t	Sun Mar 04 07:03:50 2018 -0500
@@ -2767,26 +2767,26 @@
   $ hg log -T '{date'
   hg: parse error at 1: unterminated template expansion
   ({date
-   ^ here)
+    ^ here)
   [255]
   $ hg log -T '{date(}'
-  hg: parse error at 7: not a prefix: end
+  hg: parse error at 6: not a prefix: end
   ({date(}
          ^ here)
   [255]
   $ hg log -T '{date)}'
   hg: parse error at 5: invalid token
   ({date)}
-       ^ here)
+        ^ here)
   [255]
   $ hg log -T '{date date}'
   hg: parse error at 6: invalid token
   ({date date}
-        ^ here)
+         ^ here)
   [255]
 
   $ hg log -T '{}'
-  hg: parse error at 2: not a prefix: end
+  hg: parse error at 1: not a prefix: end
   ({}
     ^ here)
   [255]
@@ -2838,13 +2838,13 @@
   $ hg log -T '{"date'
   hg: parse error at 2: unterminated string
   ({"date
-    ^ here)
+     ^ here)
   [255]
 
   $ hg log -T '{"foo{date|?}"}'
   hg: parse error at 11: syntax error
   ({"foo{date|?}"}
-             ^ here)
+              ^ here)
   [255]
 
 Thrown an error if a template function doesn't exist
@@ -3377,7 +3377,7 @@
   $ hg debugtemplate '{(-)}\n'
   hg: parse error at 3: not a prefix: )
   ({(-)}\n
-     ^ here)
+      ^ here)
   [255]
   $ hg debugtemplate '{(-a)}\n'
   hg: parse error: negation needs an integer argument
@@ -3544,7 +3544,7 @@
   $ hg log -r 2 -T '{if(rev, "{if(rev, \")}")}\n'
   hg: parse error at 21: unterminated string
   ({if(rev, "{if(rev, \")}")}\n
-                       ^ here)
+                        ^ here)
   [255]
   $ hg log -r 2 -T '{if(rev, \"\\"")}\n'
   hg: parse error: trailing \ in string
--- a/tests/test-export.t	Sun Mar 04 10:42:51 2018 -0500
+++ b/tests/test-export.t	Sun Mar 04 07:03:50 2018 -0500
@@ -219,7 +219,7 @@
   $ hg export -o '%m{' tip
   hg: parse error at 3: unterminated template expansion
   (%m{
-     ^ here)
+      ^ here)
   [255]
   $ hg export -o '%\' tip
   abort: invalid format spec '%\' in output filename
--- a/tests/test-log.t	Sun Mar 04 10:42:51 2018 -0500
+++ b/tests/test-log.t	Sun Mar 04 07:03:50 2018 -0500
@@ -2293,7 +2293,7 @@
 
 simple error
   $ hg log -r . -T '{shortest(node}'
-  hg: parse error at 15: unexpected token: end
+  hg: parse error at 14: unexpected token: end
   ({shortest(node}
                  ^ here)
   [255]
@@ -2303,7 +2303,7 @@
   > line2
   > {shortest(node}
   > line4\nline5'
-  hg: parse error at 28: unexpected token: end
+  hg: parse error at 27: unexpected token: end
   (line 1\nline2\n{shortest(node}\nline4\nline5
                                 ^ here)
   [255]