a
    f=ict\                     @   s  d dl Z d dlZd dlZd dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlZdZeeZeddefdeje j fd	efgZed
ZG dd de jZG dd de jZG dd dee Zee edddZdedddZd/ejeje ee e ee dddZ!ee
ej e dddZ"G d d! d!Z#G d"d# d#Z$eje d$d%d&Z%d'd( Z&e
ej d)d*d+Z'e&d0ejeje e$eeee e eje d,	d-d.Z(dS )1    N)List
NamedTupleGenericTypeVarDictCallableSetIterable)Template)StringIOaM  <!DOCTYPE html>
<html>
<head>
    {% if not head %}
        <style type="text/css">
            .railroad-heading {
                font-family: monospace;
            }
        </style>
    {% else %}
        {{ head | safe }}
    {% endif %}
</head>
<body>
{{ body | safe }}
{% for diagram in diagrams %}
    <div class="railroad-group">
        <h1 class="railroad-heading">{{ diagram.title }}</h1>
        <div class="railroad-description">{{ diagram.text }}</div>
        <div class="railroad-svg">
            {{ diagram.svg }}
        </div>
    </div>
{% endfor %}
</body>
</html>
NamedDiagramnamediagramindexTc                       s$   e Zd ZdZdZ fddZ  ZS )EachItemz
    Custom railroad item to compose a:
    - Group containing a
      - OneOrMore containing a
        - Choice of the elements in the Each
    with the group label indicating that all must be matched
    z[ALL]c                    s<   t jt|d g|R  }t j|d}t j|| jd d S )N   itemlabel)railroadChoicelen	OneOrMoresuper__init__	all_label)selfitemsZchoice_itemZone_or_more_item	__class__ k/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pyparsing/diagram/__init__.pyr   I   s    zEachItem.__init__)__name__
__module____qualname____doc__r   r   __classcell__r"   r"   r    r#   r   >   s   r   c                       s&   e Zd ZdZed fddZ  ZS )AnnotatedItemzC
    Simple subclass of Group that creates an annotation label
    r   c                    s"   t  j||rd|n|d d S )Nz[{}]r   r   )r   r   format)r   r   r   r    r"   r#   r   T   s    zAnnotatedItem.__init__)r$   r%   r&   r'   strr   r(   r"   r"   r    r#   r)   O   s   r)   c                   @   s`   e Zd ZdZedef eedddZe	edef dddd	Z
ed
d ZedddZdS )EditablePartialz
    Acts like a functools.partial, but can be edited. In other words, it represents a type that hasn't yet been
    constructed.
    .funcargskwargsc                 C   s   || _ || _|| _d S Nr.   )r   r/   r0   r1   r"   r"   r#   r   a   s    zEditablePartial.__init__EditablePartial[T])r/   returnc                 O   s   t |t||dS )z
        If you call this function in the same way that you would call the constructor, it will store the arguments
        as you expect. For example EditablePartial.from_call(Fraction, 1, 3)() == Fraction(1, 3)
        r.   )r-   list)clsr/   r0   r1   r"   r"   r#   	from_callf   s    zEditablePartial.from_callc                 C   s
   | j d S )Nr   )r1   r   r"   r"   r#   r   n   s    zEditablePartial.namer4   c                 C   sL   | j  }| j }t| j}|j| jv r<|||j7 }| j|i |S )z<
        Evaluate the partial and return the result
        )r0   copyr1   inspectgetfullargspecr/   varargspop)r   r0   r1   Zarg_specr"   r"   r#   __call__r   s    

zEditablePartial.__call__N)r$   r%   r&   r'   r   r   r5   dictr   classmethodr7   propertyr   r?   r"   r"   r"   r#   r-   X   s   
r-   )diagramsr4   c                 K   sp   g }| D ]R}|j du rqt }|j |j |j}|jdkrD|d7 }||d| d qtj	f d|i|S )z
    Given a list of NamedDiagram, produce a single HTML string that visualises those diagrams
    :params kwargs: kwargs to be passed in to the template
    Nr   z (root) )titletextsvgrC   )
r   r   ZwriteSvgwriter   r   appendgetvaluetemplaterender)rC   r1   datar   iorE   r"   r"   r#   railroad_to_html   s    

rO   r3   )partialr4   c                 C   sd   t | tr(t| j| _t| j| _|  S t | tr@dd | D S t | tr\dd |  D S | S dS )zS
    Recursively resolves a collection of Partials into whatever type they are
    c                 S   s   g | ]}t |qS r"   resolve_partial).0xr"   r"   r#   
<listcomp>       z#resolve_partial.<locals>.<listcomp>c                 S   s   i | ]\}}|t |qS r"   rQ   )rS   keyrT   r"   r"   r#   
<dictcomp>   rV   z#resolve_partial.<locals>.<dictcomp>N)
isinstancer-   rR   r0   r1   r5   r@   r   )rP   r"   r"   r#   rR      s    


