NEML2 1.4.0
Loading...
Searching...
No Matches
parser_utils.cxx
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#include "neml2/misc/parser_utils.h"
26#include <cxxabi.h>
27
28namespace neml2
29{
30const char *
32{
33 return _msg.c_str();
34}
35
36namespace utils
37{
38std::stringstream &
39operator>>(std::stringstream & in, torch::Tensor & )
40{
41 throw ParserException("Cannot parse torch::Tensor");
42 return in;
43}
44
45std::string
46demangle(const char * name)
47{
48 int status = -4;
49 std::unique_ptr<char, void (*)(void *)> res{abi::__cxa_demangle(name, NULL, NULL, &status),
50 std::free};
51 return (status == 0) ? res.get() : name;
52}
53
54std::vector<std::string>
55split(const std::string & str, const std::string & delims)
56{
57 std::vector<std::string> tokens;
58
59 std::string::size_type last_pos = str.find_first_not_of(delims, 0);
60 std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + 1, str.size()));
61
62 while (last_pos != std::string::npos)
63 {
64 tokens.push_back(str.substr(last_pos, pos - last_pos));
65 // skip delims between tokens
66 last_pos = str.find_first_not_of(delims, pos);
67 if (last_pos == std::string::npos)
68 break;
69 pos = str.find_first_of(delims, std::min(last_pos + 1, str.size()));
70 }
71
72 return tokens;
73}
74
75std::string
76trim(const std::string & str, const std::string & white_space)
77{
78 const auto begin = str.find_first_not_of(white_space);
79 if (begin == std::string::npos)
80 return ""; // no content
81 const auto end = str.find_last_not_of(white_space);
82 return str.substr(begin, end - begin + 1);
83}
84
85bool
86start_with(std::string_view str, std::string_view prefix)
87{
88 return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
89}
90
91bool
92end_with(std::string_view str, std::string_view suffix)
93{
94 return str.size() >= suffix.size() &&
95 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
96}
97
98template <>
99bool
100parse<bool>(const std::string & raw_str)
101{
102 std::string val = parse<std::string>(raw_str);
103 if (val == "true")
104 return true;
105 if (val == "false")
106 return false;
107
108 throw ParserException("Failed to parse boolean value. Only 'true' and 'false' are recognized.");
109}
110
111template <>
113parse<VariableName>(const std::string & raw_str)
114{
115 auto tokens = split(raw_str, "/ \t\n\v\f\r");
116 return VariableName(tokens);
117}
118
119template <>
121parse<TorchShape>(const std::string & raw_str)
122{
123 if (!start_with(raw_str, "(") || !end_with(raw_str, ")"))
124 throw ParserException("Trying to parse " + raw_str +
125 " as a shape, but a shape must start with '(' and end with ')'");
126
127 auto inner = trim(raw_str, "() \t\n\v\f\r");
128 auto tokens = split(inner, ", \t\n\v\f\r");
129
131 for (auto & token : tokens)
132 shape.push_back(parse<TorchSize>(token));
133
134 return shape;
135}
136} // namespace utils
137} // 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
std::string trim(const std::string &str, const std::string &white_space)
Definition parser_utils.cxx:76
bool parse< bool >(const std::string &raw_str)
Definition parser_utils.cxx:100
std::stringstream & operator>>(std::stringstream &in, torch::Tensor &)
This is a dummy to prevent compilers whining about not know how to >> torch::Tensor.
Definition parser_utils.cxx:39
std::string demangle(const char *name)
Definition parser_utils.cxx:46
VariableName parse< VariableName >(const std::string &raw_str)
Definition parser_utils.cxx:113
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition parser_utils.cxx:55
bool start_with(std::string_view str, std::string_view prefix)
Definition parser_utils.cxx:86
bool end_with(std::string_view str, std::string_view suffix)
Definition parser_utils.cxx:92
TorchShape parse< TorchShape >(const std::string &raw_str)
Definition parser_utils.cxx:121
Definition CrossRef.cxx:32
std::vector< TorchSize > TorchShape
Definition types.h:34
LabeledAxisAccessor VariableName
Definition Variable.h:35