B –+jbYã@s´dZdZddgZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZGdd„deƒZd d „Zddd„Zdd„Zdd„Zdd„Zedkr°e eeƒ ƒ¡dS)zZParser driver. This provides a high-level interface to parse a file into a syntax tree. z#Guido van Rossum ÚDriverÚ load_grammaréNé)ÚgrammarÚparseÚtokenÚtokenizeÚpgenc@sHeZdZddd„Zddd„Zddd„Zdd d „Zdd d „Zdd d„ZdS)rNcCs&||_|dkrt ¡}||_||_dS)N)rÚloggingÚ getLoggerÚloggerÚconvert)Úselfrr r ©rú9/opt/alt/python37/lib64/python3.7/lib2to3/pgen2/driver.pyÚ__init__ s zDriver.__init__FcCs”t |j|j¡}| ¡d}d}d}}}} } d} xR|D]4} | \}}}} } |||fkrÀ||f|ks|t||f|fƒ‚|\} }|| kr¤| d| |7} | }d}||krÀ| | ||…7} |}|tjtjfkrø| |7} | \}}|  d¡r@|d7}d}q@|t j krtj |}|r,|j  dt j||| ¡| ||| |f¡rT|rR|j  d¡Pd} | \}}|  d¡r@|d7}d}q@Wt d||| |f¡‚|jS) z4Parse a series of tokens and return the syntax tree.rrNÚÚ z%s %r (prefix=%r)zStop.zincomplete input)rZParserrr ZsetupÚAssertionErrorrÚCOMMENTÚNLÚendswithrÚOPZopmapr ÚdebugÚtok_nameZaddtokenZ ParseErrorZrootnode)rÚtokensrÚpÚlinenoÚcolumnÚtypeÚvalueÚstartÚendZ line_textÚprefixZ quintupleZs_linenoZs_columnrrrÚ parse_tokens'sT      zDriver.parse_tokenscCst |j¡}| ||¡S)z*Parse a stream and return the syntax tree.)rÚgenerate_tokensÚreadliner$)rÚstreamrrrrrÚparse_stream_rawWs zDriver.parse_stream_rawcCs | ||¡S)z*Parse a stream and return the syntax tree.)r()rr'rrrrÚ parse_stream\szDriver.parse_streamc Cs(tj|d|d}| ||¡SQRXdS)z(Parse a file and return the syntax tree.Úr)ÚencodingN)ÚioÚopenr))rÚfilenamer+rr'rrrÚ parse_file`szDriver.parse_filecCst t |¡j¡}| ||¡S)z*Parse a string and return the syntax tree.)rr%r,ÚStringIOr&r$)rÚtextrrrrrÚ parse_stringeszDriver.parse_string)NN)F)F)F)NF)F) Ú__name__Ú __module__Ú __qualname__rr$r(r)r/r2rrrrrs   0   cCs:tj |¡\}}|dkrd}||d tttjƒ¡dS)Nz.txtrÚ.z.pickle)ÚosÚpathÚsplitextÚjoinÚmapÚstrÚsysÚ version_info)ÚgtÚheadÚtailrrrÚ_generate_pickle_nameksrBú Grammar.txtTFc Cs®|dkrt ¡}|dkr t|ƒn|}|s2t||ƒs˜| d|¡t |¡}|rª| d|¡y| |¡Wqªtk r”}z| d|¡Wdd}~XYqªXnt   ¡}|  |¡|S)z'Load the grammar (maybe from a pickle).Nz!Generating grammar tables from %szWriting grammar tables to %szWriting failed: %s) r r rBÚ_newerÚinfor Zgenerate_grammarÚdumpÚOSErrorrÚGrammarÚload)r?ZgpÚsaveÚforcer ÚgÚerrrrrs     cCs8tj |¡sdStj |¡s dStj |¡tj |¡kS)z0Inquire whether file a was written since file b.FT)r7r8ÚexistsÚgetmtime)ÚaÚbrrrrD‡s   rDcCsFtj |¡rt|ƒSttj |¡ƒ}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. ) r7r8ÚisfilerrBÚbasenameÚpkgutilÚget_datarrHÚloads)ÚpackageZgrammar_sourceZ pickled_nameÚdatarLrrrÚload_packaged_grammars   rYcGsF|stjdd…}tjtjtjddx|D]}t|dddq,WdS)zMain program, when run as a script: produce grammar pickle files. Calls load_grammar for each argument, a path to a grammar text file. rNz %(message)s)Úlevelr'ÚformatT)rJrK)r=Úargvr Z basicConfigÚINFOÚstdoutr)Úargsr?rrrÚmain¤s  r`Ú__main__)rCNTFN)Ú__doc__Ú __author__Ú__all__Úcodecsr,r7r rTr=rrrrrr ÚobjectrrBrrDrYr`r3ÚexitÚintrrrrÚ s$M