rR      F)elementdiagram_kwargsverticalshow_results_namesshow_groupsr4   c                 C   s   t |pi d}t| |d|||d t| }||v rV| jsBd|| _|| j||dd t|j }t	|dkrt
 }g }	|D ]:}
|
jdkrq~|
jdur~|
j|vr~||
j |	|
 q~d	d
 |	D }ndd
 |D }t|dd dS )a  
    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
    r\   N)lookupparentr]   r^   r_   rD   T)forcer   z...c                 S   s   g | ]}t |qS r"   rQ   rS   rP   r"   r"   r#   rU      rV   zto_railroad.<locals>.<listcomp>c                 S   s   g | ]}t |qS r"   rQ   rd   r"   r"   r#   rU      rV   c                 S   s   | j S r2   r   )Zdiagr"   r"   r#   <lambda>   rV   zto_railroad.<locals>.<lambda>rW   )ConverterState_to_diagram_elementid
customNamer   mark_for_extractionr5   rC   valuesr   setaddrI   sorted)r[   r\   r]   r^   r_   ra   Zroot_idZdiagsseenZdeduped_diagsdresolvedr"   r"   r#   to_railroad   s6    	

rt   )specificationexprsr4   c                 C   s    | du rdS t t|| kS dS )zF
    Returns true if we should return a vertical list of elements
    NF)r   _visible_exprs)ru   rv   r"   r"   r#   _should_vertical   s    rx   c                   @   sH   e Zd ZdZdejeeeee	j
e dddZdedeedd	d
ZdS )ElementStatez<
    State recorded for an individual pyparsing Element
    N)r[   	convertedrb   numberr   parent_indexc                 C   s4   || _ || _|| _|| _|| _|| _d| _d| _d S )NF)r[   r   rz   rb   r{   r|   extractcomplete)r   r[   rz   rb   r{   r   r|   r"   r"   r#   r      s    
zElementState.__init__Frh   )el_idstater   rc   c                 C   sT   d| _ | js2|r|| _n| jjr,| jj| _nd| _|sF| jrPt| jrP|| dS )a  
        Called when this instance has been seen twice, and thus should eventually be extracted into a sub-diagram
        :param el_id: id of the element
        :param state: element/diagram state tracker
        :param name: name to use for this element's text
        :param force: If true, force extraction now, regardless of the state of this. Only useful for extracting the
        root element when we know we're finished
        TrD   N)r}   r   r[   rk   r~   _worth_extractingextract_into_diagram)r   r   r   r   rc   r"   r"   r#   rl     s    z ElementState.mark_for_extraction)NN)NF)r$   r%   r&   r'   	pyparsingParserElementr-   intr,   typingOptionalr   boolrl   r"   r"   r"   r#   ry      s      ry   c                   @   s   e Zd ZdZdeje dddZee	dddZ
