diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-05 07:54:27 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-05 07:54:27 -0700 |
commit | a6105f89965b61a8ae70b5953059d1f986d42abb (patch) | |
tree | 7ab2818815556ce3372a8d8c252ceac239f2db53 /pystache | |
parent | 906a0961e303859f23d67fa2b5e03e0913dc55c4 (diff) | |
download | pystache-a6105f89965b61a8ae70b5953059d1f986d42abb.tar.gz |
Reduced use of recursion in Parser.parse().
Diffstat (limited to 'pystache')
-rw-r--r-- | pystache/parser.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/pystache/parser.py b/pystache/parser.py index 806dac9..12e887e 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -97,9 +97,10 @@ class Parser(object): """ parsed_template = ParsedTemplate() - content_end_index, parsed_section = None, None + states = [] + while True: match = self._template_re.search(template, start_index) @@ -142,17 +143,28 @@ class Parser(object): leading_whitespace = '' if tag_type in ('#', '^'): - start_index, content_end_index, parsed_section = self.parse(template, end_index, tag_key) + # Cache current state. + state = (tag_type, tag_key, leading_whitespace, end_index, section_key, parsed_template) + states.append(state) + + # Initialize new state + start_index, section_key = end_index, tag_key + parsed_template = ParsedTemplate() + content_end_index, parsed_section = None, None - elif tag_type == '/': + continue + + if tag_type == '/': if tag_key != section_key: raise ParsingError("Section end tag mismatch: %s != %s" % (tag_key, section_key)) - return end_index, match_index, parsed_template + # Restore previous state with newly found section data. + start_index, content_end_index, parsed_section = end_index, match_index, parsed_template + + (tag_type, tag_key, leading_whitespace, end_index, section_key, parsed_template) = states.pop() else: start_index = end_index - # Variable index is now the next character to process. node = self._make_node(template, tag_type, tag_key, leading_whitespace, end_index, content_end_index, parsed_section) |