diff options
author | ptmcg <ptmcg@austin.rr.com> | 2022-04-09 18:37:28 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2022-04-09 18:37:28 -0500 |
commit | 4e627f2948df8ad0eb0c3e90378e9a5c6660db3d (patch) | |
tree | 238b50a0ac7c88bd5ce0f2090f5f61c1fc446d77 /pyparsing | |
parent | 6afabf9889b8c5282126bf057296cef7696954c4 (diff) | |
download | pyparsing-git-pyparsing_3.0.8.tar.gz |
Added show_groups arg to create_diagram; prep for releasepyparsing_3.0.8
Diffstat (limited to 'pyparsing')
-rw-r--r-- | pyparsing/__init__.py | 2 | ||||
-rw-r--r-- | pyparsing/core.py | 4 | ||||
-rw-r--r-- | pyparsing/diagram/__init__.py | 16 |
3 files changed, 19 insertions, 3 deletions
diff --git a/pyparsing/__init__.py b/pyparsing/__init__.py index 82eefa0..45f334d 100644 --- a/pyparsing/__init__.py +++ b/pyparsing/__init__.py @@ -129,7 +129,7 @@ class version_info(NamedTuple): __version_info__ = version_info(3, 0, 8, "final", 0) -__version_time__ = "29 Mar 2022 16:15 UTC" +__version_time__ = "09 Apr 2022 23:29 UTC" __version__ = __version_info__.__version__ __versionTime__ = __version_time__ __author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>" diff --git a/pyparsing/core.py b/pyparsing/core.py index bac4112..454bd57 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -2149,6 +2149,7 @@ class ParserElement(ABC): output_html: Union[TextIO, Path, str], vertical: int = 3, show_results_names: bool = False, + show_groups: bool = False, **kwargs, ) -> None: """ @@ -2161,7 +2162,7 @@ class ParserElement(ABC): instead of horizontally (default=3) - show_results_names - bool flag whether diagram should show annotations for defined results names - + - show_groups - bool flag whether groups should be highlighted with an unlabeled surrounding box Additional diagram-formatting keyword arguments can also be included; see railroad.Diagram class. """ @@ -2179,6 +2180,7 @@ class ParserElement(ABC): self, vertical=vertical, show_results_names=show_results_names, + show_groups=show_groups, diagram_kwargs=kwargs, ) if isinstance(output_html, (str, Path)): diff --git a/pyparsing/diagram/__init__.py b/pyparsing/diagram/__init__.py index 849fa10..2d0c587 100644 --- a/pyparsing/diagram/__init__.py +++ b/pyparsing/diagram/__init__.py @@ -16,6 +16,7 @@ from jinja2 import Template from io import StringIO import inspect + with open(resource_filename(__name__, "template.jinja2"), encoding="utf-8") as fp: template = Template(fp.read()) @@ -137,6 +138,7 @@ def to_railroad( diagram_kwargs: Optional[dict] = None, vertical: int = 3, show_results_names: bool = False, + show_groups: bool = False, ) -> List[NamedDiagram]: """ Convert a pyparsing element tree into a list of diagrams. This is the recommended entrypoint to diagram @@ -147,6 +149,8 @@ def to_railroad( shown vertically instead of horizontally :param show_results_names - bool to indicate whether results name annotations should be included in the diagram + :param show_groups - bool to indicate whether groups should be highlighted with an unlabeled + surrounding box """ # Convert the whole tree underneath the root lookup = ConverterState(diagram_kwargs=diagram_kwargs or {}) @@ -156,6 +160,7 @@ def to_railroad( parent=None, vertical=vertical, show_results_names=show_results_names, + show_groups=show_groups, ) root_id = id(element) @@ -362,6 +367,7 @@ def _apply_diagram_item_enhancements(fn): index: int = 0, name_hint: str = None, show_results_names: bool = False, + show_groups: bool = False, ) -> Optional[EditablePartial]: ret = fn( @@ -372,6 +378,7 @@ def _apply_diagram_item_enhancements(fn): index, name_hint, show_results_names, + show_groups, ) # apply annotation for results name, if present @@ -411,6 +418,7 @@ def _to_diagram_element( index: int = 0, name_hint: str = None, show_results_names: bool = False, + show_groups: bool = False, ) -> Optional[EditablePartial]: """ Recursively converts a PyParsing Element to a railroad Element @@ -423,6 +431,7 @@ def _to_diagram_element( :param name_hint: If provided, this will override the generated name :param show_results_names: bool flag indicating whether to add annotations for results names :returns: The converted version of the input element, but as a Partial that hasn't yet been constructed + :param show_groups: bool flag indicating whether to show groups using bounding box """ exprs = element.recurse() name = name_hint or element.customName or element.__class__.__name__ @@ -457,6 +466,7 @@ def _to_diagram_element( index=index, name_hint=propagated_name, show_results_names=show_results_names, + show_groups=show_groups, ) # If the element isn't worth extracting, we always treat it as the first time we say it @@ -511,7 +521,10 @@ def _to_diagram_element( elif isinstance(element, pyparsing.PrecededBy): ret = EditablePartial.from_call(AnnotatedItem, label="LOOKBEHIND", item="") elif isinstance(element, pyparsing.Group): - ret = EditablePartial.from_call(AnnotatedItem, label="", item="") + if show_groups: + ret = EditablePartial.from_call(AnnotatedItem, label="", item="") + else: + ret = EditablePartial.from_call(railroad.Group, label="", item="") elif isinstance(element, pyparsing.TokenConverter): ret = EditablePartial.from_call(AnnotatedItem, label=type(element).__name__.lower(), item="") elif isinstance(element, pyparsing.Opt): @@ -562,6 +575,7 @@ def _to_diagram_element( vertical=vertical, index=i, show_results_names=show_results_names, + show_groups=show_groups, ) # Some elements don't need to be shown in the diagram |