o
    GZhN                     @   s|   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ edZ	G dd dZ
e	dur0e
 Zdefd	d
ZdddZdS )    N)Path)import_module)TransformToSymPyExprZlarkc                   @   s(   e Zd ZdZd
ddZdef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 u rtt j|djdd}nt|jdd}tj||ddddd	d	d
d	| _	|| _
|| _|d u rDt | _d S | | _d S )Nzgrammar/z
latex.larkzutf-8)encodingZearleyZlatex_stringautoZexplicitFT)source_pathparserstartlexerZ	ambiguityZpropagate_positionsZmaybe_placeholdersZkeep_all_tokens)ospathjoindirname__file__r   	read_text_larkZLarkr	   print_debug_outputtransform_exprr   transformer)selfr   	transformZgrammar_filer   Zgrammar_dir_pathZlatex_grammar r   T/var/www/auris/lib/python3.10/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s*tj	d| tj	| tj	|
  |S | j r<tj	d| tj	|
  | j|}| j rLt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   doparseF   s   zLarkLaTeXParser.doparse)FTNN)__name__
__module____qualname____doc__r   strr#   r   r   r   r   r      s    
r   r   c                 C   s   t du 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_larke   s   
r+   Tc                    s   t | tjr	| jS t| j}|d}|rtdd|}|dk}|r'|d  n| d}| p/}|r:|t|d 7 }|rO|dd	 fd	d
| j
D  7 }n|d	 fdd
| j
D 7 }|re|d7 }|S )NZ
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   r2   r4   r7   r   r   r:      s    ))
isinstancer   Tokenvaluer(   data
startswithresubr   children)treeindentr9   r@   Zis_exprZis_ambigoutputZ	show_noder   r7   r   r3   q   s(   



& r3   )r   T)r   r   rB   pathlibr   Zsympy.externalr   Z$sympy.parsing.latex.lark.transformerr   r   r   r*   r(   r+   r3   r   r   r   r   <module>   s    U