Skip to content
Myr

This commit makes the parser and lexer use regular string comparison to support jython

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Ali Afshar · committed by Ali Afshar · parent 62ccd1b · Browse files · 3 files+56 added−56 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+56 added−56 removed

  • other:+56 added−56 removed

3 files+56 added−56 removed

modified:jinja2/defaults.py+1 added-1 removed
34 unmodified lines
35}363738# export all constants39__all__ = tuple(x for x in locals() if x.isupper())39__all__ = tuple(x for x in locals().keys() if x.isupper())
modified:jinja2/lexer.py+3 added-3 removed
99 unmodified lines
100101 def __str__(self):102 if self.type in reverse_operators:103 return reverse_operators[self.type]104 elif self.type is 'name':104 elif self.type == 'name':105 return self.value106 return self.type107108 def test(self, expr):
100 unmodified lines
209 """Go one token ahead and return the old one"""210 rv = self.current211 if self._pushed:212 self.current = self._pushed.popleft()213 elif self.current.type is not 'eof':213 elif self.current.type != 'eof':214 try:215 self.current = self._next()216 except StopIteration:217 self.close()
11 unmodified lines
229 """230 if not self.current.test(expr):231 if ':' in expr:232 expr = expr.split(':')[1]233 if self.current.type is 'eof':233 if self.current.type == 'eof':234 raise TemplateSyntaxError('unexpected end of template, '235 'expected %r.' % expr,236 self.current.lineno,237 self.name, self.filename)
324 unmodified lines
modified:jinja2/parser.py+52 added-52 removed
61 unmodified lines
6263 def parse_statement(self):64 """Parse a single statement."""65 token = self.stream.current66 if token.type is not 'name':66 if token.type != 'name':67 self.fail('tag name expected', token.lineno)68 if token.value in _statement_keywords:69 return getattr(self, 'parse_' + self.stream.current.value)()70 if token.value == 'call':
130 unmodified lines
201202 while 1:203 if node.names:204 self.stream.expect('comma')205 if self.stream.current.type is 'name':205 if self.stream.current.type == 'name':206 if parse_context():207 break208 target = self.parse_assign_target(name_only=True)209 if target.name.startswith('_'):
4 unmodified lines
214 alias = self.parse_assign_target(name_only=True)215 node.names.append((target.name, alias.name))216 else:217 node.names.append(target.name)218 if parse_context() or self.stream.current.type is not 'comma':218 if parse_context() or self.stream.current.type != 'comma':219 break220 else:221 break222 if not hasattr(node, 'with_context'):
4 unmodified lines
227 def parse_signature(self, node):228 node.args = args = []229 node.defaults = defaults = []230 self.stream.expect('lparen')231 while self.stream.current.type is not 'rparen':231 while self.stream.current.type != 'rparen':232 if args:233 self.stream.expect('comma')234 arg = self.parse_assign_target(name_only=True)235 arg.set_ctx('param')
3 unmodified lines
239 self.stream.expect('rparen')240241 def parse_call_block(self):242 node = nodes.CallBlock(lineno=self.stream.next().lineno)243 if self.stream.current.type is 'lparen':243 if self.stream.current.type == 'lparen':244 self.parse_signature(node)245 else:246 node.args = []247 node.defaults = []
21 unmodified lines
269270 def parse_print(self):271 node = nodes.Output(lineno=self.stream.next().lineno)272 node.nodes = []273 while self.stream.current.type is not 'block_end':273 while self.stream.current.type != 'block_end':274 if node.nodes:275 self.stream.expect('comma')276 node.nodes.append(self.parse_expression())277 return node
86 unmodified lines
364365 def parse_add(self):366 lineno = self.stream.current.lineno367 left = self.parse_sub()368 while self.stream.current.type is 'add':368 while self.stream.current.type == 'add':369 self.stream.next()370 right = self.parse_sub()371 left = nodes.Add(left, right, lineno=lineno)372 lineno = self.stream.current.lineno
1 unmodified line
374375 def parse_sub(self):376 lineno = self.stream.current.lineno377 left = self.parse_concat()378 while self.stream.current.type is 'sub':378 while self.stream.current.type == 'sub':379 self.stream.next()380 right = self.parse_concat()381 left = nodes.Sub(left, right, lineno=lineno)382 lineno = self.stream.current.lineno
1 unmodified line
384385 def parse_concat(self):386 lineno = self.stream.current.lineno387 args = [self.parse_mul()]388 while self.stream.current.type is 'tilde':388 while self.stream.current.type == 'tilde':389 self.stream.next()390 args.append(self.parse_mul())391 if len(args) == 1:392 return args[0]
1 unmodified line
394395 def parse_mul(self):396 lineno = self.stream.current.lineno397 left = self.parse_div()398 while self.stream.current.type is 'mul':398 while self.stream.current.type == 'mul':399 self.stream.next()400 right = self.parse_div()401 left = nodes.Mul(left, right, lineno=lineno)402 lineno = self.stream.current.lineno
1 unmodified line
404405 def parse_div(self):406 lineno = self.stream.current.lineno407 left = self.parse_floordiv()408 while self.stream.current.type is 'div':408 while self.stream.current.type == 'div':409 self.stream.next()410 right = self.parse_floordiv()411 left = nodes.Div(left, right, lineno=lineno)412 lineno = self.stream.current.lineno
1 unmodified line
414415 def parse_floordiv(self):416 lineno = self.stream.current.lineno417 left = self.parse_mod()418 while self.stream.current.type is 'floordiv':418 while self.stream.current.type == 'floordiv':419 self.stream.next()420 right = self.parse_mod()421 left = nodes.FloorDiv(left, right, lineno=lineno)422 lineno = self.stream.current.lineno
1 unmodified line
424425 def parse_mod(self):426 lineno = self.stream.current.lineno427 left = self.parse_pow()428 while self.stream.current.type is 'mod':428 while self.stream.current.type == 'mod':429 self.stream.next()430 right = self.parse_pow()431 left = nodes.Mod(left, right, lineno=lineno)432 lineno = self.stream.current.lineno
1 unmodified line
434435 def parse_pow(self):436 lineno = self.stream.current.lineno437 left = self.parse_unary()438 while self.stream.current.type is 'pow':438 while self.stream.current.type == 'pow':439 self.stream.next()440 right = self.parse_unary()441 left = nodes.Pow(left, right, lineno=lineno)442 lineno = self.stream.current.lineno
1 unmodified line
444445 def parse_unary(self):446 token_type = self.stream.current.type447 lineno = self.stream.current.lineno448 if token_type is 'name' and self.stream.current.value == 'not':448 if token_type == 'name' and self.stream.current.value == 'not':449 self.stream.next()450 node = self.parse_unary()451 return nodes.Not(node, lineno=lineno)452 if token_type is 'sub':452 if token_type == 'sub':453 self.stream.next()454 node = self.parse_unary()455 return nodes.Neg(node, lineno=lineno)456 if token_type is 'add':456 if token_type == 'add':457 self.stream.next()458 node = self.parse_unary()459 return nodes.Pos(node, lineno=lineno)460 return self.parse_primary()461462 def parse_primary(self, with_postfix=True):463 token = self.stream.current464 if token.type is 'name':464 if token.type == 'name':465 if token.value in ('true', 'false', 'True', 'False'):466 node = nodes.Const(token.value in ('true', 'True'),467 lineno=token.lineno)468 elif token.value in ('none', 'None'):469 node = nodes.Const(None, lineno=token.lineno)470 else:471 node = nodes.Name(token.value, 'load', lineno=token.lineno)472 self.stream.next()473 elif token.type is 'string':473 elif token.type == 'string':474 self.stream.next()475 buf = [token.value]476 lineno = token.lineno477 while self.stream.current.type is 'string':477 while self.stream.current.type == 'string':478 buf.append(self.stream.current.value)479 self.stream.next()480 node = nodes.Const(''.join(buf), lineno=lineno)481 elif token.type in ('integer', 'float'):482 self.stream.next()483 node = nodes.Const(token.value, lineno=token.lineno)484 elif token.type is 'lparen':484 elif token.type == 'lparen':485 self.stream.next()486 node = self.parse_tuple()487 self.stream.expect('rparen')488 elif token.type is 'lbracket':488 elif token.type == 'lbracket':489 node = self.parse_list()490 elif token.type is 'lbrace':490 elif token.type == 'lbrace':491 node = self.parse_dict()492 else:493 self.fail("unexpected token '%s'" % (token,), token.lineno)494 if with_postfix:
30 unmodified lines
525 self.stream.expect('comma')526 if self.is_tuple_end(extra_end_rules):527 break528 args.append(parse())529 if self.stream.current.type is 'comma':529 if self.stream.current.type == 'comma':530 is_tuple = True531 else:532 break533 lineno = self.stream.current.lineno
3 unmodified lines
537538 def parse_list(self):539 token = self.stream.expect('lbracket')540 items = []541 while self.stream.current.type is not 'rbracket':541 while self.stream.current.type != 'rbracket':542 if items:543 self.stream.expect('comma')544 if self.stream.current.type == 'rbracket':545 break
3 unmodified lines
549550 def parse_dict(self):551 token = self.stream.expect('lbrace')552 items = []553 while self.stream.current.type is not 'rbrace':553 while self.stream.current.type != 'rbrace':554 if items:555 self.stream.expect('comma')556 if self.stream.current.type == 'rbrace':557 break
6 unmodified lines
564565 def parse_postfix(self, node):566 while 1:567 token_type = self.stream.current.type568 if token_type is 'dot' or token_type is 'lbracket':568 if token_type == 'dot' or token_type == 'lbracket':569 node = self.parse_subscript(node)570 elif token_type is 'lparen':570 elif token_type == 'lparen':571 node = self.parse_call(node)572 elif token_type is 'pipe':572 elif token_type == 'pipe':573 node = self.parse_filter(node)574 elif token_type is 'name' and self.stream.current.value == 'is':574 elif token_type == 'name' and self.stream.current.value == 'is':575 node = self.parse_test(node)576 else:577 break578 return node579580 def parse_subscript(self, node):581 token = self.stream.next()582 if token.type is 'dot':582 if token.type == 'dot':583 attr_token = self.stream.current584 self.stream.next()585 if attr_token.type is 'name':585 if attr_token.type == 'name':586 return nodes.Getattr(node, attr_token.value, 'load',587 lineno=token.lineno)588 elif attr_token.type is not 'integer':588 elif attr_token.type != 'integer':589 self.fail('expected name or number', attr_token.lineno)590 arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)591 return nodes.Getitem(node, arg, 'load', lineno=token.lineno)592 if token.type is 'lbracket':592 if token.type == 'lbracket':593 priority_on_attribute = False594 args = []595 while self.stream.current.type is not 'rbracket':595 while self.stream.current.type != 'rbracket':596 if args:597 self.stream.expect('comma')598 args.append(self.parse_subscribed())599 self.stream.expect('rbracket')
6 unmodified lines
606607 def parse_subscribed(self):608 lineno = self.stream.current.lineno609610 if self.stream.current.type is 'colon':610 if self.stream.current.type == 'colon':611 self.stream.next()612 args = [None]613 else:614 node = self.parse_expression()615 if self.stream.current.type is not 'colon':615 if self.stream.current.type != 'colon':616 return node617 self.stream.next()618 args = [node]619620 if self.stream.current.type is 'colon':620 if self.stream.current.type == 'colon':621 args.append(None)622 elif self.stream.current.type not in ('rbracket', 'comma'):623 args.append(self.parse_expression())624 else:625 args.append(None)626627 if self.stream.current.type is 'colon':627 if self.stream.current.type == 'colon':628 self.stream.next()629 if self.stream.current.type not in ('rbracket', 'comma'):630 args.append(self.parse_expression())631 else:
14 unmodified lines
646 if not expr:647 self.fail('invalid syntax for function call expression',648 token.lineno)649650 while self.stream.current.type is not 'rparen':650 while self.stream.current.type != 'rparen':651 if require_comma:652 self.stream.expect('comma')653 # support for trailing comma654 if self.stream.current.type is 'rparen':654 if self.stream.current.type == 'rparen':655 break656 if self.stream.current.type is 'mul':656 if self.stream.current.type == 'mul':657 ensure(dyn_args is None and dyn_kwargs is None)658 self.stream.next()659 dyn_args = self.parse_expression()660 elif self.stream.current.type is 'pow':660 elif self.stream.current.type == 'pow':661 ensure(dyn_kwargs is None)662 self.stream.next()663 dyn_kwargs = self.parse_expression()664 else:665 ensure(dyn_args is None and dyn_kwargs is None)666 if self.stream.current.type is 'name' and \667 self.stream.look().type is 'assign':666 if self.stream.current.type == 'name' and \667 self.stream.look().type == 'assign':668 key = self.stream.current.value669 self.stream.skip(2)670 value = self.parse_expression()671 kwargs.append(nodes.Keyword(key, value,
15 unmodified lines
687 if not start_inline:688 self.stream.next()689 token = self.stream.expect('name')690 name = token.value691 while self.stream.current.type is 'dot':691 while self.stream.current.type == 'dot':692 self.stream.next()693 name += '.' + self.stream.expect('name').value694 if self.stream.current.type is 'lparen':694 if self.stream.current.type == 'lparen':695 args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None)696 else:697 args = []698 kwargs = []
10 unmodified lines
709 negated = True710 else:711 negated = False712 name = self.stream.expect('name').value713 while self.stream.current.type is 'dot':713 while self.stream.current.type == 'dot':714 self.stream.next()715 name += '.' + self.stream.expect('name').value716 dyn_args = dyn_kwargs = None717 kwargs = []718 if self.stream.current.type is 'lparen':718 if self.stream.current.type == 'lparen':719 args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None)720 elif self.stream.current.type in ('name', 'string', 'integer',721 'float', 'lparen', 'lbracket',722 'lbrace') and not \
22 unmodified lines
745 del data_buffer[:]746747 while self.stream:748 token = self.stream.current749 if token.type is 'data':749 if token.type == 'data':750 if token.value:751 add_data(nodes.TemplateData(token.value,752 lineno=token.lineno))753 self.stream.next()754 elif token.type is 'variable_begin':754 elif token.type == 'variable_begin':755 self.stream.next()756 add_data(self.parse_tuple(with_condexpr=True))757 self.stream.expect('variable_end')758 elif token.type is 'block_begin':758 elif token.type == 'block_begin':759 flush_data()760 self.stream.next()761 if end_tokens is not None and \762 self.stream.current.test_any(*end_tokens):
18 unmodified lines