Skip to content

Commit

Permalink
Improve to_type
Browse files Browse the repository at this point in the history
  • Loading branch information
r-e-d committed Aug 2, 2015
1 parent 77a43a6 commit cbad221
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
55 changes: 25 additions & 30 deletions cpp/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,12 @@ def to_type(self, tokens):
"""
result = []
name_tokens = []
reference = pointer = False
reference = pointer = array = False
inside_array = False
empty_array = True
templated_tokens = []

def add_type(templated_types):
def add_type():
if not name_tokens:
return

Expand All @@ -435,51 +438,43 @@ def add_type(templated_types):
names.append(t.name)
name = ''.join(names)

templated_types = self.to_type(templated_tokens)
result.append(Type(name_tokens[0].start, name_tokens[-1].end,
name, templated_types, modifiers,
reference, pointer, False))
reference, pointer, array))
del name_tokens[:]
del templated_tokens[:]

i = 0
end = len(tokens)
while i < end:
token = tokens[i]
if token.name == '<':
new_tokens, new_end = self._get_template_end(tokens, i + 1)
if new_end < end:
if tokens[new_end].name == '::':
name_tokens.append(tokens[new_end])
name_tokens.append(tokens[new_end + 1])
new_end += 2
elif tokens[new_end].name == '*':
pointer = True
new_end += 1
elif tokens[new_end].name == '&':
reference = True
new_end += 1
add_type(self.to_type(new_tokens))
# If there is a comma after the template, we need to consume
# that here otherwise it becomes part of the name.
i = new_end
reference = pointer = False
if token.name == ']':
inside_array = False
if empty_array:
pointer = True
else:
array = True
elif inside_array:
empty_array = False
elif token.name == '<':
templated_tokens, i = self._get_template_end(tokens, i + 1)
continue
elif token.name == ',' or token.name == '(':
add_type([])
reference = pointer = False
add_type()
reference = pointer = array = False
empty_array = True
elif token.name == '*':
pointer = True
elif token.name == '&':
reference = True
elif token.name == '[':
pointer = True
elif token.name == ']' or token.name == ')':
pass
else:
inside_array = True
elif token.name != ')':
name_tokens.append(token)
i += 1

if name_tokens:
# No '<' in the tokens, just a simple name and no template.
add_type([])
add_type()
return result

def declaration_to_parts(self, parts, needs_name_removed):
Expand Down
12 changes: 12 additions & 0 deletions test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ def test_template_with_function_arg_and_nested_template(self):
Type('int')]
self.assertEqual(Type('function', templated_types=types), result[0])

def test_array(self):
tokens = get_tokens('Foo[]')
result = self.converter.to_type(list(tokens))
self.assertEqual(1, len(result))
self.assertEqual(Type('Foo', pointer=True), result[0])

def test_array_with_size(self):
tokens = get_tokens('Foo[42]')
result = self.converter.to_type(list(tokens))
self.assertEqual(1, len(result))
self.assertEqual(Type('Foo', array=True), result[0])


class TypeConverterCreateReturnTypeTest(unittest.TestCase):

Expand Down

0 comments on commit cbad221

Please sign in to comment.