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
27namespace 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>
58 {
59 return RESULT<T>( std::move( aVal ) );
60 }
61
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
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}
void AddWarning(std::string aWarning)
std::string GetAllMessages() const
const std::vector< std::string > & GetErrors() const
std::vector< std::string > m_warnings
void AddSyntaxError(int aLine=-1, int aColumn=-1)
const std::vector< std::string > & GetWarnings() const
std::vector< std::string > m_errors
void AddError(std::string aError)
const std::string & GetError() const
const T & GetValue() const
std::variant< T, std::string > m_data
RESULT(std::string aError)
RESULT< T > MakeError(std::string aMsg)
std::variant< double, std::string > VALUE
RESULT< T > MakeValue(T aVal)
STL namespace.
wxString result
Test unit parsing edge cases and error handling.