Added basic inline command parser
This commit is contained in:
143
src/uml/command_parser.cc
Normal file
143
src/uml/command_parser.cc
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
/**
|
||||||
|
* src/uml/command_parser.cc
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 "command_parser.h"
|
||||||
|
|
||||||
|
#include "util/util.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace clanguml {
|
||||||
|
namespace command_parser {
|
||||||
|
|
||||||
|
std::shared_ptr<command> command::from_string(std::string_view c)
|
||||||
|
{
|
||||||
|
if (c.find("note") == 0) {
|
||||||
|
return note::from_string(c);
|
||||||
|
}
|
||||||
|
else if (c.find("style") == 0) {
|
||||||
|
return style::from_string(c);
|
||||||
|
}
|
||||||
|
else if (c.find("aggregation") == 0) {
|
||||||
|
return aggregation::from_string(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<command> note::from_string(std::string_view c)
|
||||||
|
{
|
||||||
|
auto res = std::make_shared<note>();
|
||||||
|
auto it = c.begin();
|
||||||
|
// Skip 'note'
|
||||||
|
std::advance(it, 4);
|
||||||
|
|
||||||
|
if (*it != '[')
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::advance(it, 1);
|
||||||
|
|
||||||
|
auto pos = std::distance(c.begin(), it);
|
||||||
|
res->position = c.substr(pos, c.find("]", pos) - pos);
|
||||||
|
|
||||||
|
std::advance(it, res->position.size() + 1);
|
||||||
|
|
||||||
|
pos = std::distance(c.begin(), it);
|
||||||
|
res->text = c.substr(pos, c.find("}", pos) - pos);
|
||||||
|
|
||||||
|
res->position = util::trim(res->position);
|
||||||
|
res->text = util::trim(res->text);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<command> style::from_string(std::string_view c)
|
||||||
|
{
|
||||||
|
auto res = std::make_shared<style>();
|
||||||
|
auto it = c.begin();
|
||||||
|
// Skip 'style'
|
||||||
|
std::advance(it, 5);
|
||||||
|
|
||||||
|
if (*it != '[')
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::advance(it, 1);
|
||||||
|
|
||||||
|
auto pos = std::distance(c.begin(), it);
|
||||||
|
res->spec = c.substr(pos, c.find("]", pos) - pos);
|
||||||
|
|
||||||
|
res->spec = util::trim(res->spec);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<command> aggregation::from_string(std::string_view c)
|
||||||
|
{
|
||||||
|
auto res = std::make_shared<aggregation>();
|
||||||
|
auto it = c.begin();
|
||||||
|
// Skip 'aggregation'
|
||||||
|
std::advance(it, 11);
|
||||||
|
|
||||||
|
if (*it != '[')
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::advance(it, 1);
|
||||||
|
|
||||||
|
auto pos = std::distance(c.begin(), it);
|
||||||
|
res->multiplicity = c.substr(pos, c.find("]", pos) - pos);
|
||||||
|
|
||||||
|
res->multiplicity = util::trim(res->multiplicity);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<command>> parse(std::string documentation_block)
|
||||||
|
{
|
||||||
|
std::vector<std::shared_ptr<command>> res;
|
||||||
|
const std::string begin_tag{"@clanguml"};
|
||||||
|
const auto begin_tag_size = begin_tag.size();
|
||||||
|
|
||||||
|
// First replace all \clanguml occurences with @clanguml
|
||||||
|
util::replace_all(documentation_block, "\\clanguml", "@clanguml");
|
||||||
|
documentation_block = util::trim(documentation_block);
|
||||||
|
|
||||||
|
std::string_view block_view{documentation_block};
|
||||||
|
|
||||||
|
auto pos = block_view.find("@clanguml{");
|
||||||
|
while (pos < documentation_block.size()) {
|
||||||
|
auto c_begin = pos + begin_tag_size;
|
||||||
|
auto c_end = documentation_block.find("}", c_begin);
|
||||||
|
|
||||||
|
if (c_end == std::string::npos)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
auto com =
|
||||||
|
command::from_string(block_view.substr(c_begin + 1, c_end - 2));
|
||||||
|
|
||||||
|
if (com)
|
||||||
|
res.emplace_back(std::move(com));
|
||||||
|
|
||||||
|
pos = block_view.find("@clanguml{", c_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace command_parser
|
||||||
|
} // namespace clanguml
|
||||||
|
|
||||||
55
src/uml/command_parser.h
Normal file
55
src/uml/command_parser.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* src/uml/command_parser.h
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "config/config.h"
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace clanguml {
|
||||||
|
namespace command_parser {
|
||||||
|
|
||||||
|
struct command {
|
||||||
|
virtual ~command() = default;
|
||||||
|
static std::shared_ptr<command> from_string(std::string_view c);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct note : public command {
|
||||||
|
std::string position;
|
||||||
|
std::string text;
|
||||||
|
|
||||||
|
static std::shared_ptr<command> from_string(std::string_view c);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct style : public command {
|
||||||
|
std::string spec;
|
||||||
|
static std::shared_ptr<command> from_string(std::string_view c);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct aggregation : public command {
|
||||||
|
std::string multiplicity;
|
||||||
|
static std::shared_ptr<command> from_string(std::string_view c);
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<command>> parse(std::string documentation_block);
|
||||||
|
|
||||||
|
} // namespace command_parser
|
||||||
|
} // namespace clanguml
|
||||||
|
|
||||||
@@ -126,5 +126,20 @@ bool find_element_alias(
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool replace_all(
|
||||||
|
std::string &input, std::string pattern, std::string replace_with)
|
||||||
|
{
|
||||||
|
bool replaced{false};
|
||||||
|
|
||||||
|
auto pos = input.find(pattern);
|
||||||
|
while (pos < input.size()) {
|
||||||
|
input.replace(pos, pattern.size(), replace_with);
|
||||||
|
pos = input.find(pattern, pos + replace_with.size());
|
||||||
|
replaced = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return replaced;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,5 +104,15 @@ std::string unqualify(const std::string &s);
|
|||||||
*/
|
*/
|
||||||
bool find_element_alias(
|
bool find_element_alias(
|
||||||
const std::string &input, std::tuple<std::string, size_t, size_t> &result);
|
const std::string &input, std::tuple<std::string, size_t, size_t> &result);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Find and replace in string
|
||||||
|
*
|
||||||
|
* Replaces all occurences of pattern with replace_with in input string.
|
||||||
|
*
|
||||||
|
* @return True if at least on replacement was made
|
||||||
|
*/
|
||||||
|
bool replace_all(
|
||||||
|
std::string &input, std::string pattern, std::string replace_with);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,14 @@ set(CLANG_UML_TEST_CASES_HEADER
|
|||||||
catch.h
|
catch.h
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(CLANG_UML_TEST_COMMAND_PARSER_SRC
|
||||||
|
test_command_parser.cc
|
||||||
|
${TEST_UTIL_SOURCES}
|
||||||
|
)
|
||||||
|
set(CLANG_UML_TEST_COMMAND_PARSER_HEADER
|
||||||
|
catch.h
|
||||||
|
)
|
||||||
|
|
||||||
add_executable(test_util
|
add_executable(test_util
|
||||||
${CLANG_UML_TEST_UTIL_SRC}
|
${CLANG_UML_TEST_UTIL_SRC}
|
||||||
${CLANG_UML_TEST_UTIL_HEADER})
|
${CLANG_UML_TEST_UTIL_HEADER})
|
||||||
@@ -33,6 +41,15 @@ target_link_libraries(test_util
|
|||||||
${YAML_CPP_LIBRARIES}
|
${YAML_CPP_LIBRARIES}
|
||||||
spdlog::spdlog clang-umllib cppast)
|
spdlog::spdlog clang-umllib cppast)
|
||||||
|
|
||||||
|
add_executable(test_command_parser
|
||||||
|
${CLANG_UML_TEST_COMMAND_PARSER_SRC}
|
||||||
|
${CLANG_UML_TEST_COMMAND_PARSER_HEADER})
|
||||||
|
|
||||||
|
target_link_libraries(test_command_parser
|
||||||
|
PRIVATE
|
||||||
|
${LIBCLANG_LIBRARIES}
|
||||||
|
${YAML_CPP_LIBRARIES}
|
||||||
|
spdlog::spdlog clang-umllib cppast)
|
||||||
|
|
||||||
add_executable(test_cases
|
add_executable(test_cases
|
||||||
${CLANG_UML_TEST_CASES_SRC}
|
${CLANG_UML_TEST_CASES_SRC}
|
||||||
@@ -57,4 +74,5 @@ foreach(TEST_CASE_CONFIG ${TEST_CASE_CONFIGS})
|
|||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
add_test(NAME test_util COMMAND test_util)
|
add_test(NAME test_util COMMAND test_util)
|
||||||
|
add_test(NAME test_command_parser COMMAND test_command_parser)
|
||||||
add_test(NAME test_cases COMMAND test_cases)
|
add_test(NAME test_cases COMMAND test_cases)
|
||||||
|
|||||||
113
tests/test_command_parser.cc
Normal file
113
tests/test_command_parser.cc
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/**
|
||||||
|
* tests/test_command_parser.cc
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
|
||||||
|
#include "uml/command_parser.h"
|
||||||
|
|
||||||
|
#include "catch.h"
|
||||||
|
|
||||||
|
TEST_CASE("Test command parser on regular comment", "[unit-test]")
|
||||||
|
{
|
||||||
|
std::string comment = R"(
|
||||||
|
\brief This is a comment.
|
||||||
|
|
||||||
|
This is a longer comment.
|
||||||
|
|
||||||
|
\param a int an int
|
||||||
|
\param b float a float
|
||||||
|
|
||||||
|
)";
|
||||||
|
|
||||||
|
using namespace clanguml::command_parser;
|
||||||
|
|
||||||
|
auto commands = parse(comment);
|
||||||
|
|
||||||
|
CHECK(commands.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test command parser on note", "[unit-test]")
|
||||||
|
{
|
||||||
|
std::string comment = R"(
|
||||||
|
\brief This is a comment.
|
||||||
|
|
||||||
|
This is a longer comment.
|
||||||
|
|
||||||
|
\
|
||||||
|
|
||||||
|
\param a int an int
|
||||||
|
\param b float a float
|
||||||
|
|
||||||
|
\clanguml{note[left] This is a comment}
|
||||||
|
@clanguml{note[ top ]
|
||||||
|
|
||||||
|
This is a comment }
|
||||||
|
)";
|
||||||
|
|
||||||
|
using namespace clanguml::command_parser;
|
||||||
|
|
||||||
|
auto commands = parse(comment);
|
||||||
|
|
||||||
|
CHECK(commands.size() == 2);
|
||||||
|
|
||||||
|
auto n1 = std::dynamic_pointer_cast<note>(commands.at(0));
|
||||||
|
auto n2 = std::dynamic_pointer_cast<note>(commands.at(1));
|
||||||
|
|
||||||
|
CHECK(n1);
|
||||||
|
CHECK(n1->position == "left");
|
||||||
|
CHECK(n1->text == "This is a comment");
|
||||||
|
|
||||||
|
CHECK(n2);
|
||||||
|
CHECK(n2->position == "top");
|
||||||
|
CHECK(n2->text == "This is a comment");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test command parser on style", "[unit-test]")
|
||||||
|
{
|
||||||
|
std::string comment = R"(
|
||||||
|
\clanguml{style[#green,dashed,thickness=4]}
|
||||||
|
)";
|
||||||
|
|
||||||
|
using namespace clanguml::command_parser;
|
||||||
|
|
||||||
|
auto commands = parse(comment);
|
||||||
|
|
||||||
|
CHECK(commands.size() == 1);
|
||||||
|
|
||||||
|
auto n1 = std::dynamic_pointer_cast<style>(commands.at(0));
|
||||||
|
|
||||||
|
CHECK(n1);
|
||||||
|
CHECK(n1->spec == "#green,dashed,thickness=4");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test command parser on aggregation", "[unit-test]")
|
||||||
|
{
|
||||||
|
std::string comment = R"(
|
||||||
|
\clanguml{aggregation[0..1:0..*]}
|
||||||
|
)";
|
||||||
|
|
||||||
|
using namespace clanguml::command_parser;
|
||||||
|
|
||||||
|
auto commands = parse(comment);
|
||||||
|
|
||||||
|
CHECK(commands.size() == 1);
|
||||||
|
|
||||||
|
auto n1 = std::dynamic_pointer_cast<aggregation>(commands.at(0));
|
||||||
|
|
||||||
|
CHECK(n1);
|
||||||
|
CHECK(n1->multiplicity == "0..1:0..*");
|
||||||
|
}
|
||||||
@@ -46,3 +46,25 @@ TEST_CASE("Test ns_relative", "[unit-test]")
|
|||||||
"static const vector<a>&");
|
"static const vector<a>&");
|
||||||
CHECK(ns_relative({"clanguml::t0"}, "clanguml::t0") == "t0");
|
CHECK(ns_relative({"clanguml::t0"}, "clanguml::t0") == "t0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test replace_all", "[unit-test]")
|
||||||
|
{
|
||||||
|
using namespace clanguml::util;
|
||||||
|
|
||||||
|
const std::string orig = R"(
|
||||||
|
Lorem ipsum \clanguml{note} something something...
|
||||||
|
|
||||||
|
\clanguml{style}
|
||||||
|
|
||||||
|
)";
|
||||||
|
std::string text{orig};
|
||||||
|
|
||||||
|
CHECK(replace_all(text, "NOTHERE", "HERE") == false);
|
||||||
|
CHECK(replace_all(text, "\\clanguml", "@clanguml") == true);
|
||||||
|
CHECK(replace_all(text, "something", "nothing") == true);
|
||||||
|
CHECK(replace_all(text, "something", "nothing") == false);
|
||||||
|
CHECK(replace_all(text, "nothing", "something") == true);
|
||||||
|
CHECK(replace_all(text, "@clanguml", "\\clanguml") == true);
|
||||||
|
|
||||||
|
CHECK(text == orig);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user