summaryrefslogtreecommitdiffstats
path: root/src/serialbus/qcandbcfileparser.cpp
blob: c32c9bbf017b0ae4892d25583c88db18569989d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qcandbcfileparser.h"
#include "qcandbcfileparser_p.h"
#include "qcanmessagedescription.h"
#include "qcansignaldescription.h"
#include "qcanuniqueiddescription.h"
#include "private/qcanmessagedescription_p.h"
#include "private/qcansignaldescription_p.h"

#include <QtCore/QFile>
#include <QtCore/QRegularExpression>

#include <optional>

QT_BEGIN_NAMESPACE

/*!
    \class QCanDbcFileParser
    \inmodule QtSerialBus
    \since 6.5
    \preliminary

    \brief The QCanDbcFileParser class can be used to parse DBC files.

    A CAN database or CAN DBC file is an ASCII text file that contains
    information on how to decode and interpret raw CAN bus data. Some more
    details about the format can be found \l {CSSElectronics DBC Intro}{here}
    or \l {OpenVehicles DBC Intro}{here}.

    The QCanDbcFileParser class takes the input DBC file, parses it, and
    provides a list of \l {QCanMessageDescription}s as an output. These message
    descriptions can be forwarded to \l QCanFrameProcessor, and later used
    as rules to encode or decode \l {QCanBusFrame}s.

    Use one of \l parse() overloads to specify a file or a list of files that
    will be processed. Both overloads return \c true if the parsing completes
    successfully and \c false otherwise.

    Call the \l error() method to get the error which occurred during the
    parsing. If the parsing completes successfully, this method will return
    \l {QCanDbcFileParser::}{None}. Otherwise, you can use an
    \l errorString() method to get the string representation of an error.

    During the parsing some non-critical problems may occur as well. Such
    problems will be logged, but the parsing process will not be aborted. You
    can use the \l warnings() method to get the full list of such problems
    after the parsing is completed.

    If the parsing completes successfully, call \l messageDescriptions() to get
    a list of the message descriptions that were extracted during the last
    \l parse() call. Call \l messageValueDescriptions() to get the textual
    descriptions of signal raw values, if they are available.

    Use the static \l uniqueIdDescription() function to get a
    \l QCanUniqueIdDescription for the DBC format.

    \code
    QCanDbcFileParser fileParser;
    const bool result = fileParser.parse(u"path/to/file.dbc"_s);
    // Check result, call error() and warnings() if needed

    // Prepare a QCanFrameProcessor to decode or encode DBC frames
    QCanFrameProcessor frameProcessor;
    frameProcessor.setUniqueIdDescription(QCanDbcFileParser::uniqueIdDescription());
    frameProcessor.setMessageDescriptions(fileParser.messageDescriptions());
    \endcode

    \note The parser is stateful, which means that all the results (like
    extracted message descriptions, error code, or warnings) are reset once the
    next parsing starts.

    \section2 Supported Keywords

    The current implementation supports only a subset of keywords that you can
    find in a DBC file:

    \list
        \li \c {BO_} - message description.
        \li \c {SG_} - signal description.
        \li \c {SIG_VALTYPE_} - signal type description.
        \li \c {SG_MUL_VAL_} - extended multiplexing description.
        \li \c {CM_} - comments (only for message and signal descriptions).
        \li \c {VAL_} - textual descriptions for raw signal values.
    \endlist

    Lines starting from other keywords are simply ignored.

    \sa QCanMessageDescription, QCanFrameProcessor
*/

/*!
    \typealias QCanDbcFileParser::ValueDescriptions

    This is a type alias for \c {QHash<quint32, QString>}.

    The keys of the hash represent raw signal values, and the values of the
    hash represent corresponding string descriptions.
*/

/*!
    \typealias QCanDbcFileParser::SignalValueDescriptions

    This is a type alias for \c {QHash<QString, ValueDescriptions>}.

    The keys of the hash represent signal names, and the values of the
    hash contain the corresponding \l QCanDbcFileParser::ValueDescriptions
    entries.

    \sa QCanDbcFileParser::ValueDescriptions
*/

/*!
    \typealias QCanDbcFileParser::MessageValueDescriptions

    This is a type alias for
    \c {QHash<QtCanBus::UniqueId, SignalValueDescriptions>}.

    The keys of the hash represent message unique ids, and the values of the
    hash contain the corresponding \l QCanDbcFileParser::SignalValueDescriptions
    entries.

    \sa QCanDbcFileParser::SignalValueDescriptions
*/

/*!
    \enum QCanDbcFileParser::Error

    This enum represents the possible errors that can happen during the parsing
    of a DBC file.

    \value None No error occurred.
    \value FileReading An error occurred while opening or reading the file.
    \value Parsing An error occurred while parsing the content of the file.
*/

/*!
    Constructs a DBC file parser.
*/
QCanDbcFileParser::QCanDbcFileParser()
    : d(std::make_unique<QCanDbcFileParserPrivate>())
{
}

/*!
    Destroys this DBC file parser.
*/
QCanDbcFileParser::~QCanDbcFileParser() = default;

