• 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

79.82
/source/app/transform/graphtransformconfigparser.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 "graphtransformconfigparser.h"
21

22
#include "app/configgrammar.h"
23

24
#include "shared/utils/container.h"
25
#include "shared/utils/recursivevalue.h"
26

27
#include <lexy_utils.h>
28
#include <lexy/action/parse.hpp>
29
#include <lexy/input/string_input.hpp>
30

31
#include <QByteArray>
32
#include <QDebug>
33

34
#include <utility>
35
#include <vector>
36

37
using namespace Qt::Literals::StringLiterals;
38

39
namespace LexyGraphTransformParser
40
{
41

42
namespace dsl = lexy::dsl;
43

44
using LexyConfigGrammar::AttributeParameters;
45
using LexyConfigGrammar::Flags;
46
using LexyConfigGrammar::idPattern;
47
using LexyConfigGrammar::String;
48

49
constexpr auto equalityOps = lexy::symbol_table<ConditionFnOp::Equality>
50
    .map<LEXY_SYMBOL("==")>(ConditionFnOp::Equality::Equal)
51
    .map<LEXY_SYMBOL("!=")>(ConditionFnOp::Equality::NotEqual);
52

53
constexpr auto numericalOps = lexy::symbol_table<ConditionFnOp::Numerical>
54
    .map<LEXY_SYMBOL("<")>(ConditionFnOp::Numerical::LessThan)
55
    .map<LEXY_SYMBOL(">")>(ConditionFnOp::Numerical::GreaterThan)
56
    .map<LEXY_SYMBOL("<=")>(ConditionFnOp::Numerical::LessThanOrEqual)
57
    .map<LEXY_SYMBOL(">=")>(ConditionFnOp::Numerical::GreaterThanOrEqual);
58

59
constexpr auto stringOps = lexy::symbol_table<ConditionFnOp::String>
60
    .map<LEXY_SYMBOL("includes")>(ConditionFnOp::String::Includes)
61
    .map<LEXY_SYMBOL("excludes")>(ConditionFnOp::String::Excludes)
62
    .map<LEXY_SYMBOL("starts")>(ConditionFnOp::String::Starts)
63
    .map<LEXY_SYMBOL("ends")>(ConditionFnOp::String::Ends)
64
    .map<LEXY_SYMBOL("matches")>(ConditionFnOp::String::MatchesRegex)
65
    .map<LEXY_SYMBOL("matchesCaseInsensitive")>(ConditionFnOp::String::MatchesRegexCaseInsensitive);
66

67
constexpr auto logicalOps = lexy::symbol_table<ConditionFnOp::Logical>
68
    .map<LEXY_SYMBOL("or")>(ConditionFnOp::Logical::Or)
69
    .map<LEXY_SYMBOL("||")>(ConditionFnOp::Logical::Or)
70
    .map<LEXY_SYMBOL("and")>(ConditionFnOp::Logical::And)
71
    .map<LEXY_SYMBOL("&&")>(ConditionFnOp::Logical::And);
72

73
constexpr auto unaryOps = lexy::symbol_table<ConditionFnOp::Unary>
74
    .map<LEXY_SYMBOL("hasValue")>(ConditionFnOp::Unary::HasValue);
75

76
constexpr auto keywordUsing = LEXY_KEYWORD("using", idPattern);
77
constexpr auto keywordWith = LEXY_KEYWORD("with", idPattern);
78
constexpr auto keywordWhere = LEXY_KEYWORD("where", idPattern);
79

80
// Note the attribute name productions are token productions, so that no
81
// whitespace is skipped between the name and any of its parameters
82

83
struct AttributeNameNoDollar : lexy::token_production
84
{
85
    static constexpr auto rule = dsl::lit_c<'$'> >>
86
        (dsl::p<String> + dsl::p<AttributeParameters>);
87

88
    static constexpr auto value = LexyConfigGrammar::asAttributeName;
89
};
90

91
struct AttributeName : lexy::token_production
92
{
93
    static constexpr auto rule = dsl::p<AttributeNameNoDollar>;
94

95
    static constexpr auto value = lexy::callback<GraphTransformConfig::AttributeRef>(
96
        [](QString&& name) { return GraphTransformConfig::AttributeRef{std::move(name)}; });
3,148 ✔
97
};
98

99
struct Number
100
{
101
    static constexpr auto rule = dsl::capture(lexyutils::number);
102
    static constexpr auto value = lexyutils::asNumber<GraphTransformConfig::ParameterValue>;
103
};
104

105
struct NumberOperand
106
{
107
    static constexpr auto rule = dsl::capture(lexyutils::number);
108
    static constexpr auto value = lexyutils::asNumber<GraphTransformConfig::TerminalValue>;
109
};
110

111
struct ValueOperand
112
{
113
    static constexpr auto rule = dsl::p<AttributeName> | dsl::p<NumberOperand> | dsl::p<String>;
114
    static constexpr auto value = lexy::construct<GraphTransformConfig::TerminalValue>;
115
};
116

117
struct TerminalOp
118
{
119
    static constexpr auto rule =
120
        dsl::symbol<equalityOps> | dsl::symbol<numericalOps> | dsl::symbol<stringOps>;
121

122
    static constexpr auto value = lexy::construct<GraphTransformConfig::TerminalOp>;
123
};
124

125
struct UnaryOp
126
{
127
    static constexpr auto rule = dsl::symbol<unaryOps>;
128
    static constexpr auto value = lexy::forward<ConditionFnOp::Unary>;
129
};
130

131
struct LogicalOp
132
{
133
    static constexpr auto rule = dsl::symbol<logicalOps>;
134
    static constexpr auto value = lexy::forward<ConditionFnOp::Logical>;
135
};
136

137
// Both terminal and unary conditions start with a value operand,
138
// so they're only distinguishable by the operator that follows
139
struct TerminalOrUnaryCondition
140
{
141
    static constexpr auto rule = dsl::p<ValueOperand> >>
142
        ((dsl::p<TerminalOp> >> dsl::p<ValueOperand>) | dsl::p<UnaryOp>);
143

144
    static constexpr auto value = lexy::callback<GraphTransformConfig::Condition>(
145
        [](GraphTransformConfig::TerminalValue&& lhs,
146
           GraphTransformConfig::TerminalOp op,
147
           GraphTransformConfig::TerminalValue&& rhs)
148
        {
3,100 ✔
149
            return GraphTransformConfig::Condition(GraphTransformConfig::TerminalCondition{
3,100 ✔
150
                ._lhs = std::move(lhs), ._op = op, ._rhs = std::move(rhs)});
3,100 ✔
151
        },
3,100 ✔
152
        [](GraphTransformConfig::TerminalValue&& lhs, ConditionFnOp::Unary op)
153
        {
34 ✔
154
            return GraphTransformConfig::Condition(
34 ✔
155
                GraphTransformConfig::UnaryCondition{._lhs = std::move(lhs), ._op = op});
34 ✔
156
        });
34 ✔
157
};
158

159
struct Condition;
160

161
struct Operand
162
{
163
    static constexpr auto rule =
164
        (dsl::lit_c<'('> >> (dsl::recurse<Condition> + dsl::lit_c<')'>)) |
165
        dsl::p<TerminalOrUnaryCondition>;
166

167
    static constexpr auto value = lexy::forward<GraphTransformConfig::Condition>;
168
};
169

170
struct Condition
171
{
172
    static constexpr auto rule = dsl::p<Operand> >>
173
        dsl::opt(dsl::p<LogicalOp> >> dsl::p<Operand>);
174

175
    static constexpr auto value = lexy::callback<GraphTransformConfig::Condition>(
176
        [](GraphTransformConfig::Condition&& condition, lexy::nullopt)
177
        {
2,994 ✔
178
            return std::move(condition);
2,994 ✔
179
        },
2,994 ✔
180
        [](GraphTransformConfig::Condition&& lhs, ConditionFnOp::Logical op,
181
            GraphTransformConfig::Condition&& rhs)
182
        {
84 ✔
183
            return GraphTransformConfig::Condition(
84 ✔
184
                RecursiveValue<GraphTransformConfig::CompoundCondition>(
84 ✔
185
                GraphTransformConfig::CompoundCondition{
84 ✔
186
                ._lhs = std::move(lhs), ._op = op, ._rhs = std::move(rhs)}));
84 ✔
187
        });
84 ✔
188
};
189

190
struct Parameter
191
{
192
    // A parameter is only committed to once its = has been seen, otherwise a
193
    // trailing where clause would be mistaken for the start of another parameter
194
    static constexpr auto rule =
195
        dsl::peek(dsl::p<String> + dsl::while_(dsl::ascii::space) + dsl::lit_c<'='>) >>
196
        (dsl::p<String> + dsl::lit_c<'='> + (dsl::p<Number> | dsl::p<String>));
197

198
    static constexpr auto value = lexy::construct<GraphTransformConfig::Parameter>;
199
};
200

201
struct Attributes
202
{
203
    static constexpr auto rule =
204
        dsl::opt(keywordUsing >> dsl::list(dsl::p<AttributeNameNoDollar>));
205

206
    static constexpr auto value = lexy::as_list<std::vector<QString>>;
207
};
208

209
struct Parameters
210
{
211
    static constexpr auto rule = dsl::opt(keywordWith >> dsl::list(dsl::p<Parameter>));
212
    static constexpr auto value = lexy::as_list<std::vector<GraphTransformConfig::Parameter>>;
213
};
214

215
struct OptionalCondition
216
{
217
    static constexpr auto rule = dsl::opt(keywordWhere >> dsl::p<Condition>);
218

219
    static constexpr auto value = lexy::bind(
220
        lexy::forward<GraphTransformConfig::Condition>,
221
        lexy::_1 || GraphTransformConfig::NoCondition{});
222
};
223

224
struct Transform
225
{
226
    static constexpr auto whitespace = dsl::ascii::space;
227

228
    static constexpr auto rule = dsl::p<Flags> + dsl::p<String> + dsl::p<Attributes> +
229
        dsl::p<Parameters> + dsl::p<OptionalCondition> + dsl::eof;
230

231
    static constexpr auto value = lexy::construct<GraphTransformConfig>;
232
};
233

234
template<typename Table>
235
static void appendOps(QStringList& list, const Table& table)
236
{
240 ✔
237
    for(const auto& op : table)
240 ✔
238
        list.append(QString::fromUtf8(op.symbol));
828 ✔
239
}
240 ✔
240

241
template<typename Table, typename Op>
242
static QString opAsString(const Table& table, Op op)
243
{
1,116 ✔
244
    for(const auto& entry : table)
1,116 !
245
    {
2,116 ✔
246
        // cppcheck-suppress useStlAlgorithm
247
        if(entry.value == op)
2,116 !
248
            return QString::fromUtf8(entry.symbol);
1,116 ✔
249
    }
2,116 ✔
250

251
    return {};
×
252
}
1,116 ✔
253

254
template<typename Table>
255
static const typename Table::mapped_type* findOp(const Table& table, const QString& s)
256
{
220 ✔
257
    const auto bytes = s.toUtf8();
220 ✔
258
    const auto input = lexy::string_input<lexy::default_encoding>(
220 ✔
259
        bytes.constData(), bytes.constData() + bytes.size());
220 ✔
260

261
    const auto index = table.parse(input);
220 ✔
262

263
    if(!index)
220 ✔
264
        return nullptr;
140 ✔
265

266
    return &table[index];
80 ✔
267
}
220 ✔
268
} // namespace LexyGraphTransformParser
269

