3 \@sdZdZddgZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZGdddeZd d ZdddZddZddZddZedkrejee dS)zZParser driver. This provides a high-level interface to parse a file into a syntax tree. z#Guido van Rossum Driver load_grammarN)grammarparsetokentokenizepgenc@sHeZdZdddZdddZdddZdd d Zdd d Zdd dZdS)rNcCs&||_|dkrtj}||_||_dS)N)rlogging getLoggerloggerconvert)selfrr r r/usr/lib64/python3.6/driver.py__init__ s zDriver.__init__FcCsvtj|j|j}|jd}d}d}}}} } d} x4|D]} | \}}}} } |||fkr|\} }|| kr| d| |7} | }d}||kr| | ||7} |}|tjtjfkr| |7} | \}}|jdr@|d7}d}q@|t j krtj |}|r|j j dt j||| |j||| |fr6|r4|j j dPd} | \}}|jdr@|d7}d}q@Wtjd||| |f|jS) z4Parse a series of tokens and return the syntax tree.rrN z%s %r (prefix=%r)zStop.zincomplete input)rZParserrr ZsetuprCOMMENTNLendswithrOPZopmapr debugtok_nameZaddtokenZ ParseErrorZrootnode)rtokensrplinenocolumntypevaluestartendZ line_textprefixZ quintupleZs_linenoZs_columnrrr parse_tokens'sR      zDriver.parse_tokenscCstj|j}|j||S)z*Parse a stream and return the syntax tree.)rgenerate_tokensreadliner#)rstreamrrrrrparse_stream_rawWs zDriver.parse_stream_rawcCs |j||S)z*Parse a stream and return the syntax tree.)r')rr&rrrr parse_stream\szDriver.parse_streamc Cs*tj|d|}z |j||S|jXdS)z(Parse a file and return the syntax tree.rN)codecsopenr(close)rfilenameencodingrr&rrr parse_file`s zDriver.parse_filecCstjtj|j}|j||S)z*Parse a string and return the syntax tree.)rr$ioStringIOr%r#)rtextrrrrr parse_stringhszDriver.parse_string)NN)F)F)F)NF)F) __name__ __module__ __qualname__rr#r'r(r/r3rrrrrs   0   cCs:tjj|\}}|dkrd}||djtttjdS)Nz.txtr.z.pickle)ospathsplitextjoinmapstrsys version_info)gtheadtailrrr_generate_pickle_namensrC Grammar.txtTFcCs|dkrtj}|dkr t|n|}|s4t|| r|jd|tj|}|r|jd|y|j|Wqtk r}z|jd|WYdd}~XqXnt j }|j ||S)z'Load the grammar (maybe from a pickle).Nz!Generating grammar tables from %szWriting grammar tables to %szWriting failed: %s) r r rC_newerinfor Zgenerate_grammardumpOSErrorrGrammarload)r@Zgpsaveforcer gerrrrus     cCs8tjj|sdStjj|s dStjj|tjj|kS)z0Inquire whether file a was written since file b.FT)r8r9existsgetmtime)abrrrrEs   rEcCsFtjj|rt|Sttjj|}tj||}tj }|j ||S)aNormally, 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. ) r8r9isfilerrCbasenamepkgutilget_datarrIloads)packageZgrammar_sourceZ pickled_namedatarMrrrload_packaged_grammars   rZcGsF|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)rKrL)r>argvr Z basicConfigINFOstdoutr)argsr@rrrmains  ra__main__)rDNTFN)__doc__ __author____all__r*r0r8r rUr>rrrrrr objectrrCrrErZrar4exitintrrrr s$P