KiCad PCB EDA Suite
Loading...
Searching...
No Matches
text_eval_types.h
Go to the documentation of this file.
1
/*
2
* This program source code file is part of KiCad, a free EDA CAD application.
3
*
4
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5
*
6
* This program is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the
8
* Free Software Foundation, either version 3 of the License, or (at your
9
* option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18
*/
19
20
#pragma once
21
22
#include <string>
23
#include <variant>
24
#include <vector>
25
#include <fmt/format.h>
26
27
namespace
calc_parser
28
{
29
using
VALUE
= std::variant<double, std::string>;
30
31
template
<
typename
T>
32
class
RESULT
33
{
34
public
:
35
RESULT
(
T
aValue ) :
m_data
(
std
::
move
( aValue ) ) {}
36
RESULT
( std::string aError ) :
m_data
(
std
::
move
( aError ) ) {}
37
38
bool
HasValue
()
const
{
return
std::holds_alternative<T>(
m_data
); }
39
bool
HasError
()
const
{
return
std::holds_alternative<std::string>(
m_data
); }
40
41
const
T
&
GetValue
()
const
{
return
std::get<T>(
m_data
); }
42
const
std::string&
GetError
()
const
{
return
std::get<std::string>(
m_data
); }
43
44
explicit
operator
bool()
const
{
return
HasValue
(); }
45
46
private
:
47
std::variant<T, std::string>
m_data
;
48
};
49
50
template
<
typename
T>
51
RESULT<T>
MakeError
( std::string aMsg )
52
{
53
return
RESULT<T>
( std::move( aMsg ) );
54
}
55
56
template
<
typename
T>
57
RESULT<T>
MakeValue
(
T
aVal )
58
{
59
return
RESULT<T>
( std::move( aVal ) );
60
}
61
62
class
ERROR_COLLECTOR
63
{
64
public
:
65
void
AddError
( std::string aError )
66
{
67
m_errors
.emplace_back( std::move( aError ) );
68
}
69
70
void
AddWarning
( std::string aWarning )
71
{
72
m_warnings
.emplace_back( std::move( aWarning ) );
73
}
74
75
void
AddSyntaxError
(
int
aLine = -1,
int
aColumn = -1 )
76
{
77
if
( aLine >= 0 && aColumn >= 0 )
78
AddError
( fmt::format(
"Syntax error at line {}, column {}"
, aLine, aColumn ) );
79
else
80
AddError
(
"Syntax error in calculation expression"
);
81
}
82
83
void
AddParseFailure
()
84
{
85
AddError
(
"Parser failed to parse input"
);
86
}
87
88
bool
HasErrors
()
const
{
return
!
m_errors
.empty(); }
89
bool
HasWarnings
()
const
{
return
!
m_warnings
.empty(); }
90
const
std::vector<std::string>&
GetErrors
()
const
{
return
m_errors
; }
91
const
std::vector<std::string>&
GetWarnings
()
const
{
return
m_warnings
; }
92
93
std::string
GetAllMessages
()
const
94
{
95
std::string
result
;
96
97
for
(
const
std::string& error :
m_errors
)
98
result
+= fmt::format(
"Error: {}\n"
, error );
99
100
for
(
const
std::string& warning :
m_warnings
)
101
result
+= fmt::format(
"Warning: {}\n"
, warning );
102
103
return
result
;
104
}
105
106
void
Clear
()
107
{
108
m_errors
.clear();
109
m_warnings
.clear();
110
}
111
112
private
:
113
std::vector<std::string>
m_errors
;
114
std::vector<std::string>
m_warnings
;
115
};
116
117
// Forward declarations for parser-related types
118
class
DOC
;
119
class
PARSE_CONTEXT
;
120
class
DOC_PROCESSOR
;
121
}
BITMAPS::move
@ move
Definition
bitmaps_list.h:382
calc_parser::DOC_PROCESSOR
Definition
text_eval_parser.h:455
calc_parser::DOC
Definition
text_eval_parser.h:375
calc_parser::ERROR_COLLECTOR
Definition
text_eval_types.h:63
calc_parser::ERROR_COLLECTOR::AddWarning
void AddWarning(std::string aWarning)
Definition
text_eval_types.h:70
calc_parser::ERROR_COLLECTOR::HasErrors
bool HasErrors() const
Definition
text_eval_types.h:88
calc_parser::ERROR_COLLECTOR::GetAllMessages
std::string GetAllMessages() const
Definition
text_eval_types.h:93
calc_parser::ERROR_COLLECTOR::GetErrors
const std::vector< std::string > & GetErrors() const
Definition
text_eval_types.h:90
calc_parser::ERROR_COLLECTOR::m_warnings
std::vector< std::string > m_warnings
Definition
text_eval_types.h:114
calc_parser::ERROR_COLLECTOR::AddParseFailure
void AddParseFailure()
Definition
text_eval_types.h:83
calc_parser::ERROR_COLLECTOR::Clear
void Clear()
Definition
text_eval_types.h:106
calc_parser::ERROR_COLLECTOR::AddSyntaxError
void AddSyntaxError(int aLine=-1, int aColumn=-1)
Definition
text_eval_types.h:75
calc_parser::ERROR_COLLECTOR::HasWarnings
bool HasWarnings() const
Definition
text_eval_types.h:89
calc_parser::ERROR_COLLECTOR::GetWarnings
const std::vector< std::string > & GetWarnings() const
Definition
text_eval_types.h:91
calc_parser::ERROR_COLLECTOR::m_errors
std::vector< std::string > m_errors
Definition
text_eval_types.h:113
calc_parser::ERROR_COLLECTOR::AddError
void AddError(std::string aError)
Definition
text_eval_types.h:65
calc_parser::PARSE_CONTEXT
Definition
text_eval_parser.h:404
calc_parser::RESULT
Definition
text_eval_types.h:33
calc_parser::RESULT::GetError
const std::string & GetError() const
Definition
text_eval_types.h:42
calc_parser::RESULT::GetValue
const T & GetValue() const
Definition
text_eval_types.h:41
calc_parser::RESULT::HasError
bool HasError() const
Definition
text_eval_types.h:39
calc_parser::RESULT::m_data
std::variant< T, std::string > m_data
Definition
text_eval_types.h:47
calc_parser::RESULT::RESULT
RESULT(T aValue)
Definition
text_eval_types.h:35
calc_parser::RESULT::RESULT
RESULT(std::string aError)
Definition
text_eval_types.h:36
calc_parser::RESULT::HasValue
bool HasValue() const
Definition
text_eval_types.h:38
calc_parser
Definition
text_eval_parser.cpp:31
calc_parser::MakeError
RESULT< T > MakeError(std::string aMsg)
Definition
text_eval_types.h:51
calc_parser::VALUE
std::variant< double, std::string > VALUE
Definition
text_eval_parser.h:50
calc_parser::MakeValue
RESULT< T > MakeValue(T aVal)
Definition
text_eval_types.h:57
std
STL namespace.
result
wxString result
Test unit parsing edge cases and error handling.
Definition
test_text_eval_numeric_compat.cpp:600
TRANSLINE_PARAMETERS::T
@ T
Definition
transline_calculation_base.h:39
src
include
text_eval
text_eval_types.h
Generated on Wed Oct 7 2026 00:05:59 for KiCad PCB EDA Suite by
1.13.2