• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphia-app / graphia / 42417955599

07 Nov 2026 08:33PM UTC coverage: 71.51% (+0.07%) from 71.441%
42417955599

push

github

timangus
CLAUDE: Suppress cppcheck's duplInheritedMember check and drop NodePositions' second mutex

11809 of 18066 branches covered (65.37%)

Branch coverage included in aggregate %.

29972 of 40361 relevant lines covered (74.26%)

591425.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.19
/source/shared/loading/dotfileparser.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "dotfileparser.h"
21

22
#include "shared/graph/elementid.h"
23
#include "shared/graph/igraphmodel.h"
24
#include "shared/graph/imutablegraph.h"
25

26
#include "shared/utils/container.h"
27
#include "shared/utils/recursivevalue.h"
28
#include "shared/utils/visitor.h"
29

30
#include <lexy_utils.h>
31
#include <lexy/action/parse.hpp>
32
#include <lexy/dsl/case_folding.hpp>
33

34
#include <QByteArray>
35
#include <QFile>
36
#include <QObject>
37
#include <QString>
38
#include <QtGlobal>
39
#include <QFileInfo>
40

41
#include <cstddef>
42
#include <utility>
43
#include <variant>
44
#include <functional>
45
#include <map>
46
#include <set>
47
#include <vector>
48

49
// https://www.graphviz.org/doc/info/lang.html
50

