Patchwork [6,of,7] templater: unify "string" and "rawstring"

login
register
mail settings
Submitter Yuya Nishihara
Date July 12, 2015, 2:55 p.m.
Message ID <e12c8b4dbe016ba1c34f.1436712901@mimosa>
Download mbox | patch
Permalink /patch/9958/
State Accepted
Headers show

Comments

Yuya Nishihara - July 12, 2015, 2:55 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1434792251 -32400
#      Sat Jun 20 18:24:11 2015 +0900
# Node ID e12c8b4dbe016ba1c34fd30caabaa1c72c5b0194
# Parent  8fd451a45ea89649e85ec8051a9b6dff03d94293
templater: unify "string" and "rawstring"

"rawstring" was introduced by 5ab28a2e9962, but it's no longer necessary
because c1975809a6b5 and fd5bc660c9f0 changed the way of processing string
literals.

This patch moves string decoding to the parsing phase as it was before:

  ('rawstring', s) -> ('string', s)
  ('string', s) -> ('string', s.decode('string-escape'))

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -22,7 +22,7 @@  elements = {
     ")": (0, None, None),
     "integer": (0, ("integer",), None),
     "symbol": (0, ("symbol",), None),
-    "rawstring": (0, ("rawstring",), None),
+    "string": (0, ("string",), None),
     "template": (0, ("template",), None),
     "end": (0, None, None),
 }
@@ -50,7 +50,7 @@  def tokenize(program, start, end):
                     pos += 2
                     continue
                 if d == c:
-                    yield ('rawstring', program[s:pos], s)
+                    yield ('string', program[s:pos], s)
                     break
                 pos += 1
             else:
@@ -83,7 +83,7 @@  def tokenize(program, start, end):
             #             escaped quoted string
             if c == 'r':
                 pos += 1
-                token = 'rawstring'
+                token = 'string'
             else:
                 token = 'template'
             quote = program[pos:pos + 2]
@@ -136,7 +136,7 @@  def _parsetemplate(tmpl, start, stop, qu
     >>> _parsetemplate(r'foo\"bar"baz', 0, 12, quote='"')
     ([('string', 'foo"'), ('string', 'bar')], 9)
     >>> _parsetemplate(r'foo\\"bar', 0, 10, quote='"')
-    ([('string', 'foo\\\\')], 6)
+    ([('string', 'foo\\')], 6)
     """
     parsed = []
     sepchars = '{' + quote
@@ -146,18 +146,19 @@  def _parsetemplate(tmpl, start, stop, qu
         n = min((tmpl.find(c, pos, stop) for c in sepchars),
                 key=lambda n: (n < 0, n))
         if n < 0:
-            parsed.append(('string', tmpl[pos:stop]))
+            parsed.append(('string', tmpl[pos:stop].decode('string-escape')))
             pos = stop
             break
         c = tmpl[n]
         bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
         if bs % 2 == 1:
             # escaped (e.g. '\{', '\\\{', but not '\\{')
-            parsed.append(('string', (tmpl[pos:n - 1] + c)))
+            parsed.append(('string',
+                           tmpl[pos:n - 1].decode('string-escape') + c))
             pos = n + 1
             continue
         if n > pos:
-            parsed.append(('string', tmpl[pos:n]))
+            parsed.append(('string', tmpl[pos:n].decode('string-escape')))
         if c == quote:
             return parsed, n + 1
 
@@ -212,9 +213,6 @@  def runinteger(context, mapping, data):
     return int(data)
 
 def runstring(context, mapping, data):
-    return data.decode("string-escape")
-
-def runrawstring(context, mapping, data):
     return data
 
 def runsymbol(context, mapping, key):
@@ -659,7 +657,6 @@  def word(context, mapping, args):
 exprmethods = {
     "integer": lambda e, c: (runinteger, e[1]),
     "string": lambda e, c: (runstring, e[1]),
-    "rawstring": lambda e, c: (runrawstring, e[1]),
     "symbol": lambda e, c: (runsymbol, e[1]),
     "template": buildtemplate,
     "group": lambda e, c: compileexp(e[1], c, exprmethods),