File indexing completed on 2024-04-28 17:01:42

0001 /*
0002 SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
0003 SPDX-FileCopyrightText: 2010 Kevin Kofler <kevin.kofler@chello.at>
0004 
0005 SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef KOMPAREDIFF2_PARSERBASE_H
0009 #define KOMPAREDIFF2_PARSERBASE_H
0010 
0011 #include <QRegularExpression>
0012 #include <QStringList>
0013 
0014 #include "kompare.h"
0015 #include "difference.h"
0016 
0017 class QString;
0018 
0019 namespace Diff2
0020 {
0021 
0022 class KompareModelList;
0023 class DiffModelList;
0024 class DiffModel;
0025 
0026 class ParserBase
0027 {
0028 public:
0029     ParserBase(const KompareModelList* list, const QStringList& diff);
0030     virtual ~ParserBase();
0031 
0032     static QString escapePath(QString path);
0033     static QString unescapePath(QString path);
0034 
0035 public:
0036     enum Kompare::Format format() { return determineFormat(); };
0037     DiffModelList* parse(bool* malformed = nullptr);
0038 
0039 protected:
0040     virtual bool parseContextDiffHeader();
0041     virtual bool parseEdDiffHeader();
0042     virtual bool parseNormalDiffHeader();
0043     virtual bool parseRCSDiffHeader();
0044     virtual bool parseUnifiedDiffHeader();
0045 
0046     virtual bool parseContextHunkHeader();
0047     virtual bool parseEdHunkHeader();
0048     virtual bool parseNormalHunkHeader();
0049     virtual bool parseRCSHunkHeader();
0050     virtual bool parseUnifiedHunkHeader();
0051 
0052     virtual bool parseContextHunkBody();
0053     virtual bool parseEdHunkBody();
0054     virtual bool parseNormalHunkBody();
0055     virtual bool parseRCSHunkBody();
0056     virtual bool parseUnifiedHunkBody();
0057 
0058     virtual DiffModelList* parseContext();
0059     virtual DiffModelList* parseEd();
0060     virtual DiffModelList* parseNormal();
0061     virtual DiffModelList* parseRCS();
0062     virtual DiffModelList* parseUnified();
0063 
0064 protected: // Helper methods to speed things up
0065     bool matchesUnifiedHunkLine(const QString& line) const;
0066     void checkHeader(const QRegularExpression& header);
0067 
0068 protected:
0069     /** What is format of the diff */
0070     virtual enum Kompare::Format determineFormat();
0071 
0072 protected:
0073     // Regexps for context parsing
0074     QRegularExpression m_contextDiffHeader1;
0075     QRegularExpression m_contextDiffHeader2;
0076 
0077     QRegularExpression m_contextHunkHeader1;
0078     QRegularExpression m_contextHunkHeader2;
0079     QRegularExpression m_contextHunkHeader3;
0080     QRegularExpressionMatch m_contextHunkHeader1Match;
0081     QRegularExpressionMatch m_contextHunkHeader2Match;
0082 
0083     QRegularExpression m_contextHunkBodyRemoved;
0084     QRegularExpression m_contextHunkBodyAdded;
0085     QRegularExpression m_contextHunkBodyChanged;
0086     QRegularExpression m_contextHunkBodyContext;
0087     QRegularExpression m_contextHunkBodyLine; // Added for convenience
0088 
0089     // Regexps for normal parsing
0090     QRegularExpression m_normalDiffHeader;
0091 
0092     QRegularExpression m_normalHunkHeaderAdded;
0093     QRegularExpression m_normalHunkHeaderRemoved;
0094     QRegularExpression m_normalHunkHeaderChanged;
0095     QRegularExpressionMatch m_normalHunkHeaderAddedMatch;
0096     QRegularExpressionMatch m_normalHunkHeaderRemovedMatch;
0097     QRegularExpressionMatch m_normalHunkHeaderChangedMatch;
0098 
0099     QRegularExpression m_normalHunkBodyRemoved;
0100     QRegularExpression m_normalHunkBodyAdded;
0101     QRegularExpression m_normalHunkBodyDivider;
0102 
0103     enum Difference::Type m_normalDiffType;
0104 
0105     // RegExps for rcs parsing
0106     QRegularExpression m_rcsDiffHeader;
0107 
0108     // Regexps for unified parsing
0109     QRegularExpression m_unifiedDiffHeader1;
0110     QRegularExpression m_unifiedDiffHeader2;
0111 
0112     QRegularExpression m_unifiedHunkHeader;
0113     QRegularExpressionMatch m_unifiedHunkHeaderMatch;
0114 
0115 protected:
0116     const QStringList&         m_diffLines;
0117     DiffModel*                 m_currentModel;
0118     DiffModelList*             m_models;
0119     QStringList::ConstIterator m_diffIterator;
0120 
0121     bool                       m_singleFileDiff;
0122     bool                       m_malformed;
0123 
0124 protected:
0125     const KompareModelList* m_list;
0126 };
0127 
0128 } // End of namespace Diff2
0129 
0130 #endif