use of com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser in project checkstyle by checkstyle.
the class JavadocDetailNodeParser method createJavadocParser.
/**
* Parses block comment content as javadoc comment.
*
* @param blockComment
* block comment content.
* @param errorListener custom error listener
* @return parse tree
*/
private static JavadocParser createJavadocParser(String blockComment, DescriptiveErrorListener errorListener) {
final JavadocLexer lexer = new JavadocLexer(CharStreams.fromString(blockComment), true);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final JavadocParser parser = new JavadocParser(tokens);
// remove default error listeners
parser.removeErrorListeners();
// add custom error listener that logs syntax errors
parser.addErrorListener(errorListener);
// JavadocParserErrorStrategy stops parsing on first parse error encountered unlike the
// DefaultErrorStrategy used by ANTLR which rather attempts error recovery.
parser.setErrorHandler(new CheckstyleParserErrorStrategy());
return parser;
}
use of com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser in project checkstyle by checkstyle.
the class JavadocDetailNodeParser method parseJavadocAsDetailNode.
/**
* Parses Javadoc comment as DetailNode tree.
*
* @param javadocCommentAst
* DetailAST of Javadoc comment
* @return DetailNode tree of Javadoc comment
*/
public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
blockCommentLineNumber = javadocCommentAst.getLineNo();
final String javadocComment = JavadocUtil.getJavadocCommentContent(javadocCommentAst);
// Use a new error listener each time to be able to use
// one check instance for multiple files to be checked
// without getting side effects.
final DescriptiveErrorListener errorListener = new DescriptiveErrorListener();
// Log messages should have line number in scope of file,
// not in scope of Javadoc comment.
// Offset is line number of beginning of Javadoc comment.
errorListener.setOffset(javadocCommentAst.getLineNo() - 1);
final ParseStatus result = new ParseStatus();
try {
final JavadocParser javadocParser = createJavadocParser(javadocComment, errorListener);
final ParseTree javadocParseTree = javadocParser.javadoc();
final DetailNode tree = convertParseTreeToDetailNode(javadocParseTree);
// adjust first line to indent of /**
adjustFirstLineToJavadocIndent(tree, javadocCommentAst.getColumnNo() + JAVADOC_START.length());
result.setTree(tree);
result.firstNonTightHtmlTag = getFirstNonTightHtmlTag(javadocParser, errorListener.offset);
} catch (ParseCancellationException | IllegalArgumentException ex) {
ParseErrorMessage parseErrorMessage = null;
if (ex.getCause() instanceof FailedPredicateException || ex.getCause() instanceof NoViableAltException) {
final RecognitionException recognitionEx = (RecognitionException) ex.getCause();
if (recognitionEx.getCtx() instanceof JavadocParser.HtmlTagContext) {
final Token htmlTagNameStart = getMissedHtmlTag(recognitionEx);
parseErrorMessage = new ParseErrorMessage(errorListener.offset + htmlTagNameStart.getLine(), MSG_JAVADOC_MISSED_HTML_CLOSE, htmlTagNameStart.getCharPositionInLine(), htmlTagNameStart.getText());
}
}
if (parseErrorMessage == null) {
// If syntax error occurs then message is printed by error listener
// and parser throws this runtime exception to stop parsing.
// Just stop processing current Javadoc comment.
parseErrorMessage = errorListener.getErrorMessage();
}
result.setParseErrorMessage(parseErrorMessage);
}
return result;
}
Aggregations