ee	d	d
dZedddZedddZedddZedddZedddZdS )rh   zR
    Stores some state that persists between recursions into the element tree
    Nr`   c                 C   s.   i | _ i | _d| _d| _|pi | _t | _d S )Nr   r   )_element_diagram_statesrC   unnamed_indexr   r\   rn   Zextracted_diagram_names)r   r\   r"   r"   r#   r   .  s    
zConverterState.__init__)rW   valuec                 C   s   || j |< d S r2   r   )r   rW   r   r"   r"   r#   __setitem__;  s    zConverterState.__setitem__)rW   r4   c                 C   s
   | j | S r2   r   r   rW   r"   r"   r#   __getitem__>  s    zConverterState.__getitem__rg   c                 C   s   | j |= d S r2   r   r   r"   r"   r#   __delitem__A  s    zConverterState.__delitem__c                 C   s
   || j v S r2   r   r   r"   r"   r#   __contains__D  s    zConverterState.__contains__r9   c                 C   s   |  j d7  _ | j S )zT
        Generate a number used in the name of an otherwise unnamed diagram
        r   )r   r8   r"   r"   r#   generate_unnamedG  s    zConverterState.generate_unnamedc                 C   s   |  j d7  _ | j S )z;
        Generate a number used to index a diagram
        r   re   r8   r"   r"   r#   generate_indexN  s    zConverterState.generate_index)r   c                 C   s   | | }|j rXtjtj|jd}d|j jv r:||j jd< nd|j jv rX||j jd |j< |jj	tj
krt|jjd }n|j}tjt|jtjtj|fi | j|jd| j|< | |= dS )z
        Used when we encounter the same token twice in the same tree. When this
        happens, we replace all instances of that token with a terminal, and
        create a new subdiagram for the token
        rF   r   r   )r   r   r   N)rb   r-   r7   r   NonTerminalr   r1   r|   rz   r/   Groupr   ZDiagramr\   r{   rC   )r   r   positionretcontentr"   r"   r#   r   U  s*    	z#ConverterState.extract_into_diagram)N)r$   r%   r&   r'   r   r   r@   r   r   ry   r   r   r   r   r   r   r   r"   r"   r"   r#   rh   )  s   rh   )r[   r4   c                 C   s   |   }tdd |D S )z
    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
    c                 s   s   | ]}|  V  qd S r2   )recurse)rS   childr"   r"   r#   	<genexpr>}  rV   z$_worth_extracting.<locals>.<genexpr>)r   any)r[   childrenr"   r"   r#   r   w  s    r   c                    s6   dt jtjt tttttttjt d	 fdd}|S )z
    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)
    Nr   F	r[   rb   ra   r]   r   	name_hintr^   r_   r4   c           
   	      sT    | |||||||}|rP|d urP| j }	|	rP|	| jr8dnd7 }	tjtj||	d}|S )NrD   *r*   )resultsNamemodalResultsr-   r7   r   r   )
r[   rb   ra   r]   r   r   r^   r_   r   element_results_namefnr"   r#   _inner  s$    z0_apply_diagram_item_enhancements.<locals>._inner)NNr   NFF)	r   r   r   r   r-   rh   r   r,   r   )r   r   r"   r   r#    _apply_diagram_item_enhancements  s$    
      "r   rv   c                    s$   t jt jt jjf  fdd| D S )Nc                    s&   g | ]}|j s|jst| s|qS r"   )rk   r   rY   rS   eZnon_diagramming_exprsr"   r#   rU     s   z"_visible_exprs.<locals>.<listcomp>)r   ParseElementEnhancePositionTokenAnd
_ErrorStopr   r"   r   r#   rw     s    
rw   r   c              
   C   s8  |   }|p| jp| jj}	t| }
| j}| jsnt| tjfrn|rn|d jsP|	}nd}t	| j
|||||||dS t| r|
|v r||
 }|j|
||d tjtj|jd}|S |
|jv rtjtj|j|
 jd d}|S t| tjrV|sdS ttdd |D d	kr$tjtjd
tt|d}n.t||rBtjtjg d}ntjtjg d}nBt| tjtjfr|stdS t||rtjtjdg d}ntjtjg d}nt| tj r|sdS tjt!g d}nt| tj"rtjt#dd
d}nt| tj$rtjt#dd
d}nt| tj%r8tjt#dd
d}n`t| tj&rt|r^tjt#d
d
d}ntjtj&d
d
d}n$t| tj'rtjt#t(| j) d
d}nt| tj*rtjtj+d
d}nt| tjrtjtjd
d}nt| tj,rtjtj,d
d}nt| tj&r tjtj&d|d}nxt| tj-r<| js<d}n\t|d	kr\tjtjg d}n<t|dkr|stjtj&d
|	d}nttj.| j/}|}|du rdS t0| ||||1 d||
< | jr||
 |
|| j d}|D ]}d|jv r|jd 2|d t	|||||||d}|durbd|jv r>||jd< n"d|jv rz||jd |< |d	7 }nd|jv r|jd |= q|rd|jv rt|jd dksd|jv r|jd du rttj.|	}|
|v rd||
 _3|
|v r4||
 j4r4||
 j3r4|5|
 |dur4tjtj|j|
 jd d}|S )ay  
    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
    r   N)rb   ra   r]   r   r   r^   r_   )r   r   r   c                 s   s   | ]}|j |jfV  qd S r2   )r   r   r   r"   r"   r#   r     rV   z&_to_diagram_element.<locals>.<genexpr>r   rD   )r   repeat)r   NOT)r   r   Z	LOOKAHEADZ
LOOKBEHINDr   r*   )r[   rz   rb   r|   r{   r   )rb   ra   r]   r   r^   r_   r   T)6r   rk   r!   r$   rj   r   rY   r   Locatedri   exprr   rl   r-   r7   r   r   r   rC   r1   r   r   rn   r   r,   rx   StackSequenceOr
MatchFirstr   ZHorizontalChoiceEachr   NotAnyr)   
FollowedBy
PrecededByr   TokenConvertertypelowerOptr   
ZeroOrMoreEmptyZTerminaldefaultNamery   r   insertr~   r}   r   )r[   rb   ra   r]   r   r   r^   r_   rv   r   r   r   Zpropagated_nameZ	looked_upr   Zterminalir   r   r"   r"   r#   ri     s   	







"

ri   )NrZ   FF)NNr   NFF))r   r   r   r   r   r   r   r   r   r   r	   Zjinja2r
   rN   r   r;   Zjinja2_template_sourcerK   r,   r   ZDiagramItemr   r   r   r   r   r)   r-   rO   rR   r   r@   r   rt   rx   ry   rh   r   r   rw   ri   r"   r"   r"   r#   <module>   sp   (
	*    ;
>N	,      