a
    di                     @   s  d dl mZmZ d dlZd dlZ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mZmZmZmZmZmZmZ erddlmZ ddlmZ ddlmZ ejdkrd d	lmZ nd d	l mZ dd
l!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m+Z+m,Z,m-Z-m.Z.m/Z/ ddlm0Z0 ddl1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZD d dlEZEzd dlFZFW n eGy   dZFY n0 G dd deZHG dd de&ZIh dZJdZKdZLe	dddZMG dd de&ZNdS )    )ABCabstractmethodN)TypeVarTypeListDictIteratorCallableUnionOptionalSequenceTupleIterableIOAnyTYPE_CHECKING
Collection   )InteractiveParser)	ParseTree)Transformer)      )Literal)ConfigurationErrorassert_configUnexpectedInput)	SerializeSerializeMemoizerFSisasciilogger)load_grammarFromPackageLoaderGrammarverify_used_filesPackageResource)Tree)	LexerConf
ParserConf_ParserArgType_LexerArgType)Lexer
BasicLexerTerminalDefLexerThreadToken)ParseTreeBuilder)_validate_frontend_args_get_lexer_callbacks_deserialize_parsing_frontend_construct_parsing_frontend)Rulec                   @   s:   e Zd ZU eee ee dddZdZee	 e
d< dS )PostLex)streamreturnc                 C   s   |S N )selfr8   r;   r;   R/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lark/lark.pyprocess'   s    zPostLex.processr;   always_acceptN)__name__
__module____qualname__r   r   r0   r>   r?   r   str__annotations__r;   r;   r;   r=   r7   &   s   
r7   c                   @   sj  e Zd ZU dZee ed< eed< ded< eeef ed< eed< eeef ed< eed	< e	ed
< eed< e
ed< eed< eed< ded< ee ed< ded< eeeegef f ed< eed< eeegef  ed< ded< ee ed< dZeree7 Zdddddddddddddi ddd dg di d!Zeee
f ed"< d#d$ Zd%d& Zd'd( Zd)d* Zed+d, ZdS )-LarkOptionsz$Specifies the options for Lark

    startdebugzOptional[Transformer]transformerpropagate_positionsmaybe_placeholderscacheregexg_regex_flagskeep_all_tokens
tree_classparserlexerz0Literal["auto", "resolve", "explicit", "forest"]	ambiguitypostlexz-Optional[Literal["auto", "normal", "invert"]]prioritylexer_callbacks	use_bytesedit_terminalszUList[Union[str, Callable[[Union[None, str, PackageResource], str], Tuple[str, str]]]]import_pathssource_pathaw  
    **===  General Options  ===**

    start
            The start symbol. Either a string, or a list of strings for multiple possible starts (Default: "start")
    debug
            Display debug information and extra warnings. Use only when debugging (Default: ``False``)
            When used with Earley, it generates a forest graph as "sppf.png", if 'dot' is installed.
    transformer
            Applies the transformer to every parse tree (equivalent to applying it after the parse, but faster)
    propagate_positions
            Propagates (line, column, end_line, end_column) attributes into all tree branches.
            Accepts ``False``, ``True``, or a callable, which will filter which nodes to ignore when propagating.
    maybe_placeholders
            When ``True``, the ``[]`` operator returns ``None`` when not matched.
            When ``False``,  ``[]`` behaves like the ``?`` operator, and returns no value at all.
            (default= ``True``)
    cache
            Cache the results of the Lark grammar analysis, for x2 to x3 faster loading. LALR only for now.

            - When ``False``, does nothing (default)
            - When ``True``, caches to a temporary file in the local directory
            - When given a string, caches to the path pointed by the string
    regex
            When True, uses the ``regex`` module instead of the stdlib ``re``.
    g_regex_flags
            Flags that are applied to all terminals (both regex and strings)
    keep_all_tokens
            Prevent the tree builder from automagically removing "punctuation" tokens (Default: ``False``)
    tree_class
            Lark will produce trees comprised of instances of this class instead of the default ``lark.Tree``.

    **=== Algorithm Options ===**

    parser
            Decides which parser engine to use. Accepts "earley" or "lalr". (Default: "earley").
            (there is also a "cyk" option for legacy)
    lexer
            Decides whether or not to use a lexer stage

            - "auto" (default): Choose for me based on the parser
            - "basic": Use a basic lexer
            - "contextual": Stronger lexer (only works with parser="lalr")
            - "dynamic": Flexible and powerful (only with parser="earley")
            - "dynamic_complete": Same as dynamic, but tries *every* variation of tokenizing possible.
    ambiguity
            Decides how to handle ambiguity in the parse. Only relevant if parser="earley"

            - "resolve": The parser will automatically choose the simplest derivation
              (it chooses consistently: greedy for tokens, non-greedy for rules)
            - "explicit": The parser will return all derivations wrapped in "_ambig" tree nodes (i.e. a forest).
            - "forest": The parser will return the root of the shared packed parse forest.

    **=== Misc. / Domain Specific Options ===**

    postlex
            Lexer post-processing (Default: ``None``) Only works with the basic and contextual lexers.
    priority
            How priorities should be evaluated - "auto", ``None``, "normal", "invert" (Default: "auto")
    lexer_callbacks
            Dictionary of callbacks for the lexer. May alter tokens during lexing. Use with caution.
    use_bytes
            Accept an input of type ``bytes`` instead of ``str``.
    edit_terminals
            A callback for editing the terminals before parse.
    import_paths
            A List of either paths or loader functions to specify from where grammars are imported
    source_path
            Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading
    **=== End of Options ===**
    FNearleyautoTr   )rG   rN   rO   rK   rS   rP   rQ   rH   rF   rT   rR   rL   rI   rU   rJ   rW   rM   rV   rX   rY   _plugins	_defaultsc                 C   s   t |}i }| j D ]B\}}||v rL||}t|trP|dvrPt|}n|}|||< qt|d trv|d g|d< || jd< t| j	d | j	dkr| j
