diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-03-09 17:33:46 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-03-09 17:33:46 -0500 |
commit | 4e4a408d7c1c13f072e1da534b194290be442f04 (patch) | |
tree | d25b9e3264ac10d7b76e621181388412549b61c8 /mako | |
parent | d31e4b430fd0311ee29ce10307d6d5bdf3caf9b8 (diff) | |
parent | 34f90e69aee6f1b8f6b85f903c525d1a9389451d (diff) | |
download | mako-4e4a408d7c1c13f072e1da534b194290be442f04.tar.gz |
Merge remote-tracking branch 'github/pr/19'
Diffstat (limited to 'mako')
-rw-r--r-- | mako/lexer.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/mako/lexer.py b/mako/lexer.py index 2fa08e4..a80b898 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -95,31 +95,37 @@ class Lexer(object): # (match and "TRUE" or "FALSE") return match - def parse_until_text(self, *text): + def parse_until_text(self, watch_nesting, *text): startpos = self.match_position text_re = r'|'.join(text) brace_level = 0 + paren_level = 0 + bracket_level = 0 while True: match = self.match(r'#.*\n') if match: continue - match = self.match(r'(\"\"\"|\'\'\'|\"|\')((?<!\\)\\\1|.)*?\1', + match = self.match(r'(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1', re.S) if match: continue match = self.match(r'(%s)' % text_re) - if match: - if match.group(1) == '}' and brace_level > 0: - brace_level -= 1 - continue + if match and not (watch_nesting + and (brace_level > 0 or paren_level > 0 + or bracket_level > 0)): return \ self.text[startpos: self.match_position - len(match.group(1))],\ match.group(1) - match = self.match(r"(.*?)(?=\"|\'|#|%s)" % text_re, re.S) + elif not match: + match = self.match(r"(.*?)(?=\"|\'|#|%s)" % text_re, re.S) if match: brace_level += match.group(1).count('{') brace_level -= match.group(1).count('}') + paren_level += match.group(1).count('(') + paren_level -= match.group(1).count(')') + bracket_level += match.group(1).count('[') + bracket_level -= match.group(1).count(']') continue raise exceptions.SyntaxException( "Expected: %s" % @@ -368,7 +374,7 @@ class Lexer(object): match = self.match(r"<%(!)?") if match: line, pos = self.matched_lineno, self.matched_charpos - text, end = self.parse_until_text(r'%>') + text, end = self.parse_until_text(False, r'%>') # the trailing newline helps # compiler.parse() not complain about indentation text = adjust_whitespace(text) + "\n" @@ -384,9 +390,9 @@ class Lexer(object): match = self.match(r"\${") if match: line, pos = self.matched_lineno, self.matched_charpos - text, end = self.parse_until_text(r'\|', r'}') + text, end = self.parse_until_text(True, r'\|', r'}') if end == '|': - escapes, end = self.parse_until_text(r'}') + escapes, end = self.parse_until_text(True, r'}') else: escapes = "" text = text.replace('\r\n', '\n') |