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)
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) )
def testBadParse(self): tokens = lex('var 42') self.failUnlessRaises(error.SyntaxError, lambda: VarDecl.parse(tokens) )
def testInitializer(self): tokens = lex('var x = 291') result = VarDecl.parse(tokens) assert isinstance(result, VarDecl), result
def testTypeDefinition(self): tokens = lex('var x as int') VarDecl.parse(tokens)
def testFailParse(self): tokens = lex('print 4') result = VarDecl.parse(tokens) self.failUnless(result is None, result)
def testGoodParse(self): tokens = lex('var foo') result = VarDecl.parse(tokens) self.failUnless(isinstance(result, VarDecl), result)