From 2b60fe5c916a34d9ac857749451e7f6ce59c67bb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 21 Apr 2008 08:23:59 +0200 Subject: [PATCH] fixed setup.py, a type and removed the possibility to use multiple statements per block. The latter makes it easier to write custom tags --HG-- branch : trunk --- jinja2/ext.py | 1 + jinja2/i18n.py | 3 +-- jinja2/lexer.py | 5 ++--- jinja2/parser.py | 34 ++++++---------------------------- setup.py | 2 +- 5 files changed, 11 insertions(+), 34 deletions(-) diff --git a/jinja2/ext.py b/jinja2/ext.py index 75952ed..230c022 100644 --- a/jinja2/ext.py +++ b/jinja2/ext.py @@ -34,6 +34,7 @@ class Extension(object): class CacheExtension(Extension): + """An example extension that adds cacheable blocks.""" tags = set(['cache']) def parse(self, parser): diff --git a/jinja2/i18n.py b/jinja2/i18n.py index 3e1392c..f6ec791 100644 --- a/jinja2/i18n.py +++ b/jinja2/i18n.py @@ -150,7 +150,6 @@ class TransExtension(Extension): referenced.update(plural_names) else: parser.stream.next() - parser.end_statement() # register free names as simple name expressions for var in referenced: @@ -173,7 +172,7 @@ class TransExtension(Extension): variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y) for x, y in variables.items()]) else: - vairables = None + variables = None node = self._make_node(singular, plural, variables, plural_expr) node.set_lineno(lineno) diff --git a/jinja2/lexer.py b/jinja2/lexer.py index 364d3f5..eab6e88 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -38,9 +38,8 @@ float_re = re.compile(r'\d+\.\d+') # set of used keywords keywords = set(['and', 'block', 'elif', 'else', 'endblock', 'print', 'endfilter', 'endfor', 'endif', 'endmacro', 'endraw', - 'extends', 'filter', 'for', 'if', 'in', - 'include', 'is', 'macro', 'not', 'or', 'raw', - 'recursive', 'set', 'call', 'endcall']) + 'extends', 'filter', 'for', 'if', 'in', 'include' + 'is', 'macro', 'not', 'or', 'raw', 'call', 'endcall']) # bind operators to token types operators = { diff --git a/jinja2/parser.py b/jinja2/parser.py index 27f54ef..54aa06e 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -16,9 +16,7 @@ from jinja2.exceptions import TemplateSyntaxError _statement_keywords = frozenset(['for', 'if', 'block', 'extends', 'print', 'macro', 'include']) _compare_operators = frozenset(['eq', 'ne', 'lt', 'lteq', 'gt', 'gteq', 'in']) -statement_end_tokens = set(['elif', 'else', 'endblock', 'endfilter', - 'endfor', 'endif', 'endmacro', 'variable_end', - 'in', 'recursive', 'endcall', 'block_end']) +statement_end_tokens = set(['variable_end', 'block_end', 'in']) class Parser(object): @@ -40,15 +38,6 @@ class Parser(object): for tag in extension.tags: self.extensions[tag] = extension.parse - def end_statement(self): - """Make sure that the statement ends properly.""" - if self.stream.current.type is 'semicolon': - self.stream.next() - elif self.stream.current.type not in statement_end_tokens: - raise TemplateSyntaxError('ambigous end of statement', - self.stream.current.lineno, - self.filename) - def parse_statement(self): """Parse a single statement.""" token_type = self.stream.current.type @@ -68,7 +57,6 @@ class Parser(object): result = self.parse_assign(expr) else: result = nodes.ExprStmt(expr, lineno=lineno) - self.end_statement() return result def parse_assign(self, target): @@ -79,7 +67,6 @@ class Parser(object): target, target.lineno, self.filename) expr = self.parse_tuple() - self.end_statement() target.set_ctx('store') return nodes.Assign(target, expr, lineno=lineno) @@ -92,17 +79,11 @@ class Parser(object): if self.stream.current.type is 'colon': self.stream.next() - if self.stream.current.type is 'block_end': - self.stream.next() - result = self.subparse(end_tokens) - else: - result = [] - while not self.stream.current.test_many(end_tokens): - if self.stream.current.type is 'block_end': - self.stream.next() - result.extend(self.subparse(end_tokens)) - break - result.append(self.parse_statement()) + # in the future it would be possible to add whole code sections + # by adding some sort of end of statement token and parsing those here. + self.stream.expect('block_end') + result = self.subparse(end_tokens) + if drop_needle: self.stream.next() return result @@ -159,7 +140,6 @@ class Parser(object): def parse_extends(self): node = nodes.Extends(lineno=self.stream.expect('extends').lineno) node.template = self.parse_expression() - self.end_statement() return node def parse_include(self): @@ -180,7 +160,6 @@ class Parser(object): else: node.target = None node.template = expr - self.end_statement() return node def parse_signature(self, node): @@ -239,7 +218,6 @@ class Parser(object): if node.nodes: self.stream.expect('comma') node.nodes.append(self.parse_expression()) - self.end_statement() return node def parse_expression(self, no_condexpr=False): diff --git a/setup.py b/setup.py index a12d998..837de51 100644 --- a/setup.py +++ b/setup.py @@ -131,7 +131,7 @@ setup( ] ) }, - extras_require={'i18n': ['Babel>=0.8']} + extras_require={'i18n': ['Babel>=0.8']}, entry_points=""" [babel.extractors] jinja2 = jinja.i18n:babel_extract[i18n] -- 2.26.2