Package com.sonar.sslr.api

Examples of com.sonar.sslr.api.AstNode


  /** Exclude subsequent generated nodes, if they are consecutive and on the same line.
   */
  private boolean isGeneratedNodeExcluded(AstNode astNode)
  {
    AstNode prev = astNode.getPreviousAstNode();
    return prev != null &&
           prev.getTokenLine() == astNode.getTokenLine() &&
           prev.getTokenLine() == astNode.getTokenLine() &&
           prev.isCopyBookOrGeneratedNode();
  }
View Full Code Here


  private boolean isBreakStatementExcluded(AstNode astNode)
  {
    if (!excludeCaseBreak || astNode.getToken().getType() != CxxKeyword.BREAK)
      return false;

    AstNode switchGroup = astNode.getFirstAncestor(CxxGrammarImpl.switchBlockStatementGroup);
    return switchGroup != null
        && switchGroup.getTokenLine() == astNode.getTokenLine();
  }
View Full Code Here

        && switchGroup.getTokenLine() == astNode.getTokenLine();
  }

  @Override
  public boolean isExcluded(AstNode astNode) {
    AstNode statementNode = astNode.getFirstChild();
    return statementNode.is(CxxGrammarImpl.compoundStatement)
      || statementNode.is(CxxGrammarImpl.emptyStatement)
      || statementNode.is(CxxGrammarImpl.iterationStatement)
      || statementNode.is(CxxGrammarImpl.labeledStatement)
      || statementNode.is(CxxGrammarImpl.declaration)
      || (statementNode.isCopyBookOrGeneratedNode() && isGeneratedNodeExcluded(statementNode))
      || (statementNode.is(CxxGrammarImpl.jumpStatement) && isBreakStatementExcluded(statementNode));
}
View Full Code Here

      CxxGrammarImpl.iterationStatement);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode statement = astNode.getFirstChild(CxxGrammarImpl.statement);
    if (!statement.getFirstChild().is(CxxGrammarImpl.compoundStatement)) {
      getContext().createLineViolation(this, "Missing curly brace.", astNode);
    }

    if (astNode.is(CxxGrammarImpl.ifStatement)) {
      AstNode elseClause = astNode.getFirstChild(CxxKeyword.ELSE);
      if (elseClause != null) {
        statement = elseClause.getNextSibling();
        if (!statement.getFirstChild().is(CxxGrammarImpl.compoundStatement) && !statement.getFirstChild().is(CxxGrammarImpl.ifStatement)) {
          getContext().createLineViolation(this, "Missing curly brace.", elseClause);
        }
      }
    }
View Full Code Here

  }

  @Override
  public void visitNode(AstNode node) {
    if (!hasElseClause(node) && !hasDeclaration(node)) {
      AstNode enclosingIfStatement = getEnclosingIfStatement(node);
      if (enclosingIfStatement != null && !hasElseClause(enclosingIfStatement) && hasSingleTrueStatement(enclosingIfStatement) && !hasDeclaration(enclosingIfStatement)) {
        getContext().createLineViolation(this, "Merge this if statement with the enclosing one.", node);
      }
    }
  }
View Full Code Here

   * Verify if the ifStatement's condition is actually a variable declaration.
   * This is the case if the condition is not an expression. This prevents collapse, since multiple definitions and
   * expressions cannot be combined.
   */
  private static boolean hasDeclaration(AstNode node) {
    AstNode condition = node.getFirstChild(CxxGrammarImpl.condition);
    return !(condition.getNumberOfChildren() == 1 && condition.getFirstChild().is(CxxGrammarImpl.expression));
  }
View Full Code Here

    return !(condition.getNumberOfChildren() == 1 && condition.getFirstChild().is(CxxGrammarImpl.expression));
  }

  @Nullable
  private static AstNode getEnclosingIfStatement(AstNode node) {
    AstNode grandParent = node.getParent().getParent();
    if (grandParent.is(CxxGrammarImpl.ifStatement)) {
      return grandParent;
    } else if (!grandParent.is(CxxGrammarImpl.statementSeq)) {
      return null;
    }

    AstNode statement = grandParent.getFirstAncestor(CxxGrammarImpl.compoundStatement).getParent();
    if (!statement.is(CxxGrammarImpl.statement)) {
      return null;
    }

    AstNode enclosingStatement = statement.getParent();
    return enclosingStatement.is(CxxGrammarImpl.ifStatement) ? enclosingStatement : null;
  }
View Full Code Here

    AstNode enclosingStatement = statement.getParent();
    return enclosingStatement.is(CxxGrammarImpl.ifStatement) ? enclosingStatement : null;
  }

  private static boolean hasSingleTrueStatement(AstNode node) {
    AstNode statement = node.getFirstChild(CxxGrammarImpl.statement);
    return statement.hasDirectChildren(CxxGrammarImpl.compoundStatement) ?
        statement.getFirstChild(CxxGrammarImpl.compoundStatement).getFirstChild(CxxGrammarImpl.statementSeq).getChildren().size() == 1 : true;
  }
View Full Code Here

  @Test
  public void test() {
    assertThat(CppPunctuator.values()).hasSize(70);

    AstNode astNode = mock(AstNode.class);
    for (CppPunctuator punctuator : CppPunctuator.values()) {
      assertThat(punctuator.hasToBeSkippedFromAst(astNode)).isFalse();
    }
  }
View Full Code Here

  @Test
  public void test() {
    assertThat(CxxPunctuator.values()).hasSize(49);

    AstNode astNode = mock(AstNode.class);
    for (CxxPunctuator punctuator : CxxPunctuator.values()) {
      assertThat(punctuator.hasToBeSkippedFromAst(astNode)).isFalse();
    }
  }
View Full Code Here

TOP

Related Classes of com.sonar.sslr.api.AstNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.