##// END OF EJS Templates
templater: extract function scanning template string...
Yuya Nishihara -
r36258:18bdfad8 default
parent child Browse files
Show More
@@ -161,6 +161,19 b' def _parsetemplate(tmpl, start, stop, qu'
161 161 ([('string', 'foo\\')], 6)
162 162 """
163 163 parsed = []
164 for typ, val, pos in _scantemplate(tmpl, start, stop, quote):
165 if typ == 'string':
166 parsed.append((typ, val))
167 elif typ == 'template':
168 parsed.append(val)
169 elif typ == 'end':
170 return parsed, pos
171 else:
172 raise error.ProgrammingError('unexpected type: %s' % typ)
173 raise error.ProgrammingError('unterminated scanning of template')
174
175 def _scantemplate(tmpl, start, stop, quote=''):
176 """Parse template string into chunks of strings and template expressions"""
164 177 sepchars = '{' + quote
165 178 pos = start
166 179 p = parser.parser(elements)
@@ -168,29 +181,30 b' def _parsetemplate(tmpl, start, stop, qu'
168 181 n = min((tmpl.find(c, pos, stop) for c in sepchars),
169 182 key=lambda n: (n < 0, n))
170 183 if n < 0:
171 parsed.append(('string', parser.unescapestr(tmpl[pos:stop])))
184 yield ('string', parser.unescapestr(tmpl[pos:stop]), pos)
172 185 pos = stop
173 186 break
174 187 c = tmpl[n:n + 1]
175 188 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
176 189 if bs % 2 == 1:
177 190 # escaped (e.g. '\{', '\\\{', but not '\\{')
178 parsed.append(('string', parser.unescapestr(tmpl[pos:n - 1]) + c))
191 yield ('string', parser.unescapestr(tmpl[pos:n - 1]) + c, pos)
179 192 pos = n + 1
180 193 continue
181 194 if n > pos:
182 parsed.append(('string', parser.unescapestr(tmpl[pos:n])))
195 yield ('string', parser.unescapestr(tmpl[pos:n]), pos)
183 196 if c == quote:
184 return parsed, n + 1
197 yield ('end', None, n + 1)
198 return
185 199
186 200 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
187 201 if not tmpl.endswith('}', n + 1, pos):
188 202 raise error.ParseError(_("invalid token"), pos)
189 parsed.append(parseres)
203 yield ('template', parseres, n)
190 204
191 205 if quote:
192 206 raise error.ParseError(_("unterminated string"), start)
193 return parsed, pos
207 yield ('end', None, pos)
194 208
195 209 def _unnesttemplatelist(tree):
196 210 """Expand list of templates to node tuple
General Comments 0
You need to be logged in to leave comments. Login now