diff --git a/src/CommentParser.cpp b/src/CommentParser.cpp index 6e50c80..b12f207 100644 --- a/src/CommentParser.cpp +++ b/src/CommentParser.cpp @@ -1,124 +1,120 @@ /** * Copyright (C) 2019 Michael Reeves * * This file is part of KDiff3. * * KDiff3 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * KDiff3 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KDiff3. If not, see . */ #include "CommentParser.h" #include #include #include #include void DefaultCommentParser::processChar(const QString &line, const QChar &inChar) { if(!bIsEscaped) { switch(inChar.unicode()) { case '\\': if(bInString) bIsEscaped = true; break; case '\'': case '"': if(!inComment()) { if(!bInString) { bInString = true; mStartChar = inChar; } else if(mStartChar == inChar) { bInString = false; } } break; case '/': if(bInString) break; if(!inComment() && mLastChar == '/') { mCommentType = singleLine; mIsPureComment = line.startsWith("//") ? yes : no; } else if(mLastChar == '*' && mCommentType == multiLine) { //ending multi-line comment mCommentType = none; if(!isFirstLine) mIsPureComment = line.endsWith("*/") ? yes : mIsPureComment; } break; case '*': if(bInString) break; if(mLastChar == '/' && !inComment()) { mCommentType = multiLine; mIsPureComment = line.startsWith("/*") ? yes : mIsPureComment; isFirstLine = true; } break; case '\n': if(mCommentType == singleLine) { mCommentType = none; } if(mCommentType == multiLine && !isFirstLine) { mIsPureComment = yes; } isFirstLine = false; break; default: if(inComment()) { break; } mIsPureComment = no; break; } mLastChar = inChar; } else { bIsEscaped = false; mLastChar = QChar(); } }; void DefaultCommentParser::processLine(const QString &line) { for(const QChar &c : line) { processChar(line, c); } processChar(line, '\n'); } - -void DefaultCommentParser::removeComments() -{ -} diff --git a/src/CommentParser.h b/src/CommentParser.h index 5ca388e..55ac960 100644 --- a/src/CommentParser.h +++ b/src/CommentParser.h @@ -1,67 +1,65 @@ /** * Copyright (C) 2019 Michael Reeves * * This file is part of KDiff3. * * KDiff3 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * KDiff3 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KDiff3. If not, see . */ #ifndef COMMENTPARSER_H #define COMMENTPARSER_H #include #include class CommentParser { public: - inline virtual void processChar(const QString &line, const QChar &inChar) = 0; - inline virtual void processLine(const QString &line) = 0; - inline virtual bool inComment() const = 0; - inline virtual bool isPureComment() const = 0; - inline virtual void removeComments() = 0; + virtual void processChar(const QString &line, const QChar &inChar) = 0; + virtual void processLine(const QString &line) = 0; + virtual bool inComment() const = 0; + virtual bool isPureComment() const = 0; virtual ~CommentParser(){}; }; class DefaultCommentParser : public CommentParser { private: typedef enum {none, singleLine, multiLine}CommentType; typedef enum {no, yes, unknown}TriState; public: void processLine(const QString &line) override; inline bool inComment() const override { return mCommentType != none; }; inline bool isPureComment() const override { return mIsPureComment == yes; }; - inline void removeComments() override; protected: friend class CommentParserTest; void processChar(const QString &line, const QChar &inChar) override; //For tests only. inline bool isEscaped(){ return bIsEscaped; } inline bool inString(){ return bInString; } private: QChar mLastChar, mStartChar; bool isFirstLine = false; TriState mIsPureComment = unknown; bool bInString = false; bool bIsEscaped = false; CommentType mCommentType = none; quint32 pos = 0; }; #endif // !COMMENTPASER_H