Package com.sonar.sslr.api

Examples of com.sonar.sslr.api.AstNode


    subscribeTo(CxxGrammarImpl.recoveredDeclaration);
  }

  @Override
  public void visitNode(AstNode node) {
    AstNode identifierAst = node.getFirstChild(GenericTokenType.IDENTIFIER);
    if( identifierAst != null ) {
      CxxGrammarImpl.LOG.warn("[{}:{}]: syntax error, skip '{}'", new Object[] {context.getFile(), node.getToken().getLine(), identifierAst.getTokenValue()});
    }
  }
View Full Code Here


    File file = getFileUnderAnalysis();
    String filePath = file == null ? token.getURI().toString() : file.getAbsolutePath();

    if (ttype == PREPROCESSOR) {

      AstNode lineAst = null;
      try {
        lineAst = pplineParser.parse(token.getValue()).getFirstChild();
      } catch (com.sonar.sslr.api.RecognitionException re) {
        LOG.warn("Cannot parse '{}', ignoring...", token.getValue());
        return new PreprocessorAction(1, Lists.newArrayList(Trivia.createSkippedText(token)), new ArrayList<Token>());
      }

      String lineKind = lineAst.getName();

      if ("ifdefLine".equals(lineKind)) {
        return handleIfdefLine(lineAst, token, filePath);
      } else if ("elseLine".equals(lineKind)) {
        return handleElseLine(lineAst, token, filePath);
View Full Code Here

    return new PreprocessorAction(1, Lists.newArrayList(Trivia.createSkippedText(token)), new ArrayList<Token>());
  }

  private void parseIncludeLine(String includeLine, String filename) {
    AstNode includeAst = pplineParser.parse(includeLine);
    handleIncludeLine(includeAst, includeAst.getFirstDescendant(CppGrammar.includeBodyQuoted).getToken(), filename);
  }
View Full Code Here

    return parseMacroDefinition(pplineParser.parse(macroDef)
        .getFirstDescendant(CppGrammar.defineLine));
  }

  private Macro parseMacroDefinition(AstNode defineLineAst) {
    AstNode ast = defineLineAst.getFirstChild();
    AstNode nameNode = ast.getFirstDescendant(CppGrammar.ppToken);
    String macroName = nameNode.getTokenValue();

    AstNode paramList = ast.getFirstDescendant(CppGrammar.parameterList);
    List<Token> macroParams = paramList == null
        ? ast.getName().equals("objectlikeMacroDefinition") ? null : new LinkedList<Token>()
        : getParams(paramList);

    AstNode vaargs = ast.getFirstDescendant(CppGrammar.variadicparameter);
    if (vaargs != null) {
        AstNode identifier = vaargs.getFirstChild(IDENTIFIER);
        macroParams.add(identifier == null
            ? Token.builder()
                .setLine(vaargs.getToken().getLine())
                .setColumn(vaargs.getToken().getColumn())
                .setURI(vaargs.getToken().getURI())
                .setValueAndOriginalValue("__VA_ARGS__")
                .setType(IDENTIFIER)
                .setGeneratedCode(true)
                .build()
            : identifier.getToken());
    }

    AstNode replList = ast.getFirstDescendant(CppGrammar.replacementList);
    List<Token> macroBody = replList == null
        ? new LinkedList<Token>()
        : replList.getTokens().subList(0, replList.getTokens().size() - 1);

    return new Macro(macroName, macroParams, macroBody, vaargs != null);
  }
View Full Code Here

  private File findIncludedFile(AstNode ast, Token token, String currFileName) {
    String includedFileName = null;
    File includedFile = null;
    boolean quoted = false;

    AstNode node = ast.getFirstDescendant(CppGrammar.includeBodyQuoted);
    if(node != null){
      includedFileName = stripQuotes(node.getFirstChild().getTokenValue());
      quoted = true;
    } else if((node = ast.getFirstDescendant(CppGrammar.includeBodyBracketed)) != null) {
      node = node.getFirstDescendant(LT).getNextSibling();
      StringBuilder sb = new StringBuilder();
      while (true) {
        String value = node.getTokenValue();
        if (value.equals(">")) {
          break;
        }
        sb.append(value);
        node = node.getNextSibling();
      }

      includedFileName = sb.toString();
    } else if((node = ast.getFirstDescendant(CppGrammar.includeBodyFreeform)) != null) {
      // expand and recurse
      String includeBody = serialize(stripEOF(node.getTokens()), "");
      String expandedIncludeBody = serialize(stripEOF(CxxLexer.create(this).lex(includeBody)), "");

      boolean parseError = false;
      AstNode includeBodyAst = null;
      try{
        includeBodyAst = pplineParser.parse("#include " + expandedIncludeBody);
      }
      catch(com.sonar.sslr.api.RecognitionException re){
        parseError = true;
      }

      if(parseError || includeBodyAst.getFirstDescendant(CppGrammar.includeBodyFreeform) != null){
        LOG.warn("[{}:{}]: cannot parse included filename: {}'",
                 new Object[] {currFileName, token.getLine(), expandedIncludeBody});
        return null;
      }
View Full Code Here

        .build());

    /* Classes */
    builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<Grammar>(new SourceCodeBuilderCallback() {
      public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
        AstNode classNameAst = astNode.getFirstDescendant(CxxGrammarImpl.className);
        String className = classNameAst == null ? "" : classNameAst.getFirstChild().getTokenValue();
        SourceClass cls = new SourceClass(className + ":" + astNode.getToken().getLine(), className);
        cls.setStartAtLine(astNode.getTokenLine());
        return cls;
      }
    }, CxxGrammarImpl.classSpecifier));
View Full Code Here

  public boolean eval(AstNode constExpr) {
    return evalToInt(constExpr).compareTo(BigInteger.ZERO) != 0;
  }

  private BigInteger evalToInt(String constExpr) {
    AstNode constExprAst = null;
    try {
      constExprAst = parser.parse(constExpr);
    } catch (com.sonar.sslr.api.RecognitionException re) {
      LOG.warn("Error evaluating expression '{}', assuming 0", constExpr);
      return BigInteger.ZERO;
View Full Code Here

  // ////////////// logical expressions ///////////////////////////
  BigInteger evalLogicalOrExpression(AstNode exprAst) {
    int noChildren = exprAst.getNumberOfChildren();
    boolean result = eval(exprAst.getChild(0));
    for(int i = 2; i < noChildren && result != true; i+=2){
      AstNode operand = exprAst.getChild(i);
      result = result || eval(operand);
    }

    return result ? BigInteger.ONE : BigInteger.ZERO;
  }
View Full Code Here

  BigInteger evalLogicalAndExpression(AstNode exprAst) {
    int noChildren = exprAst.getNumberOfChildren();
    boolean result = eval(exprAst.getChild(0));
    for(int i = 2; i < noChildren && result != false; i+=2){
      AstNode operand = exprAst.getChild(i);
      result = result && eval(operand);
    }

    return result ? BigInteger.ONE : BigInteger.ZERO;
  }
View Full Code Here

    return result ? BigInteger.ONE : BigInteger.ZERO;
  }

  BigInteger evalEqualityExpression(AstNode exprAst) {
    String operator = exprAst.getChild(1).getTokenValue();
    AstNode lhs = exprAst.getChild(0);
    AstNode rhs = exprAst.getChild(2);
    boolean result;
    if (operator.equals("==")) {
      result = evalToInt(lhs).compareTo(evalToInt(rhs)) == 0;
    } else if (operator.equals("!=")) {
      result = evalToInt(lhs).compareTo(evalToInt(rhs)) != 0;
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.