Commit a3141ab8 by Chris Jerdonek

Changed Parser.parse() to read from the matches dictionary earlier.

parent 854b84c9
...@@ -31,7 +31,7 @@ def _compile_template_re(delimiters): ...@@ -31,7 +31,7 @@ def _compile_template_re(delimiters):
(?: (?:
(?P<change>=) \s* (?P<delims>.+?) \s* = | (?P<change>=) \s* (?P<delims>.+?) \s* = |
(?P<raw>{) \s* (?P<raw_name>.+?) \s* } | (?P<raw>{) \s* (?P<raw_name>.+?) \s* } |
(?P<tag>[%(tag_types)s]?) \s* (?P<name>[\s\S]+?) (?P<tag>[%(tag_types)s]?) \s* (?P<tag_key>[\s\S]+?)
) )
\s* %(ctag)s \s* %(ctag)s
""" % {'tag_types': tag_types, 'otag': re.escape(delimiters[0]), 'ctag': re.escape(delimiters[1])} """ % {'tag_types': tag_types, 'otag': re.escape(delimiters[0]), 'ctag': re.escape(delimiters[1])}
...@@ -99,24 +99,15 @@ class Parser(object): ...@@ -99,24 +99,15 @@ class Parser(object):
matches = match.groupdict() matches = match.groupdict()
index = self._handle_match(template, parse_tree, matches, start_index, match_index, end_index)
# Save the rest of the template.
parse_tree.append(template[index:])
return parse_tree
def _handle_match(self, template, parse_tree, matches, start_index, match_index, end_index):
engine = self.engine
# Normalize the matches dictionary. # Normalize the matches dictionary.
if matches['change'] is not None: if matches['change'] is not None:
matches.update(tag='=', name=matches['delims']) matches.update(tag='=', tag_key=matches['delims'])
elif matches['raw'] is not None: elif matches['raw'] is not None:
matches.update(tag='&', name=matches['raw_name']) matches.update(tag='&', tag_key=matches['raw_name'])
tag_type = matches['tag'] tag_type = matches['tag']
tag_key = matches['tag_key']
leading_whitespace = matches['whitespace']
# Standalone (non-interpolation) tags consume the entire line, # Standalone (non-interpolation) tags consume the entire line,
# both leading whitespace and trailing newline. # both leading whitespace and trailing newline.
...@@ -129,23 +120,32 @@ class Parser(object): ...@@ -129,23 +120,32 @@ class Parser(object):
end_index += template[end_index] == '\r' and 1 or 0 end_index += template[end_index] == '\r' and 1 or 0
if end_index < len(template): if end_index < len(template):
end_index += template[end_index] == '\n' and 1 or 0 end_index += template[end_index] == '\n' and 1 or 0
elif matches['whitespace']: elif leading_whitespace:
parse_tree.append(matches['whitespace']) parse_tree.append(leading_whitespace)
match_index += len(matches['whitespace']) match_index += len(leading_whitespace)
matches['whitespace'] = '' leading_whitespace = ''
name = matches['name'] index = self._handle_match(template, parse_tree, tag_type, tag_key, leading_whitespace, start_index, match_index, end_index)
# Save the rest of the template.
parse_tree.append(template[index:])
return parse_tree
def _handle_match(self, template, parse_tree, tag_type, tag_key, leading_whitespace, start_index, match_index, end_index):
if tag_type == '!': if tag_type == '!':
return end_index return end_index
if tag_type == '=': if tag_type == '=':
delimiters = name.split() delimiters = tag_key.split()
self._change_delimiters(delimiters) self._change_delimiters(delimiters)
return end_index return end_index
engine = self.engine
if tag_type == '>': if tag_type == '>':
func = engine._make_get_partial(name, matches['whitespace']) func = engine._make_get_partial(tag_key, leading_whitespace)
elif tag_type in ['#', '^']: elif tag_type in ['#', '^']:
try: try:
...@@ -156,20 +156,21 @@ class Parser(object): ...@@ -156,20 +156,21 @@ class Parser(object):
end_index = e.position end_index = e.position
if tag_type == '#': if tag_type == '#':
func = engine._make_get_section(name, bufr, tmpl, self._delimiters) func = engine._make_get_section(tag_key, bufr, tmpl, self._delimiters)
else: else:
func = engine._make_get_inverse(name, bufr) func = engine._make_get_inverse(tag_key, bufr)
elif tag_type == '&': elif tag_type == '&':
func = engine._make_get_literal(name) func = engine._make_get_literal(tag_key)
elif tag_type == '': elif tag_type == '':
func = engine._make_get_escaped(name) func = engine._make_get_escaped(tag_key)
elif tag_type == '/': elif tag_type == '/':
# TODO: check that tag key matches section start tag key.
# TODO: don't use exceptions for flow control. # TODO: don't use exceptions for flow control.
raise EndOfSection(parse_tree, template[start_index:match_index], end_index) raise EndOfSection(parse_tree, template[start_index:match_index], end_index)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment