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

graphia-app / graphia / 42502562509

08 Nov 2026 04:57PM UTC coverage: 71.457% (-0.05%) from 71.51%
42502562509

push

github

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

11725 of 17974 branches covered (65.23%)

Branch coverage included in aggregate %.

29934 of 40325 relevant lines covered (74.23%)

375397.35 hits per line

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

79.9
/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 <algorithm>
35
#include <iterator>
36
#include <utility>
37
#include <vector>
38

39
using namespace Qt::Literals::StringLiterals;
40

41
namespace LexyGraphTransformParser
42
{
43

44
namespace dsl = lexy::dsl;
45

46
using LexyConfigGrammar::AttributeParameters;
47
using LexyConfigGrammar::Flags;
48
using LexyConfigGrammar::idPattern;
49
using LexyConfigGrammar::String;
50

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

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

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

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

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

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

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

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

90
    static constexpr auto value = LexyConfigGrammar::asAttributeName;
91
};
92

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

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

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

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

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

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

124
    static constexpr auto value = lexy::construct<GraphTransformConfig::TerminalOp>;
125
};
126

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

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

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

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

161
struct Condition;
162

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

169
    static constexpr auto value = lexy::forward<GraphTransformConfig::Condition>;
170
};
171

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

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

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

200
    static constexpr auto value = lexy::construct<GraphTransformConfig::Parameter>;
201
};
202

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

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

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

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

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

226
struct Transform
227
{
228
    static constexpr auto whitespace = dsl::ascii::space;
229

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

233
    static constexpr auto value = lexy::construct<GraphTransformConfig>;
234
};
235

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

243
template<typename Table, typename Op>
244
static QString opAsString(const Table& table, Op op)
245
{
1,116 ✔
246
    auto entry = std::ranges::find_if(table, [op](const auto& candidate) { return candidate.value == op; });
2,116 ✔
247
    return entry != std::ranges::end(table) ? QString::fromUtf8(entry->symbol) : QString{};
1,116 !
248
}
1,116 ✔
249

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

257
    const auto index = table.parse(input);
220 ✔
258

259
    if(!index)
220 ✔
260
        return nullptr;
140 ✔
261

262
    return &table[index];
80 ✔
263
}
220 ✔
264
} // namespace LexyGraphTransformParser
265

266
bool GraphTransformConfigParser::parse(const QString& text, NamedBool<"warnOnFailure"> warnOnFailure)
267
{
3,854 ✔
268
    const auto bytes = text.toUtf8();
3,854 ✔
269
    const auto* const begin = bytes.constData();
3,854 ✔
270
    const auto* const end = begin + bytes.size();
3,854 ✔
271

272
    const auto input = lexy::string_input<lexy::default_encoding>(begin, end);
3,854 ✔
273

274
    _result = {};
3,854 ✔
275
    _failedInput.clear();
3,854 ✔
276

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

285
    auto result = lexy::parse<LexyGraphTransformParser::Transform>(input, onError);
3,854 ✔
286
    _success = result.is_success();
3,854 ✔
287

288
    if(_success)
3,854 ✔
289
        _result = result.value();
3,834 ✔
290
    else
20 ✔
291
    {
20 ✔
292
        if(failurePosition == nullptr)
20 !
293
            failurePosition = begin;
×
294

295
        _failedInput = QString::fromUtf8(failurePosition,
20 ✔
296
            static_cast<qsizetype>(end - failurePosition));
20 ✔
297

298
        if(warnOnFailure)
20 ✔
299
            qWarning() << "Failed to parse" << _failedInput;
20 ✔
300
    }
20 ✔
301

302
    return _success;
3,854 ✔
303
}
3,854 ✔
304

305
QStringList GraphTransformConfigParser::ops(ValueType valueType)
306
{
120 ✔
307
    QStringList list;
120 ✔
308

309
    LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::equalityOps);
120 ✔
310

311
    switch(valueType)
120 ✔
312
    {
120 ✔
313
    case ValueType::Float:
×
314
    case ValueType::Int:
66 ✔
315
        LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::numericalOps);
66 ✔
316
        break;
66 ✔
317

