Java Code Examples for com.intellij.util.containers.Stack#isEmpty()

The following examples show how to use com.intellij.util.containers.Stack#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HaxeConditionalExpression.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private boolean reevaluate(Project context) {
  this.context = context;
  boolean ret = false;
  if (isComplete()) {
    try {
      Stack<ASTNode> rpn = infixToRPN();
      String rpnString = LOG.isDebugEnabled() ? tokensToString(rpn) : null;
      ret = objectIsTrue(calculateRPN(rpn));
      if (LOG.isDebugEnabled()) {  // Don't create the strings unless we are debugging them...
        LOG.debug(toString() + " --> " + rpnString + " ==> " + (ret ? "true" : "false"));
      }
      if (!rpn.isEmpty()) {
        throw new CalculationException("Invalid Expression: Tokens left after calculating: " + rpn.toString());
      }
    } catch (CalculationException e) {
      String msg = "Error calculating conditional compiler expression '" + toString() + "'";
      // Add stack info if in debug mode.
      LOG.info( msg, LOG.isDebugEnabled() ? e : null );
    }
  }
  return ret;
}
 
Example 2
Source File: ProjectDataManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> void ensureTheDataIsReadyToUse(@Nonnull Collection<DataNode<T>> nodes) {
  Map<Key<?>, List<ProjectDataService<?, ?>>> servicesByKey = myServices.getValue();
  Stack<DataNode<T>> toProcess = ContainerUtil.newStack(nodes);
  while (!toProcess.isEmpty()) {
    DataNode<T> node = toProcess.pop();
    List<ProjectDataService<?, ?>> services = servicesByKey.get(node.getKey());
    if (services != null) {
      for (ProjectDataService<?, ?> service : services) {
        node.prepareData(service.getClass().getClassLoader());
      }
    }

    for (DataNode<?> dataNode : node.getChildren()) {
      toProcess.push((DataNode<T>)dataNode);
    }
  }
}
 
