o
    g÷§iÂ  ã                   @   sB   d Z ddlZddlmZ ddlmZ ddlmZ G dd„ dƒZdS )uw  
A parser is a software component that takes input data (frequently text) and builds a data structure
â€“ often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural
representation of the input while checking for correct syntax.
The parsing may be preceded or followed by other steps, or these may be combined into a single step.
é    N)ÚToken)Ú	TokenType)Ú	Tokenizerc                   @   sB   e Zd ZdZededeje fdd„ƒZ	ededefdd„ƒZ
d	S )
ÚParseru‹  
    A parser is a software component that takes input data (frequently text) and builds a data structure
    â€“ often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural
    representation of the input while checking for correct syntax.
    The parsing may be preceded or followed by other steps, or these may be combined into a single step.
    Úinfix_expressionÚreturnc                 C   sô  t  | ¡}g }g }t|ƒD ]O\}}| ¡ tjkr| |¡ q| ¡ tjkr,| |¡ q| ¡ tjkr9| |¡ q| ¡ tj	kr¨t
|ƒdkr¢|d  ¡ tjkr¢|d  ¡ pVd| ¡ p[dksk|d  ¡ | ¡ kr¢| ¡ r¢| |d ¡ | d¡ t
|ƒdkr¢|d  ¡ tjkr¢|d  ¡ pd| ¡ p’dksk|d  ¡ | ¡ kr¢| ¡ sk| |¡ q| ¡ tjkrÚt
|ƒdkrÙ|d  ¡ tjkrÙ| |d ¡ | d¡ t
|ƒdkrÙ|d  ¡ tjks¾q| ¡ tjkrç| |¡ q| ¡ tjkr]t
|ƒdks÷J ‚t
|ƒdkr%|d  ¡ tjkr%| |d ¡ | d¡ t
|ƒdkr%|d  ¡ tjkst
|ƒdks.J ‚|d  ¡ tjks:J ‚| d¡ t
|ƒdkr\|d  ¡ tjkr\| |d ¡ | d¡ qqt
|ƒdkrx| |d ¡ | d¡ t
|ƒdkse|S )Nr   éÿÿÿÿ)r   ÚtokenizeÚ	enumerateÚget_typer   ÚNUMBERÚappendÚVARIABLEÚFUNCTIONÚOPERATORÚlenÚLEFT_PARENTHESISÚget_precedenceÚget_is_left_associativeÚpopÚCOMMAÚRIGHT_PARENTHESIS)r   ÚtokensÚpostfixÚ	operatorsÚiÚt© r   úZ/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/equation/parser.pyÚ_to_postfix    s†   




ÿÿ
ô
ÿÿ

ü

ü

ï
þzParser._to_postfixÚsc                 C   s<  g }t  | ¡}t|ƒD ]†\}}| ¡ tjkr| |¡ q| ¡ tjkr)| |¡ q| ¡ tjkr]| 	¡ }|dus:J ‚t
|ƒ|ksBJ ‚td|ƒD ]}|j |d ¡ | d¡ qG| |¡ q| ¡ tjkr‘| 	¡ }|dusnJ ‚t
|ƒ|ksvJ ‚td|ƒD ]}|j |d ¡ | d¡ q{| |¡ qqt
|ƒdksšJ ‚|d S )z±
        This function converts a str to an abstract syntax tree
        :param s:   the input str
        :return:    a Token, representing the root of the syntax tree
        Nr   r   é   )r   r   r
   r   r   r   r   r   r   Úget_number_of_argumentsr   ÚrangeÚ	_childrenr   r   )r    Úargsr   r   r   Úop_number_of_argsÚ_Úfn_number_of_argsr   r   r   Úto_abstract_syntax_treem   s<   




÷zParser.to_abstract_syntax_treeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstaticmethodÚstrÚtypingÚListr   r   r)   r   r   r   r   r      s    Lr   )r-   r0   Ú%borb.pdf.canvas.layout.equation.tokenr   r   Ú)borb.pdf.canvas.layout.equation.tokenizerr   r   r   r   r   r   Ú<module>   s   