29#include "neml2/misc/string_utils.h"
30#include "neml2/misc/errors.h"
31#include "neml2/misc/types.h"
36class LabeledAxisAccessor;
39class MultiEnumSelection;
58 static const std::vector<std::
string>
sections;
69 const std::
string & additional_input = "") const = 0;
78 if constexpr (std::is_same_v<T, bool>)
79 return "Failed to parse '" + raw_str +
80 "' as a boolean. Only 'true' and 'false' (case-sensitive) are recognized.";
82 if constexpr (std::is_same_v<T, TensorShape>)
83 return "Failed to parse '" + raw_str +
84 "' as a tensor shape. Tensor shapes must be of the form "
85 "'(d1,d2,...,dn)': It must begin with '(' and end with ')', d1, d2, "
86 "... must be integers, and there must be no white spaces.";
93parse_(T & val,
const std::string & raw_str)
95 std::stringstream ss(
trim(raw_str));
105 auto success =
parse_(val, raw_str);
115 auto tokens =
split(raw_str,
" \t\n\v\f\r");
116 if constexpr (std::is_same_v<T, Device>)
117 vals.resize(tokens.size(),
kCPU);
119 vals.resize(tokens.size());
120 for (
size_t i = 0; i < tokens.size(); i++)
122 auto success = parse_<T>(vals[i], tokens[i]);
136 throw ParserException(
"Failed to parse '" + raw_str +
"' as a vector of " +
145 auto token_vecs =
split(raw_str,
";");
146 vals.resize(token_vecs.size());
147 for (
size_t i = 0; i < token_vecs.size(); i++)
149 auto success = parse_vector_<T>(vals[i], token_vecs[i]);
157std::vector<std::vector<T>>
160 std::vector<std::vector<T>> vals;
163 throw ParserException(
"Failed to parse '" + raw_str +
"' as a vector of vector of " +
170bool parse_<bool>(
bool &,
const std::string & raw_str);
173bool parse_vector_<bool>(std::vector<bool> &,
const std::string & raw_str);
175bool parse_<TensorShape>(
TensorShape &,
const std::string & raw_str);
179Device parse<Device>(
const std::string & raw_str);
181bool parse_<Device>(
Device &,
const std::string & raw_str);
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
A parser is responsible for parsing an input file into a collection of options which can be used by t...
Definition Parser.h:47
static const std::vector< std::string > sections
Known top-level sections in the input file.
Definition Parser.h:58
virtual InputFile parse(const std::filesystem::path &filename, const std::string &additional_input="") const =0
Deserialize a file.
Parser(const Parser &)=default
Parser(Parser &&) noexcept=default
std::string trim(const std::string &str, const std::string &white_space)
Definition string_utils.cxx:73
std::string parse_failure_message(const std::string &raw_str)
Definition Parser.h:76
bool parse_vector_vector_(std::vector< std::vector< T > > &vals, const std::string &raw_str)
Definition Parser.h:143
bool parse_vector_(std::vector< bool > &vals, const std::string &raw_str)
Definition Parser.cxx:63
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition string_utils.cxx:32
std::vector< T > parse_vector(const std::string &raw_str)
Definition Parser.h:131
std::vector< std::vector< T > > parse_vector_vector(const std::string &raw_str)
Definition Parser.h:158
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition string_utils.cxx:52
bool parse_(bool &val, const std::string &raw_str)
Definition Parser.cxx:39
Definition DiagnosticsInterface.cxx:30
c10::Device Device
Definition types.h:63
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:66
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
LabeledAxisAccessor VariableName
Definition LabeledAxisAccessor.h:185
constexpr auto kCPU
Definition types.h:53