File indexing completed on 2024-04-14 05:35:37

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2019-2020 Michael Reeves reeves.87@gmail.com
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 // clang-format on
0009 #ifndef COMMENTPARSER_H
0010 #define COMMENTPARSER_H
0011 
0012 #include "TypeUtils.h"
0013 
0014 #include <vector>
0015 
0016 #include <QChar>
0017 #include <QString>
0018 
0019 class CommentParser
0020 {
0021   public:
0022     virtual void processChar(const QString &line, const QChar &inChar) = 0;
0023     virtual void processLine(const QString &line) = 0;
0024     virtual void removeComment(QString &line) = 0;
0025     [[nodiscard]] virtual bool inComment() const = 0;
0026     [[nodiscard]] virtual bool isPureComment() const = 0;
0027     [[nodiscard]] virtual bool isSkipable() const = 0;
0028     virtual ~CommentParser() = default;
0029 };
0030 
0031 class DefaultCommentParser : public CommentParser
0032 {
0033   private:
0034     typedef enum {none, singleLine, multiLine}CommentType;
0035   public:
0036     void processLine(const QString &line) override;
0037     [[nodiscard]] inline bool inComment() const override { return mCommentType != none; };
0038     [[nodiscard]] inline bool isPureComment() const override { return mIsPureComment; };
0039     [[nodiscard]] inline bool isSkipable() const override { return mIsCommentOrWhite; };
0040 
0041     void removeComment(QString &line) override;
0042   protected:
0043     friend class CommentParserTest;
0044 
0045     void processChar(const QString &line, const QChar &inChar) override;
0046     //For tests only.
0047     [[nodiscard]] inline bool isEscaped() const{ return bIsEscaped; }
0048     [[nodiscard]] inline bool inString() const{ return bInString; }
0049   private:
0050     QChar mLastChar, mStartChar;
0051 
0052     struct CommentRange
0053     {
0054           QtSizeType startOffset = 0;
0055           QtSizeType endOffset = 0;
0056     };
0057 
0058     QtSizeType offset = -1;
0059 
0060     CommentRange lastComment;
0061 
0062     std::vector<CommentRange> comments;
0063 
0064     bool isFirstLine = false;
0065     bool mIsCommentOrWhite = false;
0066     bool mIsPureComment = false;
0067     bool bInString = false;
0068     bool bIsEscaped = false;
0069 
0070     CommentType mCommentType = none;
0071 };
0072 
0073 #endif // !COMMENTPASER_H