B
    18\Q2                 @   s"  d dl mZ d dlZddddddd	d
ddddgZdddddddhZG dd d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eZG dd deZdd Zd d ZG d!d" d"Zed#d$ZG d%d& d&eZd'd( ZG d)d* d*ZdS )+    )
namedtupleNbuiltin_typesparseASTModuleTypeConstructorFieldSumProductVisitorBaseCheckcheck
identifierstringbytesintobjectZ	singletonZconstantc               @   s   e Zd Zdd ZdS )r   c             C   s   t d S )N)NotImplementedError)self r   K/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Parser/asdl.py__repr__(   s    zAST.__repr__N)__name__
__module____qualname__r   r   r   r   r   r   '   s   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s    || _ || _dd |D | _d S )Nc             S   s   i | ]}|j |jqS r   )valuename).0typer   r   r   
<dictcomp>/   s    z#Module.__init__.<locals>.<dictcomp>)r   dfnstypes)r   r   r!   r   r   r   __init__,   s    zModule.__init__c             C   s
   d | S )NzModule({0.name}, {0.dfns}))format)r   r   r   r   r   1   s    zModule.__repr__N)r   r   r   r#   r   r   r   r   r   r   +   s   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s   || _ || _d S )N)r   r   )r   r   r   r   r   r   r#   5   s    zType.__init__c             C   s
   d | S )NzType({0.name}, {0.value}))r$   )r   r   r   r   r   9   s    zType.__repr__N)r   r   r   r#   r   r   r   r   r   r   4   s   c               @   s   e Zd ZdddZdd ZdS )r   Nc             C   s   || _ |pg | _d S )N)r   fields)r   r   r%   r   r   r   r#   =   s    zConstructor.__init__c             C   s
   d | S )Nz!Constructor({0.name}, {0.fields}))r$   )r   r   r   r   r   A   s    zConstructor.__repr__)N)r   r   r   r#   r   r   r   r   r   r   <   s   
c               @   s   e Zd ZdddZdd ZdS )r	   NFc             C   s   || _ || _|| _|| _d S )N)r   r   seqopt)r   r   r   r&   r'   r   r   r   r#   E   s    zField.__init__c             C   sB   | j rd}n| jrd}nd}| jd kr2d| |S d| |S d S )Nz
, seq=Truez
, opt=True zField({0.type}{1})zField({0.type}, {0.name}{1}))r&   r'   r   r$   )r   extrar   r   r   r   K   s    
zField.__repr__)NFF)r   r   r   r#   r   r   r   r   r   r	   D   s   
c               @   s   e Zd ZdddZdd ZdS )r
   Nc             C   s   || _ |pg | _d S )N)r"   
attributes)r   r"   r*   r   r   r   r#   X   s    zSum.__init__c             C   s   | j rd| S d| S d S )NzSum({0.types}, {0.attributes})zSum({0.types}))r*   r$   )r   r   r   r   r   \   s    
zSum.__repr__)N)r   r   r   r#   r   r   r   r   r   r
   W   s   
c               @   s   e Zd ZdddZdd ZdS )r   Nc             C   s   || _ |pg | _d S )N)r%   r*   )r   r%   r*   r   r   r   r#   c   s    zProduct.__init__c             C   s   | j rd| S d| S d S )Nz#Product({0.fields}, {0.attributes})zProduct({0.fields}))r*   r$   )r   r   r   r   r   g   s    
zProduct.__repr__)N)r   r   r   r#   r   r   r   r   r   r   b   s   
c               @   s    e Zd ZdZdd Zdd ZdS )r   zGeneric tree visitor for ASTs.c             C   s
   i | _ d S )N)cache)r   r   r   r   r#   u   s    zVisitorBase.__init__c          
   G   s   |j }| j|}|d kr:d|j }t| |d }|| j|< |ry||f|  W n4 tk
r } ztd||f   W d d }~X Y nX d S )NvisitzError visiting %r: %s)	__class__r+   getr   getattr	Exceptionprint)r   objargsklassmethZmethnameer   r   r   r,   x   s    

