3
j\=                 @   s   d dgZ ddlZddlZG dd deZG dd deZG dd	 d	eZG d
d deZG dd deZ	G dd de
ZG dd dZG dd deZG dd deZG dd  d edZdS )LexerLexerStateChange    Nc               @   s   e Zd ZdZdd ZdS )LexErrora  
    Exception raised if an invalid character is encountered and no default
    error handler function is defined.  The .text attribute of the exception
    contains all remaining untokenized text. The .error_index is the index
    location of the error.
    c             C   s   |f| _ || _|| _d S )N)argstexterror_index)selfmessager   r    r
   =/u1/h0/jkinne/public_html/cs420-s2019/code/sly-0.4/sly/lex.py__init__.   s    zLexError.__init__N)__name__
__module____qualname____doc__r   r
   r
   r
   r   r   '   s   r   c               @   s   e Zd ZdZdS )PatternErrorzn
    Exception raised if there's some kind of problem with the specified
    regex patterns in the lexer.
    N)r   r   r   r   r
   r
   r
   r   r   3   s   r   c               @   s   e Zd ZdZdS )LexerBuildErrorzN
    Exception raised if there's some sort of problem building the lexer.
    N)r   r   r   r   r
   r
   r
   r   r   :   s   r   c               @   s   e Zd ZdZdddZdS )r   z9
    Exception raised to force a lexing state change
    Nc             C   s   || _ || _d S )N)newstatetok)r   r   r   r
   r
   r   r   D   s    zLexerStateChange.__init__)N)r   r   r   r   r   r
   r
   r
   r   r   @   s   c               @   s   e Zd ZdZd	Zdd ZdS )
Tokenz+
    Representation of a single token.
    typevaluelinenoindexc          	   C   s&   d| j d| jd| j d| j d	S )NzToken(type=z, value=z	, lineno=z, index=))r   r   r   r   )r   r
   r
   r   __repr__M   s    zToken.__repr__N)r   r   r   r   )r   r   r   r   	__slots__r   r
   r
   r
   r   r   H   s   r   c                   s2   e Zd Zed fdd	Zdd Zdd Z  ZS )	TokenStrNc                s   t  j| |}||_||_|S )N)super__new__keyremap)clsr   r    r!   r   )	__class__r
   r   r   Q   s    zTokenStr.__new__c             C   s   | j d k	r|| j | j|f< d S )N)r!   r    )r   r    r   r
   r
   r   __setitem__Y   s    
zTokenStr.__setitem__c             C   s    | j d k	r| j| j | j|f< d S )N)r!   r    )r   r    r
   r
   r   __delitem__^   s    
zTokenStr.__delitem__)NN)r   r   r   staticmethodr   r$   r%   __classcell__r
   r
   )r#   r   r   P   s   r   c               @   s   e Zd Zdd ZdS )_Beforec             C   s   || _ || _d S )N)r   pattern)r   r   r)   r
   r
   r   r   c   s    z_Before.__init__N)r   r   r   r   r
   r
   r
   r   r(   b   s   r(   c                   s@   e Zd ZdZdd Z fddZ fddZ fdd	Z  ZS )
LexerMetaDictzZ
    Special dictionary that prohibits duplicate definitions in lexer specifications.
    c             C   s   i | _ g | _i | _d S )N)beforedeleter!   )r   r
   r
   r   r   k   s    zLexerMetaDict.__init__c                s   t |trt||| j}t |tr>|j| j|< t|j|| j}|| krt |t r| | }t |trt	|rt||_nt
d| dt j|| d S )NzName z
 redefined)
isinstancestrr   r!   r(   r   r+   r)   propertycallableAttributeErrorr   r$   )r   r    r   Zprior)r#   r
   r   r$   p   s    


zLexerMetaDict.__setitem__c                s.   | j j| || kr|j rnt j|S d S )N)r,   appendisupperr   r%   )r   r    )r#   r
   r   r%      s    zLexerMetaDict.__delitem__c                sH   || kr8|j dd j r8|d d dkr8t||| jS t j|S d S )Nignore_   _)splitr3   r   r!   r   __getitem__)r   r    )r#   r
   r   r9      s    *zLexerMetaDict.__getitem__)	r   r   r   r   r   r$   r%   r9   r'   r
   r
   )r#   r   r*   g   s
   r*   c                   s,   e Zd ZdZedd Z fddZ  ZS )	LexerMetaz/
    Metaclass for collecting lexing rules
    c             C   s"   t  }dd }||d< t|d< |S )Nc                s   | f|  fdd}|S )Nc                s:   dj dd  D }t| dr0|d | j | _n|| _| S )N|c             s   s   | ]}d | dV  qdS )(r   Nr
   ).0Zpatr
   r
   r   	<genexpr>   s    zELexerMeta.__prepare__.<locals>._.<locals>.decorate.<locals>.<genexpr>r)   )joinhasattrr)   )funcr)   )patternsr
   r   decorate   s
    
z2LexerMeta.__prepare__.<locals>._.<locals>.decorater
   )r)   ZextrarC   r
   )rB   r   r6      s    
z LexerMeta.__prepare__.<locals>._r6   r+   )r*   r(   )metanamebasesdr6   r
   r
   r   __prepare__   s
    zLexerMeta.__prepare__c                s^   |d= |d= dd |j  D }t j| |||}t||_|j|_|j|_|j	|_
|j  |S )Nr6   r+   c             S   s,   i | ]$\}}t |trt|n|t|qS r
   )r-   r   r.   )r=   r    valr
   r
   r   
