Esempio n. 1
0
    def _parse_function(self):
        """
        The parser for a text functions. Process the tokens, which follow a
        function definition.

        :return: Return a Scope representation of the tokens.
        :rtype: Function
        """
        first_pos = self.start_pos
        token_type, fname = self.next()
        if token_type != tokenize.NAME:
            return None

        fname = pr.Name(self.module, [(fname, self.start_pos)], self.start_pos,
                        self.end_pos)

        token_type, open = self.next()
        if open != '(':
            return None
        params = self._parse_parentheses()

        token_type, colon = self.next()
        annotation = None
        if colon in ['-', '->']:
            # parse annotations
            if colon == '-':
                # The Python 2 tokenizer doesn't understand this
                token_type, colon = self.next()
                if colon != '>':
                    return None
            annotation, colon = self._parse_statement(added_breaks=[':'])

        if colon != ':':
            return None

        # because of 2 line func param definitions
        scope = pr.Function(self.module, fname, params, first_pos, annotation)
        if self.user_scope and scope != self.user_scope \
                and self.user_position > first_pos:
            self.user_scope = scope
        return scope
Esempio n. 2
0
    def _parse_function(self):
        """
        The parser for a text functions. Process the tokens, which follow a
        function definition.

        :return: Return a Scope representation of the tokens.
        :rtype: Function
        """
        first_pos = self._gen.current.start_pos
        tok = next(self._gen)
        if tok.type != tokenize.NAME:
            return None

        fname = pr.Name(self.module, [(tok.string, tok.start_pos)],
                        tok.start_pos, tok.end_pos)

        tok = next(self._gen)
        if tok.string != '(':
            return None
        params = self._parse_parentheses()

        colon = next(self._gen)
        annotation = None
        if colon.string in ('-', '->'):
            # parse annotations
            if colon.string == '-':
                # The Python 2 tokenizer doesn't understand this
                colon = next(self._gen)
                if colon.string != '>':
                    return None
            annotation, colon = self._parse_statement(added_breaks=[':'])

        if colon.string != ':':
            return None

        # because of 2 line func param definitions
        return pr.Function(self.module, fname, params, first_pos, annotation)