diff --git a/cpp/ast.py b/cpp/ast.py index 8d61ecd..cc51c2b 100644 --- a/cpp/ast.py +++ b/cpp/ast.py @@ -984,12 +984,11 @@ def _get_method(self, return_type_and_name, modifiers, templated_types, assert token.name == '(', token name = return_type_and_name.pop() - if ( - len(return_type_and_name) > 2 and - return_type_and_name[-1].name == 'operator' - ): + if (len(return_type_and_name) > 2 and + (return_type_and_name[-1].name == 'operator' or + return_type_and_name[-1].name == '~')): op = return_type_and_name.pop() - name = tokenize.Token(tokenize.NAME, 'operator' + name.name, + name = tokenize.Token(tokenize.NAME, op.name + name.name, op.start, name.end) # Handle templatized ctors. elif name.name == '>': @@ -1125,7 +1124,7 @@ def _get_method(self, return_type_and_name, modifiers, templated_types, raise ParseError((token, return_type_and_name, parameters)) # Looks like we got a method, not a function. - if len(return_type) > 2 and return_type[-1].name == '::': + if len(return_type) > 1 and return_type[-1].name == '::': return_type, in_class = \ self._get_return_type_and_class_name(return_type) return Method(indices.start, indices.end, name.name, in_class, diff --git a/test_ast.py b/test_ast.py index ae1bd31..3707f8a 100755 --- a/test_ast.py +++ b/test_ast.py @@ -682,6 +682,18 @@ def test_class_virtual_inheritance_reverse(self): self.assertEqual(1, len(nodes)) self.assertEqual(Class('Foo', bases=[Type('Bar')], body=[]), nodes[0]) + def test_constructor(self): + code = 'Foo::Foo() {}' + nodes = list(MakeBuilder(code).generate()) + self.assertEqual(1, len(nodes)) + self.assertEqual(Method('Foo', list(get_tokens('Foo')), [], [], body=[]), nodes[0]) + + def test_destructor(self): + code = 'Foo::~Foo() {}' + nodes = list(MakeBuilder(code).generate()) + self.assertEqual(1, len(nodes)) + self.assertEqual(Method('~Foo', list(get_tokens('Foo')), [], [], body=[]), nodes[0]) + def test_class_virtual_inline_destructor(self): code = 'class Foo { virtual inline ~Foo(); };' nodes = list(MakeBuilder(code).generate())