/*!
    Parses the file \a fileName. Returns \c true if the parsing completed
    successfully or \c false otherwise.

    If the parsing completed successfully, call the \l messageDescriptions()
    method to get the list of all extracted message descriptions.

    If the parsing failed, call the \l error() and \l errorString() methods
    to get the information about the error.

    Call the \l warnings() method to get the list of warnings that were
    logged during the parsing.

    \sa messageDescriptions(), error(), warnings()
*/
bool QCanDbcFileParser::parse(const QString &fileName)
{
    d->reset();
    return d->parseFile(fileName);
}

/*!
    \overload

    Parses a list of files \a fileNames. Returns \c true if the parsing
    completed successfully or \c false otherwise.

    If the parsing completed successfully, call the \l messageDescriptions()
    method to get the list of all extracted message descriptions.

    The parsing stops at the first error. Call the \l error() and
    \l errorString() methods to get the information about the error.

    Call the \l warnings() method to get the list of warnings that were
    logged during the parsing.

    \sa messageDescriptions(), error(), warnings()
*/
bool QCanDbcFileParser::parse(const QStringList &fileNames)
{
    d->reset();
    for (const auto &fileName : fileNames) {
        if (!d->parseFile(fileName))
            return false;
    }
    return true;
}

/*!
    Returns the list of message descriptions that were extracted during the
    last \l parse() call.

    \sa parse(), error()
*/
QList<QCanMessageDescription> QCanDbcFileParser::messageDescriptions() const
{
    return d->getMessages();
}

/*!
    Returns the textual descriptions for signal raw values.

    DBC supports the possibility to provide textual descriptions to signal raw
    values. If such data exists in the parsed DBC file(s), it can be accessed
    using this function.

    The textual descriptions are unique for a certain signal within a specific
    message, so the returned structure contains the information about the
    message unique id and the signal name, as well as the actual value
    descriptions.

    \sa QCanDbcFileParser::MessageValueDescriptions,
    QCanDbcFileParser::SignalValueDescriptions,
    QCanDbcFileParser::ValueDescriptions
*/
QCanDbcFileParser::MessageValueDescriptions QCanDbcFileParser::messageValueDescriptions() const
{
    return d->m_valueDescriptions;
}

/*!
    Returns the last error which occurred during the parsing.

    \sa errorString(), parse()
*/
QCanDbcFileParser::Error QCanDbcFileParser::error() const
{
    return d->m_error;
}

/*!
    Returns the text representation of the last error which occurred during the
    parsing or an empty string if there was no error.

    \sa error()
*/
QString QCanDbcFileParser::errorString() const
{
    return d->m_errorString;
}

/*!
    Returns the list of non-critical problems which occurred during the parsing.

    A typical problem can be a malformed message or signal description. In such
    cases the malformed message or signal is skipped, but the rest of the file
    can be processed as usual.

    \sa error(), parse()
*/
QStringList QCanDbcFileParser::warnings() const
{
    return d->m_warnings;
}

/*!
    Returns a unique identifier description. DBC protocol always uses the
    Frame Id as an identifier, and therefore the unique identifier description
    is always the same.

    Use this method to get an instance of \l QCanUniqueIdDescription and pass
    it to \l QCanFrameProcessor.

    \sa QCanFrameProcessor::setUniqueIdDescription()
*/
QCanUniqueIdDescription QCanDbcFileParser::uniqueIdDescription()
{
    QCanUniqueIdDescription desc;
    desc.setSource(QtCanBus::DataSource::FrameId);
    desc.setEndian(QSysInfo::Endian::LittleEndian);
    desc.setStartBit(0);
    desc.setBitLength(29); // for both extended and normal frame id
    return desc;
}

/* QCanDbcFileParserPrivate implementation */

using namespace Qt::StringLiterals;

// signal name with whitespaces is invalid in DBC, so we can safely use it
// for internal purposes
static const auto kQtDummySignal = u"Qt Dummy Signal"_s;

static constexpr auto kMessageDef = "BO_ "_L1;
static constexpr auto kSignalDef = "SG_ "_L1;
static constexpr auto kSigValTypeDef = "SIG_VALTYPE_ "_L1;
static constexpr auto kCommentDef = "CM_ "_L1;
static constexpr auto kExtendedMuxDef = "SG_MUL_VAL_ "_L1;
static constexpr auto kValDef = "VAL_ "_L1;

static constexpr auto kUnsignedIntRegExp = "\\d+"_L1;
static constexpr auto kDoubleRegExp = "[+-]?\\d+(.\\d+([eE][+-]?\\d+)?)?"_L1;
static constexpr auto kDbcIdentRegExp = "[_[:alpha:]][_[:alnum:]]+"_L1;
static constexpr auto kOneOrMoreSpaceRegExp = "[ ]+"_L1;
static constexpr auto kMaybeSpaceRegExp = "[ ]*"_L1;
static constexpr auto kMuxIndicatorRegExp = "M|m\\d+M?"_L1;
static constexpr auto kByteOrderRegExp = "0|1"_L1;
static constexpr auto kValueTypeRegExp = "\\+|\\-"_L1;
// The pattern matches all printable characters, except double-quote (") and backslash (\).
static constexpr auto kCharStrRegExp = "((?![\\\"\\\\])\\P{Cc})*"_L1;

