1 /*
2  * KDiff3 - Text Diff And Merge Tool
3  *
4  * SPDX-FileCopyrightText: 2019-2020 Michael Reeves reeves.87@gmail.com
5  * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 #ifndef COMMENTPARSER_H
8 #define COMMENTPARSER_H
9 
10 #include <vector>
11 #include <QChar>
12 #include <QString>
13 
14 class CommentParser
15 {
16   public:
17     virtual void processChar(const QString &line, const QChar &inChar) = 0;
18     virtual void processLine(const QString &line) = 0;
19     virtual void removeComment(QString &line) = 0;
20     virtual bool inComment() const = 0;
21     virtual bool isPureComment() const = 0;
22 
23     virtual bool isSkipable() const = 0;
24     virtual ~CommentParser() = default;
25 };
26 
27 class DefaultCommentParser : public CommentParser
28 {
29   private:
30     typedef enum {none, singleLine, multiLine}CommentType;
31   public:
32     void processLine(const QString &line) override;
inComment()33     inline bool inComment() const override { return mCommentType != none; };
34 
isPureComment()35     inline bool isPureComment() const override { return mIsPureComment; };
36 
isSkipable()37     inline bool isSkipable() const override { return mIsCommentOrWhite; };
38 
39     void removeComment(QString &line) override;
40   protected:
41     friend class CommentParserTest;
42 
43     void processChar(const QString &line, const QChar &inChar) override;
44     //For tests only.
isEscaped()45     inline bool isEscaped() const{ return bIsEscaped; }
inString()46     inline bool inString() const{ return bInString; }
47   private:
48     QChar mLastChar, mStartChar;
49 
50     struct CommentRange
51     {
52           qint32 startOffset = 0;
53           qint32 endOffset = 0;
54     };
55 
56     qint32 offset = -1;
57 
58     CommentRange lastComment;
59 
60     std::vector<CommentRange> comments;
61 
62     bool isFirstLine = false;
63     bool mIsCommentOrWhite = false;
64     bool mIsPureComment = false;
65     bool bInString = false;
66     bool bIsEscaped = false;
67 
68     CommentType mCommentType = none;
69 };
70 
71 #endif // !COMMENTPASER_H
72