Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._vendor.pyparsing.diagram Namespace Reference

Data Structures

class  AnnotatedItem
 
class  ConverterState
 
class  EachItem
 
class  EditablePartial
 
class  ElementState
 

Functions

str railroad_to_html (List[NamedDiagram] diagrams, embed=False, **kwargs)
 
T resolve_partial ("EditablePartial[T]" partial)
 
List[NamedDiagramto_railroad (pyparsing.ParserElement element, typing.Optional[dict] diagram_kwargs=None, int vertical=3, bool show_results_names=False, bool show_groups=False)
 
bool _should_vertical (int specification, Iterable[pyparsing.ParserElement] exprs)
 
bool _worth_extracting (pyparsing.ParserElement element)
 
 _apply_diagram_item_enhancements (fn)
 
 _visible_exprs (Iterable[pyparsing.ParserElement] exprs)
 
typing.Optional[EditablePartial_to_diagram_element (pyparsing.ParserElement element, typing.Optional[EditablePartial] parent, ConverterState lookup=None, int vertical=None, int index=0, str name_hint=None, bool show_results_names=False, bool show_groups=False)
 

Variables

str jinja2_template_source
 
 template = Template(jinja2_template_source)
 
 NamedDiagram
 
 T = TypeVar("T")
 

Function Documentation

◆ _apply_diagram_item_enhancements()

_apply_diagram_item_enhancements (   fn)
protected
decorator to ensure enhancements to a diagram item (such as results name annotations)
get applied on return from _to_diagram_element (we do this since there are several
returns in _to_diagram_element)

Definition at line 395 of file __init__.py.

395def _apply_diagram_item_enhancements(fn):
396 """
397 decorator to ensure enhancements to a diagram item (such as results name annotations)
398 get applied on return from _to_diagram_element (we do this since there are several
399 returns in _to_diagram_element)
400 """
401
402 def _inner(
403 element: pyparsing.ParserElement,
404 parent: typing.Optional[EditablePartial],
405 lookup: ConverterState = None,
406 vertical: int = None,
407 index: int = 0,
408 name_hint: str = None,
409 show_results_names: bool = False,
410 show_groups: bool = False,
411 ) -> typing.Optional[EditablePartial]:
412 ret = fn(
413 element,
414 parent,
415 lookup,
416 vertical,
417 index,
418 name_hint,
419 show_results_names,
420 show_groups,
421 )
422
423 # apply annotation for results name, if present
424 if show_results_names and ret is not None:
425 element_results_name = element.resultsName
426 if element_results_name:
427 # add "*" to indicate if this is a "list all results" name
428 element_results_name += "" if element.modalResults else "*"
430 railroad.Group, item=ret, label=element_results_name
431 )
432
433 return ret
434
435 return _inner
436
437
for i

References i.

◆ _should_vertical()

bool _should_vertical ( int  specification,
Iterable[pyparsing.ParserElement]   exprs 
)
protected
Returns true if we should return a vertical list of elements

Definition at line 234 of file __init__.py.

236) -> bool:
237 """
238 Returns true if we should return a vertical list of elements
239 """
240 if specification is None:
241 return False
242 else:
243 return len(_visible_exprs(exprs)) >= specification
244
245

References pip._vendor.pyparsing.diagram._visible_exprs(), and i.

Referenced by pip._vendor.pyparsing.diagram._to_diagram_element().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _to_diagram_element()