318
    case ValueType::String:
54 ✔
319
        LexyGraphTransformParser::appendOps(list, LexyGraphTransformParser::stringOps);
54 ✔
320
        break;
54 ✔
321

322
    default: break;
×
323
    }
120 ✔
324

325
    return list;
120 ✔
326
}
120 ✔
327

328
QString GraphTransformConfigParser::opToString(ConditionFnOp::Equality op)
329
{
142 ✔
330
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::equalityOps, op);
142 ✔
331
}
142 ✔
332

333
QString GraphTransformConfigParser::opToString(ConditionFnOp::Numerical op)
334
{
814 ✔
335
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::numericalOps, op);
814 ✔
336
}
814 ✔
337

338
QString GraphTransformConfigParser::opToString(ConditionFnOp::String op)
339
{
48 ✔
340
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::stringOps, op);
48 ✔
341
}
48 ✔
342

343
QString GraphTransformConfigParser::opToString(ConditionFnOp::Logical op)
344
{
48 ✔
345
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::logicalOps, op);
48 ✔
346
}
48 ✔
347

348
QString GraphTransformConfigParser::opToString(ConditionFnOp::Unary op)
349
{
64 ✔
350
    return LexyGraphTransformParser::opAsString(LexyGraphTransformParser::unaryOps, op);
64 ✔
351
}
64 ✔
352

353
GraphTransformConfig::TerminalOp GraphTransformConfigParser::stringToOp(const QString& s)
354
{
×
355
    const auto* equalityOp = LexyGraphTransformParser::findOp(
×
356
        LexyGraphTransformParser::equalityOps, s);
×
357
    if(equalityOp != nullptr)
×
358
        return *equalityOp;
×
359

360
    const auto* numericalOp = LexyGraphTransformParser::findOp(
×
361
        LexyGraphTransformParser::numericalOps, s);
×
362
    if(numericalOp != nullptr)
×
363
        return *numericalOp;
×
364

365
    const auto* stringOp = LexyGraphTransformParser::findOp(
×
366
        LexyGraphTransformParser::stringOps, s);
×
367
    if(stringOp != nullptr)
×
368
        return *stringOp;
×
369

370
    return {};
×
371
}
×
372

373
bool GraphTransformConfigParser::opIsUnary(const QString& op)
374
{
×
375
    return LexyGraphTransformParser::findOp(LexyGraphTransformParser::unaryOps, op) != nullptr;
×
376
}
×
377

378
TransformDisplayText::Op GraphTransformConfigParser::opDisplayText(const QString& op)
379
{
82 ✔
380
    if(const auto* equalityOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::equalityOps, op))
82 ✔
381
        return TransformDisplayText::op(*equalityOp);
16 ✔
382

383
    if(const auto* numericalOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::numericalOps, op))
66 ✔
384
        return TransformDisplayText::op(*numericalOp);
24 ✔
385

386
    if(const auto* stringOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::stringOps, op))
42 ✔
387
        return TransformDisplayText::op(*stringOp);
18 ✔
388

389
    if(const auto* logicalOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::logicalOps, op))
24 ✔
390
        return TransformDisplayText::op(*logicalOp);
18 ✔
391

392
    if(const auto* unaryOp = LexyGraphTransformParser::findOp(LexyGraphTransformParser::unaryOps, op))
6 ✔
393
        return TransformDisplayText::op(*unaryOp);
4 ✔
394

395
    return {._name = op, ._phrase = u"%1 "_s + op + u" %2"_s};
2 ✔
396
}
6 ✔
397

398
QString GraphTransformConfigParser::parseForDisplay(const QString& text, const GraphTransformFactories& factories,
399
    const GraphTransformConfig::AttributeDisplayNameFn& attributeDisplayName,
400
    const GraphTransformConfig::AttributeDisplayValueFn& attributeDisplayValue)
401
{
30 ✔
402
    GraphTransformConfigParser parser;
30 ✔
403

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

410
        return result.asString("forDisplay"_yes, factory, attributeDisplayName, attributeDisplayValue);
28 ✔
411
    }
28 ✔
412

413
    return {};
2 ✔
414
}
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