zyManc@sdZdZddlZddlZddlmZdZiaddZGdd d e Z Gd d d e Z Gd d d e Z ddZ Gddde ZGdddeZGdddeZGdddeZGdddeZddZdS(u Python parse tree definitions. This is a very concrete parse tree; we need to keep every token and even the comments and whitespace between tokens. There's also a pattern matching implementation here. u#Guido van Rossum iN(uStringIOicCshtsXddlm}x?|jjD]+\}}t|tkr&|t|= 256), a sequence of child nodes, and an optional context keyword argument. As a side effect, the parent pointers of the children are updated. N(utypeulistuchildrenuparentuNoneuprefixufixers_applied(uselfutypeuchildrenucontextuprefixufixers_applieduch((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__init__s    u Node.__init__cCs#d|jjt|j|jfS(u)Return a canonical string representation.u %s(%s, %r)(u __class__u__name__u type_reprutypeuchildren(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__repr__s  u Node.__repr__cCsdjtt|jS(uk Return a pretty string representation. This reproduces the input source exactly. u(ujoinumapustruchildren(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu __unicode__suNode.__unicode__iicCs"|j|jf|j|jfkS(uCompare two nodes for equality.(utypeuchildren(uselfuother((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_eq suNode._eqcCs)t|jdd|jDd|jS(u$Return a cloned (deep) copy of self.cSsg|]}|jqS((uclone(u.0uch((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu s uNode.clone..ufixers_applied(uNodeutypeuchildrenufixers_applied(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuclone su Node.cloneccs9x-|jD]"}x|jD] }|VqWq W|VdS(u*Return a post-order iterator for the tree.N(uchildrenu post_order(uselfuchildunode((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu post_orders uNode.post_orderccs9|Vx-|jD]"}x|jD] }|Vq"WqWdS(u)Return a pre-order iterator for the tree.N(uchildrenu pre_order(uselfuchildunode((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu pre_ordersuNode.pre_ordercCs|js dS|jdjS(uO The whitespace and comments preceding this node in the input. ui(uchildrenuprefix(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_prefix_getter s uNode._prefix_gettercCs |jr||jd_ndS(Ni(uchildrenuprefix(uselfuprefix((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_prefix_setter(s uNode._prefix_settercCs4||_d|j|_||j|<|jdS(u Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. N(uparentuNoneuchildrenuchanged(uselfuiuchild((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu set_child.s  uNode.set_childcCs*||_|jj|||jdS(u Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. N(uparentuchildrenuinsertuchanged(uselfuiuchild((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu insert_child8s uNode.insert_childcCs'||_|jj||jdS(u Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. N(uparentuchildrenuappenduchanged(uselfuchild((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu append_childAs uNode.append_childN(ii(u__name__u __module__u __qualname__u__doc__uNoneu__init__u__repr__u __unicode__usysu version_infou__str__u_equcloneu post_orderu pre_orderu_prefix_getteru_prefix_setterupropertyuprefixu set_childu insert_childu append_child(u __locals__((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuNodes$          uNodecBs|EeZdZdZdZdZdZddgddZddZ dd Z e j dkrme Z nd d Zd dZddZddZddZddZddZeeeZdS(uLeafu'Concrete implementation for leaf nodes.uicCsk|dk r*|\|_\|_|_n||_||_|dk rT||_n|dd|_dS(u Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. N(uNoneu_prefixulinenoucolumnutypeuvalueufixers_applied(uselfutypeuvalueucontextuprefixufixers_applied((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__init__Ts     u Leaf.__init__cCsd|jj|j|jfS(u)Return a canonical string representation.u %s(%r, %r)(u __class__u__name__utypeuvalue(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__repr__gs u Leaf.__repr__cCs|jt|jS(uk Return a pretty string representation. This reproduces the input source exactly. (uprefixustruvalue(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu __unicode__msuLeaf.__unicode__icCs"|j|jf|j|jfkS(uCompare two nodes for equality.(utypeuvalue(uselfuother((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_eqxsuLeaf._eqcCs4t|j|j|j|j|jffd|jS(u$Return a cloned (deep) copy of self.ufixers_applied(uLeafutypeuvalueuprefixulinenoucolumnufixers_applied(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuclone|su Leaf.cloneccs |VdS(N((uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuleavessu Leaf.leavesccs |VdS(u*Return a post-order iterator for the tree.N((uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu post_ordersuLeaf.post_orderccs |VdS(u)Return a pre-order iterator for the tree.N((uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu pre_ordersuLeaf.pre_ordercCs|jS(uP The whitespace and comments preceding this token in the input. (u_prefix(uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_prefix_gettersuLeaf._prefix_gettercCs|j||_dS(N(uchangedu_prefix(uselfuprefix((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu_prefix_setters uLeaf._prefix_setterN(ii(u__name__u __module__u __qualname__u__doc__u_prefixulinenoucolumnuNoneu__init__u__repr__u __unicode__usysu version_infou__str__u_equcloneuleavesu post_orderu pre_orderu_prefix_getteru_prefix_setterupropertyuprefix(u __locals__((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuLeafKs&          uLeafcCsk|\}}}}|s'||jkrTt|dkrA|dSt||d|St||d|SdS(u Convert raw node information to a Node or Leaf instance. This is passed to the parser driver which calls it whenever a reduction of a grammar rule produces a new complete node, so that the tree is build strictly bottom-up. iiucontextN(u number2symbolulenuNodeuLeaf(ugruraw_nodeutypeuvalueucontextuchildren((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuconverts uconvertcBsz|EeZdZdZdZdZdZddZddZ ddZ ddd Z dd d Z d d Z dS(u BasePatternu A pattern is a tree matching pattern. It looks for a specific node type (token or symbol), and optionally for a specific content. This is an abstract base class. There are three concrete subclasses: - LeafPattern matches a single leaf node; - NodePattern matches a single node (usually non-leaf); - WildcardPattern matches a sequence of nodes of variable length. cOs tj|S(u>Constructor that prevents BasePattern from being instantiated.(uobjectu__new__(uclsuargsukwds((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__new__suBasePattern.__new__cCsht|j|j|jg}x!|rA|ddkrA|d=q!Wd|jjdjtt |fS(Niu%s(%s)u, ii( u type_reprutypeucontentunameuNoneu __class__u__name__ujoinumapurepr(uselfuargs((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__repr__s uBasePattern.__repr__cCs|S(u A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. ((uself((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuoptimizesuBasePattern.optimizecCs|jdk r%|j|jkr%dS|jdk r~d}|dk rOi}n|j||sedS|r~|j|q~n|dk r|jr|||j|EeZdZdZdddddZdddZdS(u NodePatterncCs|dk rn|dk rbt|}x8t|D]'\}}t|tr4d|_q4q4Wn||_||_||_ dS(ud Initializer. Takes optional type, content, and name. The type, if given, must be a symbol type (>= 256). If the type is None this matches *any* single node (leaf or not), except if content is not None, in which it only matches non-leaf nodes that also match the content pattern. The content, if not None, must be a sequence of Patterns that must match the node's children exactly. If the content is given, the type must not be None. If a name is given, the matching node is stored in the results dict under that key. NT( uNoneulistu enumerateu isinstanceuWildcardPatternuTrueu wildcardsutypeucontentuname(uselfutypeucontentunameuiuitem((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__init__2s     uNodePattern.__init__cCs|jrhxXt|j|jD]A\}}|t|jkr|dk r\|j|ndSqWdSt|jt|jkrdSx9t |j|jD]"\}}|j ||sdSqWdS(u Match the pattern's content to the node's children. This assumes the node type matches and self.content is not None. Returns True if it matches, False if not. If results is not None, it must be a dict which will be updated with the nodes matching named subpatterns. When returning False, the results dict may still be updated. NTF( u wildcardsugenerate_matchesucontentuchildrenulenuNoneuupdateuTrueuFalseuzipumatch(uselfunodeuresultsucuru subpatternuchild((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu _submatchOs " "uNodePattern._submatchNF(u__name__u __module__u __qualname__uFalseu wildcardsuNoneu__init__u _submatch(u __locals__((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu NodePattern.su NodePatterncBs|EeZdZdZddedddZddZdddZdd d Z d d Z d dZ ddZ ddZ dS(uWildcardPatternu A wildcard pattern can match zero or more nodes. This has all the flexibility needed to implement patterns like: .* .+ .? .{m,n} (a b c | d e | f) (...)* (...)+ (...)? (...){m,n} except it always uses non-greedy matching. icCs]|dk r5ttt|}x|D]}q(Wn||_||_||_||_dS(u Initializer. Args: content: optional sequence of subsequences of patterns; if absent, matches one node; if present, each subsequence is an alternative [*] min: optional minimum number of times to match, default 0 max: optional maximum number of times to match, default HUGE name: optional name assigned to this match [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is equivalent to (a b c | d e | f g h); if content is None, this is equivalent to '.' in regular expression terms. The min and max parameters work as follows: min=0, max=maxint: .* min=1, max=maxint: .+ min=0, max=1: .? min=1, max=1: . If content is not None, replace the dot with the parenthesized list of alternatives, e.g. (a b c | d e | f g h)* N(uNoneutupleumapucontentuminumaxuname(uselfucontentuminumaxunameualt((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu__init__ys     uWildcardPattern.__init__c Cs/d}|jdk rWt|jdkrWt|jddkrW|jdd}n|jdkr|jdkr|jdkrtd|jS|dk r|j|jkr|jSn|jdkr+t|t r+|jdkr+|j|jkr+t |j|j|j|j|j|jS|S(u+Optimize certain stacked wildcard patterns.iiunameN( uNoneucontentulenuminumaxu NodePatternunameuoptimizeu isinstanceuWildcardPattern(uselfu subpattern((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuoptimizes . !    uWildcardPattern.optimizecCs|j|g|S(u'Does this pattern exactly match a node?(u match_seq(uselfunodeuresults((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyumatchsuWildcardPattern.matchcCsuxn|j|D]]\}}|t|kr|dk ri|j||jrit|||js uNegatedPattern.generate_matchesN(u__name__u __module__u __qualname__uNoneu__init__umatchu match_sequgenerate_matches(u __locals__((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyuNegatedPattern's  uNegatedPatternc cs|sdifVn|d|dd}}x|j|D]y\}}|s_||fVq?xVt|||dD];\}}i}|j||j||||fVqyWq?WdS(uR Generator yielding matches for a sequence of patterns and nodes. Args: patterns: a sequence of patterns nodes: a sequence of nodes Yields: (count, results) tuples where: count: the entire sequence of patterns matches nodes[:count]; results: dict containing named submatches. iiN(ugenerate_matchesuupdate( upatternsunodesupurestuc0ur0uc1ur1ur((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyugenerate_matchesJs &  ugenerate_matches(u__doc__u __author__usysuwarningsuiouStringIOuHUGEu _type_reprsu type_repruobjectuBaseuNodeuLeafuconvertu BasePatternu LeafPatternu NodePatternuWildcardPatternuNegatedPatternugenerate_matches(((u3/opt/alt/python33/lib64/python3.3/lib2to3/pytree.pyu s"   pN V,=#