B
    ?™c_  ã               @   s´   d Z dZddgZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZmZ G dd„ deƒZd	d
„ Zddd„Zdd„ Zdd„ Zdd„ Zedkr°e eeƒ  ƒ¡ dS )zZParser driver.

This provides a high-level interface to parse a file into a syntax tree.

z#Guido van Rossum <guido@python.org>ÚDriverÚload_grammaré    Né   )ÚgrammarÚparseÚtokenÚtokenizeÚpgenc               @   sH   e Zd Zddd„Zddd„Zddd„Zdd	d
„Zddd„Zddd„ZdS )r   Nc             C   s&   || _ |d krt ¡ }|| _|| _d S )N)r   ÚloggingÚ	getLoggerÚloggerÚconvert)Úselfr   r   r   © r   ú%lib/python3.7/lib2to3/pgen2/driver.pyÚ__init__    s
    zDriver.__init__Fc             C   s”  t  | j| j¡}| ¡  d}d}d } } } }	}
d}xR|D ]4}|\}}}}	}
|||fkrÀ||f|ks|t||f|fƒ‚|\}}||k r¤|d||  7 }|}d}||k rÀ||
||… 7 }|}|tjtjfkrø||7 }|	\}}| 	d¡r@|d7 }d}q@|t
jkrtj| }|r,| j dt
j| ||¡ | ||||f¡rT|rR| j d¡ P d}|	\}}| 	d¡r@|d7 }d}q@W t  d||||f¡‚|jS )	z4Parse a series of tokens and return the syntax tree.r   r   NÚ Ú
z%s %r (prefix=%r)zStop.zincomplete input)r   ZParserr   r   ZsetupÚAssertionErrorr   ÚCOMMENTÚNLÚendswithr   ÚOPZopmapr   ÚdebugÚtok_nameZaddtokenZ
ParseErrorZrootnode)r   Útokensr   ÚpÚlinenoÚcolumnÚtypeÚvalueÚstartÚendZ	line_textÚprefixZ	quintupleZs_linenoZs_columnr   r   r   Úparse_tokens'   sT    


zDriver.parse_tokensc             C   s   t  |j¡}|  ||¡S )z*Parse a stream and return the syntax tree.)r   Úgenerate_tokensÚreadliner$   )r   Ústreamr   r   r   r   r   Úparse_stream_rawW   s    zDriver.parse_stream_rawc             C   s   |   ||¡S )z*Parse a stream and return the syntax tree.)r(   )r   r'   r   r   r   r   Úparse_stream\   s    zDriver.parse_streamc          	   C   s(   t j|d|d}|  ||¡S Q R X dS )z(Parse a file and return the syntax tree.Úr)ÚencodingN)ÚioÚopenr)   )r   Úfilenamer+   r   r'   r   r   r   Ú
parse_file`   s    zDriver.parse_filec             C   s   t  t |¡j¡}|  ||¡S )z*Parse a string and return the syntax tree.)r   r%   r,   ÚStringIOr&   r$   )r   Útextr   r   r   r   r   Úparse_stringe   s    zDriver.parse_string)NN)F)F)F)NF)F)	Ú__name__Ú
__module__Ú__qualname__r   r$   r(   r)   r/   r2   r   r   r   r   r      s   

0


c             C   s:   t j | ¡\}}|dkrd}|| d tttjƒ¡ d S )Nz.txtr   Ú.z.pickle)ÚosÚpathÚsplitextÚjoinÚmapÚstrÚsysÚversion_info)ÚgtÚheadÚtailr   r   r   Ú_generate_pickle_namek   s    rB   úGrammar.txtTFc          
   C   s®   |dkrt  ¡ }|dkr t| ƒn|}|s2t|| ƒs˜| d| ¡ t | ¡}|rª| d|¡ y| |¡ W qª tk
r” } z| d|¡ W dd}~X Y qªX nt	 
¡ }| |¡ |S )z'Load the grammar (maybe from a pickle).Nz!Generating grammar tables from %szWriting grammar tables to %szWriting failed: %s)r
   r   rB   Ú_newerÚinfor	   Zgenerate_grammarÚdumpÚOSErrorr   ÚGrammarÚload)r?   ZgpÚsaveÚforcer   ÚgÚer   r   r   r   r   s    
 
c             C   s8   t j | ¡sdS t j |¡s dS t j | ¡t j |¡kS )z0Inquire whether file a was written since file b.FT)r7   r8   ÚexistsÚgetmtime)ÚaÚbr   r   r   rD   ‡   s
    rD   c             C   sF   t j |¡rt|ƒS tt j |¡ƒ}t | |¡}t 	¡ }| 
|¡ |S )aÙ  Normally, loads a pickled grammar by doing
        pkgutil.get_data(package, pickled_grammar)
    where *pickled_grammar* is computed from *grammar_source* by adding the
    Python version and using a ``.pickle`` extension.

    However, if *grammar_source* is an extant file, load_grammar(grammar_source)
    is called instead. This facilitates using a packaged grammar file when needed
    but preserves load_grammar's automatic regeneration behavior when possible.

    )r7   r8   Úisfiler   rB   ÚbasenameÚpkgutilÚget_datar   rH   Úloads)ÚpackageZgrammar_sourceZpickled_nameÚdatarL   r   r   r   Úload_packaged_grammar   s    
rY   c              G   sF   | st jdd… } tjtjt jdd x| D ]}t|ddd q,W dS )zMain program, when run as a script: produce grammar pickle files.

    Calls load_grammar for each argument, a path to a grammar text file.
    r   Nz%(message)s)Úlevelr'   ÚformatT)rJ   rK   )r=   Úargvr
   ZbasicConfigÚINFOÚstdoutr   )Úargsr?   r   r   r   Úmain¤   s    
r`   Ú__main__)rC   NTFN)Ú__doc__Ú
__author__Ú__all__Úcodecsr,   r7   r
   rT   r=   r   r   r   r   r   r	   Úobjectr   rB   r   rD   rY   r`   r3   ÚexitÚintr   r   r   r   Ú<module>   s$   M 
	