[PATCH 1 of 3] parser: move unescape helper from templater

Yuya Nishihara yuya at tcha.org
Thu Sep 10 15:10:57 UTC 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1441895110 -32400
#      Thu Sep 10 23:25:10 2015 +0900
# Node ID 04afdd74ed1642a951baa8e7bb9af3c6cffb1950
# Parent  a4da463df6cf0d0bcf36afa478ed18d1c818ff3e
parser: move unescape helper from templater

revset and fileset have a similar problem, so let's make it a common helper
function.

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -122,6 +122,13 @@ def buildargsdict(trees, funcname, keys,
         args[k] = x[2]
     return args
 
+def unescapestr(s):
+    try:
+        return s.decode("string_escape")
+    except ValueError as e:
+        # mangle Python's exception into our format
+        raise error.ParseError(str(e).lower())
+
 def _prettyformat(tree, leafnodes, level, lines):
     if not isinstance(tree, tuple) or tree[0] in leafnodes:
         lines.append((level, str(tree)))
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -39,13 +39,6 @@ elements = {
     "end": (0, None, None, None, None),
 }
 
-def _unescape(s):
-    try:
-        return s.decode("string_escape")
-    except ValueError as e:
-        # mangle Python's exception into our format
-        raise error.ParseError(str(e).lower())
-
 def tokenize(program, start, end):
     pos = start
     while pos < end:
@@ -113,7 +106,7 @@ def tokenize(program, start, end):
                     continue
                 if program.startswith(quote, pos, end):
                     # interpret as if it were a part of an outer string
-                    data = _unescape(program[s:pos])
+                    data = parser.unescapestr(program[s:pos])
                     if token == 'template':
                         data = _parsetemplate(data, 0, len(data))[0]
                     yield (token, data, s)
@@ -162,18 +155,18 @@ 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', _unescape(tmpl[pos:stop])))
+            parsed.append(('string', parser.unescapestr(tmpl[pos:stop])))
             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', _unescape(tmpl[pos:n - 1]) + c))
+            parsed.append(('string', parser.unescapestr(tmpl[pos:n - 1]) + c))
             pos = n + 1
             continue
         if n > pos:
-            parsed.append(('string', _unescape(tmpl[pos:n])))
+            parsed.append(('string', parser.unescapestr(tmpl[pos:n])))
         if c == quote:
             return parsed, n + 1
 


More information about the Mercurial-devel mailing list