001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle;
021
022import java.util.ArrayDeque;
023import java.util.Deque;
024import java.util.List;
025
026import org.antlr.v4.runtime.BailErrorStrategy;
027import org.antlr.v4.runtime.BaseErrorListener;
028import org.antlr.v4.runtime.BufferedTokenStream;
029import org.antlr.v4.runtime.CharStreams;
030import org.antlr.v4.runtime.CommonToken;
031import org.antlr.v4.runtime.CommonTokenStream;
032import org.antlr.v4.runtime.FailedPredicateException;
033import org.antlr.v4.runtime.InputMismatchException;
034import org.antlr.v4.runtime.NoViableAltException;
035import org.antlr.v4.runtime.Parser;
036import org.antlr.v4.runtime.ParserRuleContext;
037import org.antlr.v4.runtime.RecognitionException;
038import org.antlr.v4.runtime.Recognizer;
039import org.antlr.v4.runtime.Token;
040import org.antlr.v4.runtime.misc.Interval;
041import org.antlr.v4.runtime.misc.ParseCancellationException;
042import org.antlr.v4.runtime.tree.ParseTree;
043import org.antlr.v4.runtime.tree.TerminalNode;
044
045import com.google.common.base.CaseFormat;
046import com.puppycrawl.tools.checkstyle.api.DetailAST;
047import com.puppycrawl.tools.checkstyle.api.DetailNode;
048import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
049import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl;
050import com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer;
051import com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser;
052import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
053
054/**
055 * Used for parsing Javadoc comment as DetailNode tree.
056 *
057 */
058public class JavadocDetailNodeParser {
059
060    /**
061     * Message key of error message. Missed close HTML tag breaks structure
062     * of parse tree, so parser stops parsing and generates such error
063     * message. This case is special because parser prints error like
064     * {@code "no viable alternative at input 'b \n *\n'"} and it is not
065     * clear that error is about missed close HTML tag.
066     */
067    public static final String MSG_JAVADOC_MISSED_HTML_CLOSE = "javadoc.missed.html.close";
068
069    /**
070     * Message key of error message.
071     */
072    public static final String MSG_JAVADOC_WRONG_SINGLETON_TAG =
073        "javadoc.wrong.singleton.html.tag";
074
075    /**
076     * Parse error while rule recognition.
077     */
078    public static final String MSG_JAVADOC_PARSE_RULE_ERROR = "javadoc.parse.rule.error";
079
080    /**
081     * Message property key for the Unclosed HTML message.
082     */
083    public static final String MSG_UNCLOSED_HTML_TAG = "javadoc.unclosedHtml";
084
085    /** Symbols with which javadoc starts. */
086    private static final String JAVADOC_START = "/**";
087
088    /**
089     * Line number of the Block comment AST that is being parsed.
090     */
091    private int blockCommentLineNumber;
092
093    /**
094     * Custom error listener.
095     */
096    private DescriptiveErrorListener errorListener;
097
098    /**
099     * Parses Javadoc comment as DetailNode tree.
100     * @param javadocCommentAst
101     *        DetailAST of Javadoc comment
102     * @return DetailNode tree of Javadoc comment
103     */
104    public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
105        blockCommentLineNumber = javadocCommentAst.getLineNo();
106
107        final String javadocComment = JavadocUtil.getJavadocCommentContent(javadocCommentAst);
108
109        // Use a new error listener each time to be able to use
110        // one check instance for multiple files to be checked
111        // without getting side effects.
112        errorListener = new DescriptiveErrorListener();
113
114        // Log messages should have line number in scope of file,
115        // not in scope of Javadoc comment.
116        // Offset is line number of beginning of Javadoc comment.
117        errorListener.setOffset(javadocCommentAst.getLineNo() - 1);
118
119        final ParseStatus result = new ParseStatus();
120
121        try {
122            final JavadocParser javadocParser = createJavadocParser(javadocComment);
123
124            final ParseTree javadocParseTree = javadocParser.javadoc();
125
126            final DetailNode tree = convertParseTreeToDetailNode(javadocParseTree);
127            // adjust first line to indent of /**
128            adjustFirstLineToJavadocIndent(tree,
129                        javadocCommentAst.getColumnNo()
130                                + JAVADOC_START.length());
131            result.setTree(tree);
132            result.firstNonTightHtmlTag = getFirstNonTightHtmlTag(javadocParser);
133        }
134        catch (ParseCancellationException | IllegalArgumentException ex) {
135            ParseErrorMessage parseErrorMessage = null;
136
137            if (ex.getCause() instanceof FailedPredicateException
138                    || ex.getCause() instanceof NoViableAltException) {
139                final RecognitionException recognitionEx = (RecognitionException) ex.getCause();
140                if (recognitionEx.getCtx() instanceof JavadocParser.HtmlTagContext) {
141                    final Token htmlTagNameStart = getMissedHtmlTag(recognitionEx);
142                    parseErrorMessage = new ParseErrorMessage(
143                            errorListener.offset + htmlTagNameStart.getLine(),
144                            MSG_JAVADOC_MISSED_HTML_CLOSE,
145                            htmlTagNameStart.getCharPositionInLine(),
146                            htmlTagNameStart.getText());
147                }
148            }
149
150            if (parseErrorMessage == null) {
151                // If syntax error occurs then message is printed by error listener
152                // and parser throws this runtime exception to stop parsing.
153                // Just stop processing current Javadoc comment.
154                parseErrorMessage = errorListener.getErrorMessage();
155            }
156
157            result.setParseErrorMessage(parseErrorMessage);
158        }
159
160        return result;
161    }
162
163    /**
164     * Parses block comment content as javadoc comment.
165     * @param blockComment
166     *        block comment content.
167     * @return parse tree
168     */
169    private JavadocParser createJavadocParser(String blockComment) {
170        final JavadocLexer lexer = new JavadocLexer(CharStreams.fromString(blockComment));
171
172        final CommonTokenStream tokens = new CommonTokenStream(lexer);
173
174        final JavadocParser parser = new JavadocParser(tokens);
175
176        // remove default error listeners
177        parser.removeErrorListeners();
178
179        // add custom error listener that logs syntax errors
180        parser.addErrorListener(errorListener);
181
182        // JavadocParserErrorStrategy stops parsing on first parse error encountered unlike the
183        // DefaultErrorStrategy used by ANTLR which rather attempts error recovery.
184        parser.setErrorHandler(new JavadocParserErrorStrategy());
185
186        return parser;
187    }
188
189    /**
190     * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
191     *
192     * @param parseTreeNode root node of ParseTree
193     * @return root of DetailNode tree
194     * @noinspection SuspiciousArrayCast
195     */
196    private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
197        final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);
198
199        JavadocNodeImpl currentJavadocParent = rootJavadocNode;
200        ParseTree parseTreeParent = parseTreeNode;
201
202        while (currentJavadocParent != null) {
203            // remove unnecessary children tokens
204            if (currentJavadocParent.getType() == JavadocTokenTypes.TEXT) {
205                currentJavadocParent
206                        .setChildren((DetailNode[]) JavadocNodeImpl.EMPTY_DETAIL_NODE_ARRAY);
207            }
208
209            final JavadocNodeImpl[] children =
210                    (JavadocNodeImpl[]) currentJavadocParent.getChildren();
211
212            insertChildrenNodes(children, parseTreeParent);
213
214            if (children.length > 0) {
215                currentJavadocParent = children[0];
216                parseTreeParent = parseTreeParent.getChild(0);
217            }
218            else {
219                JavadocNodeImpl nextJavadocSibling = (JavadocNodeImpl) JavadocUtil
220                        .getNextSibling(currentJavadocParent);
221
222                ParseTree nextParseTreeSibling = getNextSibling(parseTreeParent);
223
224                if (nextJavadocSibling == null) {
225                    JavadocNodeImpl tempJavadocParent =
226                            (JavadocNodeImpl) currentJavadocParent.getParent();
227
228                    ParseTree tempParseTreeParent = parseTreeParent.getParent();
229
230                    while (nextJavadocSibling == null && tempJavadocParent != null) {
231                        nextJavadocSibling = (JavadocNodeImpl) JavadocUtil
232                                .getNextSibling(tempJavadocParent);
233
234                        nextParseTreeSibling = getNextSibling(tempParseTreeParent);
235
236                        tempJavadocParent = (JavadocNodeImpl) tempJavadocParent.getParent();
237                        tempParseTreeParent = tempParseTreeParent.getParent();
238                    }
239                }
240                currentJavadocParent = nextJavadocSibling;
241                parseTreeParent = nextParseTreeSibling;
242            }
243        }
244
245        return rootJavadocNode;
246    }
247
248    /**
249     * Creates child nodes for each node from 'nodes' array.
250     * @param parseTreeParent original ParseTree parent node
251     * @param nodes array of JavadocNodeImpl nodes
252     */
253    private void insertChildrenNodes(final JavadocNodeImpl[] nodes, ParseTree parseTreeParent) {
254        for (int i = 0; i < nodes.length; i++) {
255            final JavadocNodeImpl currentJavadocNode = nodes[i];
256            final ParseTree currentParseTreeNodeChild = parseTreeParent.getChild(i);
257            final JavadocNodeImpl[] subChildren =
258                    createChildrenNodes(currentJavadocNode, currentParseTreeNodeChild);
259            currentJavadocNode.setChildren((DetailNode[]) subChildren);
260        }
261    }
262
263    /**
264     * Creates children Javadoc nodes base on ParseTree node's children.
265     * @param parentJavadocNode node that will be parent for created children
266     * @param parseTreeNode original ParseTree node
267     * @return array of Javadoc nodes
268     */
269    private JavadocNodeImpl[]
270            createChildrenNodes(JavadocNodeImpl parentJavadocNode, ParseTree parseTreeNode) {
271        final JavadocNodeImpl[] children =
272                new JavadocNodeImpl[parseTreeNode.getChildCount()];
273
274        for (int j = 0; j < children.length; j++) {
275            final JavadocNodeImpl child =
276                    createJavadocNode(parseTreeNode.getChild(j), parentJavadocNode, j);
277
278            children[j] = child;
279        }
280        return children;
281    }
282
283    /**
284     * Creates root JavadocNodeImpl node base on ParseTree root node.
285     * @param parseTreeNode ParseTree root node
286     * @return root Javadoc node
287     */
288    private JavadocNodeImpl createRootJavadocNode(ParseTree parseTreeNode) {
289        final JavadocNodeImpl rootJavadocNode = createJavadocNode(parseTreeNode, null, -1);
290
291        final int childCount = parseTreeNode.getChildCount();
292        final DetailNode[] children = rootJavadocNode.getChildren();
293
294        for (int i = 0; i < childCount; i++) {
295            final JavadocNodeImpl child = createJavadocNode(parseTreeNode.getChild(i),
296                    rootJavadocNode, i);
297            children[i] = child;
298        }
299        rootJavadocNode.setChildren(children);
300        return rootJavadocNode;
301    }
302
303    /**
304     * Creates JavadocNodeImpl node on base of ParseTree node.
305     *
306     * @param parseTree ParseTree node
307     * @param parent DetailNode that will be parent of new node
308     * @param index child index that has new node
309     * @return JavadocNodeImpl node on base of ParseTree node.
310     */
311    private JavadocNodeImpl createJavadocNode(ParseTree parseTree, DetailNode parent, int index) {
312        final JavadocNodeImpl node = new JavadocNodeImpl();
313        if (parseTree.getChildCount() == 0
314                || "Text".equals(getNodeClassNameWithoutContext(parseTree))) {
315            node.setText(parseTree.getText());
316        }
317        else {
318            node.setText(getFormattedNodeClassNameWithoutContext(parseTree));
319        }
320        node.setColumnNumber(getColumn(parseTree));
321        node.setLineNumber(getLine(parseTree) + blockCommentLineNumber);
322        node.setIndex(index);
323        node.setType(getTokenType(parseTree));
324        node.setParent(parent);
325        node.setChildren((DetailNode[]) new JavadocNodeImpl[parseTree.getChildCount()]);
326        return node;
327    }
328
329    /**
330     * Adjust first line nodes to javadoc indent.
331     * @param tree DetailNode tree root
332     * @param javadocColumnNumber javadoc indent
333     */
334    private void adjustFirstLineToJavadocIndent(DetailNode tree, int javadocColumnNumber) {
335        if (tree.getLineNumber() == blockCommentLineNumber) {
336            ((JavadocNodeImpl) tree).setColumnNumber(tree.getColumnNumber() + javadocColumnNumber);
337            final DetailNode[] children = tree.getChildren();
338            for (DetailNode child : children) {
339                adjustFirstLineToJavadocIndent(child, javadocColumnNumber);
340            }
341        }
342    }
343
344    /**
345     * Gets line number from ParseTree node.
346     * @param tree
347     *        ParseTree node
348     * @return line number
349     */
350    private static int getLine(ParseTree tree) {
351        final int line;
352        if (tree instanceof TerminalNode) {
353            line = ((TerminalNode) tree).getSymbol().getLine() - 1;
354        }
355        else {
356            final ParserRuleContext rule = (ParserRuleContext) tree;
357            line = rule.start.getLine() - 1;
358        }
359        return line;
360    }
361
362    /**
363     * Gets column number from ParseTree node.
364     * @param tree
365     *        ParseTree node
366     * @return column number
367     */
368    private static int getColumn(ParseTree tree) {
369        final int column;
370        if (tree instanceof TerminalNode) {
371            column = ((TerminalNode) tree).getSymbol().getCharPositionInLine();
372        }
373        else {
374            final ParserRuleContext rule = (ParserRuleContext) tree;
375            column = rule.start.getCharPositionInLine();
376        }
377        return column;
378    }
379
380    /**
381     * Gets next sibling of ParseTree node.
382     * @param node ParseTree node
383     * @return next sibling of ParseTree node.
384     */
385    private static ParseTree getNextSibling(ParseTree node) {
386        ParseTree nextSibling = null;
387
388        if (node.getParent() != null) {
389            final ParseTree parent = node.getParent();
390            int index = 0;
391            while (true) {
392                final ParseTree currentNode = parent.getChild(index);
393                if (currentNode.equals(node)) {
394                    nextSibling = parent.getChild(index + 1);
395                    break;
396                }
397                index++;
398            }
399        }
400        return nextSibling;
401    }
402
403    /**
404     * Gets token type of ParseTree node from JavadocTokenTypes class.
405     * @param node ParseTree node.
406     * @return token type from JavadocTokenTypes
407     */
408    private static int getTokenType(ParseTree node) {
409        final int tokenType;
410
411        if (node.getChildCount() == 0) {
412            tokenType = ((TerminalNode) node).getSymbol().getType();
413        }
414        else {
415            final String className = getNodeClassNameWithoutContext(node);
416            final String typeName =
417                    CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, className);
418            tokenType = JavadocUtil.getTokenId(typeName);
419        }
420
421        return tokenType;
422    }
423
424    /**
425     * Gets class name of ParseTree node and removes 'Context' postfix at the
426     * end and formats it.
427     * @param node {@code ParseTree} node whose class name is to be formatted and returned
428     * @return uppercased class name without the word 'Context' and with appropriately
429     *     inserted underscores
430     */
431    private static String getFormattedNodeClassNameWithoutContext(ParseTree node) {
432        final String classNameWithoutContext = getNodeClassNameWithoutContext(node);
433        return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, classNameWithoutContext);
434    }
435
436    /**
437     * Gets class name of ParseTree node and removes 'Context' postfix at the
438     * end.
439     * @param node
440     *        ParseTree node.
441     * @return class name without 'Context'
442     */
443    private static String getNodeClassNameWithoutContext(ParseTree node) {
444        final String className = node.getClass().getSimpleName();
445        // remove 'Context' at the end
446        final int contextLength = 7;
447        return className.substring(0, className.length() - contextLength);
448    }
449
450    /**
451     * Method to get the missed HTML tag to generate more informative error message for the user.
452     * This method doesn't concern itself with
453     * <a href="https://www.w3.org/TR/html51/syntax.html#void-elements">void elements</a>
454     * since it is forbidden to close them.
455     * Missed HTML tags for the following tags will <i>not</i> generate an error message from ANTLR:
456     * {@code
457     * <p>
458     * <li>
459     * <tr>
460     * <td>
461     * <th>
462     * <body>
463     * <colgroup>
464     * <dd>
465     * <dt>
466     * <head>
467     * <html>
468     * <option>
469     * <tbody>
470     * <thead>
471     * <tfoot>
472     * }
473     * @param exception {@code NoViableAltException} object catched while parsing javadoc
474     * @return returns appropriate {@link Token} if a HTML close tag is missed;
475     *     null otherwise
476     */
477    private static Token getMissedHtmlTag(RecognitionException exception) {
478        Token htmlTagNameStart = null;
479        final Interval sourceInterval = exception.getCtx().getSourceInterval();
480        final List<Token> tokenList = ((BufferedTokenStream) exception.getInputStream())
481                .getTokens(sourceInterval.a, sourceInterval.b);
482        final Deque<Token> stack = new ArrayDeque<>();
483        int prevTokenType = JavadocTokenTypes.EOF;
484        for (final Token token : tokenList) {
485            final int tokenType = token.getType();
486            if (tokenType == JavadocTokenTypes.HTML_TAG_NAME
487                    && prevTokenType == JavadocTokenTypes.START) {
488                stack.push(token);
489            }
490            else if (tokenType == JavadocTokenTypes.HTML_TAG_NAME && !stack.isEmpty()) {
491                if (stack.peek().getText().equals(token.getText())) {
492                    stack.pop();
493                }
494                else {
495                    htmlTagNameStart = stack.pop();
496                }
497            }
498            prevTokenType = tokenType;
499        }
500        if (htmlTagNameStart == null) {
501            htmlTagNameStart = stack.pop();
502        }
503        return htmlTagNameStart;
504    }
505
506    /**
507     * This method is used to get the first non-tight HTML tag encountered while parsing javadoc.
508     * This shall eventually be reflected by the {@link ParseStatus} object returned by
509     * {@link #parseJavadocAsDetailNode(DetailAST)} method via the instance member
510     * {@link ParseStatus#firstNonTightHtmlTag}, and checks not supposed to process non-tight HTML
511     * or the ones which are supposed to log violation for non-tight javadocs can utilize that.
512     *
513     * @param javadocParser The ANTLR recognizer instance which has been used to parse the javadoc
514     * @return First non-tight HTML tag if one exists; null otherwise
515     */
516    private Token getFirstNonTightHtmlTag(JavadocParser javadocParser) {
517        final CommonToken offendingToken;
518        final ParserRuleContext nonTightTagStartContext = javadocParser.nonTightTagStartContext;
519        if (nonTightTagStartContext == null) {
520            offendingToken = null;
521        }
522        else {
523            final Token token = ((TerminalNode) nonTightTagStartContext.getChild(1))
524                    .getSymbol();
525            offendingToken = new CommonToken(token);
526            offendingToken.setLine(offendingToken.getLine() + errorListener.offset);
527        }
528        return offendingToken;
529    }
530
531    /**
532     * Custom error listener for JavadocParser that prints user readable errors.
533     */
534    private static class DescriptiveErrorListener extends BaseErrorListener {
535
536        /**
537         * Offset is line number of beginning of the Javadoc comment. Log
538         * messages should have line number in scope of file, not in scope of
539         * Javadoc comment.
540         */
541        private int offset;
542
543        /**
544         * Error message that appeared while parsing.
545         */
546        private ParseErrorMessage errorMessage;
547
548        /**
549         * Getter for error message during parsing.
550         * @return Error message during parsing.
551         */
552        private ParseErrorMessage getErrorMessage() {
553            return errorMessage;
554        }
555
556        /**
557         * Sets offset. Offset is line number of beginning of the Javadoc
558         * comment. Log messages should have line number in scope of file, not
559         * in scope of Javadoc comment.
560         * @param offset
561         *        offset line number
562         */
563        public void setOffset(int offset) {
564            this.offset = offset;
565        }
566
567        /**
568         * Logs parser errors in Checkstyle manner. Parser can generate error
569         * messages. There is special error that parser can generate. It is
570         * missed close HTML tag. This case is special because parser prints
571         * error like {@code "no viable alternative at input 'b \n *\n'"} and it
572         * is not clear that error is about missed close HTML tag. Other error
573         * messages are not special and logged simply as "Parse Error...".
574         *
575         * <p>{@inheritDoc}
576         */
577        @Override
578        public void syntaxError(
579                Recognizer<?, ?> recognizer, Object offendingSymbol,
580                int line, int charPositionInLine,
581                String msg, RecognitionException ex) {
582            final int lineNumber = offset + line;
583
584            if (MSG_JAVADOC_WRONG_SINGLETON_TAG.equals(msg)) {
585                errorMessage = new ParseErrorMessage(lineNumber,
586                        MSG_JAVADOC_WRONG_SINGLETON_TAG, charPositionInLine,
587                        ((Token) offendingSymbol).getText());
588
589                throw new IllegalArgumentException(msg);
590            }
591            else {
592                final int ruleIndex = ex.getCtx().getRuleIndex();
593                final String ruleName = recognizer.getRuleNames()[ruleIndex];
594                final String upperCaseRuleName = CaseFormat.UPPER_CAMEL.to(
595                        CaseFormat.UPPER_UNDERSCORE, ruleName);
596
597                errorMessage = new ParseErrorMessage(lineNumber,
598                        MSG_JAVADOC_PARSE_RULE_ERROR, charPositionInLine, msg, upperCaseRuleName);
599            }
600        }
601
602    }
603
604    /**
605     * Contains result of parsing javadoc comment: DetailNode tree and parse
606     * error message.
607     */
608    public static class ParseStatus {
609
610        /**
611         * DetailNode tree (is null if parsing fails).
612         */
613        private DetailNode tree;
614
615        /**
616         * Parse error message (is null if parsing is successful).
617         */
618        private ParseErrorMessage parseErrorMessage;
619
620        /**
621         * Stores the first non-tight HTML tag encountered while parsing javadoc.
622         *
623         * @see <a
624         *     href="http://checkstyle.sourceforge.net/writingjavadocchecks.html#Tight-HTML_rules">
625         *     Tight HTML rules</a>
626         */
627        private Token firstNonTightHtmlTag;
628
629        /**
630         * Getter for DetailNode tree.
631         * @return DetailNode tree if parsing was successful, null otherwise.
632         */
633        public DetailNode getTree() {
634            return tree;
635        }
636
637        /**
638         * Sets DetailNode tree.
639         * @param tree DetailNode tree.
640         */
641        public void setTree(DetailNode tree) {
642            this.tree = tree;
643        }
644
645        /**
646         * Getter for error message during parsing.
647         * @return Error message if parsing was unsuccessful, null otherwise.
648         */
649        public ParseErrorMessage getParseErrorMessage() {
650            return parseErrorMessage;
651        }
652
653        /**
654         * Sets parse error message.
655         * @param parseErrorMessage Parse error message.
656         */
657        public void setParseErrorMessage(ParseErrorMessage parseErrorMessage) {
658            this.parseErrorMessage = parseErrorMessage;
659        }
660
661        /**
662         * This method is used to check if the javadoc parsed has non-tight HTML tags.
663         *
664         * @return returns true if the javadoc has at least one non-tight HTML tag; false otherwise
665         * @see <a
666         *     href="http://checkstyle.sourceforge.net/writingjavadocchecks.html#Tight-HTML_rules">
667         *     Tight HTML rules</a>
668         */
669        public boolean isNonTight() {
670            return firstNonTightHtmlTag != null;
671        }
672
673        /**
674         * Getter for {@link #firstNonTightHtmlTag}.
675         *
676         * @return the first non-tight HTML tag that is encountered while parsing Javadoc,
677         *     if one exists
678         */
679        public Token getFirstNonTightHtmlTag() {
680            return firstNonTightHtmlTag;
681        }
682
683    }
684
685    /**
686     * Contains information about parse error message.
687     */
688    public static class ParseErrorMessage {
689
690        /**
691         * Line number where parse error occurred.
692         */
693        private final int lineNumber;
694
695        /**
696         * Key for error message.
697         */
698        private final String messageKey;
699
700        /**
701         * Error message arguments.
702         */
703        private final Object[] messageArguments;
704
705        /**
706         * Initializes parse error message.
707         *
708         * @param lineNumber line number
709         * @param messageKey message key
710         * @param messageArguments message arguments
711         */
712        ParseErrorMessage(int lineNumber, String messageKey, Object... messageArguments) {
713            this.lineNumber = lineNumber;
714            this.messageKey = messageKey;
715            this.messageArguments = messageArguments.clone();
716        }
717
718        /**
719         * Getter for line number where parse error occurred.
720         * @return Line number where parse error occurred.
721         */
722        public int getLineNumber() {
723            return lineNumber;
724        }
725
726        /**
727         * Getter for key for error message.
728         * @return Key for error message.
729         */
730        public String getMessageKey() {
731            return messageKey;
732        }
733
734        /**
735         * Getter for error message arguments.
736         * @return Array of error message arguments.
737         */
738        public Object[] getMessageArguments() {
739            return messageArguments.clone();
740        }
741
742    }
743
744    /**
745     * The DefaultErrorStrategy used by ANTLR attempts to recover from parse errors
746     * which might result in a performance overhead. Also, a parse error indicate
747     * that javadoc doesn't follow checkstyle Javadoc grammar and the user should be made aware
748     * of it.
749     * <a href="http://www.antlr.org/api/Java/org/antlr/v4/runtime/BailErrorStrategy.html">
750     * BailErrorStrategy</a> is used to make ANTLR generated parser bail out on the first error
751     * in parser and not attempt any recovery methods but it doesn't report error to the
752     * listeners. This class is to ensure proper error reporting.
753     *
754     * @see DescriptiveErrorListener
755     * @see <a href="http://www.antlr.org/api/Java/org/antlr/v4/runtime/ANTLRErrorStrategy.html">
756     *     ANTLRErrorStrategy</a>
757     */
758    private static class JavadocParserErrorStrategy extends BailErrorStrategy {
759
760        @Override
761        public Token recoverInline(Parser recognizer) {
762            reportError(recognizer, new InputMismatchException(recognizer));
763            return super.recoverInline(recognizer);
764        }
765
766    }
767
768}