270
bool GraphTransformConfigParser::parse(const QString& text, NamedBool<"warnOnFailure"> warnOnFailure)
271
{
3,822 ✔
272
    const auto bytes = text.toUtf8();
3,822 ✔
273
    const auto* const begin = bytes.constData();
3,822 ✔
274
    const auto* const end = begin + bytes.size();
3,822 ✔
275

276
    const auto input = lexy::string_input<lexy::default_encoding>(begin, end);
3,822 ✔
277

278
    _result = {};
3,822 ✔
279
    _failedInput.clear();
3,822 ✔
280

281
    const char* failurePosition = nullptr;
3,822 ✔
282
    const auto onError = lexy::callback<void>([&failurePosition](const auto&, const auto& error)
3,822 ✔
283
    {
3,822 ✔
284
        // Only the first (i.e. earliest) failure is of interest
285
        if(failurePosition == nullptr)
20 !
286
            failurePosition = error.position();
20 ✔
287
    });
20 ✔
288

289
    auto result = lexy::parse<LexyGraphTransformParser::Transform>(input, onError);
3,822 ✔
290
    _success = result.is_success();
3,822 ✔
291

292
    if(_success)
3,822 ✔
293
        _result = result.value();
3,802 ✔
294
    else
20 ✔
295
    {
20 ✔
296
        if(failurePosition == nullptr)
20 !
297
            failurePosition = begin;
×
298

299
        _failedInput = QString::fromUtf8(failurePosition,
20 ✔
300
            static_cast<qsizetype>(end - failurePosition));
20 ✔
301

302
        if(warnOnFailure)
20 ✔
303
            qWarning() << "Failed to parse" << _failedInput;
20 ✔
304
    }
20 ✔
305

306
    return _success;
3,822 ✔
307
}
3,822 ✔
308

