Skip to content
Draft
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
72 changes: 57 additions & 15 deletions cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,15 @@ def _create_value(self, toks: LexTokenList) -> Value:
# Parsing begins here
#

def parse(self) -> None:
"""
Parse the header contents
def _parse_token(self, tok: LexToken, doxygen: typing.Optional[str]) -> bool:
"""Parse a single token from the current parser state.

Returns True if the caller should keep the current doxygen comment for
the next token.
"""

# non-ambiguous parsing functions for each token type
_translation_unit_tokens: typing.Dict[
translation_unit_tokens: typing.Dict[
str, typing.Callable[[LexToken, typing.Optional[str]], typing.Any]
] = {
"__attribute__": self._consume_gcc_attribute,
Expand All @@ -359,7 +361,21 @@ def parse(self) -> None:
";": lambda _1, _2: None,
}

_keep_doxygen = {"__declspec", "alignas", "__attribute__", "DBL_LBRACKET"}
keep_doxygen = {"__declspec", "alignas", "__attribute__", "DBL_LBRACKET"}

fn = translation_unit_tokens.get(tok.type)
if fn:
fn(tok, doxygen)
return tok.type in keep_doxygen

# this processes ambiguous declarations
self._parse_declarations(tok, doxygen)
return False

def parse(self) -> None:
"""
Parse the header contents
"""

tok = None

Expand All @@ -377,15 +393,7 @@ def parse(self) -> None:
if not tok:
break

fn = _translation_unit_tokens.get(tok.type)
if fn:
fn(tok, doxygen)

if tok.type not in _keep_doxygen:
doxygen = None
else:
# this processes ambiguous declarations
self._parse_declarations(tok, doxygen)
if not self._parse_token(tok, doxygen):
doxygen = None

except Exception as e:
Expand Down Expand Up @@ -1188,12 +1196,46 @@ def _parse_using_typealias(

mods.validate(var_ok=False, meth_ok=False, msg="parsing typealias")

dtype = self._parse_cv_ptr(parsed_type)
if parsed_type.typename.classkey in ("class", "struct", "union"):
tok = self.lex.token_if_in_set(self._class_enum_stage2)
if tok:
self._parse_class_decl(
parsed_type.typename,
tok,
doxygen,
template,
False,
tok.location,
mods,
[],
)
self._parse_typealias_class_body()

dtype = self._parse_cv_ptr_or_fn(parsed_type, nonptr_fn=True)

tok = self.lex.token_if("[")
while tok:
if isinstance(dtype, FunctionType):
raise CxxParseError("arrays of functions are illegal", tok)
dtype = self._parse_array_type(tok, dtype)
tok = self.lex.token_if("[")

alias = UsingAlias(id_tok.value, dtype, template, self._current_access, doxygen)

self.visitor.on_using_alias(self.state, alias)

def _parse_typealias_class_body(self) -> None:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, this seems wrong.

class_state = self.state

while self.state is not class_state.parent:
doxygen = self.lex.get_doxygen()
tok = self.lex.token()

if tok.type == "}" and self.state is class_state:
self._pop_state()
else:
self._parse_token(tok, doxygen)

def _parse_using(
self,
tok: LexToken,
Expand Down
2 changes: 1 addition & 1 deletion cxxheaderparser/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ class UsingAlias:
"""

alias: str
type: DecoratedType
type: typing.Union[DecoratedType, FunctionType]

template: typing.Optional[TemplateDecl] = None

Expand Down
Loading
Loading