Пример #1
0
 public void traverse(VisitorIF<?> visitor) throws Exception {
   if (elts != null) {
     for (PythonTree t : elts) {
       if (t != null) t.accept(visitor);
     }
   }
 }
Пример #2
0
 void recurseSetContext(PythonTree tree, expr_contextType context) {
   if (tree instanceof Context) {
     ((Context) tree).setContext(context);
   }
   if (tree instanceof GeneratorExp) {
     GeneratorExp g = (GeneratorExp) tree;
     recurseSetContext(g.getInternalElt(), context);
   } else if (tree instanceof ListComp) {
     ListComp lc = (ListComp) tree;
     recurseSetContext(lc.getInternalElt(), context);
   } else if (tree instanceof SetComp) {
     SetComp sc = (SetComp) tree;
     recurseSetContext(sc.getInternalElt(), context);
   } else if (tree instanceof DictComp) {
     DictComp dc = (DictComp) tree;
     recurseSetContext(dc.getInternalKey(), context);
     recurseSetContext(dc.getInternalValue(), context);
   } else if (!(tree instanceof ListComp)
       && (!(tree instanceof DictComp))
       && (!(tree instanceof SetComp))) {
     for (int i = 0; i < tree.getChildCount(); i++) {
       recurseSetContext(tree.getChild(i), context);
     }
   }
 }
Пример #3
0
 public void freshenParentAndChildIndexes(int offset) {
   int n = getChildCount();
   for (int c = offset; c < n; c++) {
     PythonTree child = (PythonTree) getChild(c);
     child.setChildIndex(c);
     child.setParent(this);
   }
 }
    public DeclarationLocation getLocation() {
      PythonTree node = element.getNode();
      int lineOffset = node != null ? node.getCharStartIndex() : -1;
      DeclarationLocation loc =
          new DeclarationLocation(element.getFileObject(), lineOffset, element);

      return loc;
    }
Пример #5
0
 public PythonTree getFirstChildWithType(int type) {
   for (int i = 0; children != null && i < children.size(); i++) {
     PythonTree t = (PythonTree) children.get(i);
     if (t.getAntlrType() == type) {
       return t;
     }
   }
   return null;
 }
Пример #6
0
 /**
  * Delete children from start to stop and replace with t even if t is a list (nil-root tree). num
  * of children can increase or decrease. For huge child lists, inserting children can force
  * walking rest of children to set their childindex; could be slow.
  */
 public void replaceChildren(int startChildIndex, int stopChildIndex, Object t) {
   /*
   System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+
   				   " with "+((PythonTree)t).toStringTree());
   System.out.println("in="+toStringTree());
   */
   if (children == null) {
     throw new IllegalArgumentException("indexes invalid; no children in list");
   }
   int replacingHowMany = stopChildIndex - startChildIndex + 1;
   int replacingWithHowMany;
   PythonTree newTree = (PythonTree) t;
   List newChildren = null;
   // normalize to a list of children to add: newChildren
   if (newTree.isNil()) {
     newChildren = newTree.children;
   } else {
     newChildren = new ArrayList(1);
     newChildren.add(newTree);
   }
   replacingWithHowMany = newChildren.size();
   int numNewChildren = newChildren.size();
   int delta = replacingHowMany - replacingWithHowMany;
   // if same number of nodes, do direct replace
   if (delta == 0) {
     int j = 0; // index into new children
     for (int i = startChildIndex; i <= stopChildIndex; i++) {
       PythonTree child = (PythonTree) newChildren.get(j);
       children.set(i, child);
       child.setParent(this);
       child.setChildIndex(i);
       j++;
     }
   } else if (delta > 0) { // fewer new nodes than there were
     // set children and then delete extra
     for (int j = 0; j < numNewChildren; j++) {
       children.set(startChildIndex + j, newChildren.get(j));
     }
     int indexToDelete = startChildIndex + numNewChildren;
     for (int c = indexToDelete; c <= stopChildIndex; c++) {
       // delete same index, shifting everybody down each time
       PythonTree killed = (PythonTree) children.remove(indexToDelete);
     }
     freshenParentAndChildIndexes(startChildIndex);
   } else { // more new nodes than were there before
     // fill in as many children as we can (replacingHowMany) w/o moving data
     for (int j = 0; j < replacingHowMany; j++) {
       children.set(startChildIndex + j, newChildren.get(j));
     }
     int numToInsert = replacingWithHowMany - replacingHowMany;
     for (int j = replacingHowMany; j < replacingWithHowMany; j++) {
       children.add(startChildIndex + j, newChildren.get(j));
     }
     freshenParentAndChildIndexes(startChildIndex);
   }
   // System.out.println("out="+toStringTree());
 }
Пример #7
0
 List<Name> makeDottedName(Token top, List<PythonTree> attrs) {
   List<Name> result = new ArrayList<Name>();
   result.add(new Name(top, top.getText(), expr_contextType.Load));
   if (attrs != null) {
     for (PythonTree attr : attrs) {
       Token token = attr.getToken();
       result.add(new Name(token, token.getText(), expr_contextType.Load));
     }
   }
   return result;
 }
Пример #8
0
 public void traverse(VisitorIF<?> visitor) throws Exception {
   if (test != null) test.accept(visitor);
   if (body != null) {
     for (PythonTree t : body) {
       if (t != null) t.accept(visitor);
     }
   }
   if (orelse != null) {
     for (PythonTree t : orelse) {
       if (t != null) t.accept(visitor);
     }
   }
 }
