Пример #1
0
 def parse_primary(self):
     token = self.stream.current
     if token.type == 'name':
         if token.value in ('true', 'false', 'True', 'False'):
             node = nodes.Const(token.value in ('true', 'True'),
                                lineno=token.lineno)
         elif token.value in ('none', 'None'):
             node = nodes.Const(None, lineno=token.lineno)
         else:
             node = nodes.Name(token.value, 'load', lineno=token.lineno)
         next(self.stream)
     elif token.type == 'string':
         next(self.stream)
         buf = [token.value]
         lineno = token.lineno
         while self.stream.current.type == 'string':
             buf.append(self.stream.current.value)
             next(self.stream)
         node = nodes.Const(''.join(buf), lineno=lineno)
     elif token.type in ('integer', 'float'):
         next(self.stream)
         node = nodes.Const(token.value, lineno=token.lineno)
     elif token.type == 'lparen':
         next(self.stream)
         node = self.parse_tuple(explicit_parentheses=True)
         self.stream.expect('rparen')
     elif token.type == 'lbracket':
         node = self.parse_list()
     elif token.type == 'lbrace':
         node = self.parse_dict()
     else:
         self.fail("unexpected '%s'" % describe_token(token), token.lineno)
     return node
Пример #2
0
 def parse_primary(self):
     token = self.stream.current
     if token.type == 'name':
         if token.value in ('true', 'false', 'True', 'False'):
             node = nodes.Const(token.value in ('true', 'True'),
                                lineno=token.lineno)
         elif token.value in ('none', 'None'):
             node = nodes.Const(None, lineno=token.lineno)
         else:
             node = nodes.Name(token.value, 'load', lineno=token.lineno)
         next(self.stream)
     elif token.type == 'string':
         next(self.stream)
         buf = [token.value]
         lineno = token.lineno
         while self.stream.current.type == 'string':
             buf.append(self.stream.current.value)
             next(self.stream)
         node = nodes.Const(''.join(buf), lineno=lineno)
     elif token.type in ('integer', 'float'):
         next(self.stream)
         node = nodes.Const(token.value, lineno=token.lineno)
     elif token.type == 'lparen':
         next(self.stream)
         node = self.parse_tuple(explicit_parentheses=True)
         self.stream.expect('rparen')
     elif token.type == 'lbracket':
         node = self.parse_list()
     elif token.type == 'lbrace':
         node = self.parse_dict()
     else:
         self.fail("unexpected '%s'" % describe_token(token), token.lineno)
     return node
Пример #3
0
    def parse_tuple(self,
                    simplified=False,
                    with_condexpr=True,
                    extra_end_rules=None,
                    explicit_parentheses=False):
        """Works like `parse_expression` but if multiple expressions are
        delimited by a comma a :class:`~ambari_jinja2.nodes.Tuple` node is created.
        This method could also return a regular expression instead of a tuple
        if no commas where found.

        The default parsing mode is a full tuple.  If `simplified` is `True`
        only names and literals are parsed.  The `no_condexpr` parameter is
        forwarded to :meth:`parse_expression`.

        Because tuples do not require delimiters and may end in a bogus comma
        an extra hint is needed that marks the end of a tuple.  For example
        for loops support tuples between `for` and `in`.  In that case the
        `extra_end_rules` is set to ``['name:in']``.

        `explicit_parentheses` is true if the parsing was triggered by an
        expression in parentheses.  This is used to figure out if an empty
        tuple is a valid expression or not.
        """
        lineno = self.stream.current.lineno
        if simplified:
            parse = self.parse_primary
        elif with_condexpr:
            parse = self.parse_expression
        else:
            parse = lambda: self.parse_expression(with_condexpr=False)
        args = []
        is_tuple = False
        while 1:
            if args:
                self.stream.expect('comma')
            if self.is_tuple_end(extra_end_rules):
                break
            args.append(parse())
            if self.stream.current.type == 'comma':
                is_tuple = True
            else:
                break
            lineno = self.stream.current.lineno

        if not is_tuple:
            if args:
                return args[0]

            # if we don't have explicit parentheses, an empty tuple is
            # not a valid expression.  This would mean nothing (literally
            # nothing) in the spot of an expression would be an empty
            # tuple.
            if not explicit_parentheses:
                self.fail('Expected an expression, got \'%s\'' %
                          describe_token(self.stream.current))

        return nodes.Tuple(args, 'load', lineno=lineno)
Пример #4
0
    def parse_tuple(self, simplified=False, with_condexpr=True,
                    extra_end_rules=None, explicit_parentheses=False):
        """Works like `parse_expression` but if multiple expressions are
        delimited by a comma a :class:`~ambari_jinja2.nodes.Tuple` node is created.
        This method could also return a regular expression instead of a tuple
        if no commas where found.

        The default parsing mode is a full tuple.  If `simplified` is `True`
        only names and literals are parsed.  The `no_condexpr` parameter is
        forwarded to :meth:`parse_expression`.

        Because tuples do not require delimiters and may end in a bogus comma
        an extra hint is needed that marks the end of a tuple.  For example
        for loops support tuples between `for` and `in`.  In that case the
        `extra_end_rules` is set to ``['name:in']``.

        `explicit_parentheses` is true if the parsing was triggered by an
        expression in parentheses.  This is used to figure out if an empty
        tuple is a valid expression or not.
        """
        lineno = self.stream.current.lineno
        if simplified:
            parse = self.parse_primary
        elif with_condexpr:
            parse = self.parse_expression
        else:
            parse = lambda: self.parse_expression(with_condexpr=False)
        args = []
        is_tuple = False
        while 1:
            if args:
                self.stream.expect('comma')
            if self.is_tuple_end(extra_end_rules):
                break
            args.append(parse())
            if self.stream.current.type == 'comma':
                is_tuple = True
            else:
                break
            lineno = self.stream.current.lineno

        if not is_tuple:
            if args:
                return args[0]

            # if we don't have explicit parentheses, an empty tuple is
            # not a valid expression.  This would mean nothing (literally
            # nothing) in the spot of an expression would be an empty
            # tuple.
            if not explicit_parentheses:
                self.fail('Expected an expression, got \'%s\'' %
                          describe_token(self.stream.current))

        return nodes.Tuple(args, 'load', lineno=lineno)