void QCanDbcFileParserPrivate::reset()
{
    m_fileName.clear();
    m_error = QCanDbcFileParser::Error::None;
    m_errorString.clear();
    m_warnings.clear();
    m_lineOffset = 0;
    m_isProcessingMessage = false;
    m_seenExtraData = false;
    m_currentMessage = {};
    m_messageDescriptions.clear();
    m_valueDescriptions.clear();
}

/*!
    \internal
    Returns \c false only in case of hard error. Returns \c true even if some
    warnings occurred during parsing.
*/
bool QCanDbcFileParserPrivate::parseFile(const QString &fileName)
{
    QFile f(fileName);
    if (!f.open(QIODevice::ReadOnly)) {
        m_error = QCanDbcFileParser::Error::FileReading;
        m_errorString = f.errorString();
        return false;
    }
    m_fileName = fileName;
    m_seenExtraData = false;

    while (!f.atEnd()) {
        const QString str = QString::fromUtf8(f.readLine().trimmed());
        if (!processLine({str.constData(), str.size()})) // also sets the error properly
            return false;
    }
    addCurrentMessage(); // check if we need to add the message
    // now when we parsed the whole file, we can verify the signal multiplexing
    postProcessSignalMultiplexing();

    return true;
}

/*!
    \internal
    Returns \c false only in case of hard error. Returns \c true even if some
    warnings occurred during parsing.
*/
bool QCanDbcFileParserPrivate::processLine(const QStringView line)
{
    QStringView data = line;
    m_lineOffset = 0;
    if (data.startsWith(kMessageDef)) {
        if (m_seenExtraData) {
            // Unexpected position of message description
            m_error = QCanDbcFileParser::Error::Parsing;
            m_errorString = QObject::tr("Failed to parse file %1. Unexpected position "
                                        "of %2 section.").arg(m_fileName, kMessageDef);
            return false;
        }
        addCurrentMessage();
        if (!parseMessage(data))
            return false;
    }
    // signal definitions can be on the same line as message definition,
    // or on a separate line
    data = data.sliced(m_lineOffset).trimmed();
    while (data.startsWith(kSignalDef)) {
        if (!m_isProcessingMessage || m_seenExtraData) {
            // Unexpected position of signal description
            m_error = QCanDbcFileParser::Error::Parsing;
            m_errorString = QObject::tr("Failed to parse file %1. Unexpected position "
                                        "of %2 section.").arg(m_fileName, kSignalDef);
            return false;
        }
        if (!parseSignal(data))
            return false;
        data = data.sliced(m_lineOffset).trimmed();
    }
    // If we detect one of the following lines, then message description is
    // finished. We also assume that we can have only one key at each line.
    if (data.startsWith(kSigValTypeDef)) {
        m_seenExtraData = true;
        addCurrentMessage();
        parseSignalType(data);
    } else if (data.startsWith(kCommentDef)) {
        m_seenExtraData = true;
        addCurrentMessage();
        parseComment(data);
    } else if (data.startsWith(kExtendedMuxDef)) {
        m_seenExtraData = true;
        addCurrentMessage();
        parseExtendedMux(data);
    } else if (data.startsWith(kValDef)) {
        m_seenExtraData = true;
        addCurrentMessage();
        parseValueDescriptions(data);
    }
    return true;
}

static std::optional<QtCanBus::UniqueId> extractUniqueId(QStringView view)
{
    bool ok = false;
    const uint value = view.toUInt(&ok);
    if (ok)
        return QtCanBus::UniqueId{value & 0x1FFFFFFF};
    return std::nullopt;
}

/*!
    \internal
    Returns \c false only in case of hard error. Returns \c true even if some
    warnings occurred during parsing.
*/
bool QCanDbcFileParserPrivate::parseMessage(const QStringView data)
{
    // The regexp matches the following definition:
    // BO_ message_id message_name ':' message_size transmitter
    // also considering the fact that spaces around ':' seem to be optional, and
    // allowing more than one space between parts.

    // %1 - messageDef
    // %2 - maybeSpace
    // %3 - unsignedInt
    // %4 - oneOrMoreSpace
    // %5 - DbcIdentifier
    static const QString regExStr =
            "%1%2(?<messageId>%3)%4(?<name>%5)%2:%2(?<size>%3)%4(?<transmitter>%5)"_L1.
            arg(kMessageDef, kMaybeSpaceRegExp, kUnsignedIntRegExp, kOneOrMoreSpaceRegExp,
                kDbcIdentRegExp);
    static const QRegularExpression messageRegExp(regExStr);

    m_isProcessingMessage = false;
    const auto match = messageRegExp.matchView(data);
    if (match.hasMatch()) {
        m_currentMessage = extractMessage(match);
        // can't check for isValid() here, because demands signal descriptions
        if (!m_currentMessage.name().isEmpty()) {
            m_isProcessingMessage = true;
        } else {
            addWarning(QObject::tr("Failed to parse message description from "
                                   "string %1").arg(data));
        }
        m_lineOffset = match.capturedEnd(0);
    } else {
        addWarning(QObject::tr("Failed to find message description in string %1").arg(data));
        m_lineOffset = data.size(); // skip this string
    }
    return true;
}

