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 along
17 * with this program. If not, see <http://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 private:
35 std::variant<T, std::string> m_data;
36
37 public:
38 Result( T aValue ) : m_data( std::move( aValue ) ) {}
39 Result( std::string aError ) : m_data( std::move( aError ) ) {}
40
41 auto HasValue() const -> bool { return std::holds_alternative<T>( m_data ); }
42 auto HasError() const -> bool { return std::holds_alternative<std::string>( m_data ); }
43
44 auto GetValue() const -> const T& { return std::get<T>( m_data ); }
45 auto GetError() const -> const std::string& { return std::get<std::string>( m_data ); }
46
47 explicit operator bool() const { return HasValue(); }
48 };
49
50 template<typename T>
51 auto MakeError( std::string aMsg ) -> Result<T>
52 {
53 return Result<T>( std::move( aMsg ) );
54 }
55
56 template<typename T>
57 auto MakeValue( T aVal ) -> Result<T>
58 {
59 return Result<T>( std::move( aVal ) );
60 }
61
63 {
64 private:
65 std::vector<std::string> m_errors;
66 std::vector<std::string> m_warnings;
67
68 public:
69 auto AddError( std::string aError ) -> void
70 {
71 m_errors.emplace_back( std::move( aError ) );
72 }
73
74 auto AddWarning( std::string aWarning ) -> void
75 {
76 m_warnings.emplace_back( std::move( aWarning ) );
77 }
78
79 auto AddSyntaxError( int aLine = -1, int aColumn = -1 ) -> void
80 {
81 if( aLine >= 0 && aColumn >= 0 )
82 AddError( fmt::format( "Syntax error at line {}, column {}", aLine, aColumn ) );
83 else
84 AddError( "Syntax error in calculation expression" );
85 }
86
87 auto AddParseFailure() -> void
88 {
89 AddError( "Parser failed to parse input" );
90 }
91
92 auto HasErrors() const -> bool { return !m_errors.empty(); }
93 auto HasWarnings() const -> bool { return !m_warnings.empty(); }
94 auto GetErrors() const -> const std::vector<std::string>& { return m_errors; }
95 auto GetWarnings() const -> const std::vector<std::string>& { return m_warnings; }
96
97 auto GetAllMessages() const -> std::string
98 {
99 std::string result;
100 for( const auto& error : m_errors )
101 result += fmt::format( "Error: {}\n", error );
102
103 for( const auto& warning : m_warnings )
104 result += fmt::format( "Warning: {}\n", warning );
105
106 return result;
107 }
108
109 auto Clear() -> void
110 {
111 m_errors.clear();
112 m_warnings.clear();
113 }
114 };
115
116 // Forward declarations for parser-related types
117 class DOC;
118 class PARSE_CONTEXT;
119 class DOC_PROCESSOR;
120}
std::vector< std::string > m_warnings
auto GetErrors() const -> const std::vector< std::string > &
auto AddError(std::string aError) -> void
auto GetWarnings() const -> const std::vector< std::string > &
auto GetAllMessages() const -> std::string
auto HasWarnings() const -> bool
auto AddWarning(std::string aWarning) -> void
auto AddSyntaxError(int aLine=-1, int aColumn=-1) -> void
auto HasErrors() const -> bool
std::vector< std::string > m_errors
auto GetError() const -> const std::string &
auto HasError() const -> bool
std::variant< T, std::string > m_data
Result(std::string aError)
auto HasValue() const -> bool
auto GetValue() const -> const T &
auto MakeValue(T aVal) -> Result< T >
std::variant< double, std::string > Value
auto MakeError(std::string aMsg) -> Result< T >
STL namespace.
wxString result
Test unit parsing edge cases and error handling.