예제 #1
0
    def parse(tokens):
        if tokens.peek() is not token.BEGIN_BLOCK:
            return None

        attributes = []
        decls = []

        tokens.expect(token.BEGIN_BLOCK)
        while tokens.peek() is not token.END_BLOCK:
            decl = None

            attr = Attribute.parse(tokens)
            if attr is not None:
                attributes.append(attr)
            else:
                decl = (
                    FunctionDecl.parse(tokens) or
                    VarDecl.parse(tokens) or
                    PassStatement.parse(tokens)
                )

                if decl is None:
                    raise error.SyntaxError(tokens.peek().position, 'Expected end block, variable or class declaration, or pass statement.  Got %r' % tokens.peek())

                if not isinstance(decl, PassStatement):
                    decls.append(decl)

            while tokens.peek() is token.END_OF_STATEMENT:
                tokens.getNext()

        tokens.expect(token.END_BLOCK)

        return ClassBody(decls, attributes)
예제 #2
0
    def testSemantic(self):
        tokens = lex('var x as int')
        scope = Scope(parent=None)

        decl = VarDecl.parse(tokens)
        result = decl.semantic(scope)
        self.failUnless(isinstance(result, VarDecl), result)
        self.failUnless('x' in scope)

        # x is already defined in the scope, this should fail, as it is a duplicate
        self.failUnlessRaises(Exception,
            lambda: tokens.semantic(scope)
        )
예제 #3
0
    def testBadParse(self):
        tokens = lex('var 42')

        self.failUnlessRaises(error.SyntaxError,
            lambda: VarDecl.parse(tokens)
        )
예제 #4
0
    def testInitializer(self):
        tokens = lex('var x = 291')

        result = VarDecl.parse(tokens)
        assert isinstance(result, VarDecl), result
예제 #5
0
    def testTypeDefinition(self):
        tokens = lex('var x as int')

        VarDecl.parse(tokens)
예제 #6
0
    def testFailParse(self):
        tokens = lex('print 4')

        result = VarDecl.parse(tokens)
        self.failUnless(result is None, result)
예제 #7
0
    def testGoodParse(self):
        tokens = lex('var foo')

        result = VarDecl.parse(tokens)
        self.failUnless(isinstance(result, VarDecl), result)