/** * src/decorators/decorators.cc * * Copyright (c) 2021-2022 Bartek Kryza * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "decorators.h" #include "util/util.h" #include #include namespace clanguml::decorators { std::shared_ptr decorator::from_string(std::string_view c) { if (c.find(note::label) == 0) { return note::from_string(c); } else if (c.find(skip_relationship::label) == 0) { return skip_relationship::from_string(c); } else if (c.find(skip::label) == 0) { return skip::from_string(c); } else if (c.find(style::label) == 0) { return style::from_string(c); } else if (c.find(aggregation::label) == 0) { return aggregation::from_string(c); } else if (c.find(composition::label) == 0) { return composition::from_string(c); } else if (c.find(association::label) == 0) { return association::from_string(c); } return {}; } bool decorator::applies_to_diagram(const std::string &name) { return diagrams.empty() || (std::find(diagrams.begin(), diagrams.end(), name) != diagrams.end()); } decorator_toks decorator::tokenize(const std::string &label, std::string_view c) { decorator_toks res; res.label = label; size_t pos{}; auto it = c.begin(); std::advance(it, label.size()); if (*it == ':') { std::advance(it, 1); pos = std::distance(c.begin(), it); // If the diagram list is provided after ':', [] is mandatory // even if empty auto d = c.substr(pos, c.find('[', pos) - pos); if (!d.empty()) { std::string d_str{d}; d_str.erase(std::remove_if(d_str.begin(), d_str.end(), (int (*)(int))std::isspace), d_str.end()); res.diagrams = util::split(d_str, ","); } std::advance(it, d.size()); } if (*it == '[') { std::advance(it, 1); pos = std::distance(c.begin(), it); res.param = c.substr(pos, c.find(']', pos) - pos); std::advance(it, res.param.size() + 1); } else if (std::isspace(*it)) { std::advance(it, 1); } pos = std::distance(c.begin(), it); res.text = c.substr(pos, c.find('}', pos) - pos); res.text = util::trim(res.text); res.param = util::trim(res.param); return res; } std::shared_ptr note::from_string(std::string_view c) { auto res = std::make_shared(); auto toks = res->tokenize(note::label, c); res->diagrams = toks.diagrams; if (!toks.param.empty()) res->position = toks.param; res->text = toks.text; return res; } std::shared_ptr skip::from_string(std::string_view /*c*/) { return std::make_shared(); } std::shared_ptr skip_relationship::from_string( std::string_view /*c*/) { return std::make_shared(); } std::shared_ptr style::from_string(std::string_view c) { auto res = std::make_shared