Index: test/lexer.py =================================================================== --- test/lexer.py (revision 302) +++ test/lexer.py (working copy) @@ -226,6 +226,22 @@ """ nodes = Lexer(template).parse() assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u"hello + '''heres '{|}' text | | }''' ", ['escape1'], (3, 13)), Text(u'\n ', (3, 62))])""" + + def test_embedded_braces(self): + template = """ + + ${{1:1}[0]} + """ + nodes = Lexer(template).parse() + assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u'{1:1}[0]', [], (3, 13)), Text(u'\n ', (3, 24))])""" + + template = """ + + ${{'a':'}', + 'b':'{'}[0]} + """ + nodes = Lexer(template).parse() + assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u"{'a':'}',\n 'b':'{'}[0]", [], (3, 13)), Text(u'\n ', (4, 29))])""" def test_tricky_code(self): template = """<% print 'hi %>' %>""" Index: lib/mako/lexer.py =================================================================== --- lib/mako/lexer.py (revision 302) +++ lib/mako/lexer.py (working copy) @@ -7,6 +7,7 @@ """provides the Lexer class for parsing template strings into parse trees.""" import re, codecs +import codeop from mako import parsetree, exceptions from mako.pygen import adjust_whitespace @@ -281,6 +282,11 @@ if match: (line, pos) = (self.matched_lineno, self.matched_charpos) (text, end) = self.parse_until_text(r'\|', r'}') + text = text.lstrip() + while end and not codeop.compile_command(text,'