summaryrefslogtreecommitdiff
path: root/pyparsing/diagram
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2022-04-09 18:37:28 -0500
committerptmcg <ptmcg@austin.rr.com>2022-04-09 18:37:28 -0500
commit4e627f2948df8ad0eb0c3e90378e9a5c6660db3d (patch)
tree238b50a0ac7c88bd5ce0f2090f5f61c1fc446d77 /pyparsing/diagram
parent6afabf9889b8c5282126bf057296cef7696954c4 (diff)
downloadpyparsing-git-pyparsing_3.0.8.tar.gz
Added show_groups arg to create_diagram; prep for releasepyparsing_3.0.8
Diffstat (limited to 'pyparsing/diagram')
-rw-r--r--pyparsing/diagram/__init__.py16
1 files changed, 15 insertions, 1 deletions
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