__init__.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. # mypy: ignore-errors
  2. from __future__ import annotations
  3. import itertools
  4. import railroad
  5. import pyparsing
  6. import dataclasses
  7. import typing
  8. from typing import (
  9. Generic,
  10. TypeVar,
  11. Callable,
  12. Iterable,
  13. )
  14. from jinja2 import Template
  15. from io import StringIO
  16. import inspect
  17. import re
  18. jinja2_template_source = """\
  19. {% if not embed %}
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. {% endif %}
  24. {% if not head %}
  25. <style>
  26. .railroad-heading {
  27. font-family: monospace;
  28. }
  29. </style>
  30. {% else %}
  31. {{ head | safe }}
  32. {% endif %}
  33. {% if not embed %}
  34. </head>
  35. <body>
  36. {% endif %}
  37. <meta charset="UTF-8"/>
  38. {{ body | safe }}
  39. {% for diagram in diagrams %}
  40. <div class="railroad-group">
  41. <h1 class="railroad-heading" id="{{ diagram.bookmark }}">{{ diagram.title }}</h1>
  42. <div class="railroad-description">{{ diagram.text }}</div>
  43. <div class="railroad-svg">
  44. {{ diagram.svg }}
  45. </div>
  46. </div>
  47. {% endfor %}
  48. {% if not embed %}
  49. </body>
  50. </html>
  51. {% endif %}
  52. """
  53. template = Template(jinja2_template_source)
  54. _bookmark_lookup = {}
  55. _bookmark_ids = itertools.count(start=1)
  56. def _make_bookmark(s: str) -> str:
  57. """
  58. Converts a string into a valid HTML bookmark (ID or anchor name).
  59. """
  60. if s in _bookmark_lookup:
  61. return _bookmark_lookup[s]
  62. # Replace invalid characters with hyphens and ensure only valid characters
  63. bookmark = re.sub(r'[^a-zA-Z0-9-]+', '-', s)
  64. # Ensure it starts with a letter by adding 'z' if necessary
  65. if not bookmark[:1].isalpha():
  66. bookmark = f"z{bookmark}"
  67. # Convert to lowercase and strip hyphens
  68. bookmark = bookmark.lower().strip('-')
  69. _bookmark_lookup[s] = bookmark = f"{bookmark}-{next(_bookmark_ids):04d}"
  70. return bookmark
  71. def _collapse_verbose_regex(regex_str: str) -> str:
  72. if "\n" not in regex_str:
  73. return regex_str
  74. collapsed = pyparsing.Regex(r"#.*$").suppress().transform_string(regex_str)
  75. collapsed = re.sub(r"\s*\n\s*", "", collapsed)
  76. return collapsed
  77. @dataclasses.dataclass
  78. class NamedDiagram:
  79. """
  80. A simple structure for associating a name with a railroad diagram
  81. """
  82. name: str
  83. index: int
  84. diagram: railroad.DiagramItem = None
  85. @property
  86. def bookmark(self):
  87. bookmark = _make_bookmark(self.name)
  88. return bookmark
  89. T = TypeVar("T")
  90. class EachItem(railroad.Group):
  91. """
  92. Custom railroad item to compose a:
  93. - :class:`railroad.Group` containing a
  94. - :class:`railroad.OneOrMore` containing a
  95. - :class:`railroad.Choice` of the elements in the
  96. :class:`railroad.Each`
  97. with the group label indicating that all must be matched
  98. """
  99. all_label = "[ALL]"
  100. def __init__(self, *items) -> None:
  101. choice_item = railroad.Choice(len(items) - 1, *items)
  102. one_or_more_item = railroad.OneOrMore(item=choice_item)
  103. super().__init__(one_or_more_item, label=self.all_label)
  104. class AnnotatedItem(railroad.Group):
  105. """
  106. Simple subclass of Group that creates an annotation label
  107. """
  108. def __init__(self, label: str, item) -> None:
  109. super().__init__(item=item, label=f"[{label}]" if label else "")
  110. class EditablePartial(Generic[T]):
  111. """
  112. Acts like a functools.partial, but can be edited. In other words, it represents a type that hasn't yet been
  113. constructed.
  114. """
  115. # We need this here because the railroad constructors actually transform the data, so can't be called until the
  116. # entire tree is assembled
  117. def __init__(self, func: Callable[..., T], args: list, kwargs: dict) -> None:
  118. self.func = func
  119. self.args = args
  120. self.kwargs = kwargs
  121. @classmethod
  122. def from_call(cls, func: Callable[..., T], *args, **kwargs) -> EditablePartial[T]:
  123. """
  124. If you call this function in the same way that you would call the constructor,
  125. it will store the arguments as you expect. For example
  126. ``EditablePartial.from_call(Fraction, 1, 3)() == Fraction(1, 3)``
  127. """
  128. return EditablePartial(func=func, args=list(args), kwargs=kwargs)
  129. @property
  130. def name(self):
  131. return self.kwargs["name"]
  132. def __call__(self) -> T:
  133. """
  134. Evaluate the partial and return the result
  135. """
  136. args = self.args.copy()
  137. kwargs = self.kwargs.copy()
  138. # This is a helpful hack to allow you to specify varargs parameters (e.g. *args) as keyword args (e.g.
  139. # args=['list', 'of', 'things'])
  140. arg_spec = inspect.getfullargspec(self.func)
  141. if arg_spec.varargs in self.kwargs:
  142. args += kwargs.pop(arg_spec.varargs)
  143. return self.func(*args, **kwargs)
  144. def railroad_to_html(diagrams: list[NamedDiagram], embed=False, **kwargs) -> str:
  145. """
  146. Given a list of :class:`NamedDiagram`, produce a single HTML string
  147. that visualises those diagrams.
  148. :params kwargs: kwargs to be passed in to the template
  149. """
  150. data = []
  151. for diagram in diagrams:
  152. if diagram.diagram is None:
  153. continue
  154. io = StringIO()
  155. try:
  156. css = kwargs.get("css")
  157. diagram.diagram.writeStandalone(io.write, css=css)
  158. except AttributeError:
  159. diagram.diagram.writeSvg(io.write)
  160. title = diagram.name
  161. if diagram.index == 0:
  162. title += " (root)"
  163. data.append(
  164. {
  165. "title": title, "text": "", "svg": io.getvalue(), "bookmark": diagram.bookmark
  166. }
  167. )
  168. return template.render(diagrams=data, embed=embed, **kwargs)
  169. def resolve_partial(partial: EditablePartial[T]) -> T:
  170. """
  171. Recursively resolves a collection of Partials into whatever type they are
  172. """
  173. if isinstance(partial, EditablePartial):
  174. partial.args = resolve_partial(partial.args)
  175. partial.kwargs = resolve_partial(partial.kwargs)
  176. return partial()
  177. elif isinstance(partial, list):
  178. return [resolve_partial(x) for x in partial]
  179. elif isinstance(partial, dict):
  180. return {key: resolve_partial(x) for key, x in partial.items()}
  181. else:
  182. return partial
  183. def to_railroad(
  184. element: pyparsing.ParserElement,
  185. diagram_kwargs: typing.Optional[dict] = None,
  186. vertical: int = 3,
  187. show_results_names: bool = False,
  188. show_groups: bool = False,
  189. show_hidden: bool = False,
  190. ) -> list[NamedDiagram]:
  191. """
  192. Convert a pyparsing element tree into a list of diagrams. This is the recommended entrypoint to diagram
  193. creation if you want to access the Railroad tree before it is converted to HTML
  194. :param element: base element of the parser being diagrammed
  195. :param diagram_kwargs: kwargs to pass to the :meth:`Diagram` constructor
  196. :param vertical: (optional) int - limit at which number of alternatives
  197. should be shown vertically instead of horizontally
  198. :param show_results_names: bool to indicate whether results name
  199. annotations should be included in the diagram
  200. :param show_groups: bool to indicate whether groups should be highlighted
  201. with an unlabeled surrounding box
  202. :param show_hidden: bool to indicate whether internal elements that are
  203. typically hidden should be shown
  204. """
  205. # Convert the whole tree underneath the root
  206. lookup = ConverterState(diagram_kwargs=diagram_kwargs or {})
  207. _to_diagram_element(
  208. element,
  209. lookup=lookup,
  210. parent=None,
  211. vertical=vertical,
  212. show_results_names=show_results_names,
  213. show_groups=show_groups,
  214. show_hidden=show_hidden,
  215. )
  216. root_id = id(element)
  217. # Convert the root if it hasn't been already
  218. if root_id in lookup:
  219. if not element.customName:
  220. lookup[root_id].name = ""
  221. lookup[root_id].mark_for_extraction(root_id, lookup, force=True)
  222. # Now that we're finished, we can convert from intermediate structures into Railroad elements
  223. diags = list(lookup.diagrams.values())
  224. if len(diags) > 1:
  225. # collapse out duplicate diags with the same name
  226. seen = set()
  227. deduped_diags = []
  228. for d in diags:
  229. # don't extract SkipTo elements, they are uninformative as subdiagrams
  230. if d.name == "...":
  231. continue
  232. if d.name is not None and d.name not in seen:
  233. seen.add(d.name)
  234. deduped_diags.append(d)
  235. resolved = [resolve_partial(partial) for partial in deduped_diags]
  236. else:
  237. # special case - if just one diagram, always display it, even if
  238. # it has no name
  239. resolved = [resolve_partial(partial) for partial in diags]
  240. return sorted(resolved, key=lambda diag: diag.index)
  241. def _should_vertical(
  242. specification: int, exprs: Iterable[pyparsing.ParserElement]
  243. ) -> bool:
  244. """
  245. Returns true if we should return a vertical list of elements
  246. """
  247. if specification is None:
  248. return False
  249. else:
  250. return len(_visible_exprs(exprs)) >= specification
  251. @dataclasses.dataclass
  252. class ElementState:
  253. """
  254. State recorded for an individual pyparsing Element
  255. """
  256. #: The pyparsing element that this represents
  257. element: pyparsing.ParserElement
  258. #: The output Railroad element in an unconverted state
  259. converted: EditablePartial
  260. #: The parent Railroad element, which we store so that we can extract this if it's duplicated
  261. parent: EditablePartial
  262. #: The order in which we found this element, used for sorting diagrams if this is extracted into a diagram
  263. number: int
  264. #: The name of the element
  265. name: str = None
  266. #: The index of this inside its parent
  267. parent_index: typing.Optional[int] = None
  268. #: If true, we should extract this out into a subdiagram
  269. extract: bool = False
  270. #: If true, all of this element's children have been filled out
  271. complete: bool = False
  272. def mark_for_extraction(
  273. self, el_id: int, state: ConverterState, name: str = None, force: bool = False
  274. ):
  275. """
  276. Called when this instance has been seen twice, and thus should eventually be extracted into a sub-diagram
  277. :param el_id: id of the element
  278. :param state: element/diagram state tracker
  279. :param name: name to use for this element's text
  280. :param force: If true, force extraction now, regardless of the state of this. Only useful for extracting the
  281. root element when we know we're finished
  282. """
  283. self.extract = True
  284. # Set the name
  285. if not self.name:
  286. if name:
  287. # Allow forcing a custom name
  288. self.name = name
  289. elif self.element.customName:
  290. self.name = self.element.customName
  291. else:
  292. self.name = ""
  293. # Just because this is marked for extraction doesn't mean we can do it yet. We may have to wait for children
  294. # to be added
  295. # Also, if this is just a string literal etc, don't bother extracting it
  296. if force or (self.complete and _worth_extracting(self.element)):
  297. state.extract_into_diagram(el_id)
  298. class ConverterState:
  299. """
  300. Stores some state that persists between recursions into the element tree
  301. """
  302. index_generator = itertools.count(start=1)
  303. def __init__(self, diagram_kwargs: typing.Optional[dict] = None) -> None:
  304. #: A dictionary mapping ParserElements to state relating to them
  305. self._element_diagram_states: dict[int, ElementState] = {}
  306. #: A dictionary mapping ParserElement IDs to subdiagrams generated from them
  307. self.diagrams: dict[int, EditablePartial[NamedDiagram]] = {}
  308. #: The index of the next element. This is used for sorting
  309. self.index: int = 0
  310. #: Shared kwargs that are used to customize the construction of diagrams
  311. self.diagram_kwargs: dict = diagram_kwargs or {}
  312. self.extracted_diagram_names: set[str] = set()
  313. def __setitem__(self, key: int, value: ElementState):
  314. self._element_diagram_states[key] = value
  315. def __getitem__(self, key: int) -> ElementState:
  316. return self._element_diagram_states[key]
  317. def __delitem__(self, key: int):
  318. del self._element_diagram_states[key]
  319. def __contains__(self, key: int):
  320. return key in self._element_diagram_states
  321. def get(self, key, default=None):
  322. try:
  323. return self[key]
  324. except KeyError:
  325. return default
  326. def generate_index(self) -> int:
  327. """
  328. Generate a number used to index a diagram
  329. """
  330. return next(self.index_generator)
  331. def extract_into_diagram(self, el_id: int):
  332. """
  333. Used when we encounter the same token twice in the same tree. When this
  334. happens, we replace all instances of that token with a terminal, and
  335. create a new subdiagram for the token
  336. """
  337. position = self[el_id]
  338. # Replace the original definition of this element with a regular block
  339. if position.parent:
  340. href = f"#{_make_bookmark(position.name)}"
  341. ret = EditablePartial.from_call(railroad.NonTerminal, text=position.name, href=href)
  342. if "item" in position.parent.kwargs:
  343. position.parent.kwargs["item"] = ret
  344. elif "items" in position.parent.kwargs:
  345. position.parent.kwargs["items"][position.parent_index] = ret
  346. # If the element we're extracting is a group, skip to its content but keep the title
  347. if position.converted.func == railroad.Group:
  348. content = position.converted.kwargs["item"]
  349. else:
  350. content = position.converted
  351. self.diagrams[el_id] = EditablePartial.from_call(
  352. NamedDiagram,
  353. name=position.name,
  354. diagram=EditablePartial.from_call(
  355. railroad.Diagram, content, **self.diagram_kwargs
  356. ),
  357. index=position.number,
  358. )
  359. del self[el_id]
  360. def _worth_extracting(element: pyparsing.ParserElement) -> bool:
  361. """
  362. Returns true if this element is worth having its own sub-diagram. Simply, if any of its children
  363. themselves have children, then its complex enough to extract
  364. """
  365. children = element.recurse()
  366. return any(child.recurse() for child in children)
  367. def _apply_diagram_item_enhancements(fn):
  368. """
  369. decorator to ensure enhancements to a diagram item (such as results name annotations)
  370. get applied on return from _to_diagram_element (we do this since there are several
  371. returns in _to_diagram_element)
  372. """
  373. def _inner(
  374. element: pyparsing.ParserElement,
  375. parent: typing.Optional[EditablePartial],
  376. lookup: ConverterState = None,
  377. vertical: int = None,
  378. index: int = 0,
  379. name_hint: str = None,
  380. show_results_names: bool = False,
  381. show_groups: bool = False,
  382. show_hidden: bool = False,
  383. ) -> typing.Optional[EditablePartial]:
  384. ret = fn(
  385. element,
  386. parent,
  387. lookup,
  388. vertical,
  389. index,
  390. name_hint,
  391. show_results_names,
  392. show_groups,
  393. show_hidden,
  394. )
  395. # apply annotation for results name, if present
  396. if show_results_names and ret is not None:
  397. element_results_name = element.resultsName
  398. if element_results_name:
  399. # add "*" to indicate if this is a "list all results" name
  400. modal_tag = "" if element.modalResults else "*"
  401. ret = EditablePartial.from_call(
  402. railroad.Group,
  403. item=ret,
  404. label=f"{repr(element_results_name)}{modal_tag}",
  405. )
  406. return ret
  407. return _inner
  408. def _visible_exprs(exprs: Iterable[pyparsing.ParserElement]):
  409. non_diagramming_exprs = (
  410. pyparsing.ParseElementEnhance,
  411. pyparsing.PositionToken,
  412. pyparsing.And._ErrorStop,
  413. )
  414. return [
  415. e
  416. for e in exprs
  417. if not isinstance(e, non_diagramming_exprs)
  418. ]
  419. @_apply_diagram_item_enhancements
  420. def _to_diagram_element(
  421. element: pyparsing.ParserElement,
  422. parent: typing.Optional[EditablePartial],
  423. lookup: ConverterState = None,
  424. vertical: int = None,
  425. index: int = 0,
  426. name_hint: str = None,
  427. show_results_names: bool = False,
  428. show_groups: bool = False,
  429. show_hidden: bool = False,
  430. ) -> typing.Optional[EditablePartial]:
  431. """
  432. Recursively converts a PyParsing Element to a railroad Element
  433. :param lookup: The shared converter state that keeps track of useful things
  434. :param index: The index of this element within the parent
  435. :param parent: The parent of this element in the output tree
  436. :param vertical: Controls at what point we make a list of elements vertical. If this is an integer (the default),
  437. it sets the threshold of the number of items before we go vertical. If True, always go vertical, if False, never
  438. do so
  439. :param name_hint: If provided, this will override the generated name
  440. :param show_results_names: bool flag indicating whether to add annotations for results names
  441. :param show_groups: bool flag indicating whether to show groups using bounding box
  442. :param show_hidden: bool flag indicating whether to show elements that are typically hidden
  443. :returns: The converted version of the input element, but as a Partial that hasn't yet been constructed
  444. """
  445. exprs = element.recurse()
  446. name = name_hint or element.customName or type(element).__name__
  447. # Python's id() is used to provide a unique identifier for elements
  448. el_id = id(element)
  449. element_results_name = element.resultsName
  450. # Here we basically bypass processing certain wrapper elements if they contribute nothing to the diagram
  451. if not element.customName:
  452. if isinstance(
  453. element,
  454. (
  455. # pyparsing.TokenConverter,
  456. pyparsing.Forward,
  457. pyparsing.Located,
  458. pyparsing.AtStringStart,
  459. pyparsing.AtLineStart,
  460. ),
  461. ):
  462. # However, if this element has a useful custom name, and its child does not, we can pass it on to the child
  463. if exprs:
  464. if not exprs[0].customName:
  465. propagated_name = name
  466. else:
  467. propagated_name = None
  468. return _to_diagram_element(
  469. element.expr,
  470. parent=parent,
  471. lookup=lookup,
  472. vertical=vertical,
  473. index=index,
  474. name_hint=propagated_name,
  475. show_results_names=show_results_names,
  476. show_groups=show_groups,
  477. show_hidden=show_hidden,
  478. )
  479. # If the element isn't worth extracting, we always treat it as the first time we say it
  480. if _worth_extracting(element):
  481. looked_up = lookup.get(el_id)
  482. if looked_up and looked_up.name is not None:
  483. # If we've seen this element exactly once before, we are only just now finding out that it's a duplicate,
  484. # so we have to extract it into a new diagram.
  485. looked_up.mark_for_extraction(el_id, lookup, name=name_hint)
  486. href = f"#{_make_bookmark(looked_up.name)}"
  487. ret = EditablePartial.from_call(railroad.NonTerminal, text=looked_up.name, href=href)
  488. return ret
  489. elif el_id in lookup.diagrams:
  490. # If we have seen the element at least twice before, and have already extracted it into a subdiagram, we
  491. # just put in a marker element that refers to the sub-diagram
  492. text = lookup.diagrams[el_id].kwargs["name"]
  493. ret = EditablePartial.from_call(
  494. railroad.NonTerminal, text=text, href=f"#{_make_bookmark(text)}"
  495. )
  496. return ret
  497. # Recursively convert child elements
  498. # Here we find the most relevant Railroad element for matching pyparsing Element
  499. # We use ``items=[]`` here to hold the place for where the child elements will go once created
  500. # see if this element is normally hidden, and whether hidden elements are desired
  501. # if not, just return None
  502. if not element.show_in_diagram and not show_hidden:
  503. return None
  504. if isinstance(element, pyparsing.And):
  505. # detect And's created with ``expr*N`` notation - for these use a OneOrMore with a repeat
  506. # (all will have the same name, and resultsName)
  507. if not exprs:
  508. return None
  509. if len(set((e.name, e.resultsName) for e in exprs)) == 1 and len(exprs) > 2:
  510. ret = EditablePartial.from_call(
  511. railroad.OneOrMore, item="", repeat=str(len(exprs))
  512. )
  513. elif _should_vertical(vertical, exprs):
  514. ret = EditablePartial.from_call(railroad.Stack, items=[])
  515. else:
  516. ret = EditablePartial.from_call(railroad.Sequence, items=[])
  517. elif isinstance(element, (pyparsing.Or, pyparsing.MatchFirst)):
  518. if not exprs:
  519. return None
  520. if _should_vertical(vertical, exprs):
  521. ret = EditablePartial.from_call(railroad.Choice, 0, items=[])
  522. else:
  523. ret = EditablePartial.from_call(railroad.HorizontalChoice, items=[])
  524. elif isinstance(element, pyparsing.Each):
  525. if not exprs:
  526. return None
  527. ret = EditablePartial.from_call(EachItem, items=[])
  528. elif isinstance(element, pyparsing.NotAny):
  529. ret = EditablePartial.from_call(AnnotatedItem, label="NOT", item="")
  530. elif isinstance(element, pyparsing.FollowedBy):
  531. ret = EditablePartial.from_call(AnnotatedItem, label="LOOKAHEAD", item="")
  532. elif isinstance(element, pyparsing.PrecededBy):
  533. ret = EditablePartial.from_call(AnnotatedItem, label="LOOKBEHIND", item="")
  534. elif isinstance(element, pyparsing.Group):
  535. if show_groups:
  536. ret = EditablePartial.from_call(AnnotatedItem, label="", item="")
  537. else:
  538. ret = EditablePartial.from_call(
  539. railroad.Group, item=None, label=element_results_name
  540. )
  541. elif isinstance(element, pyparsing.TokenConverter):
  542. label = type(element).__name__.lower()
  543. if label == "tokenconverter":
  544. ret = EditablePartial.from_call(railroad.Sequence, items=[])
  545. else:
  546. ret = EditablePartial.from_call(AnnotatedItem, label=label, item="")
  547. elif isinstance(element, pyparsing.Opt):
  548. ret = EditablePartial.from_call(railroad.Optional, item="")
  549. elif isinstance(element, pyparsing.OneOrMore):
  550. if element.not_ender is not None:
  551. args = [
  552. parent,
  553. lookup,
  554. vertical,
  555. index,
  556. name_hint,
  557. show_results_names,
  558. show_groups,
  559. show_hidden,
  560. ]
  561. return _to_diagram_element(
  562. (~element.not_ender.expr + element.expr)[1, ...].set_name(element.name),
  563. *args,
  564. )
  565. ret = EditablePartial.from_call(railroad.OneOrMore, item=None)
  566. elif isinstance(element, pyparsing.ZeroOrMore):
  567. if element.not_ender is not None:
  568. args = [
  569. parent,
  570. lookup,
  571. vertical,
  572. index,
  573. name_hint,
  574. show_results_names,
  575. show_groups,
  576. show_hidden,
  577. ]
  578. return _to_diagram_element(
  579. (~element.not_ender.expr + element.expr)[...].set_name(element.name),
  580. *args,
  581. )
  582. ret = EditablePartial.from_call(railroad.ZeroOrMore, item="")
  583. elif isinstance(element, pyparsing.Empty) and not element.customName:
  584. # Skip unnamed "Empty" elements
  585. ret = None
  586. elif isinstance(element, pyparsing.ParseElementEnhance):
  587. ret = EditablePartial.from_call(railroad.Sequence, items=[])
  588. elif len(exprs) > 0 and not element_results_name:
  589. ret = EditablePartial.from_call(railroad.Group, item="", label=name)
  590. elif isinstance(element, pyparsing.Regex):
  591. collapsed_patt = _collapse_verbose_regex(element.pattern)
  592. ret = EditablePartial.from_call(railroad.Terminal, collapsed_patt)
  593. elif len(exprs) > 0:
  594. ret = EditablePartial.from_call(railroad.Sequence, items=[])
  595. else:
  596. terminal = EditablePartial.from_call(railroad.Terminal, element.defaultName)
  597. ret = terminal
  598. if ret is None:
  599. return
  600. # Indicate this element's position in the tree so we can extract it if necessary
  601. lookup[el_id] = ElementState(
  602. element=element,
  603. converted=ret,
  604. parent=parent,
  605. parent_index=index,
  606. number=lookup.generate_index(),
  607. )
  608. if element.customName:
  609. lookup[el_id].mark_for_extraction(el_id, lookup, element.customName)
  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. item = _to_diagram_element(
  616. expr,
  617. parent=ret,
  618. lookup=lookup,
  619. vertical=vertical,
  620. index=i,
  621. show_results_names=show_results_names,
  622. show_groups=show_groups,
  623. show_hidden=show_hidden,
  624. )
  625. # Some elements don't need to be shown in the diagram
  626. if item is not None:
  627. if "item" in ret.kwargs:
  628. ret.kwargs["item"] = item
  629. elif "items" in ret.kwargs:
  630. # If we've already extracted the child, don't touch this index, since it's occupied by a nonterminal
  631. ret.kwargs["items"][i] = item
  632. i += 1
  633. elif "items" in ret.kwargs:
  634. # If we're supposed to skip this element, remove it from the parent
  635. del ret.kwargs["items"][i]
  636. # If all this items children are none, skip this item
  637. if ret and (
  638. ("items" in ret.kwargs and len(ret.kwargs["items"]) == 0)
  639. or ("item" in ret.kwargs and ret.kwargs["item"] is None)
  640. ):
  641. ret = EditablePartial.from_call(railroad.Terminal, name)
  642. # Mark this element as "complete", ie it has all of its children
  643. if el_id in lookup:
  644. lookup[el_id].complete = True
  645. if el_id in lookup and lookup[el_id].extract and lookup[el_id].complete:
  646. lookup.extract_into_diagram(el_id)
  647. if ret is not None:
  648. text = lookup.diagrams[el_id].kwargs["name"]
  649. href = f"#{_make_bookmark(text)}"
  650. ret = EditablePartial.from_call(
  651. railroad.NonTerminal, text=text, href=href
  652. )
  653. return ret