##// 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 ([('string', 'foo\\')], 6)
161 ([('string', 'foo\\')], 6)
162 """
162 """
163 parsed = []
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 sepchars = '{' + quote
177 sepchars = '{' + quote
165 pos = start
178 pos = start
166 p = parser.parser(elements)
179 p = parser.parser(elements)
@@ -168,29 +181,30 b' def _parsetemplate(tmpl, start, stop, qu'
168 n = min((tmpl.find(c, pos, stop) for c in sepchars),
181 n = min((tmpl.find(c, pos, stop) for c in sepchars),
169 key=lambda n: (n < 0, n))
182 key=lambda n: (n < 0, n))
170 if n < 0:
183 if n < 0:
171 parsed.append(('string', parser.unescapestr(tmpl[pos:stop])))
184 yield ('string', parser.unescapestr(tmpl[pos:stop]), pos)
172 pos = stop
185 pos = stop
173 break
186 break
174 c = tmpl[n:n + 1]
187 c = tmpl[n:n + 1]
175 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
188 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
176 if bs % 2 == 1:
189 if bs % 2 == 1:
177 # escaped (e.g. '\{', '\\\{', but not '\\{')
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 pos = n + 1
192 pos = n + 1
180 continue
193 continue
181 if n > pos:
194 if n > pos:
182 parsed.append(('string', parser.unescapestr(tmpl[pos:n])))
195 yield ('string', parser.unescapestr(tmpl[pos:n]), pos)
183 if c == quote:
196 if c == quote:
184 return parsed, n + 1
197 yield ('end', None, n + 1)
198 return
185
199
186 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
200 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
187 if not tmpl.endswith('}', n + 1, pos):
201 if not tmpl.endswith('}', n + 1, pos):
188 raise error.ParseError(_("invalid token"), pos)
202 raise error.ParseError(_("invalid token"), pos)
189 parsed.append(parseres)
203 yield ('template', parseres, n)
190
204
191 if quote:
205 if quote:
192 raise error.ParseError(_("unterminated string"), start)
206 raise error.ParseError(_("unterminated string"), start)
193 return parsed, pos
207 yield ('end', None, pos)
194
208
195 def _unnesttemplatelist(tree):
209 def _unnesttemplatelist(tree):
196 """Expand list of templates to node tuple
210 """Expand list of templates to node tuple
General Comments 0
You need to be logged in to leave comments. Login now