Skip to content

Commit

Permalink
Merge pull request #82 from robotpy/trailing-return-type-body
Browse files Browse the repository at this point in the history
Consume function body if present after trailing return type
  • Loading branch information
virtuald committed Nov 14, 2023
2 parents 9883a4e + 42bc6b6 commit 0e732f1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,11 +1914,12 @@ def _parse_fn_end(self, fn: Function) -> None:
fn_template = fn_template[0]
fn_template.raw_requires_post = self._parse_requires(rtok)

if self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)

if self.lex.token_if("{"):
self._discard_contents("{", "}")
fn.has_body = True
elif self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)

def _parse_method_end(self, method: Method) -> None:
"""
Expand Down Expand Up @@ -1963,6 +1964,9 @@ def _parse_method_end(self, method: Method) -> None:
method.ref_qualifier = tok_value
elif tok_value == "->":
self._parse_trailing_return_type(method)
if self.lex.token_if("{"):
self._discard_contents("{", "}")
method.has_body = True
break
elif tok_value == "throw":
tok = self._next_token_must_be("(")
Expand Down
64 changes: 64 additions & 0 deletions tests/test_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,67 @@ class C {
]
)
)


def test_fn_trailing_return_with_body() -> None:
content = """
auto test() -> void
{
}
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
functions=[
Function(
return_type=Type(
typename=PQName(segments=[FundamentalSpecifier(name="void")])
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
)
]
)
)


def test_method_trailing_return_with_body() -> None:
content = """
struct X {
auto test() -> void
{
}
};
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="X")], classkey="struct"
)
),
methods=[
Method(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
access="public",
)
],
)
]
)
)

0 comments on commit 0e732f1

Please sign in to comment.