Skip to content

Commit

Permalink
Merge pull request #44 from abap34/json
Browse files Browse the repository at this point in the history
Dot言語への変換を実装
  • Loading branch information
abap34 authored Oct 15, 2023
2 parents e9d6638 + a0ef54f commit 32c8c2b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/almo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ int main(int argc, char* argv[]) {
bool debug = false;
std::string editor_theme = "ace/theme/xcode";
std::string syntax_theme = "github.min";
bool plot_graph = false;

// 出力ファイルのデフォルト値を設定。 .md -> .html に置き換える
std::string out_path = argv[1];
Expand Down Expand Up @@ -48,6 +49,9 @@ int main(int argc, char* argv[]) {
else if (argv[i][1] == 's') {
syntax_theme = argv[i + 1];
}
else if (argv[i][1] == 'g') {
plot_graph = true;
}
else if (argv[i][1] == 'h') {
if (argc > 3) {
throw InvalidCommandLineArgumentsError("不正なコマンドライン引数です。 -h オプションと他のオプションは同時に指定できません。");
Expand All @@ -60,6 +64,7 @@ int main(int argc, char* argv[]) {
std::cout << " -c <CSS> CSSファイルを指定します。デフォルトは テーマに付属するものが使用されます。" << std::endl;
std::cout << " -e <テーマ> エディタのテーマを指定します。デフォルトは です。" << std::endl;
std::cout << " -d デバッグモードで実行します。" << std::endl;
std::cout << " -g 構文木をdot言語として出力します。" << std::endl;
std::cout << " -h ヘルプを表示します。" << std::endl;
}
else {
Expand Down Expand Up @@ -108,6 +113,18 @@ int main(int argc, char* argv[]) {
std::cout << output << std::endl;
}

if (plot_graph) {
std::string graph = ast.to_dot();
std::cout << "digraph g {" << std::endl;
std::cout << " graph [" << std::endl;
std::cout << " labelloc=\"t\";" << std::endl;
std::cout << " ];" << std::endl;
std::cout << graph << std::endl;
std::cout << "}" << std::endl;
}



almo::meta_data = meta_data;

almo::render(ast, meta_data, out_path);
Expand Down
59 changes: 59 additions & 0 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ namespace almo {
return json + "}";
}

// 部分木を dot 言語に変換する.
std::string to_dot() const {
std::map<std::string, std::string> properties = get_properties();

std::string node = properties["uuid"];
std::string label = "";

std::string label_header = properties["class"] + " | ";

int i = 1;
for (auto property : properties) {
if (property.first == "class" || property.first == "uuid") {
continue;
}
label += "<f" + std::to_string(i) + "> " + property.first + ": " + escape(property.second) + " | ";
i++;
}

return node + "[label=\"" + label_header + label + "\", shape=\"record\"]\n";
}

};


Expand Down Expand Up @@ -106,6 +127,44 @@ namespace almo {
return json;
}

// 部分木を dot 言語に変換する.
virtual std::string to_dot() const {
std::map<std::string, std::string> properties = get_properties();

std::string node = properties["uuid"];
std::string label = "";

std::string label_header = "<f0> " + properties["class"] + " | ";

int i = 1;
for (auto property : properties) {
if (property.first == "class" || property.first == "uuid") {
continue;
}
label += "<f" + std::to_string(i) + "> " + property.first + ": " + escape(property.second) + " | ";
i++;
}

// 子ノードを追加する
std::string childs_dot = "";
for (auto child : childs) {
if (child->is_leaf()) {
childs_dot += std::dynamic_pointer_cast<LeafNode>(child)->to_dot();
}
else {
childs_dot += std::dynamic_pointer_cast<NonLeafNode>(child)->to_dot();
}
}

// 子ノードと繋ぐ
std::string edges = "";
for (auto child : childs) {
edges += node + ":f" + std::to_string(edges.length()) + " -> " + child->get_properties()["uuid"] + "\n";
}

return node + "[label=\"" + label_header + label + "\", shape=\"record\"]\n" + childs_dot + edges;
}

};

// H1~6 に対応する構文木のノード
Expand Down
9 changes: 9 additions & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ std::string escape(std::string s) {
else if (s[i] == '\\') {
result += "\\\\";
}
else if (s[i] == '\r') {
result += "\\r";
}
else if (s[i] == '{') {
result += "\\{";
}
else if (s[i] == '}') {
result += "\\}";
}
else {
result += s[i];
}
Expand Down

0 comments on commit 32c8c2b

Please sign in to comment.