Ejemplo n.º 1
0
    /**
     * Verifies that the actual <code>{@link Rule}</code> partially matches a given input.
     *
     * @param prefixToBeMatched the prefix that must be fully matched
     * @param remainingInput the remainder of the input, which is not to be matched
     * @return this assertion object.
     */
    public ParserAssert matchesPrefix(String prefixToBeMatched, String remainingInput) {
      isNotNull();
      try {
        JavaScriptTree tree = (JavaScriptTree) actual.parse(prefixToBeMatched + remainingInput);
        SyntaxToken lastToken = tree.getLastToken();

        if (prefixToBeMatched.length() != lastToken.column() + lastToken.text().length()) {
          throw new RecognitionException(
              0,
              "Rule '"
                  + getRuleName()
                  + "' should match:\n"
                  + prefixToBeMatched
                  + "\nwhen followed by:\n"
                  + remainingInput);
        }
      } catch (RecognitionException e) {
        throw new RecognitionException(
            0,
            e.getMessage()
                + "\n"
                + "Rule '"
                + getRuleName()
                + "' should match:\n"
                + prefixToBeMatched
                + "\nwhen followed by:\n"
                + remainingInput);
      }
      return this;
    }
Ejemplo n.º 2
0
 public ParserAssert matches(String input) {
   isNotNull();
   Preconditions.checkArgument(
       !hasTrailingWhitespaces(input), "Trailing whitespaces in input are not supported");
   String expected = "Rule '" + getRuleName() + "' should match:\n" + input;
   try {
     parseTillEof(input);
   } catch (RecognitionException e) {
     String actual = e.getMessage();
     throw new ParsingResultComparisonFailure(expected, actual);
   }
   return this;
 }
Ejemplo n.º 3
0
  private void simpleScan(File file, VisitorContext context) {
    context.setFile(file);
    try {
      Tree ast = parser.parse(file);
      visitor.visitFile(ast);
    } catch (RecognitionException e) {
      checkInterrrupted(e);
      LOG.error("Unable to parse source file : " + file.getAbsolutePath());
      LOG.error(e.getMessage());

      parseErrorWalkAndVisit(e, file);
    } catch (Exception e) {
      checkInterrrupted(e);
      throw new AnalysisException(getAnalyisExceptionMessage(file), e);
    }
  }
  private void analyse(
      SensorContext sensorContext, InputFile inputFile, List<TreeVisitor> visitors) {
    ScriptTree scriptTree;

    try {
      scriptTree = (ScriptTree) parser.parse(new java.io.File(inputFile.absolutePath()));
      scanFile(sensorContext, inputFile, visitors, scriptTree);

    } catch (RecognitionException e) {
      checkInterrupted(e);
      LOG.error("Unable to parse file: " + inputFile.absolutePath());
      LOG.error(e.getMessage());
      processRecognitionException(e, sensorContext, inputFile);

    } catch (Exception e) {
      checkInterrupted(e);
      processException(e, sensorContext, inputFile);
      throw new AnalysisException("Unable to analyse file: " + inputFile.absolutePath(), e);
    }
  }
  private void processRecognitionException(
      RecognitionException e, SensorContext sensorContext, InputFile inputFile) {
    if (parsingErrorRuleKey != null) {
      NewIssue newIssue = sensorContext.newIssue();

      NewIssueLocation primaryLocation =
          newIssue
              .newLocation()
              .message(e.getMessage())
              .on(inputFile)
              .at(inputFile.selectLine(e.getLine()));

      newIssue.forRule(parsingErrorRuleKey).at(primaryLocation).save();
    }

    if (sensorContext.getSonarQubeVersion().isGreaterThanOrEqual(V6_0)) {
      sensorContext
          .newAnalysisError()
          .onFile(inputFile)
          .at(inputFile.newPointer(e.getLine(), 0))
          .message(e.getMessage())
          .save();
    }
  }