typing.Optional[EditablePartial] _to_diagram_element ( pyparsing.ParserElement  element,
typing.Optional[EditablePartial parent,
ConverterState   lookup = None,
int   vertical = None,
int   index = 0,
str   name_hint = None,
bool   show_results_names = False,
bool   show_groups = False 
)
protected
Recursively converts a PyParsing Element to a railroad Element
:param lookup: The shared converter state that keeps track of useful things
:param index: The index of this element within the parent
:param parent: The parent of this element in the output tree
:param vertical: Controls at what point we make a list of elements vertical. If this is an integer (the default),
it sets the threshold of the number of items before we go vertical. If True, always go vertical, if False, never
do so
: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

Definition at line 452 of file __init__.py.

461) -> typing.Optional[EditablePartial]:
462 """
463 Recursively converts a PyParsing Element to a railroad Element
464 :param lookup: The shared converter state that keeps track of useful things
465 :param index: The index of this element within the parent
466 :param parent: The parent of this element in the output tree
467 :param vertical: Controls at what point we make a list of elements vertical. If this is an integer (the default),
468 it sets the threshold of the number of items before we go vertical. If True, always go vertical, if False, never
469 do so
470 :param name_hint: If provided, this will override the generated name
471 :param show_results_names: bool flag indicating whether to add annotations for results names
472 :returns: The converted version of the input element, but as a Partial that hasn't yet been constructed
473 :param show_groups: bool flag indicating whether to show groups using bounding box
474 """
475 exprs = element.recurse()
477
478 # Python's id() is used to provide a unique identifier for elements
479 el_id = id(element)
480
481 element_results_name = element.resultsName
482
483 # Here we basically bypass processing certain wrapper elements if they contribute nothing to the diagram
484 if not element.customName:
485 if isinstance(
486 element,
487 (
488 # pyparsing.TokenConverter,
489 # pyparsing.Forward,
491 ),
492 ):
493 # However, if this element has a useful custom name, and its child does not, we can pass it on to the child
494 if exprs:
495 if not exprs[0].customName:
496 propagated_name = name
497 else:
498 propagated_name = None
499
500 return _to_diagram_element(
502 parent=parent,
503 lookup=lookup,
504 vertical=vertical,
505 index=index,
506 name_hint=propagated_name,
507 show_results_names=show_results_names,
508 show_groups=show_groups,
509 )
510
511 # If the element isn't worth extracting, we always treat it as the first time we say it
512 if _worth_extracting(element):
513 if el_id in lookup:
514 # If we've seen this element exactly once before, we are only just now finding out that it's a duplicate,
515 # so we have to extract it into a new diagram.
516 looked_up = lookup[el_id]
517 looked_up.mark_for_extraction(el_id, lookup, name=name_hint)
519 return ret
520
521 elif el_id in lookup.diagrams:
522 # If we have seen the element at least twice before, and have already extracted it into a subdiagram, we
523 # just put in a marker element that refers to the sub-diagram
525 railroad.NonTerminal, text=lookup.diagrams[el_id].kwargs["name"]
526 )
527 return ret
528
529 # Recursively convert child elements
530 # Here we find the most relevant Railroad element for matching pyparsing Element
531 # We use ``items=[]`` here to hold the place for where the child elements will go once created
532 if isinstance(element, pyparsing.And):
533 # detect And's created with ``expr*N`` notation - for these use a OneOrMore with a repeat
534 # (all will have the same name, and resultsName)
535 if not exprs:
536 return None
537 if len(set((e.name, e.resultsName) for e in exprs)) == 1:
539 railroad.OneOrMore, item="", repeat=str(len(exprs))
540 )
541 elif _should_vertical(vertical, exprs):
543 else:
546 if not exprs:
547 return None
548 if _should_vertical(vertical, exprs):
550 else:
552 elif isinstance(element, pyparsing.Each):
553 if not exprs:
554 return None
555 ret = EditablePartial.from_call(EachItem, items=[])
556 elif isinstance(element, pyparsing.NotAny):
557 ret = EditablePartial.from_call(AnnotatedItem, label="NOT", item="")
558 elif isinstance(element, pyparsing.FollowedBy):
559 ret = EditablePartial.from_call(AnnotatedItem, label="LOOKAHEAD", item="")
560 elif isinstance(element, pyparsing.PrecededBy):
561 ret = EditablePartial.from_call(AnnotatedItem, label="LOOKBEHIND", item="")
562 elif isinstance(element, pyparsing.Group):
563 if show_groups:
564 ret = EditablePartial.from_call(AnnotatedItem, label="", item="")
565 else:
566 ret = EditablePartial.from_call(railroad.Group, label="", item="")
568 label = type(element).__name__.lower()
569 if label == "tokenconverter":
571 else:
572 ret = EditablePartial.from_call(AnnotatedItem, label=label, item="")
573 elif isinstance(element, pyparsing.Opt):
575 elif isinstance(element, pyparsing.OneOrMore):
577 elif isinstance(element, pyparsing.ZeroOrMore):
579 elif isinstance(element, pyparsing.Group):
581 railroad.Group, item=None, label=element_results_name
582 )
583 elif isinstance(element, pyparsing.Empty) and not element.customName:
584 # Skip unnamed "Empty" elements
585 ret = None
588 elif len(exprs) > 0 and not element_results_name:
589 ret = EditablePartial.from_call(railroad.Group, item="", label=name)
590 elif len(exprs) > 0:
592 else:
594 ret = terminal
595
596 if ret is None:
597 return
598
599 # Indicate this element's position in the tree so we can extract it if necessary
600 lookup[el_id] = ElementState(
601 element=element,
602 converted=ret,
603 parent=parent,
604 parent_index=index,
605 number=lookup.generate_index(),
606 )
608 lookup[el_id].mark_for_extraction(el_id, lookup, element.customName)
609
610 i = 0
611 for expr in exprs:
612 # Add a placeholder index in case we have to extract the child before we even add it to the parent
613 if "items" in ret.kwargs:
614 ret.kwargs["items"].insert(i, None)
615
616 item = _to_diagram_element(
617 expr,
618 parent=ret,
619 lookup=lookup,
620 vertical=vertical,
621 index=i,
622 show_results_names=show_results_names,
623 show_groups=show_groups,
624 )
625
626 # Some elements don't need to be shown in the diagram
627 if item is not None:
628 if "item" in ret.kwargs:
629 ret.kwargs["item"] = item
630 elif "items" in ret.kwargs:
631 # If we've already extracted the child, don't touch this index, since it's occupied by a nonterminal
632 ret.kwargs["items"][i] = item
633 i += 1
634 elif "items" in ret.kwargs:
635 # If we're supposed to skip this element, remove it from the parent
636 del ret.kwargs["items"][i]
637
638 # If all this items children are none, skip this item
639 if ret and (
640 ("items" in ret.kwargs and len(ret.kwargs["items"]) == 0)
641 or ("item" in ret.kwargs and ret.kwargs["item"] is None)
642 ):
644
645 # Mark this element as "complete", ie it has all of its children
646 if el_id in lookup:
647 lookup[el_id].complete = True
648
649 if el_id in lookup and lookup[el_id].extract and lookup[el_id].complete:
651 if ret is not None:
653 railroad.NonTerminal, text=lookup.diagrams[el_id].kwargs["name"]
654 )
655
656 return ret

