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