NEML2 1.4.0
Loading...
Searching...
No Matches
parser_utils.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/misc/utils.h"
28
29namespace neml2
30{
31// Forward decl
32class LabeledAxisAccessor;
34
35class ParserException : public std::exception
36{
37public:
38 ParserException(const std::string & msg)
39 : _msg(msg)
40 {
41 }
42
43 virtual const char * what() const noexcept;
44
46 std::string _msg;
47};
48
49namespace utils
50{
52std::stringstream & operator>>(std::stringstream & in, torch::Tensor &);
53
54std::vector<std::string> split(const std::string & str, const std::string & delims);
55
56std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
57
58bool start_with(std::string_view str, std::string_view prefix);
59
60bool end_with(std::string_view str, std::string_view suffix);
61
62template <typename T>
63void
64parse_(T & val, const std::string & raw_str)
65{
66 std::stringstream ss(trim(raw_str));
67 ss >> val;
68 if (ss.fail() || !ss.eof())
69 throw ParserException("Failed to parse '" + raw_str + "' as a " +
70 utils::demangle(typeid(T).name()));
71}
72
73template <typename T>
74T
75parse(const std::string & raw_str)
76{
77 T val;
79 return val;
80}
81
82template <typename T>
83void
84parse_vector_(std::vector<T> & vals, const std::string & raw_str)
85{
86 auto tokens = split(raw_str, " \t\n\v\f\r");
87 vals.resize(tokens.size());
88 for (size_t i = 0; i < tokens.size(); i++)
90}
91
92template <typename T>
93std::vector<T>
94parse_vector(const std::string & raw_str)
95{
96 std::vector<T> vals;
98 return vals;
99}
100
101template <typename T>
102void
103parse_vector_vector_(std::vector<std::vector<T>> & vals, const std::string & raw_str)
104{
105 auto token_vecs = split(raw_str, ";");
106 vals.resize(token_vecs.size());
107 for (size_t i = 0; i < token_vecs.size(); i++)
109}
110
111template <typename T>
112std::vector<std::vector<T>>
113parse_vector_vector(const std::string & raw_str)
114{
115 std::vector<std::vector<T>> vals;
117 return vals;
118}
119
120// template specializations for special options types
121template <>
122void parse_<bool>(bool &, const std::string & raw_str);
124template <>
125void parse_vector_<bool>(std::vector<bool> &, const std::string & raw_str);
126template <>
127void parse_<TensorShape>(TensorShape &, const std::string & raw_str);
128template <>
129void parse_<VariableName>(VariableName &, const std::string & raw_str);
130} // namespace utils
131} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:47
Definition parser_utils.h:36
virtual const char * what() const noexcept
Definition parser_utils.cxx:31
ParserException(const std::string &msg)
Definition parser_utils.h:38
std::string trim(const std::string &str, const std::string &white_space)
Definition parser_utils.cxx:67
void parse_vector_(std::vector< bool > &vals, const std::string &raw_str)
Definition parser_utils.cxx:104
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:33
std::vector< T > parse_vector(const std::string &raw_str)
Definition parser_utils.h:94
std::vector< std::vector< T > > parse_vector_vector(const std::string &raw_str)
Definition parser_utils.h:113
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition parser_utils.cxx:46
void parse_(bool &val, const std::string &raw_str)
Definition parser_utils.cxx:91
void parse_vector_vector_(std::vector< std::vector< T > > &vals, const std::string &raw_str)
Definition parser_utils.h:103
T parse(const std::string &raw_str)
Definition parser_utils.h:75
Definition CrossRef.cxx:30
torch::SmallVector< Size > TensorShape
Definition types.h:34
std::stringstream & operator>>(std::stringstream &ss, EnumSelection &es)
Definition EnumSelection.cxx:38