zVisitorBase.visitN)r   r   r   __doc__r#   r,   r   r   r   r   r   s   s   c                   sP   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )r   zgA visitor that checks a parsed ASDL tree for correctness.

    Errors are printed and accumulated.
    c                s$   t t|   i | _d| _i | _d S )Nr   )superr   r#   conserrorsr"   )r   )r-   r   r   r#      s    zCheck.__init__c             C   s   x|j D ]}| | qW d S )N)r!   r,   )r   moddfnr   r   r   visitModule   s    zCheck.visitModulec             C   s   |  |jt|j d S )N)r,   r   strr   )r   r   r   r   r   	visitType   s    zCheck.visitTypec             C   s    x|j D ]}| || qW d S )N)r"   r,   )r   sumr   tr   r   r   visitSum   s    zCheck.visitSumc             C   sv   t |j}| j|}|d kr*|| j|< n,td| td|| |  jd7  _x|jD ]}| || q^W d S )NzRedefinition of constructor {}zDefined in {} and {}   )	r>   r   r9   r.   r1   r$   r:   r%   r,   )r   r9   r   keyZconflictfr   r   r   visitConstructor   s    
zCheck.visitConstructorc             C   s&   t |j}| j|g }|| d S )N)r>   r   r"   
setdefaultappend)r   Zfieldr   rD   lr   r   r   
visitField   s    
zCheck.visitFieldc             C   s    x|j D ]}| || qW d S )N)r%   r,   )r   Zprodr   rE   r   r   r   visitProduct   s    zCheck.visitProduct)r   r   r   r7   r#   r=   r?   rB   rF   rJ   rK   __classcell__r   r   )r-   r   r      s   c             C   sh   t  }||  xN|jD ]D}|| jkr|tkr| jd7  _d|j| }td|| qW |j S )zCheck the parsed ASDL tree for correctness.

    Return True if success. For failure, the errors are printed out and False
    is returned.
    rC   z, zUndefined type {}, used in {})r   r,   r"   r   r:   joinr1   r$   )r;   vrA   Zusesr   r   r   r      s    
c          	   C   s(   t | }t }|| S Q R X dS )zFParse ASDL from the given file and return a Module node describing it.N)open
ASDLParserr   read)filenamerE   parserr   r   r   r      s    
c               @   sF   e Zd ZdZed\ZZZZZ	Z
ZZZZZeee	e
eeeeed	ZdS )	TokenKindz9TokenKind is provides a scope for enumerated token kinds.   )	=,?|()*{}N)r   r   r   r7   rangeConstructorIdTypeIdEqualsCommaQuestionPipeAsteriskLParenRParenLBraceRBraceoperator_tabler   r   r   r   rT      s   
rT   Tokenzkind value linenoc               @   s   e Zd ZdddZdd ZdS )ASDLSyntaxErrorNc             C   s   || _ |pd| _d S )Nz	<unknown>)msglineno)r   rn   ro   r   r   r   r#      s    zASDLSyntaxError.__init__c             C   s
   d | S )Nz(Syntax error on line {0.lineno}: {0.msg})r$   )r   r   r   r   __str__   s    zASDLSyntaxError.__str__)N)r   r   r   r#   rp   r   r   r   r   rm      s   
rm   c          
   c   s   xt |  dD ]\}}xtd| D ]}|d}|d  rt|d  rbtt	j
