U
    ڲg:                     @   s<   d Z ddlZG dd deZG dd dZG dd dZdS )	z
This module contains a tokenizer for Excel formulae.

The tokenizer is based on the Javascript tokenizer found at
http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html written by Eric
Bachtal
    Nc                   @   s   e Zd ZdZdS )TokenizerErrorz$Base class for all Tokenizer errors.N)__name__
__module____qualname____doc__ r   r   >/tmp/pip-unpacked-wheel-dtlj1ams/openpyxl/formula/tokenizer.pyr      s   r   c                   @   s   e Zd ZdZedZedZededdZdZ	dZ
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd'd d!Zd"d# Zd$d% Zd&S )(	Tokenizera^  
    A tokenizer for Excel worksheet formulae.

    Converts a str string representing an Excel formula (in A1 notation)
    into a sequence of `Token` objects.

    `formula`: The str string to tokenize

    Tokenizer defines a method `._parse()` to parse the formula into tokens,
    which can then be accessed through the `.items` attribute.

    z^[1-9](\.[0-9]+)?[Ee]$z[ \n]+z"(?:[^"]*"")*[^"]*"(?!")z'(?:[^']*'')*[^']*'(?!')"')z#NULL!z#DIV/0!z#VALUE!z#REF!z#NAME?z#NUM!z#N/Az#GETTING_DATAz,;}) +-*/^&=><%c                 C   s*   || _ g | _g | _d| _g | _|   d S )Nr   )formulaitemstoken_stackoffsettoken_parse)selfr   r   r   r   __init__.   s    zTokenizer.__init__c              
   C   s>  | j r
dS | jsdS | jd dkr2|  j d7  _ n| jt| jtj dS d| jfd| jfd| jfd| j	fd	| j	fd
| j
fd| jfd| jfd| jff	}i }|D ]\}}|t|| q| j t| jk r2|  rq| j| j  }|| jkr|   ||kr|  j ||  7  _ q| j| |  j d7  _ q|   dS )z5Populate self.items with the tokens from the formula.Nr   =   z"'[# 
z
+-*/^&=><%z{()}z;,)r   r   r   appendTokenLITERAL_parse_string_parse_brackets_parse_error_parse_whitespace_parse_operator_parse_opener_parse_closer_parse_separatorupdatedictfromkeyslencheck_scientific_notationTOKEN_ENDERS
save_tokenr   )r   Z	consumers
dispatchercharsZconsumer	curr_charr   r   r   r   7   s@    

zTokenizer._parsec                 C   s   | j dd | j| j }|dks$t| j| }|| j| jd }|dkrr|dkrXdnd}td| d	| j |d
}|dkr| j	t
| n| j	| t|S )a  
        Parse a "-delimited string or '-delimited link.

        The offset must be pointing to either a single quote ("'") or double
        quote ('"') character. The strings are parsed according to Excel
        rules where to escape the delimiter you just double it up. E.g.,
        "abc""def" in Excel is parsed as 'abc"def' in Python.

        Returns the number of characters matched. (Does not update
        self.offset)

        :
can_followr
   Nr   stringlinkz%Reached end of formula while parsing z in r   )assert_empty_tokenr   r   AssertionErrorSTRING_REGEXESmatchr   groupr   r   r   make_operandr   r*   )r   delimregexr9   subtyper   r   r   r   _   s    

zTokenizer._parse_stringc                 C   s   | j | j dkstdd td| j | jd D }dd td| j | jd D }d}t|| D ]F\}}||7 }|dkrh|d	 }| j| j | j| j|   |  S qhtd
| j  dS )z
        Consume all the text between square brackets [].

        Returns the number of characters matched. (Does not update
        self.offset)

        r   c                 S   s   g | ]}|  d fqS )r   start.0tr   r   r   