Example 3
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findLeftmostLParen(HighlighterIterator iterator,
                                     IElementType lparenTokenType,
                                     CharSequence fileText,
                                     FileType fileType) {
  int lastLbraceOffset = -1;

  Stack<IElementType> braceStack = new Stack<IElementType>();
  for (; !iterator.atEnd(); iterator.retreat()) {
    final IElementType tokenType = iterator.getTokenType();

    if (isLBraceToken(iterator, fileText, fileType)) {
      if (!braceStack.isEmpty()) {
        IElementType topToken = braceStack.pop();
        if (!isPairBraces(tokenType, topToken, fileType)) {
          break; // unmatched braces
        }
      }
      else {
        if (tokenType == lparenTokenType) {
          lastLbraceOffset = iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isRBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastLbraceOffset;
}
 
Example 4
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findLeftLParen(HighlighterIterator iterator,
                                 IElementType lparenTokenType,
                                 CharSequence fileText,
                                 FileType fileType) {
  int lastLbraceOffset = -1;

  Stack<IElementType> braceStack = new Stack<IElementType>();
  for (; !iterator.atEnd(); iterator.retreat()) {
    final IElementType tokenType = iterator.getTokenType();

    if (isLBraceToken(iterator, fileText, fileType)) {
      if (!braceStack.isEmpty()) {
        IElementType topToken = braceStack.pop();
        if (!isPairBraces(tokenType, topToken, fileType)) {
          break; // unmatched braces
        }
      }
      else {
        if (tokenType == lparenTokenType) {
          return iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isRBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastLbraceOffset;
}
 
Example 5
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findRightmostRParen(HighlighterIterator iterator,
                                      IElementType rparenTokenType,
                                      CharSequence fileText,
                                      FileType fileType) {
  int lastRbraceOffset = -1;

  Stack<IElementType> braceStack = new Stack<IElementType>();
  for (; !iterator.atEnd(); iterator.advance()) {
    final IElementType tokenType = iterator.getTokenType();

    if (isRBraceToken(iterator, fileText, fileType)) {
      if (!braceStack.isEmpty()) {
        IElementType topToken = braceStack.pop();
        if (!isPairBraces(tokenType, topToken, fileType)) {
          break; // unmatched braces
        }
      }
      else {
        if (tokenType == rparenTokenType) {
          lastRbraceOffset = iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isLBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastRbraceOffset;
}
 
Example 6
Source File: HaxeConditionalExpression.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
/**
 * Converts an infix expression into an RPN expression.  (Re-orders and removes parenthesis.)
 * For example: !(cpp && js) -> cpp js && !
 *         and: (( cpp || js ) && (haxe-ver < 3))  -> cpp js || haxe-ver 3 < &&
 * See https://en.wikipedia.org/wiki/Reverse_Polish_notation
 * @return
 * @throws CalculationException
 */
private Stack<ASTNode> infixToRPN() throws CalculationException {
  // This is a simplified shunting-yard algorithm: http://https://en.wikipedia.org/wiki/Shunting-yard_algorithm
  Stack<ASTNode> rpnOutput = new Stack<ASTNode>();
  Stack<ASTNode> operatorStack = new Stack<ASTNode>();

  try {
    for (ASTNode token : tokens) {
      if (isWhitespace(token)) {
        continue;
      }
      if (isLiteral(token) || isStringQuote(token) || isString(token)) {
        rpnOutput.push(token);
      }
      else if (isLeftParen(token)) {
        operatorStack.push(token);
      }
      else if (isRightParen(token)) {
        boolean foundLeftParen = false;
        while (!operatorStack.isEmpty()) {
          ASTNode op = operatorStack.pop();
          if (!isLeftParen(op)) {
            rpnOutput.push(op);
          }
          else {
            foundLeftParen = true;
            break;
          }
        }
        if (operatorStack.isEmpty() && !foundLeftParen) {
          // mismatched parens.
          // TODO: Report errors back through a reporter class.
          throw new CalculationException("Mismatched right parenthesis.");
        }
      }
      else if (isCCOperator(token)) {
        while (!operatorStack.isEmpty()
               && !isLeftParen(operatorStack.peek())  // Parens have the highest priority, but should not be considered for comparison.
               && HaxeOperatorPrecedenceTable.shuntingYardCompare(token.getElementType(), operatorStack.peek().getElementType())) {
          rpnOutput.push(operatorStack.pop());
        }
        operatorStack.push(token);
      }
      else {
        throw new CalculationException("Couldn't process token '" + token.toString() + "' when converting to RPN.");
      }
    }
  } catch (HaxeOperatorPrecedenceTable.OperatorNotFoundException e) {
    LOG.warn("IntelliJ-Haxe plugin internal error: Unknown operator encountered while calculating compiler conditional exression:"
             + toString(), e);
    throw new CalculationException(e.toString());
  }

  // Anything left in the operator stack means an error.
  while(!operatorStack.isEmpty()) {
    ASTNode node = operatorStack.pop();
    if (isLeftParen(node)) {
      // Mismatched parens.
      // TODO: Report errors back through a reporter class.
      throw new CalculationException("Mismatched left parenthesis.");
    } else {
      rpnOutput.push(node);
    }
  }

  return rpnOutput;
}
 
Example 7
Source File: LightStubBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected void buildStubTree(@Nonnull LighterAST tree, @Nonnull LighterASTNode root, @Nonnull StubElement rootStub) {
  final Stack<LighterASTNode> parents = new Stack<>();
  final TIntStack childNumbers = new TIntStack();
  final BooleanStack parentsStubbed = new BooleanStack();
  final Stack<List<LighterASTNode>> kinderGarden = new Stack<>();
  final Stack<StubElement> parentStubs = new Stack<>();

  LighterASTNode parent = null;
  LighterASTNode element = root;
  List<LighterASTNode> children = null;
  int childNumber = 0;
  StubElement parentStub = rootStub;
  boolean immediateParentStubbed = true;

  nextElement:
  while (element != null) {
    ProgressManager.checkCanceled();

    final StubElement stub = createStub(tree, element, parentStub);
    boolean hasStub = stub != parentStub || parent == null;
    if (hasStub && !immediateParentStubbed) {
      ((ObjectStubBase) stub).markDangling();
    }

    if (parent == null || !skipNode(tree, parent, element)) {
      final List<LighterASTNode> kids = tree.getChildren(element);
      if (!kids.isEmpty()) {
        if (parent != null) {
          parents.push(parent);
          childNumbers.push(childNumber);
          kinderGarden.push(children);
          parentStubs.push(parentStub);
          parentsStubbed.push(immediateParentStubbed);
        }
        parent = element;
        immediateParentStubbed = hasStub;
        element = (children = kids).get(childNumber = 0);
        parentStub = stub;
        if (!skipNode(tree, parent, element)) continue nextElement;
      }
    }

    while (children != null && ++childNumber < children.size()) {
      element = children.get(childNumber);
      if (!skipNode(tree, parent, element)) continue nextElement;
    }

    element = null;
    while (!parents.isEmpty()) {
      parent = parents.pop();
      childNumber = childNumbers.pop();
      children = kinderGarden.pop();
      parentStub = parentStubs.pop();
      immediateParentStubbed = parentsStubbed.pop();
      while (++childNumber < children.size()) {
        element = children.get(childNumber);
        if (!skipNode(tree, parent, element)) continue nextElement;
      }
      element = null;
    }
  }
}
 
Example 8
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean findStructuralLeftBrace(@Nonnull FileType fileType, @Nonnull HighlighterIterator iterator, @Nonnull CharSequence fileText) {
  final Stack<IElementType> braceStack = new Stack<IElementType>();
  final Stack<String> tagNameStack = new Stack<String>();

  BraceMatcher matcher = getBraceMatcher(fileType, iterator);

  while (!iterator.atEnd()) {
    if (isStructuralBraceToken(fileType, iterator, fileText)) {
      if (isRBraceToken(iterator, fileText, fileType)) {
        braceStack.push(iterator.getTokenType());
        tagNameStack.push(getTagName(matcher, fileText, iterator));
      }
      if (isLBraceToken(iterator, fileText, fileType)) {
        if (braceStack.isEmpty()) return true;

        final int group = matcher.getBraceTokenGroupId(iterator.getTokenType());

        final IElementType topTokenType = braceStack.pop();
        final IElementType tokenType = iterator.getTokenType();

        boolean isStrict = isStrictTagMatching(matcher, fileType, group);
        boolean isCaseSensitive = areTagsCaseSensitive(matcher, fileType, group);

        String topTagName = null;
        String tagName = null;
        if (isStrict) {
          topTagName = tagNameStack.pop();
          tagName = getTagName(matcher, fileText, iterator);
        }

        if (!isPairBraces(topTokenType, tokenType, fileType)
            || isStrict && !Comparing.equal(topTagName, tagName, isCaseSensitive)) {
          return false;
        }
      }
    }

    iterator.retreat();
  }

  return false;
}
 
Example 9
Source File: ArrangementEngine.java    From consulo with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private <E extends ArrangementEntry> void doArrange(Context<E> context) {
  // The general idea is to process entries bottom-up where every processed group belongs to the same parent. We may not bother
  // with entries text ranges then. We use a list and a stack for achieving that than.
  //
  // Example:
  //            Entry1              Entry2
  //            /    \              /    \
  //      Entry11   Entry12    Entry21  Entry22
  //
  //    --------------------------
  //    Stage 1:
  //      list: Entry1 Entry2    <-- entries to process
  //      stack: [0, 0, 2]       <-- holds current iteration info at the following format:
  //                                 (start entry index at the auxiliary list (inclusive); current index; end index (exclusive))
  //    --------------------------
  //    Stage 2:
  //      list: Entry1 Entry2 Entry11 Entry12
  //      stack: [0, 1, 2]
  //             [2, 2, 4]
  //    --------------------------
  //    Stage 3:
  //      list: Entry1 Entry2 Entry11 Entry12
  //      stack: [0, 1, 2]
  //             [2, 3, 4]
  //    --------------------------
  //    Stage 4:
  //      list: Entry1 Entry2 Entry11 Entry12
  //      stack: [0, 1, 2]
  //             [2, 4, 4]
  //    --------------------------
  //      arrange 'Entry11 Entry12'
  //    --------------------------
  //    Stage 5:
  //      list: Entry1 Entry2
  //      stack: [0, 1, 2]
  //    --------------------------
  //    Stage 6:
  //      list: Entry1 Entry2 Entry21 Entry22
  //      stack: [0, 2, 2]
  //             [2, 2, 4]
  //    --------------------------
  //    Stage 7:
  //      list: Entry1 Entry2 Entry21 Entry22
  //      stack: [0, 2, 2]
  //             [2, 3, 4]
  //    --------------------------
  //    Stage 8:
  //      list: Entry1 Entry2 Entry21 Entry22
  //      stack: [0, 2, 2]
  //             [2, 4, 4]
  //    --------------------------
  //      arrange 'Entry21 Entry22'
  //    --------------------------
  //    Stage 9:
  //      list: Entry1 Entry2
  //      stack: [0, 2, 2]
  //    --------------------------
  //      arrange 'Entry1 Entry2'

  List<ArrangementEntryWrapper<E>> entries = new ArrayList<ArrangementEntryWrapper<E>>();
  Stack<StackEntry> stack = new Stack<StackEntry>();
  entries.addAll(context.wrappers);
  stack.push(new StackEntry(0, context.wrappers.size()));
  while (!stack.isEmpty()) {
    StackEntry stackEntry = stack.peek();
    if (stackEntry.current >= stackEntry.end) {
      List<ArrangementEntryWrapper<E>> subEntries = entries.subList(stackEntry.start, stackEntry.end);
      // arrange entries even if subEntries.size() == 1, because we don't want to miss new section comments here
      doArrange(subEntries, context);
      subEntries.clear();
      stack.pop();
    }
    else {
      ArrangementEntryWrapper<E> wrapper = entries.get(stackEntry.current++);
      List<ArrangementEntryWrapper<E>> children = wrapper.getChildren();
      if (!children.isEmpty()) {
        entries.addAll(children);
        stack.push(new StackEntry(stackEntry.end, children.size()));
      }
    }
  }
}