QCanMessageDescription
QCanDbcFileParserPrivate::extractMessage(const QRegularExpressionMatch &match)
{
    Q_ASSERT(match.hasMatch());
    QCanMessageDescription desc;
    desc.setName(match.captured(u"name"_s));

    const auto id = extractUniqueId(match.capturedView(u"messageId"_s));
    if (id.has_value()) {
        desc.setUniqueId(id.value());
    } else {
        addWarning(QObject::tr("Failed to parse frame id for message %1").arg(desc.name()));
        return {};
    }

    bool ok = false;
    const auto size = match.capturedView(u"size"_s).toUInt(&ok);
    if (ok) {
        desc.setSize(size);
    } else {
        addWarning(QObject::tr("Failed to parse size for message %1").arg(desc.name()));
        return {};
    }

    desc.setTransmitter(match.captured(u"transmitter"_s));

    return desc;
}

/*!
    \internal
    Returns \c false only in case of hard error. Returns \c true even if some
    warnings occurred during parsing.
*/
bool QCanDbcFileParserPrivate::parseSignal(const QStringView data)
{
    // The regexp should match the following pattern:
    //      SG_ signal_name multiplexer_indicator : start_bit |
    //      signal_size @ byte_order value_type ( factor , offset )
    //      [ minimum | maximum ] unit receiver {, receiver}
    // We also need to consider the fact that some of the spaces might be
    // optional, and we can potentially allow more spaces between parts.
    // Note that the end of the signal description can contain multiple
    // receivers. The regexp is supposed to extract all of them, but we use
    // only the first one for now.

    // %1 - SignalDef
    // %2 - MaybeSpace
    // %3 - DbcIdentifier
    // %4 - OneOrMoreSpace
    // %5 - MuxIndicator
    // %6 - unsignedInt
    // %7 - byteOrder
    // %8 - valueType
    // %9 - double
    // %10 - charStr
    static const QString regExStr =
            "%1%2(?<name>%3)(%4(?<mux>%5))?%2:%2(?<startBit>%6)%2\\|%2(?<sigSize>%6)%2@%2"
            "(?<byteOrder>%7)%2(?<valueType>%8)%4\\(%2(?<factor>%9)%2,%2(?<offset>%9)%2\\)"
            "%4\\[%2(?<min>%9)%2\\|%2(?<max>%9)%2\\]%4\"(?<unit>%10)\""
            "%4(?<receiver>%3)(%2,%2%3)*"_L1.
            arg(kSignalDef, kMaybeSpaceRegExp, kDbcIdentRegExp, kOneOrMoreSpaceRegExp,
                kMuxIndicatorRegExp, kUnsignedIntRegExp, kByteOrderRegExp, kValueTypeRegExp,
                kDoubleRegExp, kCharStrRegExp);
    static const QRegularExpression signalRegExp(regExStr);

    const auto match = signalRegExp.matchView(data);
    if (match.hasMatch()) {
        QCanSignalDescription desc = extractSignal(match);

        if (desc.isValid())
            m_currentMessage.addSignalDescription(desc);
        else
            addWarning(QObject::tr("Failed to parse signal description from string %1").arg(data));

        m_lineOffset = match.capturedEnd(0);
    } else {
        addWarning(QObject::tr("Failed to find signal description in string %1").arg(data));
        m_lineOffset = data.size(); // skip this string
    }
    return true;
}

