NEML2 2.0.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;
34class EnumSelection;
36
37class ParserException : public std::exception
38{
39public:
40 ParserException(std::string msg)
41 : _msg(std::move(msg))
42 {
43 }
44
45 const char * what() const noexcept override;
46
48 std::string _msg;
49};
50
51namespace utils
52{
54std::stringstream & operator>>(std::stringstream & in, torch::Tensor &);
55
56std::string join(const std::vector<std::string> & strs, const std::string & delim);
57
58std::vector<std::string> split(const std::string & str, const std::string & delims);
59
60std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
61
62bool start_with(std::string_view str, std::string_view prefix);
63
64bool end_with(std::string_view str, std::string_view suffix);
65
66template <typename T>
67void
68parse_(T & val, const std::string & raw_str)
69{
70 std::stringstream ss(trim(raw_str));
71 ss >> val;
72 if (ss.fail())
73 throw ParserException("Failed to parse '" + raw_str + "' as a " +
74 utils::demangle(typeid(T).name()));
75}
76
77template <typename T>
78T
79parse(const std::string & raw_str)
80{
81 T val;
83 return val;
84}
85
86template <typename T>
87void
88parse_vector_(std::vector<T> & vals, const std::string & raw_str)
89{
90 auto tokens = split(raw_str, " \t\n\v\f\r");
91 vals.resize(tokens.size());
92 for (size_t i = 0; i < tokens.size(); i++)
94}
95
96template <typename T>
97std::vector<T>
98parse_vector(const std::string & raw_str)
99{
100 std::vector<T> vals;
102 return vals;
103}
104
105template <typename T>
106void
107parse_vector_vector_(std::vector<std::vector<T>> & vals, const std::string & raw_str)
108{
109 auto token_vecs = split(raw_str, ";");
110 vals.resize(token_vecs.size());
111 for (size_t i = 0; i < token_vecs.size(); i++)
113}
114
115template <typename T>
116std::vector<std::vector<T>>
117parse_vector_vector(const std::string & raw_str)
118{
119 std::vector<std::vector<T>> vals;
121 return vals;
122}
123
124// template specializations for special options types
125template <>
126void parse_<bool>(bool &, const std::string & raw_str);
128template <>
129void parse_vector_<bool>(std::vector<bool> &, const std::string & raw_str);
130template <>
131void parse_<TensorShape>(TensorShape &, const std::string & raw_str);
132template <>
133void parse_<VariableName>(VariableName &, const std::string & raw_str);
134} // namespace utils
135} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Selection of an enum value from a list of candidates.
Definition EnumSelection.h:41
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:58
Selection of multiple enum value from a list of candidates.
Definition MultiEnumSelection.h:41
Definition parser_utils.h:38
ParserException(std::string msg)
Definition parser_utils.h:40
const char * what() const noexcept override
Definition parser_utils.cxx:31
std::string trim(const std::string &str, const std::string &white_space)
Definition parser_utils.cxx:80
void parse_vector_(std::vector< bool > &vals, const std::string &raw_str)
Definition parser_utils.cxx:117
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:32
std::vector< T > parse_vector(const std::string &raw_str)
Definition parser_utils.h:98
std::vector< std::vector< T > > parse_vector_vector(const std::string &raw_str)
Definition parser_utils.h:117
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition parser_utils.cxx:59
void parse_(bool &val, const std::string &raw_str)
Definition parser_utils.cxx:104
void parse_vector_vector_(std::vector< std::vector< T > > &vals, const std::string &raw_str)
Definition parser_utils.h:107
T parse(const std::string &raw_str)
Definition parser_utils.h:79
Definition CrossRef.cxx:31
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
torch::SmallVector< Size > TensorShape
Definition types.h:34
std::stringstream & operator>>(std::stringstream &ss, EnumSelection &es)
Definition EnumSelection.cxx:38