<dictcomp>   s   z%LexerMeta.__new__.<locals>.<dictcomp>)itemsr   r   dict_attributesr!   _remapr+   _beforer,   _delete_build)rD   ZclsnamerF   Z
attributesZcls_attributesr"   )r#   r
   r   r      s    
zLexerMeta.__new__)r   r   r   r   classmethodrH   r   r'   r
   r
   )r#   r   r:      s   r:   c               @   s   e Zd Ze Ze ZdZdZeZ	e Z
i Ze Zi Zi Zi ZdZdZedd Zedd Zdd	 Zd
d Zdd ZdddZdd ZdS )r    r   Nc                s  g }x$ j D ]}t|tr|j|j qW t|}x6 jj D ]&\}}| jksj|j	dsjt
|dr<t|rt
|d rtd| d||kr|j||| f}||f||< |||< n|t|to| jkr$ j| }||kr|j||| f}|j|||f n|j||f |||< n|j||f |||< q@t|tr@|j	d r@|dkr@t| dq@W  fd	d
|D }| _d S )Nr4   r)   z	function z doesn't have a regex patternr6   ignoreliteralsz  does not match a name in tokensc                s"   g | ]\}}| j kr||fqS r
   )rP   )r=   r    r   )r"   r
   r   
<listcomp>  s    z(Lexer._collect_rules.<locals>.<listcomp>>   rT   rU   )	__bases__r-   r:   extend_rulesrL   rM   rK   _token_names
startswithr@   r0   r   r   r   rO   insertr2   r.   )r"   ZrulesbaseZexistingr    r   nr+   r
   )r"   r   _collect_rules   s4    
 




zLexer._collect_rulesc             C   s2  dt | krt| j d| jt| jB | _t| j| _t| j| _t| j	| _	x<| j
j D ].\\}}}|| j	kr~i | j	|< || j	| |< q^W t }x | j	j D ]}|j|j  qW |t| j }|rdj|}t| d| j  g }x| jD ]\}	}
|	jdr"|	dd }	| jj|	 t|
tr4|
}nt|
rR|
| j|	< t|
d}d	|	 d
| d}y| jj|| j}W n4 tk
r } ztd|	 |W Y dd}~X nX |jdrtd|	 d|j| qW |sdS | jjdj|| j| _t| jtstdt dd | j!D s.tddS )z
        Build the lexer object from the collected tokens and regular expressions.
        Validate the rules to make sure they look sane.
        tokensz) class does not define a tokens attributez, z not included in token(s)r4      Nr)   z(?P<>r   zInvalid regex for token rS   zRegex for token z matches empty inputr;   z!ignore specifier must be a stringc             s   s   | ]}t |tV  qd S )N)r-   r.   )r=   Zlitr
   r
   r   r>   M  s    zLexer._build.<locals>.<genexpr>z%literals must be specified as strings)"varsr   r   rZ   setr`   _ignored_tokensrL   _token_funcs
_remappingrN   rK   valuesupdater?   r_   rY   r[   addr-   r.   r0   getattrregex_modulecompilereflags	Exceptionr   matchr2   
_master_rerT   allrU   )r"   r    rI   ZnewtokZremapped_toksrG   Z	undefinedZmissingZpartsZtoknamer   r)   partZcpater
   r
   r   rQ     sV    





"zLexer._buildc             C   s,   t |tstd| jr"| j| || _dS )z)
        Begin a new lexer state
        z!state must be a subclass of LexerN)r-   r:   AssertionError_Lexer__set_stater#   )r   r"   r
   r
   r   beginP  s    
zLexer.beginc             C   s.   | j dkrg | _ | j jt|  | j| dS )z7
        Push a new lexer state onto the stack
        N)_Lexer__state_stackr2   r   rw   )r   r"   r
   r
   r   
push_stateY  s    
zLexer.push_statec             C   s   | j | jj  dS )z2
        Pop a lexer state from the stack
        N)rw   rx   pop)r   r
   r
   r   	pop_stateb  s    zLexer.pop_stater5   c             #   s  d        fdd}|| _ |t|  || _zdx\y||  krf|d7 }wNW n tk
r|   d S X t }||_||_j||}|r0|j }|j	 |_
|j|_|jkrވ|j j|j
|j|_|jkr|| _|| _|j | |}| j}| j}|sqN|jkr(qN|V  qN|| kr`|| |_
|j
|_|d7 }|V  qN|| _|| _d|_||d  |_
| j|}|d k	r|V  | j}| j}qNW W d || _|| _|| _X d S )Nc                s(   | j | j| j | j| j| jd S )N)re   rq   rT   rf   rU   rg   )r"   )_ignorere   	_literalsrq   rg   rf   r
   r   
_set_statek  s    z"Lexer.tokenize.<locals>._set_stater5   ZERROR)rv   r   r   
IndexErrorr   r   r   rp   endgroupr   Z	lastgroupgeterror)r   r   r   r   r~   r   mr
   )r|   re   r}   rq   rg   rf   r   tokenizeh  sf    	




zLexer.tokenizec             C   s(   t d|jd d| j |j| jd S )NzIllegal character r   z
 at index )r   r   r   )r   tr
   r
   r   r     s    zLexer.error)r5   r   )r   r   r   rd   r`   rU   rT   rn   rerl   rZ   rf   re   rg   rP   rN   rx   rv   rR   r_   rQ   rw   ry   r{   r   r   r
   r
   r
   r   r      s(   <J		
N)	metaclass)__all__r   copyro   r   r   r   r   objectr   r.   r   r(   rL   r*   r   r:   r   r
   r
   r
   r   <module>"   s   ((