NEML2 1.4.0
Loading...
Searching...
No Matches
parser_utils.h
1// Copyright 2023, 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/types.h"
28#include "neml2/misc/error.h"
29#include "neml2/tensors/Variable.h"
30#include "neml2/base/CrossRef.h"
31
32namespace neml2
33{
34class ParserException : public std::exception
35{
36public:
37 ParserException(const std::string & msg)
38 : _msg(msg)
39 {
40 }
41
42 virtual const char * what() const noexcept;
43
45 std::string _msg;
46};
47
48namespace utils
49{
51std::stringstream & operator>>(std::stringstream & in, torch::Tensor &);
52
53std::string demangle(const char * name);
54
55std::vector<std::string> split(const std::string & str, const std::string & delims);
56
57std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
58
59bool start_with(std::string_view str, std::string_view prefix);
60
61bool end_with(std::string_view str, std::string_view suffix);
62
63template <typename T>
64T
65parse(const std::string & raw_str)
66{
67 T val;
68 std::stringstream ss(trim(raw_str));
69 ss >> val;
70 if (ss.fail() || !ss.eof())
71 throw ParserException("Failed to parse '" + raw_str + "' as a " + demangle(typeid(T).name()));
72 return val;
73}
74
75template <typename T>
76std::vector<T>
77parse_vector(const std::string & raw_str)
78{
79 auto tokens = split(raw_str, " \t\n\v\f\r");
80 std::vector<T> ret(tokens.size());
81 for (size_t i = 0; i < tokens.size(); i++)
82 ret[i] = parse<T>(tokens[i]);
83 return ret;
84}
85
86template <typename T>
87std::vector<std::vector<T>>
88parse_vector_vector(const std::string & raw_str)
89{
90 auto token_vecs = split(raw_str, ";");
91 std::vector<std::vector<T>> ret(token_vecs.size());
92 for (size_t i = 0; i < token_vecs.size(); i++)
94 return ret;
95}
96
97// @{ template specializations for parse
98template <>
99bool parse<bool>(const std::string & raw_str);
100template <>
101TorchShape parse<TorchShape>(const std::string & raw_str);
102template <>
103VariableName parse<VariableName>(const std::string & raw_str);
104// @}
105} // namespace utils
106} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
Definition parser_utils.h:35
virtual const char * what() const noexcept
Definition parser_utils.cxx:31
ParserException(const std::string &msg)
Definition parser_utils.h:37
std::string trim(const std::string &str, const std::string &white_space)
Definition parser_utils.cxx:76
std::string demangle(const char *name)
Definition parser_utils.cxx:46
std::vector< T > parse_vector(const std::string &raw_str)
Definition parser_utils.h:77
std::vector< std::vector< T > > parse_vector_vector(const std::string &raw_str)
Definition parser_utils.h:88
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition parser_utils.cxx:55
T parse(const std::string &raw_str)
Definition parser_utils.h:65
Definition CrossRef.cxx:32
std::vector< TorchSize > TorchShape
Definition types.h:36
std::stringstream & operator>>(std::stringstream &, CrossRef< T > &)
Definition CrossRef.h:113