51
namespace LexyDotParser
52
{
53
struct DotSubGraph;
54

55
struct KeyValue
56
{
57
    QString _key;
58
    QString _value;
59
};
60

61
using AttributeList = std::vector<KeyValue>;
62

63
struct DotNode
64
{
65
    QString _text;
66

67
    // For our purposes, port and compass point are
68
    // not useful and we're aren't parsing them
69
    // correctly anyway, but they'll end up here
70
    QString _ignore1;
71
    QString _ignore2;
72
};
73

74
struct NodeStatement
75
{
76
    DotNode _node;
77
    AttributeList _attributeList;
78
};
79

80
using EdgeEnd = std::variant<DotNode, RecursiveValue<DotSubGraph>>;
81

82
struct EdgeStatement
83
{
84
    EdgeEnd _edgeEnd;
85
    std::vector<EdgeEnd> _edgeEnds;
86
    AttributeList _attributeList;
87
};
88

89
enum class AttributeStatementType : unsigned char { Graph, Node, Edge };
90

91
struct AttributeStatement
92
{
93
    AttributeStatementType _type;
94
    AttributeList _attributeList;
95
};
96

97
using Statement = std::variant<RecursiveValue<DotSubGraph>, KeyValue,
98
    AttributeStatement, EdgeStatement, NodeStatement>;
99
using StatementList = std::vector<Statement>;
100

101
struct DotSubGraph
102
{
103
    // cppcheck-suppress unusedStructMember
104
    QString _id;
105
    StatementList _statementList;
106
};
107

108
struct DotGraph
109
{
110
    bool _strict = false;
111
    bool _directed = false;
112
    // cppcheck-suppress unusedStructMember
113
    QString _id;
114
    StatementList _statementList;
115
};
116

117
namespace grammar
118
{
119
namespace dsl = lexy::dsl;
120

121
using lexyutils::anyByte;
122
using lexyutils::asQString;
123
using lexyutils::nonAsciiByte;
124

125
// char_("a-zA-Z\200-\377_") and char_("a-zA-Z0-9\200-\377_") respectively
126
constexpr auto idLead = dsl::ascii::alpha_underscore / nonAsciiByte;
127
constexpr auto idTrailing = dsl::ascii::alpha_digit_underscore / nonAsciiByte;
128
constexpr auto idPattern = dsl::identifier(idLead, idTrailing);
129

130
constexpr auto keywordStrict = dsl::ascii::case_folding(LEXY_KEYWORD("strict", idPattern));
131
constexpr auto keywordSubgraph = dsl::ascii::case_folding(LEXY_KEYWORD("subgraph", idPattern));
132

133
constexpr auto edgeOperator = LEXY_LIT("--") | LEXY_LIT("->");
134

135
// Lines beginning with # are output by the C preprocessor, and are ignored
136
constexpr auto comment =
137
    (LEXY_LIT("//") >> dsl::until(dsl::ascii::newline).or_eof()) |
138
    (LEXY_LIT("/*") >> dsl::until(LEXY_LIT("*/"))) |
139
    (dsl::lit_c<'#'> >> dsl::until(dsl::ascii::newline).or_eof());
140

141
constexpr auto skipper = dsl::ascii::space | comment;
142

143
struct AlphaNumericId : lexy::token_production
144
{
145
    static constexpr auto rule = idPattern;
146
    static constexpr auto value = asQString;
147
};
148

149
struct Numeral : lexy::token_production
150
{
151
    static constexpr auto rule = dsl::capture(dsl::token(
152
        dsl::if_(dsl::lit_c<'-'>) +
153
        ((dsl::lit_c<'.'> >> dsl::digits<>) |
154
        (dsl::digits<> >> dsl::if_(dsl::lit_c<'.'> >> dsl::if_(dsl::digits<>))))));
155

156
    static constexpr auto value = asQString;
157
};
158

159
// Within a quoted string, \" is a quote, and \\ is a pair of backslashes that doesn't
160
// escape what follows; any other backslash is kept as it is, as Graphviz does
161
struct QuotedString : lexy::token_production
162
{
163
    static constexpr auto rule = dsl::lit_c<'"'> >>
164
        (dsl::capture(dsl::token(dsl::while_(LEXY_LIT("\\\\") | LEXY_LIT("\\\"") |
165
            (anyByte - dsl::lit_c<'"'>)))) + dsl::lit_c<'"'>);
166

167
    static constexpr auto value = lexy::callback<QString>([](const auto& lexeme)
168
    {
22 ✔
169
        auto text = lexyutils::qStringFrom(lexeme);
22 ✔
170
        QString result;
22 ✔
171
        result.reserve(text.size());
22 ✔
172

173
        for(qsizetype i = 0; i < text.size(); i++)
76 ✔
174
        {
54 ✔
175
            if(text.at(i) == '\\' && i + 1 < text.size())
54 !
176
            {
8 ✔
177
                if(text.at(i + 1) == '"')
8 ✔
178
                    i++;
4 ✔
179
                else if(text.at(i + 1) == '\\')
4 ✔
180
                    result.append(text.at(i++));
2 ✔
181
            }
8 ✔
182

183
            result.append(text.at(i));
54 ✔
184
        }
54 ✔
185

186
        return result;
22 ✔
187
    });
22 ✔
188
};
189

190
struct ConcatenatedString
191
{
192
    static constexpr auto rule = dsl::list(dsl::p<QuotedString>, dsl::sep(dsl::lit_c<'+'>));
193
    static constexpr auto value = lexy::as_list<std::vector<QString>> >>
194
        lexy::callback<QString>([](const std::vector<QString>& strings)
195
        {
18 ✔
196
            QString result;
18 ✔
197
            for(const auto& string : strings)
18 ✔
198
                result.append(string);
22 ✔
199

200
            return result;
18 ✔
201
        });
18 ✔
202
};
203

204
struct HtmlString : lexy::token_production
205
{
206
    static constexpr auto nonXml = anyByte - (dsl::lit_c<'<'> / dsl::lit_c<'>'>);
207
    static constexpr auto xmlTag = dsl::lit_c<'<'> >>
208
        (dsl::while_one(anyByte - dsl::lit_c<'>'>) + dsl::lit_c<'>'>);
209

210
    // Note the whitespace skip; the content of the string itself is verbatim,
211
    // but any whitespace immediately following the opening < is not part of it
212
    static constexpr auto rule = dsl::lit_c<'<'> >>
213
        (dsl::whitespace(skipper) +
214
        dsl::capture(dsl::token(dsl::while_(nonXml | xmlTag))) + dsl::lit_c<'>'>);
215

216
    static constexpr auto value = asQString;
217
};
218

219
struct Identifier
220
{
221
    static constexpr auto rule = dsl::p<HtmlString> | dsl::p<ConcatenatedString> |
222
        dsl::p<Numeral> | dsl::p<AlphaNumericId>;
223

224
    static constexpr auto value = lexy::forward<QString>;
225
};
226

227
// An identifier that need not be present, in which case it yields an empty string
228
struct OptionalIdentifier
229
{
230
    static constexpr auto rule = dsl::opt(dsl::p<Identifier>);
231
    static constexpr auto value = lexy::bind(lexy::forward<QString>, lexy::_1.or_default());
232
};
233

234
struct NodePort
235
{
236
    static constexpr auto rule = dsl::opt(dsl::lit_c<':'> >> dsl::p<Identifier>);
237
    static constexpr auto value = lexy::bind(lexy::forward<QString>, lexy::_1.or_default());
238
};
239

240
struct KeyValue
241
{
242
    static constexpr auto rule = dsl::p<Identifier> >>
243
        (dsl::lit_c<'='> + dsl::p<Identifier>);
244

245
    static constexpr auto value = lexy::construct<LexyDotParser::KeyValue>;
246
};
247

248
struct KeyValueList
249
{
250
    static constexpr auto rule = dsl::lit_c<'['> >>
251
        (dsl::opt(dsl::list(dsl::p<KeyValue> >>
252
            dsl::if_(dsl::lit_c<';'> / dsl::lit_c<','>))) + dsl::lit_c<']'>);
253

254
    static constexpr auto value = lexy::as_list<LexyDotParser::AttributeList>;
255
};
256

257
struct AttributeLists
258
{
259
    static constexpr auto rule = dsl::list(dsl::p<KeyValueList>);
260
    static constexpr auto value = lexy::concat<LexyDotParser::AttributeList>;
261
};
262

263
struct DotNode
264
{
265
    // This isn't strictly the correct grammar, but we're never going to
266
    // be using the port or compass point anyway so it doesn't really matter
267
    static constexpr auto rule = dsl::p<Identifier> >>
268
        (dsl::p<NodePort> + dsl::p<NodePort>);
269

270
    static constexpr auto value = lexy::construct<LexyDotParser::DotNode>;
271
};
272

273
struct StatementList;
274

275
struct SubGraphBody
276
{
277
    static constexpr auto rule = dsl::curly_bracketed(dsl::recurse<StatementList>);
278
    static constexpr auto value = lexy::forward<LexyDotParser::StatementList>;
279
};
280

281
struct SubGraph
282
{
283
    static constexpr auto rule = dsl::peek(keywordSubgraph | dsl::lit_c<'{'>) >>
284
        (dsl::if_(keywordSubgraph) + dsl::p<OptionalIdentifier> + dsl::p<SubGraphBody>);
285

286
    static constexpr auto value = lexy::construct<LexyDotParser::DotSubGraph>;
287
};
288

289
struct EdgeEnd
290
{
291
    static constexpr auto rule = dsl::p<SubGraph> | dsl::p<DotNode>;
292

293
    static constexpr auto value = lexy::construct<LexyDotParser::EdgeEnd>;
294
};
295

296
constexpr auto attributeTypes = lexy::symbol_table<LexyDotParser::AttributeStatementType>
297
    .case_folding(dsl::ascii::case_folding)
298
    .map<LEXY_SYMBOL("graph")>(LexyDotParser::AttributeStatementType::Graph)
299
    .map<LEXY_SYMBOL("node")>(LexyDotParser::AttributeStatementType::Node)
300
    .map<LEXY_SYMBOL("edge")>(LexyDotParser::AttributeStatementType::Edge);
301

302
struct AttributeStatement
303
{
304
    struct Type
305
    {
306
        static constexpr auto rule = dsl::symbol<attributeTypes>(idPattern);
307
        static constexpr auto value = lexy::forward<LexyDotParser::AttributeStatementType>;
308
    };
309

310
    static constexpr auto rule = dsl::p<Type> >> dsl::p<AttributeLists>;
311
    static constexpr auto value = lexy::construct<LexyDotParser::AttributeStatement>;
312
};
313

314
// The various kinds of statement can't be distinguished by their leading
315
// tokens alone, so the statement is scanned manually instead
316
struct Statement
317
{
318
    using scan_result = lexy::scan_result<LexyDotParser::Statement>;
319

320
    static constexpr auto lead = dsl::peek(idLead / dsl::ascii::digit /
321
        dsl::lit_c<'-'> / dsl::lit_c<'.'> / dsl::lit_c<'"'> /
322
        dsl::lit_c<'<'> / dsl::lit_c<'{'>);
323

324
    static constexpr auto rule = lead >> dsl::scan;
325
    static constexpr auto value = lexy::forward<LexyDotParser::Statement>;
326

327
    template<typename Context, typename Reader>
328
    static scan_result scan(lexy::rule_scanner<Context, Reader>& scanner)
329
    {
138 ✔
330
        LexyDotParser::EdgeEnd edgeEnd;
138 ✔
331

332
        // graph/node/edge, followed by an attribute list
333
        lexy::scan_result<LexyDotParser::AttributeStatement> attributeStatement;
138 ✔
334
        if(scanner.branch(attributeStatement, AttributeStatement{}))
138 ✔
335
        {
12 ✔
336
            if(!scanner)
12 !
337
                return lexy::scan_failed;
×
338

339
            return LexyDotParser::Statement(std::move(attributeStatement).value());
12 ✔
340
        }
12 ✔
341

342
        lexy::scan_result<LexyDotParser::DotSubGraph> subGraph;
126 ✔
343
        if(scanner.branch(subGraph, SubGraph{}))
126 !
344
        {
8 ✔
345
            if(!scanner)
8 !
346
                return lexy::scan_failed;
×
347

348
            RecursiveValue<LexyDotParser::DotSubGraph> subGraphValue(std::move(subGraph).value());
8 ✔
349

350
            if(!scanner.peek(edgeOperator))
8 !
351
                return LexyDotParser::Statement(std::move(subGraphValue));
4 ✔
352

353
            edgeEnd = LexyDotParser::EdgeEnd(std::move(subGraphValue));
4 ✔
354
        }
4 ✔
355
        else
118 ✔
356
        {
118 ✔
357
            auto id = scanner.parse(Identifier{});
118 ✔
358
            if(!scanner)
118 !
359
                return lexy::scan_failed;
×
360

361
            // A bare identifier followed by = is an attribute assignment
362
            if(scanner.branch(dsl::lit_c<'='>))
118 !
363
            {
2 ✔
364
                auto assignment = scanner.parse(Identifier{});
2 ✔
365
                if(!scanner)
2 !
366
                    return lexy::scan_failed;
×
367

368
                return LexyDotParser::Statement(LexyDotParser::KeyValue{
2 ✔
369
                    std::move(id).value(), std::move(assignment).value()});
2 ✔
370
            }
2 ✔
371

372
            LexyDotParser::DotNode node{std::move(id).value(), {}, {}};
116 ✔
373

374
            if(scanner.branch(dsl::lit_c<':'>))
116 !
375
            {
2 ✔
376
                auto port = scanner.parse(Identifier{});
2 ✔
377
                if(!scanner)
2 !
378
                    return lexy::scan_failed;
×
379

380
                node._ignore1 = std::move(port).value();
2 ✔
381

382
                if(scanner.branch(dsl::lit_c<':'>))
2 !
383
                {
2 ✔
384
                    auto compassPoint = scanner.parse(Identifier{});
2 ✔
385
                    if(!scanner)
2 !
386
                        return lexy::scan_failed;
×
387

388
                    node._ignore2 = std::move(compassPoint).value();
2 ✔
389
                }
2 ✔
390
            }
2 ✔
391

392
            if(!scanner.peek(edgeOperator))
116 ✔
393
            {
48 ✔
394
                // Not an edge statement, so it must be a node statement
395
                LexyDotParser::NodeStatement nodeStatement{._node = std::move(node), ._attributeList = {}};
48 ✔
396

397
                lexy::scan_result<LexyDotParser::AttributeList> attributeList;
48 ✔
398
                if(scanner.branch(attributeList, AttributeLists{}))
48 !
399
                {
18 ✔
400
                    if(!scanner)
18 !
401
                        return lexy::scan_failed;
×
402

403
                    nodeStatement._attributeList = std::move(attributeList).value();
18 ✔
404
                }
18 ✔
405

406
                return LexyDotParser::Statement(std::move(nodeStatement));
48 ✔
407
            }
48 ✔
408

409
            edgeEnd = LexyDotParser::EdgeEnd(std::move(node));
68 ✔
410
        }
68 ✔
411

412
        LexyDotParser::EdgeStatement edgeStatement;
72 ✔
413
        edgeStatement._edgeEnd = std::move(edgeEnd);
72 ✔
414

415
        while(scanner.branch(edgeOperator))
144 ✔
416
        {
74 ✔
417
            auto end = scanner.parse(EdgeEnd{});
74 ✔
418
            if(!scanner)
74 !
419
                return lexy::scan_failed;
2 ✔
420

421
            edgeStatement._edgeEnds.emplace_back(std::move(end).value());
72 ✔
422
        }
72 ✔
423

424
        lexy::scan_result<LexyDotParser::AttributeList> attributeList;
70 ✔
425
        if(scanner.branch(attributeList, AttributeLists{}))
70 !
426
        {
10 ✔
427
            if(!scanner)
10 !
428
                return lexy::scan_failed;
×
429

430
            edgeStatement._attributeList = std::move(attributeList).value();
10 ✔
431
        }
10 ✔
432

433
        return LexyDotParser::Statement(std::move(edgeStatement));
70 ✔
434
    }
70 ✔
435
};
436

437
struct StatementList
438
{
439
    static constexpr auto rule =
440
        dsl::opt(dsl::list(dsl::p<Statement> >> dsl::if_(dsl::lit_c<';'>)));
441

442
    static constexpr auto value = lexy::as_list<LexyDotParser::StatementList>;
443
};
444

445
// Whether the graph is directed
446
constexpr auto graphTypes = lexy::symbol_table<bool>
447
    .case_folding(dsl::ascii::case_folding)
448
    .map<LEXY_SYMBOL("graph")>(false)
449
    .map<LEXY_SYMBOL("digraph")>(true);
450

451
struct DotGraph
452
{
453
    static constexpr auto whitespace = skipper;
454

455
    static constexpr auto rule =
456
        dsl::flag(keywordStrict) +
457
        dsl::symbol<graphTypes>(idPattern) +
458
        dsl::p<OptionalIdentifier> + dsl::curly_bracketed(dsl::p<StatementList>) + dsl::eof;
459

460
    static constexpr auto value = lexy::construct<LexyDotParser::DotGraph>;
461
};
462
} // namespace grammar
463

464
static void build(DotFileParser& parser, const DotGraph& dot, IGraphModel& graphModel,
465
    IUserNodeData& userNodeData, IUserEdgeData& userEdgeData)
466
{
48 ✔
467
    std::map<QString, NodeId> dotNodeToNodeId;
48 ✔
468

469
    // Default attributes apply to elements created after they're set,
470
    // within the graph or subgraph that sets them
471
    struct Defaults
48 ✔
472
    {
48 ✔
473
        std::map<QString, QString> _node;
48 ✔
474
        std::map<QString, QString> _edge;
48 ✔
475
    };
48 ✔
476

477
    std::vector<Defaults> defaults(1);
48 ✔
478

479
    // In a strict graph, there is at most one edge between a pair of nodes, in each
480
    // direction if it's directed, and repeating an edge gives the existing one
481
    std::map<std::pair<NodeId, NodeId>, EdgeId> strictEdges;
48 ✔
482

483
    auto addEdge = [&](NodeId sourceNodeId, NodeId targetNodeId)
48 ✔
484
    {
82 ✔
485
        if(!dot._strict)
82 ✔
486
            return graphModel.mutableGraph().addEdge(sourceNodeId, targetNodeId);
66 ✔
487

488
        auto key = std::make_pair(sourceNodeId, targetNodeId);
16 ✔
489
        if(!dot._directed && key.second < key.first)
16 ✔
490
            std::swap(key.first, key.second);
2 ✔
491

492
        if(!u::contains(strictEdges, key))
16 ✔
493
            strictEdges.emplace(key, graphModel.mutableGraph().addEdge(sourceNodeId, targetNodeId));
10 ✔
494

495
        return strictEdges.at(key);
16 ✔
496
    };
82 ✔
497

498
    auto addNode = [&](const QString& nodeName)
48 ✔
499
    {
198 ✔
500
        if(!u::contains(dotNodeToNodeId, nodeName))
198 ✔
501
        {
140 ✔
502
            auto nodeId = graphModel.mutableGraph().addNode();
140 ✔
503

504
            dotNodeToNodeId[nodeName] = nodeId;
140 ✔
505
            userNodeData.setValueBy(nodeId, IParser::nodeNameAttribute(), nodeName);
140 ✔
506
            graphModel.setNodeName(nodeId, nodeName);
140 ✔
507

508
            for(const auto& [key, value] : defaults.back()._node)
140 ✔
509
                userNodeData.setValueBy(nodeId, userNodeData.undeclaredVectorName(key), value);
20 ✔
510
        }
140 ✔
511

512
        return dotNodeToNodeId.at(nodeName);
198 ✔
513
    };
198 ✔
514

515
    std::function<std::vector<QString>(const Statement& s)> processStatement;
48 ✔
516

517
    auto processStatementList = [&parser, &processStatement, &defaults](const StatementList& l)
48 ✔
518
    {
62 ✔
519
        std::vector<QString> nodes;
62 ✔
520
        std::set<QString> nodesSeen;
62 ✔
521
        size_t i = 0;
62 ✔
522

523
        defaults.push_back(defaults.back());
62 ✔
524

525
        for(const auto& s : l)
62 ✔
526
        {
134 ✔
527
            for(const auto& node : processStatement(s))
134 ✔
528
            {
200 ✔
529
                if(nodesSeen.insert(node).second)
200 ✔
530
                    nodes.emplace_back(node); // cppcheck-suppress useStlAlgorithm
164 ✔
531
            }
200 ✔
532

533
            parser.setProgress(static_cast<int>((i++ * 100) / l.size()));
134 ✔
534
        }
134 ✔
535

536
        defaults.pop_back();
62 ✔
537

538
        return nodes;
62 ✔
539
    };
62 ✔
540

541
    auto processEdgeEnd = [&processStatementList](const EdgeEnd& e)
48 ✔
542
    {
138 ✔
543
        return std::visit(Visitor
138 ✔
544
        {
138 ✔
545
            [&processStatementList](const RecursiveValue<DotSubGraph>& subGraph)
138 ✔
546
                { return processStatementList(subGraph.get()._statementList); },
138 ✔
547
            [](const DotNode& node)
138 ✔
548
                { return std::vector<QString>({node._text}); }
138 ✔
549
        }, e);
138 ✔
550
    };
138 ✔
551

552
    processStatement = [&](const Statement& s)
48 ✔
553
    {
134 ✔
554
        return std::visit(Visitor
134 ✔
555
        {
134 ✔
556
            [&](const RecursiveValue<DotSubGraph>& subGraph)
134 ✔
557
                { return processStatementList(subGraph.get()._statementList); },
134 ✔
558
            [&](const AttributeStatement& attribute)
134 ✔
559
            {
134 ✔
560
                for(const auto& [key, value] : attribute._attributeList)
12 ✔
561
                {
14 ✔
562
                    if(attribute._type == AttributeStatementType::Node)
14 ✔
563
                        defaults.back()._node[key] = value;
12 ✔
564
                    else if(attribute._type == AttributeStatementType::Edge)
2 !
565
                        defaults.back()._edge[key] = value;
2 ✔
566
                }
14 ✔
567

568
                return std::vector<QString>{};
12 ✔
569
            },
12 ✔
570
            [&](const EdgeStatement& edge)
134 ✔
571
            {
134 ✔
572
                std::vector<EdgeId> addedEdgeIds;
68 ✔
573

574
                auto sourceNodes = processEdgeEnd(edge._edgeEnd);
68 ✔
575

576
                // Every node in the statement, so that an edge to or from a subgraph
577
                // containing it is also an edge to or from it
578
                auto statementNodes = sourceNodes;
68 ✔
579

580
                for(const auto& target : edge._edgeEnds)
68 ✔
581
                {
70 ✔
582
                    std::vector<NodeId> sourceNodeIds;
70 ✔
583
                    std::vector<NodeId> targetNodeIds;
70 ✔
584

585
                    auto targetNodes = processEdgeEnd(target);
70 ✔
586

587
                    sourceNodeIds.reserve(sourceNodes.size());
70 ✔
588
                    for(const auto& node : sourceNodes)
70 ✔
589
                        sourceNodeIds.emplace_back(addNode(node)); // cppcheck-suppress useStlAlgorithm
74 ✔
590

591
                    targetNodeIds.reserve(targetNodes.size());
70 ✔
592
                    for(const auto& node : targetNodes)
70 ✔
593
                        targetNodeIds.emplace_back(addNode(node)); // cppcheck-suppress useStlAlgorithm
76 ✔
594

595
                    for(auto sourceNodeId : sourceNodeIds)
70 ✔
596
                    {
74 ✔
597
                        for(auto targetNodeId : targetNodeIds)
74 ✔
598
                        {
82 ✔
599
                            addedEdgeIds.emplace_back(addEdge(sourceNodeId, targetNodeId)); // cppcheck-suppress useStlAlgorithm
82 ✔
600
                        }
82 ✔
601
                    }
74 ✔
602

603
                    statementNodes.insert(statementNodes.end(), targetNodes.begin(), targetNodes.end());
70 ✔
604
                    sourceNodes = std::move(targetNodes);
70 ✔
605
                }
70 ✔
606

607
                for(auto edgeId : addedEdgeIds)
68 ✔
608
                {
82 ✔
609
                    for(const auto& [key, value] : defaults.back()._edge)
82 ✔
610
                        userEdgeData.setValueBy(edgeId, userEdgeData.undeclaredVectorName(key), value);
4 ✔
611

612
                    for(const auto& attribute : edge._attributeList)
82 ✔
613
                    {
12 ✔
614
                        const QString attributeName = userEdgeData.undeclaredVectorName(attribute._key);
12 ✔
615
                        userEdgeData.setValueBy(edgeId, attributeName, attribute._value);
12 ✔
616
                    }
12 ✔
617
                }
82 ✔
618

619
                return statementNodes;
68 ✔
620
            },
68 ✔
621
            [&](const NodeStatement& node)
134 ✔
622
            {
134 ✔
623
                auto nodeId = addNode(node._node._text);
48 ✔
624

625
                for(const auto& attribute : node._attributeList)
48 ✔
626
                {
24 ✔
627
                    const QString attributeName = userNodeData.undeclaredVectorName(attribute._key);
24 ✔
628
                    userNodeData.setValueBy(nodeId, attributeName, attribute._value);
24 ✔
629
                }
24 ✔
630

631
                return std::vector<QString>({node._node._text});
48 ✔
632
            },
48 ✔
633
            [](auto&&) { return std::vector<QString>(); } // Ignore everything else
134 ✔
634
        }, s);
134 ✔
635
    };
134 ✔
636

637
    processStatementList(dot._statementList);
48 ✔
638

639
    parser.setProgress(-1);
48 ✔
640
}
48 ✔
641

642
} // namespace LexyDotParser
643

