Skip to content

Commit

Permalink
FIX: statementlist can be parsed standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
klauer committed Jul 18, 2023
1 parent ac6b04a commit f9f40ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
42 changes: 38 additions & 4 deletions blark/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pathlib
import sys
from dataclasses import dataclass
from typing import Generator, Optional, Sequence, Union
from typing import Generator, Optional, Sequence, Type, TypeVar, Union

import lark

Expand Down Expand Up @@ -375,7 +375,39 @@ def summarize(parsed: list[ParseResult]) -> summary.CodeSummary:
return summary.CodeSummary.from_parse_results(parsed)


def dump_json(type_, obj, include_meta: bool = True, indent: Optional[int] = 2) -> str:
T = TypeVar("T")


def dump_json(
type_: Type[T],
obj: T,
include_meta: bool = True,
indent: Optional[int] = 2,
) -> str:
"""
Dump object ``obj`` as type ``type_`` with apischema and serialize to a string.
Parameters
----------
type_ : Type[T]
The type of ``obj``.
obj : T
The object to serialize.
include_meta : bool
Include ``meta`` information in the dump.
indent : int or None
Make the JSON output prettier with indentation.
Returns
-------
str
"""
if apischema is None:
raise RuntimeError(
"Optional dependency apischema is required to output a JSON "
"representation of source code."
)

serialized = apischema.serialize(
type_,
obj,
Expand All @@ -384,7 +416,7 @@ def dump_json(type_, obj, include_meta: bool = True, indent: Optional[int] = 2)
)
if not include_meta:
serialized = util.recursively_remove_keys(serialized, {"meta"})
return json.dumps(serialized, indent=2)
return json.dumps(serialized, indent=indent)


def main(
Expand Down Expand Up @@ -477,8 +509,10 @@ def get_items():
if use_json:
assert apischema is not None

# We allow for StatementList to be parsed directly here, though
# it's not acceptable as a top-level item in the grammar
serialized = dump_json(
tf.SourceCode,
tf.ExtendedSourceCode,
res.transform(),
include_meta=include_meta,
)
Expand Down
10 changes: 10 additions & 0 deletions blark/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,6 +3089,16 @@ def __str__(self):
return "\n".join(str(item) for item in self.items)


@dataclass
class ExtendedSourceCode(SourceCode):
"""
Top-level source code item - extended to include the possibility of
standalone implementation details (i.e., statement lists).
"""

items: List[Union[SourceCodeItem, StatementList]]


def _annotator_wrapper(handler):
def wrapped(self: GrammarTransformer, data: Any, children: list, meta: lark.tree.Meta) -> Any:
result = handler(*children)
Expand Down

0 comments on commit f9f40ca

Please sign in to comment.