Ejemplo n.º 1
0
    def parse(tokens):
        from ast.parameter import Parameter

        oldPos = tokens.getPosition()

        position = tokens.peek().position

        if tokens.peek() != 'def':
            tokens.setPosition(oldPos)
            return None

        tokens.getNext()

        name = tokens.getNext()
        if name.type != 'identifier':
            raise error.SyntaxError, 'Expected function name, got %r' % name

        tokens.expect('(')

        # parameters
        params = []
        while True:
            param = Parameter.parse(tokens)
            if param is not None:
                params.append(param)

            peek = tokens.peek()
            if peek == ')':
                break

            else:
                tokens.expect(',')

        tokens.expect(')')

        if tokens.peek() == 'as':
            tokens.expect('as')
            returnType = vartypes.Type.parse(tokens)
            if returnType is None:
                raise error.SyntaxError(position, 'Expected return type, got %r' % tokens.peek())
        else:
            returnType = vartypes.VoidType

        if tokens.peek() == ':':
            raise error.SyntaxError(position, 'Interface function cannot have definition.')

        tokens.expect(token.END_OF_STATEMENT)
        body = None

        return FunctionPrototype(position, name.value, params, returnType)
Ejemplo n.º 2
0
    def parse(tokens):
        oldPos = tokens.getPosition()

        position = tokens.peek().position

        if tokens.peek() != 'delegate':
            tokens.setPosition(oldPos)
            return None

        tokens.getNext()

        name = tokens.getNext()
        if name.type != 'identifier':
            raise error.SyntaxError, 'Expected function name, got %r' % name

        tokens.expect('(')

        # parameters
        params = []
        while True:
            param = Parameter.parse(tokens)
            if param is not None:
                params.append(param)

            peek = tokens.peek()
            if peek == ')':
                break

            else:
                tokens.expect(',')

        tokens.expect(')')

        if tokens.peek() == 'as':
            tokens.expect('as')
            returnType = vartypes.Type.parse(tokens)
            if returnType is None:
                raise error.SyntaxError(position, 'Expected return type, got %r' % tokens.peek())
        else:
            returnType = vartypes.VoidType

        tokens.expect(token.END_OF_STATEMENT)

        return DelegateDecl(position, name.value, params, returnType)
Ejemplo n.º 3
0
    def parse(tokens):
        from ast.parameter import Parameter

        oldPos = tokens.getPosition()
        flags = memberflags.MemberFlags()

        position = tokens.peek().position

        while True:
            tok = tokens.getNext()
            if tok == 'static':
                flags.static = True
            elif tok == 'virtual':
                flags.virtual = True
            elif tok == 'abstract':
                flags.abstract = True
            elif tok == 'override':
                flags.override = True
            elif tok == 'sealed':
                flags.sealed = True
            else:
                tokens.unget()
                break

        if tokens.peek() != 'def':
            tokens.setPosition(oldPos)
            return None

        tokens.getNext()

        name = tokens.getNext()
        if name.type != 'identifier':
            raise error.SyntaxError, 'Expected function name, got %r' % name

        tokens.expect('(')

        # parameters
        params = []
        while True:
            param = Parameter.parse(tokens)
            if param is not None:
                params.append(param)

            peek = tokens.peek()
            if peek == ',':
                tokens.expect(',')
                continue

            elif peek == ')':
                break

            else:
                raise error.SyntaxError, "Expected ',' or ')', got %r" % peek

        tokens.expect(')')

        if tokens.peek() == 'as':
            tokens.expect('as')
            returnType = vartypes.Type.parse(tokens)
            if returnType is None:
                raise error.SyntaxError(position, 'Expected return type, got %r' % tokens.peek())
        else:
            returnType = vartypes.VoidType

        if flags.abstract:
            if tokens.peek() == ':':
                raise error.SyntaxError(position, 'Abstract function cannot have definition.')
            tokens.expect(token.END_OF_STATEMENT)
            body = None

        else:
            tokens.expect(':')
            tokens.expect(token.END_OF_STATEMENT)

            body = BlockStatement.parse(tokens)

            if body is None:
                raise error.SyntaxError, 'Expected indented block and function body, got %r' % tokens.peek()

        return FunctionDecl(name.value, name.position, returnType, params, body, flags)