Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

fix for mako lexing bugs: embedded escaped quotes and dict literals

View mako-lexer-fix.diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
diff -r 1190503e3c7d mako/lexer.py
--- a/mako/lexer.py Sat Jan 21 18:57:01 2012 -0500
+++ b/mako/lexer.py Tue Jan 31 14:29:30 2012 -0800
@@ -91,31 +91,32 @@
def parse_until_text(self, *text):
startpos = self.match_position
+ text_re = r'|'.join(text)
+ brace_level = 0
while True:
match = self.match(r'#.*\n')
if match:
continue
- match = self.match(r'(\"\"\"|\'\'\'|\"|\')')
+ match = self.match(r'(\"\"\"|\'\'\'|\"|\')((?<!\\)\\\1|.)*?\1', re.S)
if match:
- m = self.match(r'.*?%s' % match.group(1), re.S)
- if not m:
- raise exceptions.SyntaxException(
- "Unmatched '%s'" %
- match.group(1),
- **self.exception_kwargs)
- else:
- match = self.match(r'(%s)' % r'|'.join(text))
- if match:
- return \
- self.text[startpos:self.match_position-len(match.group(1))],\
- match.group(1)
- else:
- match = self.match(r".*?(?=\"|\'|#|%s)" % r'|'.join(text), re.S)
- if not match:
- raise exceptions.SyntaxException(
- "Expected: %s" %
- ','.join(text),
- **self.exception_kwargs)
+ continue
+ match = self.match(r'(%s)' % text_re)
+ if match:
+ if match.group(1) == '}' and brace_level > 0:
+ brace_level -= 1
+ continue
+ return \
+ self.text[startpos:self.match_position-len(match.group(1))],\
+ match.group(1)
+ match = self.match(r"(.*?)(?=\"|\'|#|%s)" % text_re, re.S)
+ if match:
+ brace_level += match.group(1).count('{')
+ brace_level -= match.group(1).count('}')
+ continue
+ raise exceptions.SyntaxException(
+ "Expected: %s" %
+ ','.join(text),
+ **self.exception_kwargs)
def append_node(self, nodecls, *args, **kwargs):
kwargs.setdefault('source', self.text)
diff -r 1190503e3c7d test/test_lexer.py
--- a/test/test_lexer.py Sat Jan 21 18:57:01 2012 -0500
+++ b/test/test_lexer.py Tue Jan 31 14:29:30 2012 -0800
@@ -557,6 +557,23 @@
False, (1, 1)),
Text(u" '''and now some text '''", (10,11))]))
+ def test_tricky_code_4(self):
+ template = \
+ """<% foo = "\\"\\\\" %>"""
+ nodes = Lexer(template).parse()
+ self._compare(nodes, TemplateNode({},
+ [Code(u"""foo = "\\"\\\\" \n""",
+ False, (1, 1))]))
+
+ def test_tricky_code_5(self):
+ template = \
+ """before ${ {'key': 'value'} } after"""
+ nodes = Lexer(template).parse()
+ self._compare(nodes, TemplateNode({},
+ [Text(u'before ', (1, 1)),
+ Expression(u" {'key': 'value'} ", [], (1, 8)),
+ Text(u' after', (1, 29))]))
+
def test_control_lines(self):
template = \
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.