21#include <fmt/format.h>
22#include <pegtl/contrib/parse_tree.hpp>
28#include <boost/locale/boundary/types.hpp>
48template <
typename Rule>
86 template <
typename ActionInput>
98 template <
typename ActionInput>
102 wxCHECK2( in.string().size() >= 2,
return );
103 std::string inner = in.string().substr( 1, in.string().size() - 2 );
104 std::string unescaped;
105 unescaped.reserve( inner.size() );
107 for(
size_t ii = 0; ii < inner.size(); ++ii )
109 if( inner[ii] !=
'\\' || ii + 1 == inner.size() )
111 unescaped += inner[ii];
115 switch( inner[++ii] )
117 case 'n': unescaped +=
'\n';
break;
118 case 'r': unescaped +=
'\r';
break;
119 case '\\': unescaped +=
'\\';
break;
120 case '"': unescaped +=
'"';
break;
121 default: unescaped +=
'\\'; unescaped += inner[ii];
break;
131#define DEFINE_STRING_ACTION( Rule, StateVariable ) \
133struct LIBRARY_TABLE_PARSER_ACTION<Rule> \
135 static void apply0( LIBRARY_TABLE_PARSER_STATE& s ) \
137 s.target_string = &s.StateVariable; \
149#define DEFINE_FLAG_ACTION( Rule, StateVariable ) \
151struct LIBRARY_TABLE_PARSER_ACTION<Rule> \
153 static void apply0( LIBRARY_TABLE_PARSER_STATE& s ) \
155 s.StateVariable = true; \
180 const std::filesystem::path& aPath )
184 file_input in( aPath );
187 wxLogTrace(
traceLibraries,
"LIBRARY_TABLE_PARSER::Parse %s", aPath.string().c_str() );
191 if( !parse<LIB_TABLE_FILE, LIBRARY_TABLE_PARSER_ACTION>( in, state ) )
194 wxString msg = wxString::Format(
_(
"An unexpected error occurred while reading library table %s "),
195 aPath.string().c_str() );
199 catch(
const parse_error& e )
201 const auto& p = e.positions().front();
202 std::string msg = fmt::format(
"Error at line {}, column {}:\n{}\n{:>{}}\n{}",
203 p.line, p.column, in.line_at( p ),
"^", p.column,
209 .description = wxString::Format(
_(
"Syntax error at line %zu, column %zu" ),
218 catch( std::filesystem::filesystem_error& e )
220 wxLogTrace(
traceLibraries,
"LIBRARY_TABLE_PARSER::Parse loading '%s' error: %s", aPath.string(), e.what() );
223 .description = e.what()
231 memory_input in( aBuffer,
"" );
233 wxLogTrace(
traceLibraries,
"LIBRARY_TABLE_PARSER::Parse from string buffer" );
237 if( !parse<LIB_TABLE_FILE, LIBRARY_TABLE_PARSER_ACTION>( in, state ) )
242 .description =
_(
"An unexpected error occurred while reading library table" )
246 catch(
const parse_error& e )
248 const auto& p = e.positions().front();
249 std::string msg = fmt::format(
"Error at line {}, column {}:\n{}\n{:>{}}\n{}",
250 p.line, p.column, in.line_at( p ),
"^", p.column,
256 .description = wxString::Format(
_(
"Syntax error at line %zu, column %zu" ),
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > ParseBuffer(const std::string &aBuffer)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
#define DEFINE_FLAG_ACTION(Rule, StateVariable)
#define DEFINE_STRING_ACTION(Rule, StateVariable)
The intermediate representation that a library table is parsed into.
std::vector< LIBRARY_TABLE_ROW_IR > rows
static void apply0(LIBRARY_TABLE_PARSER_STATE &s)
static void apply0(LIBRARY_TABLE_PARSER_STATE &s)
static void apply0(LIBRARY_TABLE_PARSER_STATE &s)
static void apply0(LIBRARY_TABLE_PARSER_STATE &s)
static void apply(const ActionInput &in, LIBRARY_TABLE_PARSER_STATE &s)
static void apply(const ActionInput &in, LIBRARY_TABLE_PARSER_STATE &s)
std::string * target_string
LIBRARY_TABLE_ROW_IR current_row_model
LIBRARY_TABLE_PARSER_STATE()
wxLogTrace helper definitions.