rtd|rtd|  d S )N)rK   rV   rI   rF   options)rZ   lalrcykNrZ   zCannot specify an embedded transformer when using the Earley algorithm. Please use your transformer on the resulting parse tree, or use a different algorithm (i.e. LALR)zUnknown options: %s)dictr]   itemspop
isinstanceboolrC   __dict__r   rP   rH   r   keys)r<   Zoptions_dictor^   namedefaultvaluer;   r;   r=   __init__   s"    



zLarkOptions.__init__c              
   C   sB   z| j d | W S  ty< } zt|W Y d }~n
d }~0 0 d S )Nr^   )rf   KeyErrorAttributeError)r<   ri   er;   r;   r=   __getattr__   s    zLarkOptions.__getattr__c                 C   s    t || j d || j|< d S )Nz,%r isn't a valid option. Expected one of: %s)r   r^   rg   )r<   ri   rk   r;   r;   r=   __setattr__   s    zLarkOptions.__setattr__c                 C   s   | j S r:   r^   )r<   memor;   r;   r=   	serialize   s    zLarkOptions.serializec                 C   s   | |S r:   r;   )clsdatars   r;   r;   r=   deserialize   s    zLarkOptions.deserialize)r@   rA   rB   __doc__r   rC   rD   re   r
   intr   r*   r+   r   r7   r   r	   r0   r.   OPTIONS_DOCr]   rl   rp   rq   rt   classmethodrw   r;   r;   r;   r=   rE   -   sh   
G
rE   >
   rL   rI   rH   rV   rG   rM   r\   rO   rU   rS   )r[   normalinvertN)r[   resolveexplicitforest_TLark)boundc                   @   sn  e Zd ZU dZeed< eed< ded< eed< eed< ee	 ed< d	d
dddZ
erdedej 7 ZdZd<ddZdd Zdd Zd=ee dddZedd Zdd Zdd  Zed!d" Zed>ee eee ed#d$d%Zed&gfee eed'ed(d)d*Zd+d, Zd?eeee d-d.d/Z ee	d0d1d2Z!d@ee ee d3d4d5d6Z"dAeee d7d8d9d:d;Z#d
S )Br   a}  Main interface for the library.

    It's mostly a thin wrapper for the many different parsers, and for the tree constructor.

    Parameters:
        grammar: a string or file-object containing the grammar spec (using Lark's ebnf syntax)
        options: a dictionary controlling various aspects of Lark.

    Example:
        >>> Lark(r'''start: "foo" ''')
        Lark(...)
    rY   source_grammarr$   grammarr^   rQ   	terminalszUnion[Grammar, str, IO[str]]N)r   r9   c              	      s  t || _| jj}|r*tr t}q.tdnt}| jjd u rbz|j| _W ql ty^   d| _Y ql0 n