QCanSignalDescription QCanDbcFileParserPrivate::extractSignal(const QRegularExpressionMatch &match)
{
    Q_ASSERT(match.hasMatch());
    QCanSignalDescription desc;
    desc.setName(match.captured(u"name"_s));

    bool ok = false;

    if (match.hasCaptured(u"mux"_s)) {
        const auto muxStr = match.capturedView(u"mux"_s);
        if (muxStr == u"M"_s) {
            desc.setMultiplexState(QtCanBus::MultiplexState::MultiplexorSwitch);
        } else if (muxStr.endsWith(u"M"_s, Qt::CaseSensitive)) {
            desc.setMultiplexState(QtCanBus::MultiplexState::SwitchAndSignal);
            const auto val = muxStr.sliced(1, muxStr.size() - 2).toUInt(&ok);
            if (!ok) {
                addWarning(QObject::tr("Failed to parse multiplexor value for signal %1").
                           arg(desc.name()));
                return {};
            }
            // We have the value, but we do not really know the multiplexor
            // switch name. To know it, we potentially need to parse all signals
            // for the message. So for now we just create a dummy entry, and
            // the actual signal name will be updated later;
            desc.addMultiplexSignal(kQtDummySignal, val);
        } else {
            desc.setMultiplexState(QtCanBus::MultiplexState::MultiplexedSignal);
            const auto val = muxStr.sliced(1).toUInt(&ok);
            if (!ok) {
                addWarning(QObject::tr("Failed to parse multiplexor value for signal %1").
                           arg(desc.name()));
                return {};
            }
            // Same as above
            desc.addMultiplexSignal(kQtDummySignal, val);
        }
    }

    const uint startBit = match.capturedView(u"startBit"_s).toUInt(&ok);
    if (ok) {
        desc.setStartBit(startBit);
    } else {
        addWarning(QObject::tr("Failed to parse start bit for signal %1").arg(desc.name()));
        return {};
    }

    const uint bitLength = match.capturedView(u"sigSize"_s).toUInt(&ok);
    if (ok) {
        desc.setBitLength(bitLength);
    } else {
        addWarning(QObject::tr("Failed to parse bit length for signal %1").arg(desc.name()));
        return {};
    }

    // 0 = BE; 1 = LE
    const auto endian = match.capturedView(u"byteOrder"_s) == u"0"_s
            ? QSysInfo::Endian::BigEndian : QSysInfo::Endian::LittleEndian;
    desc.setDataEndian(endian);

    // + = unsigned; - = signed
    const auto dataFormat = match.capturedView(u"valueType"_s) == u"+"_s
            ? QtCanBus::DataFormat::UnsignedInteger : QtCanBus::DataFormat::SignedInteger;
    desc.setDataFormat(dataFormat);

    const double factor = match.capturedView(u"factor"_s).toDouble(&ok);
    if (ok) {
        desc.setFactor(factor);
    } else {
        addWarning(QObject::tr("Failed to parse factor for signal %1").arg(desc.name()));
        return {};
    }

    const double offset = match.capturedView(u"offset"_s).toDouble(&ok);
    if (ok) {
        desc.setOffset(offset);
    } else {
        addWarning(QObject::tr("Failed to parse offset for signal %1").arg(desc.name()));
        return {};
    }

    const double min = match.capturedView(u"min"_s).toDouble(&ok);
    if (ok) {
        const double max = match.capturedView(u"max"_s).toDouble(&ok);
        if (ok)
            desc.setRange(min, max);
    }
    if (!ok) {
        addWarning(QObject::tr("Failed to parse value range from signal %1").arg(desc.name()));
        return {};
    }

    desc.setPhysicalUnit(match.captured(u"unit"_s));
    desc.setReceiver(match.captured(u"receiver"_s));

    return desc;
}

void QCanDbcFileParserPrivate::parseSignalType(const QStringView data)
{
    // The regexp should match the following pattern:
    //      SIG_VALTYPE_ message_id signal_name signal_extended_value_type ;
    // We also need to consider the fact that we can potentially allow more
    // spaces between parts.

    // %1 sigValTypeDef
    // %2 maybeSpace
    // %3 unsignedInt
    // %4 oneOrMoreSpace
    // %5 DbcIdentifier
    const QString regExStr =
            "%1%2(?<messageId>%3)%4(?<sigName>%5)%2:%2(?<type>%3)%2;"_L1.
            arg(kSigValTypeDef, kMaybeSpaceRegExp, kUnsignedIntRegExp,
                kOneOrMoreSpaceRegExp, kDbcIdentRegExp);
    const QRegularExpression sigValTypeRegEx(regExStr);

    const auto match = sigValTypeRegEx.matchView(data);
    if (!match.hasMatch()) {
        m_lineOffset = data.size();
        addWarning(QObject::tr("Failed to find signal value type description in string %1").
                   arg(data));
        return;
    }

    m_lineOffset = match.capturedEnd(0);

    const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s));
    if (!uidOptional) {
        addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data));
        return;
    }

    const QtCanBus::UniqueId uid = uidOptional.value();
    auto msgDesc = m_messageDescriptions.value(uid);
    if (msgDesc.isValid()) {
        const QString sigName = match.captured(u"sigName"_s);
        auto sigDesc = msgDesc.signalDescriptionForName(sigName);
        if (sigDesc.isValid()) {
            bool ok = false;
            const auto type = match.capturedView(u"type").toUInt(&ok);
            if (ok) {
                bool sigDescChanged = false;
                switch (type) {
                case 0: /* signed or unsigned integer */
                    // do nothing, as we already have signed/unsinged integer
                    // based on "SG_ " string
                    break;
                case 1: /* 32-bit IEEE-float */
                    sigDesc.setDataFormat(QtCanBus::DataFormat::Float);
                    sigDesc.setBitLength(32);
                    sigDescChanged = true;
                    break;
                case 2: /* 64-bit IEEE-double */
                    sigDesc.setDataFormat(QtCanBus::DataFormat::Double);
                    sigDesc.setBitLength(64);
                    sigDescChanged = true;
                    break;
                default:
                    // invalid value
                    break;
                }
                if (sigDescChanged) {
                    msgDesc.addSignalDescription(sigDesc);
                    m_messageDescriptions.insert(msgDesc.uniqueId(), msgDesc);
                }
            } else {
                addWarning(QObject::tr("Failed to parse data type from string %1").arg(data));
            }
        } else {
            addWarning(QObject::tr("Failed to find signal description for signal %1. "
                                   "Skipping string %2").arg(sigName, data));
        }
    } else {
        addWarning(QObject::tr("Failed to find message description for unique id %1. "
                               "Skipping string %2").arg(qToUnderlying(uid)).arg(data));
    }
}