<listcomp>   s     z-Tokenizer._parse_brackets.<locals>.<listcomp>z\[Nc                 S   s   g | ]}|  d fqS )r?   rA   r   r   r   rD      s     z\]r   r   zEncountered unmatched '[' in )	r   r   r7   refinditersortedr   r   r   )r   ZleftsZrightsZ
open_countidxZ
open_closeZouter_rightr   r   r   r    {   s"    
zTokenizer._parse_bracketsc                 C   s   | j dd | j| j dks t| j| jd }| jD ]D}||r6| jt	d
| j|  | jdd= t|  S q6td| j d| j ddS )	z
        Consume the text following a '#' as an error.

        Looks for a match in self.ERROR_CODES and returns the number of
        characters matched. (Does not update self.offset)

        !r2   r   N zInvalid error code at position  in 'r   )r6   r   r   r7   ERROR_CODES
startswithr   r   r   r;   joinr   r*   r   )r   Z
subformulaerrr   r   r   r!      s    

zTokenizer._parse_errorc                 C   sL   | j | j dkst| jt| j | j tj | j| j | jd 	 S )z
        Consume a string of consecutive spaces.

        Returns the number of spaces found. (Does not update self.offset).

        )r   r   N)
r   r   r7   r   r   r   WSPACE	WSPACE_REr9   endr   r   r   r   r"      s    zTokenizer._parse_whitespacec                 C   s  | j | j| jd  dkrD| jt| j | j| jd  tj dS | j | j }|dks\t|dkrrtdtj}n|dkrt|tj}nt| jst|tj}n`t	dd t
| jD d}|o|jtjkp|jtjkp|jtjk}|rt|tj}nt|tj}| j| d	S )
z
        Consume the characters constituting an operator.

        Returns the number of characters consumed. (Does not update
        self.offset)

           )z>=z<=z<>z
%*/^&=><+-%z*/^&=><c                 s   s   | ]}|j tjkr|V  qd S N)typer   rQ   )rB   ir   r   r   	<genexpr>   s    z,Tokenizer._parse_operator.<locals>.<genexpr>Nr   )r   r   r   r   r   OP_INr7   OP_POSTOP_PREnextreversedr>   CLOSErX   OPERAND)r   r0   r   prevZis_infixr   r   r   r#      s8    

zTokenizer._parse_operatorc                 C   s   | j | j dkst| j | j dkr8|   td}n8| jrfd| jd }| jdd= t|}n
td}| j	| | j
	| dS )z
        Consumes a ( or { character.

        Returns the number of characters consumed. (Does not update
        self.offset)

        )({rd   rK   rc   Nr   )r   r   r7   r6   r   make_subexpr   rO   r   r   r   )r   r   Ztoken_valuer   r   r   r$      s    
zTokenizer._parse_openerc                 C   sR   | j | j dkst| j  }|j| j | j krBtd| j  | j	| dS )z
        Consumes a } or ) character.

        Returns the number of characters consumed. (Does not update
        self.offset)

        ))}zMismatched ( and { pair in '%s'r   )
r   r   r7   r   pop
get_closervaluer   r   r   )r   r   r   r   r   r%      s    zTokenizer._parse_closerc                 C   s   | j | j }|dkst|dkr,td}nTz| jd j}W n  tk
r\   tdtj}Y n$X |tj	krvtdtj}n
td}| j
| dS )z
        Consumes a ; or , character.

        Returns the number of characters consumed. (Does not update
        self.offset)

        );,rk   rE   rl   r   )r   r   r7   r   make_separatorr   rX   
IndexErrorr[   PARENr   r   )r   r0   r   Ztop_typer   r   r   r&      s    

zTokenizer._parse_separatorc                 C   sX   | j | j }|dkrTt| jdkrT| jd| jrT| j| |  jd7  _dS dS )z
        Consumes a + or - character if part of a number in sci. notation.

        Returns True if the character was consumed and self.offset was
        updated, False otherwise.

        z+-r   rK   TF)r   r   r*   r   SN_REr9   rO   r   )r   r0   r   r   r   r+     s    z#Tokenizer.check_scientific_notationr   c                 C   s2   | j r.| j d |kr.td| j d| j ddS )a:  
        Ensure that there's no token currently being parsed.

        Or if there is a token being parsed, it must end with a character in
        can_follow.

        If there are unconsumed token contents, it means we hit an unexpected
        token transition. In this case, we raise a TokenizerError

        rE   z!Unexpected character at position rL   r   N)r   r   r   r   )r   r3   r   r   r   r6   '  s    zTokenizer.assert_empty_tokenc                 C   s0   | j r,| jtd| j  | j dd= dS )z9If there's a token being parsed, add it to the item list.rK   N)r   r   r   r   r;   rO   rT   r   r   r   r-   5  s    zTokenizer.save_tokenc                 C   sB   | j s
