File indexing completed on 2024-05-12 05:51:07

0001 /*
0002     SPDX-FileCopyrightText: 2022 Héctor Mesa Jiménez <wmj.py@gmx.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QByteArray>
0010 #include <QJsonObject>
0011 #include <QObject>
0012 #include <QString>
0013 #include <QStringList>
0014 #include <optional>
0015 
0016 #include "records.h"
0017 
0018 namespace gdbmi
0019 {
0020 
0021 class GdbmiParser : public QObject
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit GdbmiParser(QObject *parent = nullptr);
0026 
0027     /**
0028      * @brief parse gdb/mi responses
0029      *
0030      * @return last position successfully parsed
0031      */
0032     struct ParserHead {
0033         int last;
0034         bool error;
0035     };
0036     ParserHead parseResponse(const QByteArray &message);
0037     static bool isMIRequest(const QString &message);
0038     /**
0039      * @return MI command in the request message without "-" or nullopt
0040      */
0041     static std::optional<QString> getMICommand(const QString &message);
0042     /**
0043      * @brief Split token and command
0044      */
0045     static QStringList splitCommand(const QString &message);
0046     static bool isMISeparator(const QString &message);
0047     /**
0048      *
0049      * @return position of the last message separator. If a separator is multi-character ("\r\n"), the last char position is returned
0050      */
0051     static int splitLines(const QByteArray &message, bool lastIndex = false);
0052 
0053 Q_SIGNALS:
0054     void outputProduced(const StreamOutput &record);
0055     void recordProduced(const Record &record);
0056     void parserError(const QString &error);
0057 
0058 private:
0059     int parseRecord(const QByteArray &message, int position);
0060 };
0061 
0062 }