References pip._vendor.pyparsing.diagram._should_vertical(), pip._vendor.pyparsing.diagram._to_diagram_element(), pip._vendor.pyparsing.diagram._worth_extracting(), and i.

Referenced by pip._vendor.pyparsing.diagram._to_diagram_element(), and pip._vendor.pyparsing.diagram.to_railroad().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _visible_exprs()

_visible_exprs ( Iterable[pyparsing.ParserElement exprs)
protected

Definition at line 438 of file __init__.py.

438def _visible_exprs(exprs: Iterable[pyparsing.ParserElement]):
439 non_diagramming_exprs = (
443 )
444 return [
445 e
446 for e in exprs
447 if not (e.customName or e.resultsName or isinstance(e, non_diagramming_exprs))
448 ]
449
450
451@_apply_diagram_item_enhancements

References i.

Referenced by pip._vendor.pyparsing.diagram._should_vertical().

Here is the caller graph for this function:

◆ _worth_extracting()

bool _worth_extracting ( pyparsing.ParserElement  element)
protected
Returns true if this element is worth having its own sub-diagram. Simply, if any of its children
themselves have children, then its complex enough to extract

Definition at line 386 of file __init__.py.

386def _worth_extracting(element: pyparsing.ParserElement) -> bool:
387 """
388 Returns true if this element is worth having its own sub-diagram. Simply, if any of its children
389 themselves have children, then its complex enough to extract
390 """
391 children = element.recurse()
392 return any(child.recurse() for child in children)
393
394

References i.

Referenced by pip._vendor.pyparsing.diagram._to_diagram_element().

Here is the caller graph for this function:

◆ railroad_to_html()

str railroad_to_html ( List[NamedDiagram diagrams,
  embed = False,
**  kwargs 
)
Given a list of NamedDiagram, produce a single HTML string that visualises those diagrams
:params kwargs: kwargs to be passed in to the template

Definition at line 137 of file __init__.py.

137def railroad_to_html(diagrams: List[NamedDiagram], embed=False, **kwargs) -> str:
138 """
139 Given a list of NamedDiagram, produce a single HTML string that visualises those diagrams
140 :params kwargs: kwargs to be passed in to the template
141 """
142 data = []
143 for diagram in diagrams:
144 if diagram.diagram is None:
145 continue
146 io = StringIO()
147 try:
148 css = kwargs.get('css')
150 except AttributeError:
152 title = diagram.name
153 if diagram.index == 0:
154 title += " (root)"
155 data.append({"title": title, "text": "", "svg": io.getvalue()})
156
157 return template.render(diagrams=data, embed=embed, **kwargs)
158
159

References i.

◆ resolve_partial()

T resolve_partial ( "EditablePartial[T]"  partial)
Recursively resolves a collection of Partials into whatever type they are

Definition at line 160 of file __init__.py.

160def resolve_partial(partial: "EditablePartial[T]") -> T:
161 """
162 Recursively resolves a collection of Partials into whatever type they are
163 """
164 if isinstance(partial, EditablePartial):
165 partial.args = resolve_partial(partial.args)
166 partial.kwargs = resolve_partial(partial.kwargs)
167 return partial()
168 elif isinstance(partial, list):
169 return [resolve_partial(x) for x in partial]
170 elif isinstance(partial, dict):
171 return {key: resolve_partial(x) for key, x in partial.items()}
172 else:
173 return partial
174
175

References i, and pip._vendor.pyparsing.diagram.resolve_partial().

Referenced by pip._vendor.pyparsing.diagram.resolve_partial(), and pip._vendor.pyparsing.diagram.to_railroad().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_railroad()

List[NamedDiagram] to_railroad ( pyparsing.ParserElement  element,
typing.Optional[dict]   diagram_kwargs = None,
int   vertical = 3,
bool   show_results_names = False,
bool   show_groups = False 
)
Convert a pyparsing element tree into a list of diagrams. This is the recommended entrypoint to diagram
creation if you want to access the Railroad tree before it is converted to HTML
:param element: base element of the parser being diagrammed
:param diagram_kwargs: kwargs to pass to the Diagram() constructor
:param vertical: (optional) - int - limit at which number of alternatives should be
   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

Definition at line 176 of file __init__.py.

182) -> List[NamedDiagram]:
183 """
184 Convert a pyparsing element tree into a list of diagrams. This is the recommended entrypoint to diagram
185 creation if you want to access the Railroad tree before it is converted to HTML
186 :param element: base element of the parser being diagrammed
187 :param diagram_kwargs: kwargs to pass to the Diagram() constructor
188 :param vertical: (optional) - int - limit at which number of alternatives should be
189 shown vertically instead of horizontally
190 :param show_results_names - bool to indicate whether results name annotations should be
191 included in the diagram
192 :param show_groups - bool to indicate whether groups should be highlighted with an unlabeled
193 surrounding box
194 """
195 # Convert the whole tree underneath the root
196 lookup = ConverterState(diagram_kwargs=diagram_kwargs or {})
197 _to_diagram_element(
198 element,
199 lookup=lookup,
200 parent=None,
201 vertical=vertical,
202 show_results_names=show_results_names,
203 show_groups=show_groups,
204 )
205
206 root_id = id(element)
207 # Convert the root if it hasn't been already
208 if root_id in lookup:
209 if not element.customName:
210 lookup[root_id].name = ""
211 lookup[root_id].mark_for_extraction(root_id, lookup, force=True)
212
213 # Now that we're finished, we can convert from intermediate structures into Railroad elements
214 diags = list(lookup.diagrams.values())
215 if len(diags) > 1:
216 # collapse out duplicate diags with the same name
217 seen = set()
218 deduped_diags = []
219 for d in diags:
220 # don't extract SkipTo elements, they are uninformative as subdiagrams
221 if d.name == "...":
222 continue
223 if d.name is not None and d.name not in seen:
226 resolved = [resolve_partial(partial) for partial in deduped_diags]
227 else:
228 # special case - if just one diagram, always display it, even if
229 # it has no name
230 resolved = [resolve_partial(partial) for partial in diags]
231 return sorted(resolved, key=lambda diag: diag.index)
232
233

References pip._vendor.pyparsing.diagram._to_diagram_element(), i, and pip._vendor.pyparsing.diagram.resolve_partial().

Here is the call graph for this function:

Variable Documentation

◆ jinja2_template_source

str jinja2_template_source

Definition at line 20 of file __init__.py.

◆ NamedDiagram

NamedDiagram
Initial value:
1= NamedTuple(
2 "NamedDiagram",
3 [("name", str), ("diagram", typing.Optional[railroad.DiagramItem]), ("index", int)],
4)

Definition at line 58 of file __init__.py.

◆ T

T = TypeVar("T")

Definition at line 66 of file __init__.py.

◆ template

template = Template(jinja2_template_source)

Definition at line 55 of file __init__.py.