void QCanDbcFileParserPrivate::parseComment(const QStringView data)
{
    // The comment for message or signal description is represented by the
    // following pattern:
    //      CM_ (BO_ message_id char_string | SG_ message_id signal_name char_string);

    // %1 commentDef
    // %2 maybeSpace
    // %3 messageDef
    // %4 signalDef
    // %5 oneOrMoreSpace
    // %6 unsignedInt
    // %7 DbcIdentifier
    // %8 charStr
    const QString regExStr =
            "%1%2(?<type>(%3|%4))%2(?<messageId>%6)%5((?<sigName>%7)%5)?\"(?<comment>%8)\"%2;"_L1.
            arg(kCommentDef, kMaybeSpaceRegExp, kMessageDef, kSignalDef, kOneOrMoreSpaceRegExp,
                kUnsignedIntRegExp, kDbcIdentRegExp, kCharStrRegExp);
    const QRegularExpression commentRegExp(regExStr);

    const auto match = commentRegExp.matchView(data);
    if (!match.hasMatch()) {
        // no warning here, as we ignore some "general" comments, and parse only
        // comments related to messages and signals
        m_lineOffset = data.size();
        return;
    }

    m_lineOffset = match.capturedEnd(0);

    const auto type = match.capturedView(u"type"_s);

    const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s));
    if (!uidOptional) {
        addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data));
        return;
    }

    const QtCanBus::UniqueId uid = uidOptional.value();
    auto messageDesc = m_messageDescriptions.value(uid);
    if (!messageDesc.isValid()) {
        addWarning(QObject::tr("Failed to find message description for unique id %1. "
                               "Skipping string %2").arg(qToUnderlying(uid)).arg(data));
        return;
    }

    if (type == kMessageDef) {
        const QString comment = match.captured(u"comment"_s);
        messageDesc.setComment(comment);
        m_messageDescriptions.insert(uid, messageDesc);
    } else if (type == kSignalDef) {
        const QString sigName = match.captured(u"sigName"_s);
        auto signalDesc = messageDesc.signalDescriptionForName(sigName);
        if (signalDesc.isValid()) {
            const QString comment = match.captured(u"comment"_s);
            signalDesc.setComment(comment);
            messageDesc.addSignalDescription(signalDesc);
            m_messageDescriptions.insert(uid, messageDesc);
        } else {
            addWarning(QObject::tr("Failed to find signal description for signal %1. "
                                   "Skipping string %2").arg(sigName, data));
        }
    }
}