dS | j d jtjkr(| j d jS dddd | j D  S )z+Convert the parsed tokens back to a string.rK   r   r   c                 s   s   | ]}|j V  qd S rW   )rj   )rB   r   r   r   r   rZ   A  s     z#Tokenizer.render.<locals>.<genexpr>)r   rX   r   r   rj   rO   rT   r   r   r   render;  s
    zTokenizer.renderN)r   )r   r   r   r   rF   compilerp   rR   r8   rM   r,   r   r   r   r    r!   r"   r#   r$   r%   r&   r+   r6   r-   rq   r   r   r   r   r	      s,   

	(&
r	   c                   @   s   e Zd ZdZdddgZdZdZdZdZd	Z	d
Z
dZdZdZdZd'ddZdZdZdZdZdZdd Zedd ZdZdZed(ddZd d! Zd"Zd#Zed$d% Zd&S ))r   a)  
    A token in an Excel formula.

    Tokens have three attributes:

    * `value`: The string value parsed that led to this token
    * `type`: A string identifying the type of token
    * `subtype`: A string identifying subtype of the token (optional, and
                 defaults to "")

    rj   rX   r>   r   ra   FUNCARRAYro   SEPzOPERATOR-PREFIXzOPERATOR-INFIXzOPERATOR-POSTFIXzWHITE-SPACErK   c                 C   s   || _ || _|| _d S rW   )rj   rX   r>   )r   rj   type_r>   r   r   r   r   _  s    zToken.__init__TEXTNUMBERLOGICALERRORRANGEc                 C   s   d | j| j| jS )Nz{0} {1} {2}:)formatrX   r>   rj   rT   r   r   r   __repr__q  s    zToken.__repr__c                 C   sp   | dr| j}nP| dr$| j}n>|dkr4| j}n.zt| | j}W n tk
r`   | j}Y nX | || j|S )zCreate an operand token.r   r   )TRUEFALSE)	rN   rw   rz   ry   floatrx   
ValueErrorr{   ra   clsrj   r>   r   r   r   r;   t  s    


zToken.make_operandOPENr`   Fc                 C   sr   |d dkst |r,td|s$t tj}n&|dkr<tj}n|dkrLtj}ntj}|dkr`| jn| j}| |||S )z
        Create a subexpression token.

        `value`: The value of the token
        `func`: If True, force the token to be of type FUNC

        rE   )rd   rg   rc   rf   z.+\(|\)z{}z()r   )	r7   rF   r9   r   rs   rt   ro   r`   r   )r   rj   funcrv   r>   r   r   r   re     s    	zToken.make_subexpc                 C   sT   | j | j| j| jfkst| j| jks*t| j | jkr:dnd}| j|| j | jkdS )z6Return a closing token that matches this token's type.rg   rf   )r   )rX   rs   rt   ro   r7   r>   r   re   )r   rj   r   r   r   ri     s    zToken.get_closerARGROWc                 C   s.   |dkst |dkr| jn| j}| || j|S )zCreate a separator token)rl   rk   rl   )r7   r   r   ru   r   r   r   r   rm     s    zToken.make_separatorN)rK   )F)r   r   r   r   	__slots__r   ra   rs   rt   ro   ru   r]   r[   r\   rQ   r   rw   rx   ry   rz   r{   r}   classmethodr;   r   r`   re   ri   r   r   rm   r   r   r   r   r   D  s<   


r   )r   rF   	Exceptionr   r	   r   r   r   r   r   <module>   s     6