##// 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 def buildargsdict(trees, funcname, keys,
122 122 args[k] = x[2]
123 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 132 def _prettyformat(tree, leafnodes, level, lines):
126 133 if not isinstance(tree, tuple) or tree[0] in leafnodes:
127 134 lines.append((level, str(tree)))
@@ -39,13 +39,6 elements = {
39 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 42 def tokenize(program, start, end):
50 43 pos = start
51 44 while pos < end:
@@ -113,7 +106,7 def tokenize(program, start, end):
113 106 continue
114 107 if program.startswith(quote, pos, end):
115 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 110 if token == 'template':
118 111 data = _parsetemplate(data, 0, len(data))[0]
119 112 yield (token, data, s)
@@ -162,18 +155,18 def _parsetemplate(tmpl, start, stop, qu
162 155 n = min((tmpl.find(c, pos, stop) for c in sepchars),
163 156 key=lambda n: (n < 0, n))
164 157 if n < 0:
165 parsed.append(('string', _unescape(tmpl[pos:stop])))
158 parsed.append(('string', parser.unescapestr(tmpl[pos:stop])))
166 159 pos = stop
167 160 break
168 161 c = tmpl[n]
169 162 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
170 163 if bs % 2 == 1:
171 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 166 pos = n + 1
174 167 continue
175 168 if n > pos:
176 parsed.append(('string', _unescape(tmpl[pos:n])))
169 parsed.append(('string', parser.unescapestr(tmpl[pos:n])))
177 170 if c == quote:
178 171 return parsed, n + 1
179 172
General Comments 0
You need to be logged in to leave comments. Login now