Пример #1
0
  /**
   * Static helper method to parses a Java source file.
   *
   * @param aContents contains the contents of the file
   * @throws TokenStreamException if lexing failed
   * @throws RecognitionException if parsing failed
   * @return the root of the AST
   */
  public static DetailAST parse(FileContents aContents)
      throws RecognitionException, TokenStreamException {
    final String fullText = aContents.getText().getFullText().toString();
    final Reader sr = new StringReader(fullText);
    final GeneratedJavaLexer lexer = new GeneratedJavaLexer(sr);
    lexer.setFilename(aContents.getFilename());
    lexer.setCommentListener(aContents);
    lexer.setTreatAssertAsKeyword(true);
    lexer.setTreatEnumAsKeyword(true);

    final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(lexer);
    parser.setFilename(aContents.getFilename());
    parser.setASTNodeClass(DetailAST.class.getName());
    parser.compilationUnit();

    return (DetailAST) parser.getAST();
  }
Пример #2
0
  /**
   * Static helper method to parses a Java source file.
   *
   * @param contents contains the contents of the file
   * @return the root of the AST
   * @throws TokenStreamException if lexing failed
   * @throws RecognitionException if parsing failed
   */
  public static DetailAST parse(FileContents contents)
      throws RecognitionException, TokenStreamException {
    final String fullText = contents.getText().getFullText().toString();
    final Reader sr = new StringReader(fullText);
    final GeneratedJavaLexer lexer = new GeneratedJavaLexer(sr);
    lexer.setFilename(contents.getFileName());
    lexer.setCommentListener(contents);
    lexer.setTreatAssertAsKeyword(true);
    lexer.setTreatEnumAsKeyword(true);
    lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");

    final TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer);
    filter.hide(TokenTypes.SINGLE_LINE_COMMENT);
    filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN);

    final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(filter);
    parser.setFilename(contents.getFileName());
    parser.setASTNodeClass(DetailAST.class.getName());
    parser.compilationUnit();

    return (DetailAST) parser.getAST();
  }