Skip to content

Commit

Permalink
Allows to use a ParsedStatement class or iterable of it instead of a …
Browse files Browse the repository at this point in the history
…block in parser
  • Loading branch information
hgrecco committed May 26, 2022
1 parent e9daaa1 commit d5f9afb
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion flexparser/flexparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,14 +999,46 @@ def parse(
) -> ParsedProject:
"""Parse sources into a ParsedProject."""

if isinstance(root_block_class, RootBlock):
CustomRootBlock = root_block_class

elif isinstance(root_block_class, (tuple, list)):

for el in root_block_class:
if not issubclass(el, (Block, ParsedStatement)):
raise TypeError(
"Elements in root_block_class must be of type Block or ParsedStatement, "
f"not {el}"
)

@dataclass(frozen=True)
class CustomRootBlock(RootBlock):
pass

CustomRootBlock.__annotations__["body"] = Multi[ty.Union[root_block_class]]

elif issubclass(root_block_class, (Block, ParsedStatement)):

@dataclass(frozen=True)
class CustomRootBlock(RootBlock):
pass

CustomRootBlock.__annotations__["body"] = Multi[root_block_class]

else:
raise TypeError(
"root_block_class must be of type RootBlock or tuple of type Block or ParsedStatement, "
f"not {type(root_block_class)}"
)

class CustomParser(Parser):

_sequence_iterator_class = SequenceIterator.subclass_with(
statement_iterator_class=StatementIterator.subclass_with(
strip_spaces=strip_spaces, delimiters=delimiters
)
)
_root_block_class = root_block_class
_root_block_class = CustomRootBlock

parser = CustomParser(config)

Expand Down

0 comments on commit d5f9afb

Please sign in to comment.