File indexing completed on 2024-04-14 03:43:52
0001 /* 0002 SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef _ERRORMSG_H_ 0008 #define _ERRORMSG_H_ 0009 0010 #include "token.h" 0011 0012 #include <QDebug> 0013 0014 #include <QList> 0015 #include <QStringList> 0016 0017 0018 0019 0020 /** 0021 * @short An object for an error message. 0022 * 0023 * Basically it is a aggregation of a QString (the message text), a Token (the Token 0024 * that is related to the error) and a code (a simple integer). 0025 * 0026 * ErrorMessages are created by the Parser and the Executer. 0027 * 0028 * @TODO investigate if it will be better to replace this class by a struct for speed. 0029 * 0030 * @author Cies Breijs 0031 */ 0032 class ErrorMessage 0033 { 0034 public: 0035 ErrorMessage() {} 0036 ErrorMessage(const QString&, const Token& t, int code); 0037 ~ErrorMessage() {} 0038 0039 const QString& text() const { return _errorText; } 0040 const Token& token() const { return _errorToken; } 0041 int code() const { return _errorCode; } 0042 0043 bool operator==(const ErrorMessage&) const; 0044 ErrorMessage& operator=(const ErrorMessage& n) = default; 0045 ErrorMessage(const ErrorMessage& n) = default; 0046 0047 private: 0048 QString _errorText; 0049 Token _errorToken; 0050 int _errorCode; 0051 }; 0052 0053 0054 /** 0055 * @short The simple subclass of QList to represent list of ErrorMessages. 0056 */ 0057 class ErrorList : public QList<ErrorMessage> 0058 { 0059 public: 0060 QStringList asStringList() 0061 { 0062 QStringList result; 0063 ErrorList::iterator error; 0064 int i = 1; 0065 for (error = this->begin(); error != this->end(); ++error) { 0066 result.append(QStringLiteral("%1: %2 [by %3 on line %4], code %5") 0067 .arg(i) 0068 .arg((*error).text()) 0069 .arg((*error).token().look()) 0070 .arg((*error).token().startRow()) 0071 .arg((*error).code())); 0072 i++; 0073 } 0074 return result; 0075 } 0076 0077 void addError(const QString& s, const Token& t, int code) 0078 { 0079 ErrorMessage error = ErrorMessage(s, t, code); 0080 addError(error); 0081 } 0082 0083 void addError(ErrorMessage error) 0084 { 0085 if (!this->contains(error)) this->append(error); 0086 // //qDebug() << 0087 // QString("ErrorList::addError(): %1 [by %2 on line %3], code %4") 0088 // .arg(error.text()) 0089 // .arg(error.token().look()) 0090 // .arg(error.token().startRow()) 0091 // .arg(error.code()); 0092 } 0093 }; 0094 0095 #endif // _ERRORMSG_H_