parser: move unescape helper from templater
authorYuya Nishihara <yuya@tcha.org>
Thu, 10 Sep 2015 23:25:10 +0900
changeset 26231 87c9c562c37a
parent 26230 6b16a3538c20
child 26232 43f9976346e9
parser: move unescape helper from templater revset and fileset have a similar problem, so let's make it a common helper function.
mercurial/parser.py
mercurial/templater.py
--- a/mercurial/parser.py	Thu Sep 10 16:14:39 2015 -0700
+++ b/mercurial/parser.py	Thu Sep 10 23:25:10 2015 +0900
@@ -122,6 +122,13 @@
         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)))
--- a/mercurial/templater.py	Thu Sep 10 16:14:39 2015 -0700
+++ b/mercurial/templater.py	Thu Sep 10 23:25:10 2015 +0900
@@ -39,13 +39,6 @@
     "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 @@
                     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 @@
         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