Skip to content

Commit

Permalink
Improve parsing of function reference
Browse files Browse the repository at this point in the history
  • Loading branch information
r-e-d committed Nov 8, 2015
1 parent 67f7742 commit 794bf56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cpp/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,10 @@ def _get_method(self, return_type_and_name, modifiers, templated_types,
# Handle pointer to functions that are really data but look
# like method declarations.
if token.name == '(':
if parameters[0].name == '*':
if parameters[0].name in '*&':
# name contains the return type.
return_type.append(name)
name = parameters.pop()
# parameters contains the name of the data.
modifiers = [p.name for p in parameters]
# Already at the ( to open the parameter list.
function_parameters = list(self._get_matching_char('(', ')'))
del function_parameters[-1] # Remove trailing ')'.
Expand All @@ -1075,8 +1074,9 @@ def _get_method(self, return_type_and_name, modifiers, templated_types,
assert_parse(token.token_type == tokenize.SYNTAX, token)
assert_parse(token.name == ';', token)

types = [t.name for t in return_type]
return self._create_variable(indices, name.name, indices.name,
modifiers, '')
[], types)
# At this point, we got something like:
# return_type (type::*name_)(params);
# This is a data member called name_ that is a function pointer.
Expand Down
6 changes: 6 additions & 0 deletions test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ def test_variable_initialization_with_complex_expression(self):
initial_value='fct()+42'),
nodes[0])

def test_variable_function_reference(self):
nodes = list(MakeBuilder('int* (&fn)();').generate())
self.assertEqual(1, len(nodes), repr(nodes))
self.assertEqual(VariableDeclaration('fn', Type('int', pointer=True)),
nodes[0])

def test_variable_anonymous_class(self):
nodes = list(MakeBuilder('class {public:} a;').generate())
self.assertEqual(1, len(nodes), repr(nodes))
Expand Down

0 comments on commit 794bf56

Please sign in to comment.