Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions cpp/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,13 @@ def _generate_one(self, token):
if last_token.name == '(':
# If there is an assignment before the paren,
# this is an expression, not a method.
if (
temp_tokens[-1].name == '=' and
temp_tokens[-2].name != 'operator'
):
new_temp = self._get_tokens_up_to(tokenize.SYNTAX, ';')
temp_tokens.append(last_token)
temp_tokens.extend(new_temp)
last_token = tokenize.Token(tokenize.SYNTAX, ';', 0, 0)
for i, elt in reversed(list(enumerate(temp_tokens))):
if elt.name == '=' and temp_tokens[i - 1].name != 'operator':
new_temp = self._get_tokens_up_to(tokenize.SYNTAX, ';')
temp_tokens.append(last_token)
temp_tokens.extend(new_temp)
last_token = tokenize.Token(tokenize.SYNTAX, ';', 0, 0)
break

if last_token.name == '[':
# Handle array, this isn't a method, unless it's an operator.
Expand Down
10 changes: 10 additions & 0 deletions test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,16 @@ class AstBuilderIntegrationTest(unittest.TestCase):

"""

def test_variable_initialization_with_function(self):
nodes = list(MakeBuilder('int value = fct();').generate())
self.assertEqual(1, len(nodes))
self.assertEqual(VariableDeclaration('value', Type('int'), initial_value='fct()'), nodes[0])

def test_variable_initialization_with_complex_expression(self):
nodes = list(MakeBuilder('int value = fct() + 42;').generate())
self.assertEqual(1, len(nodes))
self.assertEqual(VariableDeclaration('value', Type('int'), initial_value='fct()+42'), nodes[0])

def test_function_one_argument_with_name(self):
for argument in ('Foo f', 'const Foo f', 'Foo& f', 'const Foo& f'):
code = 'void fct(%s);' % argument
Expand Down