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__FcCstj|j|j}|jd}d}d}}}} } d} x|D]} | \}}}} } |||fkr|\} }|| kr| d| |7} | }d}n||kr| | ||7} |}qn|tjtjfkr9| |7} | \}}|jdrQ|d7}d}qQqQn|t j krXtj |}n|r|j j dt j||| n|j||| |fr|r|j j dnPnd} | \}}|jdrQ|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 ZsetuprCOMMENTNLendswithrOPZopmapr debugtok_nameZaddtokenZ ParseErrorZrootnode)rtokensrplinenocolumntypevaluestartendZ line_textprefixZ quintupleZs_linenoZs_columnrrr parse_tokens&sR                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(r/r3rrrrrs 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)r8r9existsgetmtime)abrrrr@s r@cGsi|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)srFTrG)r>argvr Z basicConfigINFOstdoutr)argsrErrrmains rV__main__)__doc__ __author____all__r*r0r8r r>rrrrrr objectrrr@rVr4exitintrrrr s      (P