1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#ifndef GDBMIRESULTPARSER_H
18#define GDBMIRESULTPARSER_H
19
20#include <QByteArray>
21#include <QHash>
22#include <QList>
23#include <memory>
24
25
26enum class GDBMIResultType {
27 Breakpoint,
28 BreakpointTable,
29 FrameStack,
30 LocalVariables,
31 Locals,
32 Frame,
33 Disassembly,
34 Evaluation,
35 RegisterNames,
36 RegisterValues,
37 Memory,
38 CreateVar,
39 ListVarChildren,
40 UpdateVarValue
41};
42
43
44class GDBMIResultParser
45{
46public:
47 enum class ParseValueType {
48 Value,
49 Object,
50 Array,
51 NotAssigned
52 };
53
54 class ParseValue;
55
56 class ParseObject {
57 public:
58 explicit ParseObject();
59 ParseObject(const ParseObject& object);
60 ParseValue operator[](const QByteArray& name) const;
61 ParseValue& operator[](const QByteArray& name);
62 ParseObject& operator=(const ParseObject& object);
63 private:
64 QHash<QByteArray, ParseValue> mProps;
65 };
66
67 class ParseValue {
68 public:
69 explicit ParseValue();
70 explicit ParseValue(const QByteArray& value);
71 explicit ParseValue(const ParseObject &object);
72 explicit ParseValue(const QList<ParseValue>& array);
73 ParseValue(const ParseValue& value);
74 const QByteArray &value() const;
75 const QList<ParseValue> &array() const;
76 const ParseObject &object() const;
77 int intValue(int defaultValue=-1) const;
78 int hexValue(int defaultValue=-1) const;
79
80 QString pathValue() const;
81 QString utf8PathValue() const;
82 ParseValueType type() const;
83 bool isValid() const;
84 ParseValue& operator=(const QByteArray& value);
85 ParseValue& operator=(const ParseObject& object);
86 ParseValue& operator=(const QList<ParseValue>& array);
87 ParseValue& operator=(const ParseValue& value);
88 private:
89 QByteArray mValue;
90 QList<ParseValue> mArray;
91 ParseObject mObject;
92 ParseValueType mType;
93 };
94
95 using PParseValue = std::shared_ptr<ParseValue>;
96
97public:
98 GDBMIResultParser();
99 bool parse(const QByteArray& record, const QString& command, GDBMIResultType& type, ParseObject& multiValues);
100 bool parseAsyncResult(const QByteArray& record, QByteArray& result, ParseObject& multiValue);
101private:
102 bool parseMultiValues(const char*p, ParseObject& multiValue);
103 bool parseNameAndValue(const char *&p,QByteArray& name, ParseValue& value);
104 bool parseValue(const char* &p, ParseValue& value);
105 bool parseStringValue(const char*&p, QByteArray& stringValue);
106 bool parseObject(const char*&p, ParseObject& obj);
107 bool parseArray(const char*&p, QList<ParseValue>& array);
108 void skipSpaces(const char* &p);
109 bool isNameChar(char ch);
110 bool isSpaceChar(char ch);
111private:
112 QHash<QString, GDBMIResultType> mResultTypes;
113};
114
115#endif // GDBMIRESULTPARSER_H
116