309
QStringList GraphTransformConfigParser::ops(ValueType valueType)
310
{
120 ✔
311
    QStringList list;
120 ✔
312

313
    LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::equalityOps);
120 ✔
314

315
    switch(valueType)
120 ✔
316
    {
120 ✔
317
    case ValueType::Float:
×
318
    case ValueType::Int:
66 ✔
319
        LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::numericalOps);
66 ✔
320
        break;
66 ✔
321

322
    case ValueType::String:
54 ✔
323
        LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::stringOps);
54 ✔
324
        break;
54 ✔
325

326
    default: break;
×
327
    }
120 ✔
328

329
    return list;
120 ✔
330
}
120 ✔
331

332
QString GraphTransformConfigParser::opToString(ConditionFnOp::Equality op)
333
{
142 ✔
334
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::equalityOps, op);
142 ✔
335
}
142 ✔
336

337
QString GraphTransformConfigParser::opToString(ConditionFnOp::Numerical op)
338
{
814 ✔
339
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::numericalOps, op);
814 ✔
340
}
814 ✔
341

342
QString GraphTransformConfigParser::opToString(ConditionFnOp::String op)
343
{
48 ✔
344
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::stringOps, op);
48 ✔
345
}
48 ✔
346

347
QString GraphTransformConfigParser::opToString(ConditionFnOp::Logical op)
348
{
48 ✔
349
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::logicalOps, op);
48 ✔
350
}
48 ✔
351

