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
66 changes: 60 additions & 6 deletions c_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,102 @@

from .models import (
CArray,
CAtomic,
CBool,
CChar,
CComposedType,
CConst,
CDouble,
CDoubleComplex,
CDiagnostic,
CEnum,
CEnumerator,
CField,
CFile,
CFloat,
CFloatComplex,
CFunction,
CGlobal,
CFunctionType,
CInclude,
CInitializer,
CInt,
CLong,
CLongDouble,
CLongDoubleComplex,
CLongLong,
CMacro,
CParameter,
CParseError,
CPointer,
CProject,
CQualifier,
CRestrict,
CShort,
CSignedChar,
CSourceLocation,
CStruct,
CTypeRef,
CType,
CTypedef,
CUnknownType,
CUnion,
CUnsignedChar,
CUnsignedInt,
CUnsignedLong,
CUnsignedLongLong,
CUnsignedShort,
CVariable,
CVoid,
CVolatile,
)
from .parser import CParser, parse_c_file, parse_c_project

__all__ = (
"CArray",
"CAtomic",
"CBool",
"CChar",
"CComposedType",
"CConst",
"CDouble",
"CDoubleComplex",
"CDiagnostic",
"CEnum",
"CEnumerator",
"CField",
"CFile",
"CFloat",
"CFloatComplex",
"CFunction",
"CGlobal",
"CFunctionType",
"CInclude",
"CInitializer",
"CInt",
"CLong",
"CLongDouble",
"CLongDoubleComplex",
"CLongLong",
"CMacro",
"CParameter",
"CParseError",
"CPointer",
"CParser",
"CProject",
"CQualifier",
"CRestrict",
"CShort",
"CSignedChar",
"CSourceLocation",
"CStruct",
"CTypeRef",
"CType",
"CTypedef",
"CUnion",
"CUnknownType",
"CUnsignedChar",
"CUnsignedInt",
"CUnsignedLong",
"CUnsignedLongLong",
"CUnsignedShort",
"CVariable",
"CVoid",
"CVolatile",
"parse_c_file",
"parse_c_project",
)
2 changes: 1 addition & 1 deletion c_parser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def format_c_report(report: dict[str, dict]) -> str:
lines.append(f" Unions: {len(parsed.get('unions') or [])}")
lines.append(f" Enums: {len(parsed.get('enums') or [])}")
lines.append(f" Typedefs: {len(parsed.get('typedefs') or [])}")
lines.append(f" Globals: {len(parsed.get('globals') or [])}")
lines.append(f" Variables: {len(parsed.get('variables') or [])}")
lines.append(f" Macros: {len(parsed.get('macros') or [])}")
lines.append(f" Includes: {len(parsed.get('includes') or [])}")
lines.append(f" Diagnostics: {len(parsed.get('diagnostics') or [])}")
Expand Down
19 changes: 17 additions & 2 deletions c_parser/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ def top_level_partition(text: str, delimiter: str = "=") -> tuple[str, str | Non
return text.strip(), None


def _is_aggregate_definition_header(header: str) -> bool:
"""Identify a tag definition before deciding that a brace starts a body."""
compact = " ".join(header.split())
if "(" in compact or "=" in compact:
return False
words = compact.split()
return any(word in {"struct", "union", "enum"} for word in words)


def split_top_level_c_source(
source: str,
filename: str | None = None,
Expand Down Expand Up @@ -198,6 +207,7 @@ def split_top_level_c_source(
block_start_line = 1
block_start_column = 1
block_source_line: str | None = None
aggregate_block = False

while i < len(stripped):
char = stripped[i]
Expand Down Expand Up @@ -251,13 +261,15 @@ def split_top_level_c_source(
block_start_line = start_line
block_start_column = start_column
block_source_line = _source_line(source_lines, start_line)
aggregate_block = _is_aggregate_definition_header(header)
brace_depth = 1
start_index = None
if not aggregate_block:
start_index = None
elif char == "{" and brace_depth:
brace_depth += 1
elif char == "}" and brace_depth:
brace_depth -= 1
if brace_depth == 0 and block_header:
if brace_depth == 0 and block_header and not aggregate_block:
segments.append(
CTopLevelSegment(
text=block_header,
Expand Down Expand Up @@ -296,6 +308,9 @@ def split_top_level_c_source(
)
)
start_index = None
block_header = None
block_source_line = None
aggregate_block = False

line, column = _advance_position(char, line, column)
i += 1
Expand Down
Loading
Loading