Пример #9
0
 public void setChild(int i, PythonTree t) {
   if (t == null) {
     return;
   }
   if (t.isNil()) {
     throw new IllegalArgumentException("Can't set single child to a list");
   }
   if (children == null) {
     children = createChildrenList();
   }
   children.set(i, t);
   t.setParent(this);
   t.setChildIndex(i);
 }
Пример #10
0
 public String toStringTree() {
   if (children == null || children.size() == 0) {
     return this.toString(); // + "[" + this.info() + "]";
   }
   StringBuffer buf = new StringBuffer();
   if (!isNil()) {
     buf.append("(");
     buf.append(this.toString()); // + "[" + this.info() + "]");
     buf.append(' ');
   }
   for (int i = 0; children != null && i < children.size(); i++) {
     PythonTree t = (PythonTree) children.get(i);
     if (i > 0) {
       buf.append(' ');
     }
     buf.append(t.toStringTree());
   }
   if (!isNil()) {
     buf.append(")");
   }
   return buf.toString();
 }
Пример #11
0
 /**
  * Add t as child of this node.
  *
  * <p>Warning: if t has no children, but child does and child isNil then this routine moves
  * children to t via t.children = child.children; i.e., without copying the array.
  */
 public void addChild(PythonTree t) {
   // System.out.println("add child "+t.toStringTree()+" "+this.toStringTree());
   // System.out.println("existing children: "+children);
   if (t == null) {
     return; // do nothing upon addChild(null)
   }
   PythonTree childTree = (PythonTree) t;
   if (childTree.isNil()) { // t is an empty node possibly with children
     if (this.children != null && this.children == childTree.children) {
       throw new RuntimeException("attempt to add child list to itself");
     }
     // just add all of childTree's children to this
     if (childTree.children != null) {
       if (this.children != null) { // must copy, this has children already
         int n = childTree.children.size();
         for (int i = 0; i < n; i++) {
           PythonTree c = (PythonTree) childTree.children.get(i);
           this.children.add(c);
           // handle double-link stuff for each child of nil root
           c.setParent(this);
           c.setChildIndex(children.size() - 1);
         }
       } else {
         // no children for this but t has children; just set pointer
         // call general freshener routine
         this.children = childTree.children;
         this.freshenParentAndChildIndexes();
       }
     }
   } else { // child is not nil (don't care about children)
     if (children == null) {
       children = createChildrenList(); // create children list on demand
     }
     children.add(t);
     childTree.setParent(this);
     childTree.setChildIndex(children.size() - 1);
   }
   // System.out.println("now children are: "+children);
 }
Пример #12
0
 public Object visit(PythonTree node) throws Exception {
   line = node.getLine();
   col = node.getCharPositionInLine();
   return super.visit(node);
 }
  private DeclarationLocation findImport(
      PythonParserResult info, String moduleName, String symbol) {
    PythonIndex index = PythonIndex.get(info.getSnapshot().getSource().getFileObject());

    Set<IndexedElement> elements = null;

    if (moduleName != null && symbol != null) {
      elements =
          index.getImportedElements(
              symbol, QuerySupport.Kind.EXACT, Collections.<String>singleton(moduleName), null);
    }

    if (symbol != null && (elements == null || elements.size() == 0)) {
      elements = index.getInheritedElements(null, symbol, QuerySupport.Kind.EXACT);
    }

    if (elements == null || elements.size() == 0) {
      elements = index.getModules(moduleName, QuerySupport.Kind.EXACT);
    }

    if (elements != null && elements.size() > 0) {
      AstPath path = null;
      PythonTree node = null;
      int astOffset = -1;
      int lexOffset = -1;
      return getDeclaration(info, null /*name*/, elements, path, node, index, astOffset, lexOffset);
    }

    // This never gets executed, right? Delete after EA
    for (IndexedElement candidate : elements) {
      // TODO - pick the best of the alternatives here?
      FileObject fileObject = candidate.getFileObject();
      if (fileObject == null) {
        return DeclarationLocation.NONE;
      }

      PythonTree node = candidate.getNode();
      int nodeOffset = node != null ? node.getCharStartIndex() : 0;

      DeclarationLocation loc = new DeclarationLocation(fileObject, nodeOffset, candidate);

      if (elements.size() > 1) {
        // Could the :nodoc: alternatives: if there is only one nodoc'ed alternative
        // don't ask user!
        int not_nodoced = 0;
        for (final IndexedElement mtd : elements) {
          if (!mtd.isNoDoc()) {
            not_nodoced++;
          }
        }
        if (not_nodoced >= 2) {
          for (final IndexedElement mtd : elements) {
            loc.addAlternative(new PythonAltLocation(mtd, mtd == candidate));
          }
        }
      }

      return loc;
    }

    return DeclarationLocation.NONE;
  }
Пример #14
0
 void cantBeNone(PythonTree e) {
   if (e.getText().equals("None")) {
     errorHandler.error("can't be None", e);
   }
 }
Пример #15
0
 String makeFromText(List dots, List<Name> names) {
   StringBuilder d = new StringBuilder();
   d.append(PythonTree.dottedNameListToString(names));
   return d.toString();
 }
Пример #16
0
 public PythonTree(PythonTree tree) {
   node = new CommonTree(tree.getNode());
   charStartIndex = tree.getCharStartIndex();
   charStopIndex = tree.getCharStopIndex();
 }
Пример #17
0
 public CompiledCode(PythonTree code) throws Exception {
   init(code.getChildren());
 }