352
QString GraphTransformConfigParser::opToString(ConditionFnOp::Unary op)
353
{
64 ✔
354
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::unaryOps, op);
64 ✔
355
}
64 ✔
356

357
GraphTransformConfig::TerminalOp GraphTransformConfigParser::stringToOp(const QString& s)
358
{
×
359
    const auto* equalityOp = LexyGraphTransformParser::findOp(
×
360
        LexyGraphTransformParser::equalityOps, s);
×
361
    if(equalityOp != nullptr)
×
362
        return *equalityOp;
×
363

364
    const auto* numericalOp = LexyGraphTransformParser::findOp(
×
365
        LexyGraphTransformParser::numericalOps, s);
×
366
    if(numericalOp != nullptr)
×
367
        return *numericalOp;
×
368

369
    const auto* stringOp = LexyGraphTransformParser::findOp(
×
370
        LexyGraphTransformParser::stringOps, s);
×
371
    if(stringOp != nullptr)
×
372
        return *stringOp;
×
373

374
    return {};
×
375
}
×
376

377
bool GraphTransformConfigParser::opIsUnary(const QString& op)
378
{
×
379
    return LexyGraphTransformParser::findOp(LexyGraphTransformParser::unaryOps, op) != nullptr;
×
380
}
×
381

382
TransformDisplayText::Op GraphTransformConfigParser::opDisplayText(const QString& op)
383
{
82 ✔
384
    if(const auto* equalityOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::equalityOps, op))
82 ✔
385
        return TransformDisplayText::op(*equalityOp);
16 ✔
386

387
    if(const auto* numericalOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::numericalOps, op))
66 ✔
388
        return TransformDisplayText::op(*numericalOp);
24 ✔
389

390
    if(const auto* stringOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::stringOps, op))
42 ✔
391
        return TransformDisplayText::op(*stringOp);
18 ✔
392

393
    if(const auto* logicalOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::logicalOps, op))
24 ✔
394
        return TransformDisplayText::op(*logicalOp);
18 ✔
395

396
    if(const auto* unaryOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::unaryOps, op))
6 ✔
397
        return TransformDisplayText::op(*unaryOp);
4 ✔
398

399
    return {._name = op, ._phrase = u"%1 "_s + op + u" %2"_s};
2 ✔
400
}
6 ✔
401

402
QString GraphTransformConfigParser::parseForDisplay(const QString& text, const GraphTransformFactories& factories,
403
    const GraphTransformConfig::AttributeDisplayNameFn& attributeDisplayName,
404
    const GraphTransformConfig::AttributeDisplayValueFn& attributeDisplayValue)
405
{
30 ✔
406
    GraphTransformConfigParser parser;
30 ✔
407

408
    if(parser.parse(text))
30 ✔
409
    {
28 ✔
410
        const auto& result = parser.result();
28 ✔
411
        const auto* factory = u::contains(factories, result._action) ?
28 ✔
412
            factories.at(result._action).get() : nullptr;
26 ✔
413

414
        return result.asString("forDisplay"_yes, factory, attributeDisplayName, attributeDisplayValue);
28 ✔
415
    }
28 ✔
416

417
    return {};
2 ✔
418
}
30 ✔
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