i f@sdZdZddlZddlZddlmZmZmZmZm Z m Z ddl m Z ddl m Z ejjejjedZGd d d eZd d ZGd ddeZiejd6ejd6ejd6dd6ZddZddZddZdS)zPattern compiler. The grammer is taken from PatternGrammar.txt. The compiler compiles a pattern to a pytree.*Pattern instance. z#Guido van Rossum N)driverliteralstokentokenizeparsegrammar)pytree)pygramzPatternGrammar.txtc@seZdZdS)PatternSyntaxErrorN)__name__ __module__ __qualname__rr4/opt/alt/python34/lib64/python3.4/lib2to3/patcomp.pyr s r c csqtjtjtjh}tjtj|j}x7|D]/}|\}}}}}||kr:|Vq:q:WdS)z6Tokenizes a string suppressing significant whitespace.N) rNEWLINEINDENTDEDENTrgenerate_tokensioStringIOreadline) inputskiptokensZ quintupletypevaluestartendZ line_textrrrtokenize_wrapper!s   rc@sXeZdZeddZddddZddZdd d Zd d ZdS) PatternCompilercCs^tj||_tj|j|_tj|_tj|_ tj |jdt |_dS)z^Initializer. Takes an optional alternative filename for the pattern grammar. convertN) rZ load_grammarrr ZSymbolssymsZpython_grammarZ pygrammarZpython_symbolspysymsZDriverpattern_convert)selfZ grammar_filerrr__init__-s   zPatternCompiler.__init__FcCst|}y|jj|d|}Wn:tjk rd}ztt|WYdd}~XnX|r~|j||fS|j|SdS)z=Compiles a pattern string to a nested pytree.*Pattern object.debugN)rrZ parse_tokensrZ ParseErrorr str compile_node)r%rr'Z with_treerrooterrrcompile_pattern8s %zPatternCompiler.compile_patternc sK|jjjkr%|jd}n|jjjkrfdd|jdddD}t|dkr}|dStjdd|Dddd d}|jS|jjj kr"fd d|jD}t|dkr|dStj|gddd d}|jS|jjj krlj |jdd}tj |}|jSd}|j}t|d kr|djt jkr|dj}|dd}nd}t|dkr|d jjjkr|d}|dd}nj ||}|dk r)|j} | d} | jt jkrfd} tj} nx| jt jkrd} tj} nT| jt jkrj| d} } t| d krj| d } qn| dks| dkr)|j}tj|ggd| d | }q)n|dk rA||_n|jS)zXCompiles a node, recursively. This is one big switch on the node type. rcsg|]}j|qSr)r)).0ch)r%rr Ps z0PatternCompiler.compile_node..NrcSsg|]}|gqSrr)r-arrrr/Ss minmaxcsg|]}j|qSr)r))r-r.)r%rrr/Ws r6r6)rr"ZMatcherchildrenZ Alternativeslenr WildcardPatternoptimizeZ AlternativeZ NegatedUnit compile_basicZNegatedPatternrEQUALrZRepeaterSTARZHUGEPLUSLBRACEget_intname) r%nodeZaltspZunitspatternrAnodesrepeatr7Zchildr2r3r)r%rr)Ds^)(    ( +       '  zPatternCompiler.compile_nodeNcCs|d}|jtjkrJttj|j}tjt ||S|jtj krd|j}|j r|t krt d|n|ddrt dntjt |S|dkrd}nF|jdst|j|d}|dkrt d|qn|ddrK|j|djdg}nd}tj||Sna|jdkr|j|dS|jd kr|j|d}tj|ggd dd dSdS) NrzInvalid token: %rrzCan't have details for tokenany_zInvalid symbol: %r([r2r3)rrSTRINGr(rZ evalStringrr Z LeafPattern_type_of_literalNAMEisupper TOKEN_MAPr startswithgetattrr#r)r7Z NodePatternr9)r%rErFrBrrZcontent subpatternrrrr;s8        zPatternCompiler.compile_basiccCs t|jS)N)intr)r%rBrrrr@szPatternCompiler.get_int) r r r_PATTERN_GRAMMAR_FILEr&r,r)r;r@rrrrr +s   G#r rMrKNUMBERZTOKENcCs9|djrtjS|tjkr1tj|SdSdS)Nr)isalpharrMrZopmap)rrrrrLs  rLcCsW|\}}}}|s'||jkr=tj||d|Stj||d|SdS)z9Converts raw node information to a Node or Leaf instance.contextN)Z number2symbolr ZNodeZLeaf)rZ raw_node_inforrrWr7rrrr$sr$cCstj|S)N)r r,)rDrrrr,sr,)__doc__ __author__rosZpgen2rrrrrrr r pathjoindirname__file__rT Exceptionr robjectr rMrKrUrOrLr$r,rrrr s$  .