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ú7/opt/alt/python37/lib/python3.7/lib2to3/pgen2/driver.pyÚ__init__ s zDriver.__init__FcCsvt |j|j¡}| ¡d}d}d}}}} } d} x4|D]} | \}}}} } |||fkr¤|\} }|| krˆ| d| |7} | }d}||kr¤| | ||…7} |}|tjtjfkrÜ| |7} | \}}| d¡r@|d7}d}q@|t j krðtj |}|r|j   dt j||| ¡| ||| |f¡r6|r4|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 ZsetuprÚ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'sR      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.r1rrrrrs   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_nameksrAú 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 rAÚ_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)r6r7ÚexistsÚgetmtime)ÚaÚbrrrrC‡s   rCcCsFtj |¡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. ) r6r7ÚisfilerrAÚbasenameÚpkgutilÚget_datarrGÚloads)ÚpackageZgrammar_sourceZ pickled_nameÚdatarKrrrÚload_packaged_grammars   rXcGsF|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)rIrJ)r<Úargvr Z basicConfigÚINFOÚstdoutr)Úargsr>rrrÚmain¤s  r_Ú__main__)rBNTFN)Ú__doc__Ú __author__Ú__all__Úcodecsr+r6r rSr<rrrrrr ÚobjectrrArrCrXr_r2ÚexitÚintrrrrÚ s$M