RedPanda-CPP/RedPandaIDE/gdbmiresultparser.cpp

337 lines
7.9 KiB
C++
Raw Normal View History

2021-11-12 10:51:00 +08:00
#include "gdbmiresultparser.h"
2021-11-23 21:08:33 +08:00
#include <QFileInfo>
2021-11-12 10:51:00 +08:00
#include <QList>
GDBMIResultParser::GDBMIResultParser()
{
2021-11-12 22:42:51 +08:00
mResultTypes.insert("bkpt",GDBMIResultType::Breakpoint);
mResultTypes.insert("BreakpointTable",GDBMIResultType::BreakpointTable);
mResultTypes.insert("stack",GDBMIResultType::FrameStack);
mResultTypes.insert("variables", GDBMIResultType::LocalVariables);
mResultTypes.insert("frame",GDBMIResultType::Frame);
mResultTypes.insert("asm_insns",GDBMIResultType::Disassembly);
mResultTypes.insert("value",GDBMIResultType::Evaluation);
mResultTypes.insert("register-names",GDBMIResultType::RegisterNames);
mResultTypes.insert("register-values",GDBMIResultType::RegisterValues);
mResultTypes.insert("memory",GDBMIResultType::Memory);
}
bool GDBMIResultParser::parse(const QByteArray &record, GDBMIResultType &type, ParseValue& value)
{
const char* p = record.data();
QByteArray name;
bool result = parseNameAndValue(p,name,value);
if (!result)
return false;
// if (*p!=0)
// return false;
if (!mResultTypes.contains(name))
return false;
type = mResultTypes[name];
return true;
}
2021-11-21 10:36:50 +08:00
bool GDBMIResultParser::parseAsyncResult(const QByteArray &record, QByteArray &result, ParseObject &multiValue)
{
const char* p =record.data();
if (*p!='*')
return false;
p++;
const char* start;
while (*p && *p!=',')
p++;
result = QByteArray(start,p-start);
if (*p==0)
return true;
return parseMultiValues(p,multiValue);
}
bool GDBMIResultParser::parseMultiValues(const char* p, ParseObject &multiValue)
{
while (*p) {
QByteArray propName;
ParseValue propValue;
bool result = parseNameAndValue(p,propName,propValue);
if (result) {
multiValue[propName]=propValue;
} else {
return false;
}
skipSpaces(p);
if (*p!=',')
return false;
p++; //skip ','
skipSpaces(p);
p++;
}
return true;
}
2021-11-13 11:16:05 +08:00
bool GDBMIResultParser::parseNameAndValue(const char *&p, QByteArray &name, ParseValue &value)
2021-11-12 22:42:51 +08:00
{
skipSpaces(p);
2021-11-13 11:16:05 +08:00
const char* nameStart =p;
2021-11-12 22:42:51 +08:00
while (*p!=0 && isNameChar(*p)) {
p++;
}
if (*p==0)
return false;
2021-11-13 11:16:05 +08:00
name = QByteArray(nameStart,p-nameStart);
2021-11-12 22:42:51 +08:00
skipSpaces(p);
if (*p!='=')
return false;
return parseValue(p,value);
}
2021-11-13 11:16:05 +08:00
bool GDBMIResultParser::parseValue(const char *&p, ParseValue &value)
2021-11-12 22:42:51 +08:00
{
skipSpaces(p);
bool result;
switch (*p) {
case '{': {
ParseObject obj;
result = parseObject(p,obj);
value = obj;
break;
}
case '[': {
QList<ParseObject> array;
result = parseArray(p,array);
value = array;
break;
}
case '"': {
QByteArray s;
result = parseStringValue(p,s);
value = s;
break;
}
default:
return false;
}
if (!result)
return false;
skipSpaces(p);
if (*p!=0 && *p!=',')
return false;
return true;
}
2021-11-13 11:16:05 +08:00
bool GDBMIResultParser::parseStringValue(const char *&p, QByteArray& stringValue)
2021-11-12 22:42:51 +08:00
{
if (*p!='"')
return false;
p++;
2021-11-13 11:16:05 +08:00
const char* valueStart = p;
2021-11-12 22:42:51 +08:00
while (*p!=0) {
if (*p == '"') {
break;
} else if (*p=='\\' && *(p+1)!=0) {
p+=2;
} else {
p++;
}
}
if (*p=='"') {
stringValue = QByteArray(valueStart,p-valueStart);
p++; //skip '"'
return true;
}
return false;
}
2021-11-13 11:16:05 +08:00
bool GDBMIResultParser::parseObject(const char *&p, ParseObject &obj)
2021-11-12 22:42:51 +08:00
{
if (*p!='{')
return false;
p++;
if (*p!='}') {
while (*p!=0) {
QByteArray propName;
ParseValue propValue;
bool result = parseNameAndValue(p,propName,propValue);
if (result) {
obj[propName]=propValue;
} else {
return false;
}
skipSpaces(p);
if (*p=='}')
break;
if (*p!=',')
return false;
p++; //skip ','
skipSpaces(p);
}
}
if (*p=='}') {
p++; //skip '}'
return true;
}
return false;
}
2021-11-23 21:08:33 +08:00
bool GDBMIResultParser::parseArray(const char *&p, QList<GDBMIResultParser::ParseValue> &array)
2021-11-12 22:42:51 +08:00
{
if (*p!='[')
return false;
p++;
if (*p!=']') {
while (*p!=0) {
skipSpaces(p);
2021-11-23 21:08:33 +08:00
ParseValue val;
bool result = parseValue(p,val);
2021-11-12 22:42:51 +08:00
if (result) {
2021-11-23 21:08:33 +08:00
array.append(val);
2021-11-12 22:42:51 +08:00
} else {
return false;
}
skipSpaces(p);
if (*p==']')
break;
if (*p!=',')
return false;
p++; //skip ','
skipSpaces(p);
}
}
if (*p==']') {
p++; //skip ']'
return true;
}
return false;
}
bool GDBMIResultParser::isNameChar(char ch)
{
if (ch=='-')
return true;
if (ch>='a' && ch<='z')
return true;
if (ch>='A' && ch<='Z')
return true;
return false;
}
bool GDBMIResultParser::isSpaceChar(char ch)
{
switch(ch) {
case ' ':
case '\t':
return true;
}
return false;
}
2021-11-13 11:16:05 +08:00
void GDBMIResultParser::skipSpaces(const char *&p)
2021-11-12 22:42:51 +08:00
{
while (*p!=0 && isSpaceChar(*p))
p++;
}
2021-11-12 10:51:00 +08:00
2021-11-21 08:38:03 +08:00
const QByteArray &GDBMIResultParser::ParseValue::value() const
2021-11-12 22:42:51 +08:00
{
2021-11-21 08:38:03 +08:00
Q_ASSERT(mType == ParseValueType::Value);
2021-11-12 22:42:51 +08:00
return mValue;
2021-11-12 10:51:00 +08:00
}
2021-11-23 21:08:33 +08:00
const QList<::GDBMIResultParser::ParseValue> &GDBMIResultParser::ParseValue::array() const
2021-11-12 10:51:00 +08:00
{
2021-11-21 08:38:03 +08:00
Q_ASSERT(mType == ParseValueType::Array);
2021-11-12 22:42:51 +08:00
return mArray;
2021-11-12 10:51:00 +08:00
}
2021-11-12 22:42:51 +08:00
const GDBMIResultParser::ParseObject &GDBMIResultParser::ParseValue::object() const
{
2021-11-21 08:38:03 +08:00
Q_ASSERT(mType == ParseValueType::Object);
2021-11-12 22:42:51 +08:00
return mObject;
}
2021-11-21 08:38:03 +08:00
int GDBMIResultParser::ParseValue::intValue(int defaultValue) const
{
Q_ASSERT(mType == ParseValueType::Value);
bool ok;
int value = QString(mValue).toInt(&ok);
if (ok)
return value;
else
return defaultValue;
}
2021-11-23 21:08:33 +08:00
QString GDBMIResultParser::ParseValue::pathValue() const
{
Q_ASSERT(mType == ParseValueType::Value);
return QFileInfo(QString::fromLocal8Bit(mValue)).absoluteFilePath();
}
2021-11-12 22:42:51 +08:00
GDBMIResultParser::ParseValueType GDBMIResultParser::ParseValue::type() const
{
return mType;
}
GDBMIResultParser::ParseValue::ParseValue():
mType(ParseValueType::NotAssigned) {
}
2021-11-21 08:38:03 +08:00
GDBMIResultParser::ParseValue::ParseValue(const QByteArray &value):
2021-11-12 22:42:51 +08:00
mValue(value),
mType(ParseValueType::Value)
{
}
GDBMIResultParser::ParseValue::ParseValue(const ParseObject &object):
2021-11-12 22:42:51 +08:00
mObject(object),
mType(ParseValueType::Object)
{
}
2021-11-23 21:08:33 +08:00
GDBMIResultParser::ParseValue::ParseValue(const QList<ParseValue> &array):
2021-11-12 22:42:51 +08:00
mArray(array),
mType(ParseValueType::Array)
{
}
2021-11-21 08:38:03 +08:00
GDBMIResultParser::ParseValue &GDBMIResultParser::ParseValue::operator=(const QByteArray &value)
2021-11-12 22:42:51 +08:00
{
Q_ASSERT(mType == ParseValueType::NotAssigned);
mType = ParseValueType::Value;
mValue = value;
}
GDBMIResultParser::ParseValue &GDBMIResultParser::ParseValue::operator=(const ParseObject& object)
{
Q_ASSERT(mType == ParseValueType::NotAssigned);
mType = ParseValueType::Object;
mObject = object;
}
2021-11-23 21:08:33 +08:00
GDBMIResultParser::ParseValue &GDBMIResultParser::ParseValue::operator=(const QList<ParseValue>& array)
2021-11-12 22:42:51 +08:00
{
Q_ASSERT(mType == ParseValueType::NotAssigned);
mType = ParseValueType::Array;
mArray = array;
}
2021-11-13 11:16:05 +08:00
const GDBMIResultParser::ParseValue GDBMIResultParser::ParseObject::operator[](const QByteArray &name) const
2021-11-12 22:42:51 +08:00
{
if (mProps.contains(name))
return mProps[name];
2021-11-13 11:16:05 +08:00
return ParseValue();
2021-11-12 22:42:51 +08:00
}
GDBMIResultParser::ParseObject &GDBMIResultParser::ParseObject::operator=(const ParseObject &object)
{
mProps = object.mProps;
}
GDBMIResultParser::ParseValue &GDBMIResultParser::ParseObject::operator[](const QByteArray &name) {
if (!mProps.contains(name))
mProps[name]=ParseValue();
return mProps[name];
}