o
    >Diy                     @  s   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 G dd dZ
dd	lmZmZmZmZ erBeej Zeej Zeej ZdS dS )
z
Base parser
    )annotationsN)TYPE_CHECKINGTypecast   )Sourcec                   @  s`   e Zd ZdZd!ddZd"dd	Zd#ddZd$ddZd%ddZd&ddZ	d'ddZ
d(ddZd S ))Parsera=  
    All elements defined in CommonMark's spec are included in the parser
    by default.

    Attributes:
        block_elements(dict): a dict of name: block_element pairs
        inline_elements(dict): a dict of name: inline_element pairs

    :param \*extras: extra elements to be included in parsing process.
    returnNonec                 C  sD   i | _ i | _tdd tjD dd tjD D ]}| | qd S )Nc                 s      | ]}t t|V  qd S N)getattrblock.0name r   E/var/www/Datamplify/venv/lib/python3.10/site-packages/marko/parser.py	<genexpr>       z"Parser.__init__.<locals>.<genexpr>c                 s  r   r   )r   inliner   r   r   r   r      r   )block_elementsinline_elements	itertoolschainr   __all__r   add_element)selfelr   r   r   __init__   s   zParser.__init__elementElementTypec                 C  sD   i }t |tjr| j}nt |tjr| j}ntd||| < dS )zAdd an element to the parser.

        :param element: the element class.

        .. note:: If one needs to call it inside ``__init__()``, please call it after
             ``super().__init__()`` is called.
        zMThe element should be a subclass of either `BlockElement` or `InlineElement`.N)	
issubclassr   InlineElementr   r   BlockElementr   	TypeErrorget_type)r   r    destr   r   r   r   #   s   zParser.add_elementtextstrblock.Documentc                 C  sj   t |}| |_ttj| jd  }|| | ||_| 	|| W d   |S 1 s.w   Y  |S )zDo the actual parsing and returns an AST or parsed element.

        :param text: the text to parse.
        :returns: the parsed root element
        DocumentN)
r   parserr   r   r+   r   under_stateparse_sourcechildrenparse_inline)r   r(   sourcedocr   r   r   parse7   s   
zParser.parser1   r   list[block.BlockElement]c                 C  s`   |   }g }|js.|D ]}||r'||}t|ds ||}||  nq	 |S |jr	|S )z/Parse the source into a list of block elements.priority)_build_block_element_list	exhaustedmatchr3   hasattrappend)r   r1   element_listastele_typeresultr   r   r   r.   E   s   



zParser.parse_sourceblock.BlockElementc                 C  sH   |j r| |j ||_d|_ dS |jD ]}t|tjr!| || qdS )z^Inline parsing is postponed so that all link references
        are seen before that.
         N)inline_body_parse_inliner/   
isinstancer   r$   r0   )r   r    r1   childr   r   r   r0   Y   s   

zParser.parse_inlinelist[inline.InlineElement]c                 C  s    |   }tj||| jd |dS )a   Parses text into inline elements.
        RawText is not considered in parsing but created as a wrapper of holes
        that don't match any other elements.

        :param text: the text to be parsed.
        :returns: a list of inline elements.
        RawText)fallbackr1   )_build_inline_element_listinline_parserr3   r   )r   r(   r1   r;   r   r   r   rB   f   s   zParser._parse_inlinelist[BlockElementType]c                 C  s"   t dd | j D dd ddS )zIReturn a list of block elements, ordered from highest priority to lowest.c                 s  s    | ]}|j s|V  qd S r   virtualr   er   r   r   r   v   r   z3Parser._build_block_element_list.<locals>.<genexpr>c                 S  s   | j S r   )r5   )rN   r   r   r   <lambda>w   s    z2Parser._build_block_element_list.<locals>.<lambda>T)keyreverse)sortedr   valuesr   r   r   r   r6   s   s
   z Parser._build_block_element_listlist[InlineElementType]c                 C  s   dd | j  D S )zcReturn a list of elements, each item is a list of elements
        with the same priority.
        c                 S  s   g | ]}|j s|qS r   rK   rM   r   r   r   
<listcomp>   s    z5Parser._build_inline_element_list.<locals>.<listcomp>)r   rS   rT   r   r   r   rH   {   s   z!Parser._build_inline_element_listN)r	   r
   )r    r!   r	   r
   )r(   r)   r	   r*   )r1   r   r	   r4   )r    r?   r1   r   r	   r
   )r(   r)   r1   r   r	   rE   )r	   rJ   )r	   rU   )__name__
__module____qualname____doc__r   r   r3   r.   r0   rB   r6   rH   r   r   r   r   r      s    







r   )r   r    r   rI   )rZ   
__future__r   r   typingr   r   r   r1   r   r   r@   r   r    r   rI   r$   BlockElementTyper#   InlineElementTypeElementr!   r   r   r   r   <module>   s    u