644
DotFileParser::DotFileParser(IUserNodeData* userNodeData, IUserEdgeData* userEdgeData) :
645
    _userNodeData(userNodeData), _userEdgeData(userEdgeData)
58 ✔
646
{
58 ✔
647
    // Add this up front, so that it appears first in the attribute table
648
    userNodeData->add(nodeNameAttribute());
58 ✔
649
}
58 ✔
650

651
bool DotFileParser::parse(const QUrl& url, IGraphModel* graphModel)
652
{
56 ✔
653
    Q_ASSERT(graphModel != nullptr);
56 !
654
    if(graphModel == nullptr)
56 !
655
        return false;
×
656

657
    const QString localFile = url.toLocalFile();
56 ✔
658
    const QFileInfo fileInfo(localFile);
56 ✔
659

660
    if(!fileInfo.exists())
56 !
661
        return false;
×
662

663
    if(fileInfo.size() == 0)
56 ✔
664
    {
2 ✔
665
        setFailureReason(QObject::tr("File is empty."));
2 ✔
666
        return false;
2 ✔
667
    }
2 ✔
668

669
    setProgress(-1);
54 ✔
670

671
    QFile file(localFile);
54 ✔
672
    if(!file.open(QIODevice::ReadOnly))
54 !
673
        return false;
×
674

675
    auto contents = file.readAll();
54 ✔
676
    file.close();
54 ✔
677

678
    // A UTF-8 byte order mark
679
    if(contents.startsWith("\xEF\xBB\xBF"))
54 ✔
680
        contents.remove(0, 3);
2 ✔
681

682
    if(contents.isEmpty())
54 !
683
        return false;
×
684

685
    const auto size = static_cast<size_t>(contents.size());
54 ✔
686
    lexyutils::progress_input input(contents.constData(), size);
54 ✔
687

688
    input.onPositionChanged([this, size](size_t position)
54 ✔
689
    {
54 ✔
690
        setProgress(static_cast<int>((position * 100) / size));
×
691
    });
×
692

693
    auto cancelledFn = [this] { return cancelled(); };
54 ✔
694
    input.setCancelledFn(cancelledFn);
54 ✔
695

696
    setPhase(QObject::tr("Parsing"));
54 ✔
697

698
    QString errorDescription;
54 ✔
699
    auto result = lexy::parse<LexyDotParser::grammar::DotGraph>(input,
54 ✔
700
        lexyutils::describeFirstError(contents.constData(), errorDescription));
54 ✔
701

702
    if(cancelled())
54 !
703
        return false;
×
704

705
    if(!result.is_success())
54 ✔
706
    {
6 ✔
707
        setFailureReason(errorDescription);
6 ✔
708
        return false;
6 ✔
709
    }
6 ✔
710

711
    setPhase(QObject::tr("Building Graph"));
48 ✔
712
    setProgress(-1);
48 ✔
713

714
    LexyDotParser::build(*this, result.value(), *graphModel,
48 ✔
715
        *_userNodeData, *_userEdgeData);
48 ✔
716

717
    return true;
48 ✔
718
}
54 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc