001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.grammar;
021
022import org.antlr.v4.runtime.CharStream;
023import org.antlr.v4.runtime.Lexer;
024import org.antlr.v4.runtime.atn.ATN;
025import org.antlr.v4.runtime.atn.LexerATNSimulator;
026import org.antlr.v4.runtime.atn.PredictionContextCache;
027import org.antlr.v4.runtime.dfa.DFA;
028
029/**
030 * Extends the LexerATNSimulator class in order to override
031 * the 'consume()' method so that we can handle '\r' line
032 * endings (pre-OSX macOS line endings) correctly in the
033 * ANTLR lexer.
034 */
035public class CrAwareLexerSimulator extends LexerATNSimulator {
036
037    /**
038     * Constructs a CrAwareLexerSimulator to interpret the input
039     * from the lexer.
040     *
041     * @param lexer the current lexer
042     * @param augmented the augmented transition network
043     * @param decisionToDfa the DFA to store our states in
044     * @param sharedContextCache cache of PredictionContext objects
045     */
046    public CrAwareLexerSimulator(Lexer lexer, ATN augmented,
047                                 DFA[] decisionToDfa,
048                                 PredictionContextCache sharedContextCache) {
049        super(lexer, augmented, decisionToDfa, sharedContextCache);
050    }
051
052    /**
053     * Overrides the 'consume()' method to add support for
054     * '\r' (carriage return) line endings.
055     *
056     * @param input the Character stream of the file we are parsing
057     */
058    @Override
059    public void consume(CharStream input) {
060        final int currentChar = input.LA(1);
061        if (currentChar == '\n') {
062            line++;
063            charPositionInLine = 0;
064        }
065        else if (currentChar == '\r') {
066            final int nextChar = input.LA(2);
067            if (nextChar != '\n') {
068                line++;
069                charPositionInLine = 0;
070            }
071        }
072        else {
073            charPositionInLine++;
074        }
075        input.consume();
076    }
077}