##// END OF EJS Templates
parser: move unescape helper from templater...
Yuya Nishihara -
r26231:87c9c562 default
parent child Browse files
Show More
@@ -122,6 +122,13 b' def buildargsdict(trees, funcname, keys,'
122 args[k] = x[2]
122 args[k] = x[2]
123 return args
123 return args
124
124
125 def unescapestr(s):
126 try:
127 return s.decode("string_escape")
128 except ValueError as e:
129 # mangle Python's exception into our format
130 raise error.ParseError(str(e).lower())
131
125 def _prettyformat(tree, leafnodes, level, lines):
132 def _prettyformat(tree, leafnodes, level, lines):
126 if not isinstance(tree, tuple) or tree[0] in leafnodes:
133 if not isinstance(tree, tuple) or tree[0] in leafnodes:
127 lines.append((level, str(tree)))
134 lines.append((level, str(tree)))
@@ -39,13 +39,6 b' elements = {'
39 "end": (0, None, None, None, None),
39 "end": (0, None, None, None, None),
40 }
40 }
41
41
42 def _unescape(s):
43 try:
44 return s.decode("string_escape")
45 except ValueError as e:
46 # mangle Python's exception into our format
47 raise error.ParseError(str(e).lower())
48
49 def tokenize(program, start, end):
42 def tokenize(program, start, end):
50 pos = start
43 pos = start
51 while pos < end:
44 while pos < end:
@@ -113,7 +106,7 b' def tokenize(program, start, end):'
113 continue
106 continue
114 if program.startswith(quote, pos, end):
107 if program.startswith(quote, pos, end):
115 # interpret as if it were a part of an outer string
108 # interpret as if it were a part of an outer string
116 data = _unescape(program[s:pos])
109 data = parser.unescapestr(program[s:pos])
117 if token == 'template':
110 if token == 'template':
118 data = _parsetemplate(data, 0, len(data))[0]
111 data = _parsetemplate(data, 0, len(data))[0]
119 yield (token, data, s)
112 yield (token, data, s)
@@ -162,18 +155,18 b' def _parsetemplate(tmpl, start, stop, qu'
162 n = min((tmpl.find(c, pos, stop) for c in sepchars),
155 n = min((tmpl.find(c, pos, stop) for c in sepchars),
163 key=lambda n: (n < 0, n))
156 key=lambda n: (n < 0, n))
164 if n < 0:
157 if n < 0:
165 parsed.append(('string', _unescape(tmpl[pos:stop])))
158 parsed.append(('string', parser.unescapestr(tmpl[pos:stop])))
166 pos = stop
159 pos = stop
167 break
160 break
168 c = tmpl[n]
161 c = tmpl[n]
169 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
162 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
170 if bs % 2 == 1:
163 if bs % 2 == 1:
171 # escaped (e.g. '\{', '\\\{', but not '\\{')
164 # escaped (e.g. '\{', '\\\{', but not '\\{')
172 parsed.append(('string', _unescape(tmpl[pos:n - 1]) + c))
165 parsed.append(('string', parser.unescapestr(tmpl[pos:n - 1]) + c))
173 pos = n + 1
166 pos = n + 1
174 continue
167 continue
175 if n > pos:
168 if n > pos:
176 parsed.append(('string', _unescape(tmpl[pos:n])))
169 parsed.append(('string', parser.unescapestr(tmpl[pos:n])))
177 if c == quote:
170 if c == quote:
178 return parsed, n + 1
171 return parsed, n + 1
179
172
General Comments 0
You need to be logged in to leave comments. Login now