U
    ZÊ¹ji  ã                   @   sp   d dl Z d dlZd dlZd dlmZ d dlmZ edƒZG dd„ dƒZedk	rTeƒ Z	e
dœdd	„Zddd„ZdS )é    N)Úimport_module)ÚTransformToSymPyExprZlarkc                   @   s(   e Zd ZdZd
dd„Zedœdd	„ZdS )ÚLarkLaTeXParseraF  Class for converting input `\mathrm{\LaTeX}` strings into SymPy Expressions.
    It holds all the necessary internal data for doing so, and exposes hooks for
    customizing its behavior.

    Parameters
    ==========

    print_debug_output : bool, optional

        If set to ``True``, prints debug output to the logger. Defaults to ``False``.

    transform : bool, optional

        If set to ``True``, the class runs the Transformer class on the parse tree
        generated by running ``Lark.parse`` on the input string. Defaults to ``True``.

        Setting it to ``False`` can help with debugging the `\mathrm{\LaTeX}` grammar.

    grammar_file : str, optional

        The path to the grammar file that the parser should use. If set to ``None``,
        it uses the default grammar, which is in ``grammar/latex.lark``, relative to
        the ``sympy/parsing/latex/lark/`` directory.

    transformer : str, optional

        The name of the Transformer class to use. If set to ``None``, it uses the
        default transformer class, which is :py:func:`TransformToSymPyExpr`.

    FTNc                 C   s²   t j t j t¡d¡}|d krJtt j |d¡dd��}| ¡ }W 5 Q R X n t|dd��}| ¡ }W 5 Q R X tj||ddddd	d	d
d�	| _	|| _
|| _|d kr¦tƒ | _n|ƒ | _d S )Nzgrammar/z
latex.larkzutf-8)ÚencodingZearleyZlatex_stringÚautoÚexplicitFT)Úsource_pathÚparserÚstartÚlexerZ	ambiguityZpropagate_positionsZmaybe_placeholdersZkeep_all_tokens)ÚosÚpathÚjoinÚdirnameÚ__file__ÚopenÚreadÚ_larkZLarkr	   Úprint_debug_outputÚtransform_exprr   Útransformer)Úselfr   Ú	transformZgrammar_filer   Zgrammar_dir_pathÚfZlatex_grammar© r   úd/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/parsing/latex/lark/latex_parser.pyÚ__init__*   s,    ÷
zLarkLaTeXParser.__init__©Úsc                 C   sœ   | j rtj tj¡ | j |¡}| jsTtj 	d|¡ tj 	|¡ tj 	| 
¡ ¡ |S | j rxtj 	d|¡ tj 	| 
¡ ¡ | j |¡}| j r˜tj 	d|¡ |S )Nzexpression = %szSymPy expression = %s)r   r   ÚloggerÚsetLevelÚloggingÚDEBUGr	   Úparser   ÚdebugÚprettyr   r   )r   r   Z
parse_treeZsympy_expressionr   r   r   ÚdoparseG   s    zLarkLaTeXParser.doparse)FTNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr&   r   r   r   r   r      s   
r   r   c                 C   s   t dkrtdƒ‚t | ¡S )z™
    Experimental LaTeX parser using Lark.

    This function is still under development and its API may change with the
    next releases of SymPy.
    NzLark is probably not installed)r   ÚImportErrorÚ_lark_latex_parserr&   r   r   r   r   Úparse_latex_larkf   s    r.   Tc                    sÎ   t | tjƒr| jS t| jƒ}| d¡}|r8t dd|¡}|dk}|rN|d ‰ n|‰ d}| p^ˆ}|rt|t|ƒd 7 }|rž|dd 	‡ ‡fd	d
„| j
D ƒ¡ 7 }n |d 	‡ ‡fdd
„| j
D ƒ¡7 }|rÊ|d7 }|S )NÚ
expressionz^expressionÚEZ_ambigé   Ú ú(Ú
c                    s    g | ]}d ˆ  t |ˆ ˆƒ ‘qS )ú ©Ú_pretty_print_lark_trees©Ú.0Úi©Ú
new_indentÚ	show_exprr   r   Ú
<listcomp>‹   s     z,_pretty_print_lark_trees.<locals>.<listcomp>ú,c                    s   g | ]}t |ˆ ˆƒ‘qS r   r6   r8   r;   r   r   r>   �   s     ú))Ú
isinstancer   ÚTokenÚvaluer+   ÚdataÚ
startswithÚreÚsubr   Úchildren)ÚtreeÚindentr=   rD   Zis_exprZis_ambigÚoutputZ	show_noder   r;   r   r7   r   s(    



& r7   )r   T)r   r!   rF   Zsympy.externalr   Z$sympy.parsing.latex.lark.transformerr   r   r   r-   r+   r.   r7   r   r   r   r   Ú<module>   s   W