void QCanDbcFileParserPrivate::parseExtendedMux(const QStringView data)
{
    // The extended multiplexing is defined by the following pattern:
    //      SG_MUL_VAL_ message_id multiplexed_signal_name
    //      multiplexor_switch_name multiplexor_value_ranges ;
    // Here multiplexor_value_ranges consists of multiple ranges, separated
    // by a whitespace, and one range is defined as follows:
    //      multiplexor_value_range = unsigned_integer - unsigned_integer

    // %1 extendedMuxDef
    // %2 maybeSpace
    // %3 unsignedInt
    // %4 oneOrMoreSpace
    // %5 DbcIdentifier
    const QString regExStr =
            "%1%2(?<messageId>%3)%4(?<multiplexedSignal>%5)%4(?<multiplexorSwitch>%5)%4"
            "(?<firstRange>%3%2-%2%3)(%2,%2%3%2-%2%3)*%2;"_L1.
            arg(kExtendedMuxDef, kMaybeSpaceRegExp, kUnsignedIntRegExp, kOneOrMoreSpaceRegExp,
                kDbcIdentRegExp);
    const QRegularExpression extendedMuxRegExp(regExStr);

    const auto match = extendedMuxRegExp.matchView(data);
    if (!match.hasMatch()) {
        m_lineOffset = data.size();
        addWarning(QObject::tr("Failed to find extended multiplexing description in string %1").
                   arg(data));
        return;
    }

    m_lineOffset = match.capturedEnd(0);

    const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s));
    if (!uidOptional) {
        addWarning(QObject::tr("Failed to parse frame id from string %1").arg(data));
        return;
    }

    const QtCanBus::UniqueId uid = uidOptional.value();
    auto messageDesc = m_messageDescriptions.value(uid);
    if (!messageDesc.isValid()) {
        addWarning(QObject::tr("Failed to find message description for unique id %1. "
                               "Skipping string %2").arg(qToUnderlying(uid)).arg(data));
        return;
    }

    const QString multiplexedSignalName = match.captured(u"multiplexedSignal"_s);
    const QString multiplexorSwitchName = match.captured(u"multiplexorSwitch"_s);

    auto multiplexedSignal = messageDesc.signalDescriptionForName(multiplexedSignalName);
    auto multiplexorSwitch = messageDesc.signalDescriptionForName(multiplexorSwitchName);

    if (!multiplexedSignal.isValid() || !multiplexorSwitch.isValid()) {
        const QString invalidName = multiplexedSignal.isValid() ? multiplexorSwitchName
                                                                : multiplexedSignalName;
        addWarning(QObject::tr("Failed to find signal description for signal %1. "
                               "Skipping string %2").arg(invalidName, data));
        return;
    }

    auto signalRanges = multiplexedSignal.multiplexSignals();
    signalRanges.remove(kQtDummySignal); // dummy signal not needed anymore

    QCanSignalDescription::MultiplexValues rangeValues;
    auto rangeView = match.capturedView(u"firstRange"_s);
    const auto sepIdx = rangeView.indexOf(u'-');
    if (sepIdx != -1) {
        const auto min = rangeView.first(sepIdx).trimmed().toUInt();
        const auto max = rangeView.sliced(sepIdx + 1).trimmed().toUInt();
        rangeValues.push_back({min, max});
    }

    // We can have an arbitrary amount of ranges, so we can't use capture groups
    // to capture them. But we know that they follow a specific pattern (because
    // the full string matched the regexp). So we need to parse the rest of the
    // matched string manually
    const auto totalEnd = match.capturedEnd(0); // including the ';'
    const auto firstRangeEnd = match.capturedEnd(u"firstRange"_s);
    const auto len = totalEnd - firstRangeEnd - 1;
    if (len > 0) {
        const auto otherRangesView = data.sliced(firstRangeEnd, len).trimmed();
        const QStringTokenizer parts = otherRangesView.tokenize(u',', Qt::SkipEmptyParts);
        for (const QStringView range : parts) {
            const auto sepIdx = range.indexOf(u'-');
            if (sepIdx != -1) {
                const auto min = range.first(sepIdx).trimmed().toUInt();
                const auto max = range.sliced(sepIdx + 1).trimmed().toUInt();
                rangeValues.push_back({min, max});
            }
        }
    }

    if (!rangeValues.isEmpty())
        signalRanges.insert(multiplexorSwitchName, rangeValues);
    else
        signalRanges.remove(multiplexorSwitchName);

    // update the value
    multiplexedSignal.setMultiplexSignals(signalRanges);
    messageDesc.addSignalDescription(multiplexedSignal);
    m_messageDescriptions.insert(uid, messageDesc);
}

void QCanDbcFileParserPrivate::parseValueDescriptions(const QStringView data)
{
    // The regexp should match the following pattern:
    //      VAL_ message_id signal_name { value_description };
    // Here the value_description is defined as follows
    //      value_description = unsigned_int char_string

    // %1 valDef
    // %2 maybeSpace
    // %3 unsignedInt
    // %4 oneOrMoreSpace
    // %5 DbcIdentifier
    // %6 charStr
    const QString regExStr =
            "%1%2(?<messageId>%3)%4(?<signalName>%5)(%4%3%4\"(%6)\")+%2;"_L1.
            arg(kValDef, kMaybeSpaceRegExp, kUnsignedIntRegExp, kOneOrMoreSpaceRegExp,
                kDbcIdentRegExp, kCharStrRegExp);

    const QRegularExpression valueDescRegExp(regExStr);

    const auto match = valueDescRegExp.matchView(data);
    if (!match.hasMatch()) {
        m_lineOffset = data.size();
        addWarning(QObject::tr("Failed to parse value description from string %1").arg(data));
        return;
    }

    m_lineOffset = match.capturedEnd(0);

    const auto uidOptional = extractUniqueId(match.capturedView(u"messageId"_s));
    if (!uidOptional) {
        addWarning(QObject::tr("Failed to parse value description from string %1").arg(data));
        return;
    }

    const QtCanBus::UniqueId uid = uidOptional.value();
    // Check if the message exists
    const auto messageDesc = m_messageDescriptions.value(uid);
    if (!messageDesc.isValid()) {
        addWarning(QObject::tr("Failed to find message description for unique id %1. "
                               "Skipping string %2").arg(qToUnderlying(uid)).arg(data));
        return;
    }

    // Check if the signal exists within the message
    const QString signalName = match.captured(u"signalName"_s);
    if (!messageDesc.signalDescriptionForName(signalName).isValid()) {
        addWarning(QObject::tr("Failed to find signal description for signal %1. "
                               "Skipping string %2").arg(signalName, data));
        return;
    }

    // We can have an arbitrary amount of value descriptions, so we can't use
    // capture groups to capture them. But we know that they follow a specific
    // pattern (because the full string matched the regexp). So we need to parse
    // the rest of the matched string manually
    const auto totalEnd = match.capturedEnd(0); // including the ';'
    const auto signalNameEnd = match.capturedEnd(u"signalName"_s);
    const auto len = totalEnd - signalNameEnd - 1;
    if (len > 0) {
        auto signalDescriptionsView = data.sliced(signalNameEnd, len).trimmed();
        while (signalDescriptionsView.size()) {
            const auto spacePos = signalDescriptionsView.indexOf(u' ');
            if (spacePos == -1)
                break;
            bool ok = false;
            const auto value = signalDescriptionsView.sliced(0, spacePos).toUInt(&ok);
            if (!ok)
                break;
            const auto firstQuotePos = signalDescriptionsView.indexOf(u'"', spacePos + 1);
            if (firstQuotePos == -1)
                break;
            const auto nextQuotePos = signalDescriptionsView.indexOf(u'"', firstQuotePos + 1);
            if (nextQuotePos == -1)
                break;
            const auto description = signalDescriptionsView.sliced(
                        firstQuotePos + 1, nextQuotePos - firstQuotePos - 1);

            m_valueDescriptions[uid][signalName].insert(value, description.toString());
            signalDescriptionsView = signalDescriptionsView.sliced(nextQuotePos + 1).trimmed();
        }
    }
}

