def parse_subscript_expression(self, node): """ Parse a subscript statement. Gets attributes and items from an object. """ lineno = self.stream.lineno if self.stream.current.type == 'dot': self.stream.next() token = self.stream.current if token.type in ('name', 'integer'): arg = nodes.ConstantExpression(token.value, token.lineno, self.filename) else: raise TemplateSyntaxError('expected name or number', token.lineno, self.filename) self.stream.next() elif self.stream.current.type == 'lbracket': self.stream.next() args = [] while self.stream.current.type != 'rbracket': if args: self.stream.expect('comma') args.append(self.parse_subscribed_expression()) self.stream.expect('rbracket') if len(args) == 1: arg = args[0] else: arg = nodes.TupleExpression(args, lineno, self.filename) else: raise TemplateSyntaxError('expected subscript expression', self.lineno, self.filename) return nodes.SubscriptExpression(node, arg, lineno, self.filename)
def parse_number_expression(self): """ Parse a number literal. """ token = self.stream.current if token.type not in ('integer', 'float'): raise TemplateSyntaxError('integer or float literal expected', token.lineno, self.filename) self.stream.next() return nodes.ConstantExpression(token.value, token.lineno, self.filename)
def parse_bool_expression(self): """ Parse a boolean literal. """ token = self.stream.expect('name') if token.value == 'true': value = True elif token.value == 'false': value = False else: raise TemplateSyntaxError("expected boolean literal", token.lineno, self.filename) return nodes.ConstantExpression(value, token.lineno, self.filename)
def parse_string_expression(self): """ Parse a string literal. """ token = self.stream.expect('string') return nodes.ConstantExpression(token.value, token.lineno, self.filename)
def parse_none_expression(self): """ Parse a none literal. """ token = self.stream.expect('name', 'none') return nodes.ConstantExpression(None, token.lineno, self.filename)