j f!@sdZdZddgZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z m Z GdddeZd dd d dd dZd dZddZedkrejee ndS)zZParser driver. This provides a high-level interface to parse a file into a syntax tree. z#Guido van Rossum Driver load_grammarN)grammarparsetokentokenizepgenc@speZdZddddZdddZdddZdd d Zddd d Zdd dZdS)rNcCs:||_|dkr$tj}n||_||_dS)N)rlogging getLoggerloggerconvert)selfrr r r9/opt/alt/python34/lib64/python3.4/lib2to3/pgen2/driver.py__init__s    zDriver.__init__FcCs@tj|j|j}|jd}d}d}}}} } d} x|D]} | \}}}} } |||fkr ||f|kst||f|f|\} }|| kr| d| |7} | }d}n||kr | | ||7} |}q n|tjtjfkrc| |7} | \}}|j drQ|d7}d}qQqQn|t j krtj |}n|r|j jdt j||| n|j||| |fr|r|j jdnPnd} | \}}|j drQ|d7}d}qQqQWtjd||| |f|jS) z4Parse a series of tokens and return the syntax tree.rrN z%s %r (prefix=%r)zStop.zincomplete input)rZParserrr ZsetupAssertionErrorrCOMMENTNLendswithrOPZopmapr debugtok_nameZaddtokenZ ParseErrorZrootnode)rtokensrplinenocolumntypevaluestartendZ line_textprefixZ quintupleZs_linenoZs_columnrrr parse_tokens&sT  *              zDriver.parse_tokenscCs"tj|j}|j||S)z*Parse a stream and return the syntax tree.)rgenerate_tokensreadliner$)rstreamrrrrrparse_stream_rawVszDriver.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||SWd|jXdS)z(Parse a file and return the syntax tree.rN)codecsopenr)close)rfilenameencodingrr'rrr parse_file_szDriver.parse_filecCs+tjtj|j}|j||S)z*Parse a string and return the syntax tree.)rr%ioStringIOr&r$)rtextrrrrr parse_stringgszDriver.parse_string) __name__ __module__ __qualname__rr$r(r)r0r4rrrrrs 0z Grammar.txtTFc Cs<|dkrtj}n|dkr~tjj|\}}|dkrTd}n||djtttj d}n|st || r|j d|t j |}|r8|j d|y|j|Wqtk r}z|j dt|WYdd}~XqXq8ntj}|j||S) z'Load the grammar (maybe from a pickle).Nz.txtr.z.picklez!Generating grammar tables from %szWriting grammar tables to %szWriting failed:)r r ospathsplitextjoinmapstrsys version_info_newerinfor Zgenerate_grammardumpOSErrorrZGrammarload) gtZgpsaveforcer headtailgerrrrms&    *0  cCsNtjj|sdStjj|s,dStjj|tjj|kS)z0Inquire whether file a was written since file b.FT)r9r:existsgetmtime)abrrrrAs rAcGsi|stjdd}ntjdtjdtjddx$|D]}t|ddd dqEWdS) zMain program, when run as a script: produce grammar pickle files. Calls load_grammar for each argument, a path to a grammar text file. rNlevelr'formatz %(message)srGTrH)r?argvr Z basicConfigINFOstdoutr)argsrFrrrmains rW__main__)__doc__ __author____all__r+r1r9r r?rrrrrr objectrrrArWr5exitintrrrr s      (P