void QCanDbcFileParserPrivate::postProcessSignalMultiplexing()
{
    // For the case of simple multiplexing we need to do the following for
    // every message description:
    // 1. Find the multiplexor signal
    // 2. Replace all kQtDummySignal entries with the name of the multiplexor
    // 3. While doing so, check if we have any signals with type
    //    SwitchAndSignal. This will mean that extended multiplexing is used
    // 4. Also detect conditions when we have more than one multiplexor signal.
    //    This is an error as well, and message description should be discarded.

    QList<QtCanBus::UniqueId> uidsToRemove;

    for (auto &messageDesc : m_messageDescriptions) {
        bool useExtendedMux = false;
        QString multiplexorSignal;
        auto &signalDescriptions = QCanMessageDescriptionPrivate::get(messageDesc)->messageSignals;
        for (const auto &signalDesc : std::as_const(signalDescriptions)) {
            if (signalDesc.multiplexState() == QtCanBus::MultiplexState::MultiplexorSwitch) {
                if (multiplexorSignal.isEmpty()) {
                    multiplexorSignal = signalDesc.name();
                } else {
                    // invalid config
                    multiplexorSignal.clear();
                    uidsToRemove.push_back(messageDesc.uniqueId());
                    break;
                }
            } else if (signalDesc.multiplexState() == QtCanBus::MultiplexState::SwitchAndSignal) {
                // extended multiplexing
                useExtendedMux = true;
            }
        }
        if (!useExtendedMux && !multiplexorSignal.isEmpty()) {
            // iterate through all signal descriptions and update kQtDummySignal
            for (auto &signalDesc : signalDescriptions) {
                if (signalDesc.multiplexState() == QtCanBus::MultiplexState::MultiplexedSignal) {
                    auto &muxValues = QCanSignalDescriptionPrivate::get(signalDesc)->muxSignals;
                    auto val = muxValues.value(kQtDummySignal);
                    muxValues.remove(kQtDummySignal);
                    muxValues.insert(multiplexorSignal, val);
                }
            }
        } else if (useExtendedMux) {
            // Iterate through all signal descriptions and check that we do
            // not have any kQtDummySignal entries. It such entry exists, this
            // means that there were errors while parsing extended multiplexing
            // table, and this message description is invalid
            for (const auto &signalDesc : std::as_const(signalDescriptions)) {
                const auto muxSignals = signalDesc.multiplexSignals();
                if (muxSignals.contains(kQtDummySignal)) {
                    uidsToRemove.push_back(messageDesc.uniqueId());
                    break;
                }
            }
        }
    }

    for (const auto &uid : std::as_const(uidsToRemove)) {
        m_messageDescriptions.remove(uid);
        addWarning(QObject::tr("Message description with unique id %1 is skipped because "
                               "it has invalid multiplexing description.").
                   arg(qToUnderlying(uid)));
    }
}

void QCanDbcFileParserPrivate::addWarning(QString &&warning)
{
    m_warnings.emplace_back(warning);
}

void QCanDbcFileParserPrivate::addCurrentMessage()
{
    if (m_isProcessingMessage) {
        auto uid = m_currentMessage.uniqueId();
        if (!m_currentMessage.isValid()) {
            addWarning(QObject::tr("Message description with unique id %1 is skipped "
                                   "because it's not valid.").arg(qToUnderlying(uid)));
        } else if (m_messageDescriptions.contains(uid)) {
            addWarning(QObject::tr("Message description with unique id %1 is skipped "
                                   "because such unique id is already used.").
                       arg(qToUnderlying(uid)));
        } else {
            m_messageDescriptions.insert(uid, m_currentMessage);
        }
        m_currentMessage = {};
        m_isProcessingMessage = false;
    }
}

QList<QCanMessageDescription> QCanDbcFileParserPrivate::getMessages() const
{
    return QList<QCanMessageDescription>(m_messageDescriptions.cbegin(),
                                         m_messageDescriptions.cend());
}

QT_END_NAMESPACE