| jj| _z
|j}W n ty   Y n0 | }d }d }t	|t
r|| _| jjrt|std| jjr| jjdkrtdd d fdd	| D }d
dlm}	 || |	 t
tjd d  }
t|
d }t	| jjt
rX| jj}n8| jjdurntdt d|gtjd d R   }t|rt d| t!|t" D ]}||= qt#|d}| j}zd|$ %d}t&'|}||dkr8t(|r8t&'|}| j)|fi | W W d    d S W n( t*yb   t+d|  || _Y n0 W d    n1 sz0    Y  t,|| j| jj-| jj.\| _/}nt	|t0sJ || _/| jj1dkrD| jjdkrd| j_1nd| jjdkr| jj2d urt3d d| j_1nd| j_1n*| jjdkr2d| j_1ndsDJ | jj| jj1}t	|t4rjt5|t6sJ n*t7|d | jj2d urd|v rtd| jj8dkr| jjdkrd| j_8nt7| jjd d! | jj9dkrd"| j_9| jj9t:vrtd#| jj9t:f | jj8t;vr$td$| jj8t;f | jjd u r8d%}n$| jj2d urVt!| jj2j<}nt! }| j/=| jj>|\| _?| _@| _A| jjBr| j?D ]}| jB| qd&d' | j?D | _C| jj9d(kr| j@D ]"}|jj9d ur|jj9 |j_9q| j?D ]}|j9 |_9qnF| jj9d u rH| j@D ]}|jj9d urd |j_9q| j?D ]}d)|_9q:tD| j?|| jA| jj2| jjE| jjF| jjd*| _G| jjr| H | _n|r| I | _1|rt d+| t#|d,J}|d usJ |J|dd  t&K|| | L|t" W d    n1 s
0    Y  d S )-Nz?`regex` module must be installed if calling `Lark(regex=True)`.z<string>z/Grammar must be ascii only, when use_bytes=Truer_   z+cache only works with parser='lalr' for now)rH   rS   rU   rW   r\    c                 3   s&   | ]\}}| vr|t | V  qd S r:   )rC   ).0kvZ
unhashabler;   r=   	<genexpr>)      z Lark.__init__.<locals>.<genexpr>r   )__version__   utf8Tz"cache argument must be bool or strz/.lark_cache_%s_%s_%s.tmpzLoading grammar from cache: %srb   
z<Failed to load Lark from cache: %r. We will try to carry on.r[   
contextualrZ   z~postlex can't be used with the dynamic lexer, so we use 'basic' instead. Consider using lalr with contextual instead of earleybasicdynamicr`   F)r   r   r   Zdynamic_completezGCan't use postlex with a dynamic lexer. Use basic or contextual insteadr~   )rZ   r`   zG%r doesn't support disambiguation. Use one of these parsers instead: %sr|   z.invalid priority option: %r. Must be one of %rz/invalid ambiguity option: %r. Must be one of %r*c                 S   s   i | ]}|j |qS r;   ri   r   tr;   r;   r=   
<dictcomp>  r   z!Lark.__init__.<locals>.<dictcomp>r}   r   )rV   zSaving grammar to cache: %swb)MrE   r^   rL   ImportErrorrerY   ri   rn   readrd   rC   r   rV   r    r   rK   rP   joinrb   r   r   sysversion_infohashlibmd5encode	hexdigesttempfile
gettempdirr   existsr!   rG   set_LOAD_ALLOWED_OPTIONSopenreadlinerstrippickleloadr%   _load	Exception	exceptionr"   rX   rN   r   r$   rQ   rS   infotype
issubclassr,   r   rR   rT   _VALID_PRIORITY_OPTIONS_VALID_AMBIGUITY_OPTIONSr?   compilerF   r   rulesZignore_tokensrW   _terminals_dictr(   rU   rM   
lexer_conf_build_parser_build_lexerwritedumpsave)r<   r   r^   Z	use_regex	re_moduler   Zcache_fnZ	cache_md5options_strr   sri   fZold_optionsZfile_md5Zcached_used_filesZcached_parser_dataZ
used_filesrQ   Zterminals_to_keepr   ZruleZtermr;   r   r=   rl      s    





"


, 















zLark.__init__z

)rP   r   r^   Fc                 C   s,   | j }|r$ddlm} ||}d|_t|S )Nr   )copyr;   )r   r   ignorer-   )r<   dont_ignorer   r   r;   r;   r=   r     s    zLark._build_lexerc                 C   sx   i | _ | jjdkr\t| j| jjp"t| jj| jjdko>| jjdk| jj	| _
| j
| jj| _ | j t| jj| j d S )Nr   r_   r   )
_callbacksr^   rR   r1   r   rO   r'   rI   rP   rJ   Z_parse_tree_builderZcreate_callbackrH   updater3   r   r<   r;   r;   r=   _prepare_callbacks  s    
zLark._prepare_callbacksc                 C   sL   |    t| jj| jj t| j| j| jj}t	| jj| jj| j
|| jdS )Nrr   )r   r2   r^   rP   rQ   r)   r   r   rF   r5   r   )r<   Zparser_confr;   r;   r=   r     s    zLark._build_parserr;   exclude_optionsc                    sP   |  ttg\}} r4 fdd|d  D |d< tj||d|tjd dS )zgSaves the instance into the given file object

        Useful for caching and multiprocessing.
        c                    s   i | ]\}}| vr||qS r;   r;   )r   nr   r   r;   r=   r     r   zLark.save.<locals>.<dictcomp>r^   rv   rs   )protocolN)Zmemo_serializer.   r6   rb   r   r   HIGHEST_PROTOCOL)r<   r   r   rv   mr;   r   r=   r     s    z	Lark.savec                 C   s   |  | }||S )zfLoads an instance from the given file object

        Useful for caching and multiprocessing.
        __new__r   )ru   r   instr;   r;   r=   r     s    
z	Lark.loadc                 C   sN   t |d |}|jpi |_|jr&tnt|_|j|_|j|_d|_	|j
|_
|S )Nr   T)r(   rw   rU   	callbacksrL   r   r   rV   rM   Zskip_validationrS   )r<   rv   rs   r^   r   r;   r;   r=   _deserialize_lexer_conf  s    zLark._deserialize_lexer_confc                    s   t |tr|}n
t|}|d }|d }|s2J t|ttdi  t|d }t|t	 tt
j@ r~tdt|t	 || t
| | _ fdd|d D | _d	| _t| jj| jj | |d
  | j| _| jj| _|   dd | jD | _t|d
  | j| j| j| _| S )Nrs   rv   )r6   r.   r^   z6Some options are not allowed when loading a Parser: {}c                    s   g | ]}t | qS r;   )r6   rw   )r   rrs   r;   r=   
<listcomp>  r   zLark._load.<locals>.<listcomp>r   z<deserialized>rP   c                 S   s   i | ]}|j |qS r;   r   r   r;   r;   r=   r     r   zLark._load.<locals>.<dictcomp>)rd   ra   r   r   r   rw   r6   r.   r   r   rE   r]   r   formatr   r^   r   rY   r2   rP   rQ   r   r   r   r   r   r4   r   )r<   r   kwargsdZ	memo_jsonrv   r^   r;   r   r=   r     s:    




z
Lark._loadc                 K   s"   |  | }|j||dfi |S )Nr   r   )ru   rv   rs   r   r   r;   r;   r=   _load_from_dict  s    
zLark._load_from_dict)ru   grammar_filenamerel_tor9   c                 K   s^   |rt j|}t j||}t|dd }| |fi |W  d   S 1 sP0    Y  dS )a&  Create an instance of Lark with the grammar given by its filename

        If ``rel_to`` is provided, the function will find the grammar filename in relation to it.

        Example:

            >>> Lark.open("grammar_file.lark", rel_to=__file__, parser="lalr")
            Lark(...)

        r   )encodingN)ospathdirnamer   r   )ru   r   r   r^   Zbasepathr   r;   r;   r=   r     s
    z	Lark.openr   zSequence[str])ru   packagegrammar_pathsearch_pathsr9   c                 K   sN   t ||}|d|\}}|d| |dg  |d | | |fi |S )ak  Create an instance of Lark with the grammar loaded from within the package `package`.
        This allows grammar loading from zipapps.

        Imports in the grammar will use the `package` and `search_paths` provided, through `FromPackageLoader`

        Example:

            Lark.open_from_package(__name__, "example.lark", ("grammars",), parser=...)
        NrY   rX   )r#   
setdefaultappend)ru   r   r   r   r^   Zpackage_loader	full_pathtextr;   r;   r=   open_from_package&  s    
zLark.open_from_packagec                 C   s   d| j | jj| jjf S )Nz(Lark(open(%r), parser=%r, lexer=%r, ...))rY   r^   rP   rQ   r   r;   r;   r=   __repr__8  s    zLark.__repr__)r   r   r9   c                 C   sP   t | dr|r| |}n| j}t||}|d}| jjrL| jj|S |S )a  Only lex (and postlex) the text, without parsing it. Only relevant when lexer='basic'

        When dont_ignore=True, the lexer will return all tokens, even those marked for %ignore.

        :raises UnexpectedCharacters: In case the lexer cannot find a suitable match.
        rQ   N)	hasattrr   rQ   r/   Z	from_textlexr^   rS   r>   )r<   r   r   rQ   Zlexer_threadr8   r;   r;   r=   r   <  s    
zLark.lex)ri   r9   c                 C   s
   | j | S )z Get information about a terminal)r   )r<   ri   r;   r;   r=   get_terminalM  s    zLark.get_terminalr   )r   rF   r9   c                 C   s   | j j||dS )a-  Start an interactive parsing session.

        Parameters:
            text (str, optional): Text to be parsed. Required for ``resume_parse()``.
            start (str, optional): Start symbol

        Returns:
            A new InteractiveParser instance.

        See Also: ``Lark.parse()``
        )rF   )rP   parse_interactive)r<   r   rF   r;   r;   r=   r   Q  s    zLark.parse_interactivez+Optional[Callable[[UnexpectedInput], bool]]r   )r   rF   on_errorr9   c                 C   s   | j j|||dS )a  Parse the given text, according to the options provided.

        Parameters:
            text (str): Text to be parsed.
            start (str, optional): Required if Lark was given multiple possible start symbols (using the start option).
            on_error (function, optional): if provided, will be called on UnexpectedToken error. Return true to resume parsing.
                LALR only. See examples/advanced/error_handling.py for an example of how to use on_error.

        Returns:
            If a transformer is supplied to ``__init__``, returns whatever is the
            result of the transformation. Otherwise, returns a Tree instance.

        :raises UnexpectedInput: On a parse error, one of these sub-exceptions will rise:
                ``UnexpectedCharacters``, ``UnexpectedToken``, or ``UnexpectedEOF``.
                For convenience, these sub-exceptions also inherit from ``ParserError`` and ``LexerError``.

        )rF   r   )rP   parse)r<   r   rF   r   r;   r;   r=   r   _  s    z
Lark.parse)F)r;   )N)F)NN)NN)$r@   rA   rB   rx   rC   rD   rE   r,   r   r.   rl   rz   Z__serialize_fields__r   r   r   r   r   r{   r   r   r   r   r   r   r   r   r   r   re   r   r0   r   r   r   r   r;   r;   r;   r=   r      s>   
 .



 
 ")Oabcr   r   r   r   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zparsers.lalr_interactive_parserr   treer   Zvisitorsr   r   r   Ztyping_extensions
exceptionsr   r   r   utilsr   r   r   r    r!   r"   r#   r$   r%   r&   r'   commonr(   r)   r*   r+   rQ   r,   r-   r.   r/   r0   Zparse_tree_builderr1   Zparser_frontendsr2   r3   r4   r5   r   r6   r   rL   r   r7   rE   r   r   r   r   r   r;   r;   r;   r=   <module>   s@    D

 5