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 testParseAttributeNoParameters(self): source = '[Thingie]' tok = lexer.lex(source) result = Attribute.parse(tok) self.assertTrue(isinstance(result, Attribute)) self.assertTrue(isinstance(result.className, Identifier)) self.assertEqual(result.className.name, 'Thingie') self.assertEqual(result.params, [])
def testRenameIdentifier(self): from nine.lexer import lex from ast.qualifiedname import QualifiedName name = QualifiedName.parse(lex('Foo.Bar.Baz')) newName = Attribute.renameIdentifier(name) self.assertEqual(newName.lhs, name.lhs) self.assertEqual(newName.lhs.rhs.name, name.lhs.rhs.name) self.assertEqual(newName.rhs.name, 'BazAttribute')