B
    v9a_  ã               @   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   ú/usr/lib/python3.7/driver.pyÚ__init__    s
    zDriver.__init__Fc             C   sv  t  | j| j¡}| ¡  d}d}d } } } }	}
d}x4|D ]}|\}}}}	}
|||fkr¤|\}}||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¡r6|r4| 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setupr   Ú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'   sR    



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.   r1   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    rA   ú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   rA   Ú_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)r6   r7   ÚexistsÚgetmtime)ÚaÚbr   r   r   rC   ‡   s
    rC   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.

    )r6   r7   Úisfiler   rA   ÚbasenameÚpkgutilÚget_datar   rG   Úloads)ÚpackageZgrammar_sourceZpickled_nameÚdatarK   r   r   r   Úload_packaged_grammar   s    
rX   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)rI   rJ   )r<   Úargvr
   ZbasicConfigÚINFOÚstdoutr   )Úargsr>   r   r   r   Úmain¤   s    
r_   Ú__main__)rB   NTFN)Ú__doc__Ú
__author__Ú__all__Úcodecsr+   r6   r
   rS   r<   r   r   r   r   r   r	   Úobjectr   rA   r   rC   rX   r_   r2   ÚexitÚintr   r   r   r   Ú<module>   s$   M 
	