||V  qtt	j||V  q*|dd dkrP q*yt	j| }W n" tk
r   td| |Y nX t|||V  q*W qW dS )z/Tokenize the given buffer. Yield Token objects.rC   z\s*(\w+|--.*|.)r   N   z--zInvalid operator %s)	enumerate
splitlinesrefinditerstripgroupisalphaisupperrl   rT   r`   ra   rk   KeyErrorrm   )bufro   linemcZop_kindr   r   r   tokenize_asdl   s    
r   c               @   s   e 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ejejfZdd Zdd ZdS )rP   zParser for ASDL files.

    Create, then call the parse method on a buffer containing ASDL.
    This is a simple recursive descent parser that uses tokenize_asdl for the
    lexing.
    c             C   s   d | _ d | _d S )N)
_tokenizer	cur_token)r   r   r   r   r#      s    zASDLParser.__init__c             C   s   t || _|   |  S )zKParse the ASDL in the buffer and return an AST with a Module root.
        )r   r   _advance_parse_module)r   r{   r   r   r   r      s    
zASDLParser.parsec             C   sb   |  dr|   ntd| jj| jj| | j}| t	j
 |  }| t	j t||S )NmodulezExpected "module" (found {}))_at_keywordr   rm   r$   r   r   ro   _match	_id_kindsrT   ri   _parse_definitionsrj   r   )r   r   defsr   r   r   r     s    


zASDLParser._parse_modulec             C   sH   g }x>| j jtjkrB|  }| tj |  }|t	|| qW |S )N)
r   kindrT   ra   r   r   rb   _parse_typerH   r   )r   r   typenamer   r   r   r   r     s    zASDLParser._parse_definitionsc             C   sx   | j jtjkr|  S t| tj|  g}x6| j jtj	krd| 
  |t| tj|   q0W t||  S d S )N)r   r   rT   rg   _parse_productr   r   r`   _parse_optional_fieldsre   r   rH   r
   _parse_optional_attributes)r   Zsumlistr   r   r   r     s    
zASDLParser._parse_typec             C   s   t |  |  S )N)r   _parse_fieldsr   )r   r   r   r   r   ,  s    zASDLParser._parse_productc             C   s   g }|  tj x~| jjtjkr|  }|  \}}| jj| jkrJ|  nd }|	t
||||d | jjtjkrvP q| jjtjkr|   qW |  tj |S )N)r&   r'   )r   rT   rg   r   r   ra   r    _parse_optional_field_quantifierr   rH   r	   rh   rc   )r   r%   r   is_seqis_optidr   r   r   r   /  s    zASDLParser._parse_fieldsc             C   s   | j jtjkr|  S d S d S )N)r   r   rT   rg   r   )r   r   r   r   r   ?  s    z!ASDLParser._parse_optional_fieldsc             C   s"   |  dr|   |  S d S d S )Nr*   )r   r   r   )r   r   r   r   r   E  s    
z%ASDLParser._parse_optional_attributesc             C   sF   d\}}| j jtjkr$d}|   n| j jtjkr>d}|   ||fS )N)FFT)r   r   rT   rf   r   rd   )r   r   r   r   r   r   r   L  s    
z+ASDLParser._parse_optional_field_quantifierc             C   sF   | j dkrdn| j j}yt| j| _ W n tk
r@   d| _ Y nX |S )zf Return the value of the current token and read the next one into
            self.cur_token.
        N)r   r   nextr   StopIteration)r   Zcur_valr   r   r   r   V  s    zASDLParser._advancec             C   sT   t |tr| jj|ks"| jj|kr6| jj}|   |S td|| jj| jjdS )a  The 'match' primitive of RD parsers.

        * Verifies that the current token is of the given kind (kind can
          be a tuple, in which the kind must match one of its members).
        * Returns the value of the current token
        * Reads in the next token
        zUnmatched {} (found {})N)	
isinstancetupler   r   r   r   rm   r$   ro   )r   r   r   r   r   r   r   c  s    zASDLParser._matchc             C   s   | j jtjko| j j|kS )N)r   r   rT   ra   r   )r   keywordr   r   r   r   v  s    zASDLParser._at_keywordN)r   r   r   r7   r#   r   r   r   r   r   r   r   r   r   r   rT   r`   ra   r   r   r   r   r   r   r   rP      s   	
rP   )collectionsr   rt   __all__r   r   r   r   r   r	   r
   r   r   r   r   r   r   rT   rl   r0   rm   r   rP   r   r   r   r   <module>   s*   		+	
