59 template < GUM_Numeric GUM_SCALAR >
63 text =
replace(text,
"Syntax error",
"Error");
65 text =
replace(text,
"LABEL_OR_STAR_LIST",
"declaration");
67 text =
replace(text,
"ARRAY_REFERENCE_SLOT",
"declaration");
68 text =
replace(text,
"FLOAT_AS_LABEL",
"declaration");
69 text =
replace(text,
"FLOAT_OR_INT",
"declaration");
70 text =
replace(text,
"INTEGER_AS_FLOAT",
"declaration");
71 text =
replace(text,
"INTEGER_AS_LABEL",
"declaration");
72 text =
replace(text,
"INT_TYPE_DECLARATION",
"declaration");
73 text =
replace(text,
"LABEL_OR_INT",
"declaration");
74 text =
replace(text,
"LABEL_OR_STAR",
"declaration");
75 text =
replace(text,
"NAMED_CLASS_ELEMENT",
"declaration");
76 text =
replace(text,
"REAL_TYPE_DECLARATION",
"declaration");
78 text =
replace(text,
"AGGREGATE_PARENTS",
"declaration");
79 text =
replace(text,
"CLASS_BODY",
"declaration");
80 text =
replace(text,
"CLASS_DECLARATION",
"declaration");
81 text =
replace(text,
"CLASS_ELEMENT",
"declaration");
82 text =
replace(text,
"CLASS_PARAMETER",
"declaration");
83 text =
replace(text,
"CLASS_UNIT",
"declaration");
84 text =
replace(text,
"FLOAT_LIST",
"declaration");
85 text =
replace(text,
"FORMULA_LIST",
"declaration");
86 text =
replace(text,
"IDENTIFIER_LIST",
"declaration");
87 text =
replace(text,
"IMPORT_BODY",
"declaration");
88 text =
replace(text,
"IMPORT_DECLARATION",
"declaration");
89 text =
replace(text,
"IMPORT_UNIT",
"declaration");
90 text =
replace(text,
"INTERFACE_BODY",
"declaration");
91 text =
replace(text,
"INTERFACE_DECLARATION",
"declaration");
92 text =
replace(text,
"INTERFACE_UNIT",
"declaration");
93 text =
replace(text,
"LABEL_LIST",
"declaration");
94 text =
replace(text,
"PARAMETER_LIST",
"declaration");
95 text =
replace(text,
"PREFIXED_LABEL",
"declaration");
96 text =
replace(text,
"RAW_CPT",
"declaration");
97 text =
replace(text,
"REFERENCE_SLOT",
"declaration");
98 text =
replace(text,
"RULE_CPT",
"declaration");
99 text =
replace(text,
"SYSTEM_BODY",
"declaration");
100 text =
replace(text,
"SYSTEM_DECLARATION",
"declaration");
101 text =
replace(text,
"SYSTEM_UNIT",
"declaration");
102 text =
replace(text,
"TYPE_DECLARATION",
"declaration");
103 text =
replace(text,
"TYPE_LABEL",
"declaration");
104 text =
replace(text,
"TYPE_UNIT",
"declaration");
105 text =
replace(text,
"TYPE_VALUE_LIST",
"declaration");
107 text =
replace(text,
"AGGREGATE",
"declaration");
108 text =
replace(text,
"ARRAY",
"declaration");
109 text =
replace(text,
"ATTRIBUTE",
"declaration");
110 text =
replace(text,
"CAST",
"declaration");
111 text =
replace(text,
"CHAIN",
"declaration");
112 text =
replace(text,
"CLASS",
"declaration");
114 text =
replace(text,
"FORMULA",
"declaration");
115 text =
replace(text,
"IDENTIFIER",
"declaration");
116 text =
replace(text,
"INT",
"declaration");
117 text =
replace(text,
"INTEGER",
"declaration");
118 text =
replace(text,
"INTERFACE",
"declaration");
119 text =
replace(text,
"LABEL",
"declaration");
120 text =
replace(text,
"LINK",
"declaration");
121 text =
replace(text,
"MAP",
"declaration");
122 text =
replace(text,
"PARAMETER",
"declaration");
124 text =
replace(text,
"RULE",
"declaration");
126 text =
replace(text,
"UNIT",
"declaration");
131 template < GUM_Numeric GUM_SCALAR >
133 return std::format(
"{}|{} col {}| {}",
140 template < GUM_Numeric GUM_SCALAR >
143 inputstr.seekg(0, inputstr.end);
144 int length = int(inputstr.tellg());
145 inputstr.seekg(0, inputstr.beg);
147 auto str = std::string();
148 str.resize(length,
' ');
149 auto begin = &*str.begin();
151 inputstr.read(begin, length);
161 template < GUM_Numeric GUM_SCALAR >
167 template < GUM_Numeric GUM_SCALAR >
173 template < GUM_Numeric GUM_SCALAR >
180 template < GUM_Numeric GUM_SCALAR >
188 template < GUM_Numeric GUM_SCALAR >
193 template < GUM_Numeric GUM_SCALAR >
195 if (
this == &src) {
return *
this; }
204 template < GUM_Numeric GUM_SCALAR >
206 if (
this == &src) {
return *
this; }
207 _prm_ = std::move(src._prm_);
215 template < GUM_Numeric GUM_SCALAR >
219 size_t j = class_path.find(
';');
221 while (j != std::string::npos) {
225 if (i < class_path.size()) {
226 j = class_path.find(
';', i);
228 j = std::string::npos;
232 if (i < class_path.size()) {
addClassPath(class_path.substr(i, std::string::npos)); }
235 template < GUM_Numeric GUM_SCALAR >
237 std::string path(class_path);
238 if (path[path.size() - 1] !=
'/') { path.append(
"/"); }
239 std::filesystem::directory_entry dir(path);
241 _errors_.addException(
"could not resolve class path", path);
247 template < GUM_Numeric GUM_SCALAR >
252 template < GUM_Numeric GUM_SCALAR >
257 template < GUM_Numeric GUM_SCALAR >
262 template < GUM_Numeric GUM_SCALAR >
267 template < GUM_Numeric GUM_SCALAR >
272 template < GUM_Numeric GUM_SCALAR >
280 template < GUM_Numeric GUM_SCALAR >
284 o <<
_print_(err) << std::endl;
288 template < GUM_Numeric GUM_SCALAR >
293 template < GUM_Numeric GUM_SCALAR >
298 template < GUM_Numeric GUM_SCALAR >
303 template < GUM_Numeric GUM_SCALAR >
308 template < GUM_Numeric GUM_SCALAR >
310 std::stringstream sBuff(str);
315 template < GUM_Numeric GUM_SCALAR >
318 auto const lastSlashIndex = file.find_last_of(
'/');
320 std::filesystem::directory_entry dir(file.substr(0, lastSlashIndex + 1));
322 _errors_.addException(
"could not find file", file);
326 auto const basename = file.substr(lastSlashIndex + 1);
327 auto const absFilename
328 = std::filesystem::absolute(dir.path() / std::filesystem::path(basename)).string();
330 std::ifstream input(absFilename);
331 if (input.is_open()) {
332 _readStream_(input, std::string(file), std::string(module));
334 _errors_.addException(
"could not open file", file);
344 _errors_.addException(
"unknown error", file);
349 template < GUM_Numeric GUM_SCALAR >
351 std::ostream& output,
352 std::string module) {
358 template < GUM_Numeric GUM_SCALAR >
360 const std::string& filename,
361 const std::string& module) {
363 auto buffer = std::unique_ptr< unsigned char[] >(
new unsigned char[sBuff.length() + 1]);
364 strcpy((
char*)buffer.get(), sBuff.c_str());
365 auto s =
o3prm_scanner(buffer.get(),
int(sBuff.length() + 1), filename);
368 p.set_prefix(module);
373 template < GUM_Numeric GUM_SCALAR >
378 auto module_path =
module;
379 std::replace(module_path.begin(), module_path.end(),
'.',
'/');
382 std::replace(path.begin(), path.end(),
'.',
'/');
384 auto imported =
false;
386 auto file_path = cp + path +
".o3prm";
387 std::ifstream file(file_path);
389 if (file.is_open()) {
395 file_path = cp + module + path +
".o3prm";
396 std::ifstream file2(file_path);
398 if (file2.is_open()) {
408 std::format(
"Import error: could not resolve import {}", i.
import().
label()),
416 template < GUM_Numeric GUM_SCALAR >
418 auto copy = std::vector< const O3Import* >();
419 for (
const auto& i:
_o3_prm_->imports()) {
420 if (!
_imported_.exists(i->import().label())) { copy.push_back(i.get()); }
425 template < GUM_Numeric GUM_SCALAR >
427 const std::string& file,
428 std::string module) {
429 if (module.size() > 0 && module.back() !=
'.') {
module.append("."); }
435 for (
auto i: imports) {
439 }
while (imports.size() > 0);
446 auto interface_factory
453 type_factory.build();
454 interface_factory.buildInterfaces();
455 class_factory.buildClasses();
456 interface_factory.buildElements();
457 class_factory.buildImplementations();
458 class_factory.buildParameters();
459 class_factory.buildReferenceSlots();
460 class_factory.declareAttributes();
461 class_factory.declareAggregates();
462 class_factory.completeAggregates();
463 class_factory.completeAttributes();
464 system_factory.build();
466 if (
_errors_.count() == 0) {
_errors_.addException(
"an unknown error occured", file); }
467 }
catch (...) {
_errors_.addException(
"an unknown exception occured", file); }
471 template < GUM_Numeric GUM_SCALAR >
476 template < GUM_Numeric GUM_SCALAR >
Headers for the O3prmReader class.
std::wstring widen(std::string_view str)
Cast a std::string into a std::wstring.
This class is used contain and manipulate gum::ParseError.
Base class for all aGrUM's exceptions.
GUM_NODISCARD std::string errorContent() const
Returns the message content.
Exception : operation not allowed.
This class is used to represent parsing errors for the different parser implemented in aGrUM.
std::string filename
The file of this gum::ParseError, default is "".
Idx column
The column of this gum::ParseError, default is 0.
std::string msg
The gum::ParseError message.
Idx line
The line of this gum::ParseError.
bool is_error
If false, this gum::ParseError is a warning.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Builds gum::prm::Class from gum::prm::o3prm::O3Class.
The O3Import is part of the AST of the O3PRM language.
Bulds gum::prm:PRMInterface from gum::prm::o3prm::O3Interface.
Resolves names for the different O3PRM factories.
The O3PRM is part of the AST of the O3PRM language.
Builds gum::prm::PRMSystem from gum::prm::o3prm::O3System.
Builds gum::prm::PRMType from gum::prm::o3prm::O3Type, gum::prm::o3prm::O3IntType and gum::prm::o3prm...
void setClassPath(std::string_view class_path)
This methods defines the list of paths to look for o3prm files.
Set< std::string > _imported_
void _parseStream_(std::istream &input, const std::string &filename, const std::string &module)
Idx errLine(Idx i) const
line of ith error or warning
Size readString(const std::string &string)
With readString method, you must set the current path to search from import yourself,...
std::wstring errFilename(Idx i) const
filename of ith error or warning
gum::prm::PRM< GUM_SCALAR > * prm()
void parseStream(std::istream &input, std::ostream &output, std::string module="")
Size errors() const
publishing Errors API
O3prmReader & operator=(const O3prmReader &src)
void showErrorCounts(std::ostream &o=std::cerr) const
send on std::cerr the number of errors and the number of warnings
void showElegantErrors(std::ostream &o=std::cerr) const
send on std::cerr the list of errors
const ErrorsContainer & errorsContainer() const
publishing Errors API
bool errIsError(Idx i) const
type of ith error or warning
std::string _clean_(std::string text) const
void _readStream_(std::istream &input, const std::string &file, std::string module="")
Size readFile(std::string_view file, std::string_view module="")
Read file and load its content using a PRMFactory. The package parameter set the file's content packa...
Idx errCol(Idx i) const
col of ith error or warning
std::vector< const O3Import * > _copyImports_()
std::string _print_(const ParseError &err) const
std::unique_ptr< O3PRM > _o3_prm_
void _parseImport_(const O3Import &i, const std::string &module_path)
std::string errMsg(Idx i) const
message of ith error or warning
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr) const
send on std::cerr the list of errors or warnings
PRM< GUM_SCALAR > * _prm_
std::vector< std::string > _class_path_
void addClassPath(std::string_view class_path)
Add a list of paths to look for o3prm files.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
std::string replace(std::string_view s, std::string_view val, std::string_view new_val)
Replace val by new_val in s.
gum::prm::o3prm::Scanner o3prm_scanner
gum::prm::o3prm::Parser o3prm_parser
namespace for all probabilistic relational models entities
gum is the global namespace for all aGrUM entities