diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 85cfdb3..b0da073 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,8 +6,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # Environment variable available in C++ source #------------------------------------------------------------------------------- -set(testing_directory ${CMAKE_SOURCE_DIR}/testing) - +set(test_directory ${CMAKE_SOURCE_DIR}/test) execute_process( COMMAND git rev-list --count HEAD @@ -27,6 +26,7 @@ configure_file( add_subdirectory(translator/example) add_subdirectory(translator/sequence) +add_subdirectory(translator/line_number) add_subdirectory(screen) if (Web) @@ -64,6 +64,7 @@ if (Web) target_link_libraries(main.js translator_example translator_sequence + translator_line_number screen antlr4_static ) @@ -74,6 +75,7 @@ else() target_link_libraries(main translator_example translator_sequence + translator_line_number screen antlr4_static ) @@ -82,6 +84,7 @@ else() target_link_libraries(input_output_test translator_example translator_sequence + translator_line_number screen antlr4_static stdc++fs diff --git a/src/environment.h.in b/src/environment.h.in index c6e67ed..7771132 100644 --- a/src/environment.h.in +++ b/src/environment.h.in @@ -1,2 +1,2 @@ -const char* testing_directory = "@testing_directory@"; +const char* test_directory = "@test_directory@"; const char* git_version = "@git_version@"; diff --git a/src/index.html b/src/index.html index c9bc89e..4a6bee6 100644 --- a/src/index.html +++ b/src/index.html @@ -7,28 +7,53 @@ - Input: - +
+
- Output: - + +
+ + +
- Errors: - +
+ + +
+ +
+ +
+ +
+ + + + +
+
+ + + Some content below. + + - diff --git a/src/input_output_test.cpp b/src/input_output_test.cpp index 97d5a75..b574c0d 100644 --- a/src/input_output_test.cpp +++ b/src/input_output_test.cpp @@ -9,7 +9,7 @@ int main(int, const char**) { int result = 0; - std::string path = testing_directory; + std::string path = test_directory; for (auto& dir : std::experimental::filesystem::directory_iterator(path)) { std::string type = dir.path().filename(); std::cout << "Testing " << type << std::endl; diff --git a/src/main_webassembly.cpp b/src/main_webassembly.cpp index 1aadac2..cbc9b58 100644 --- a/src/main_webassembly.cpp +++ b/src/main_webassembly.cpp @@ -17,10 +17,20 @@ void replaceAll(std::string& str, extern "C" { -void translate(const char* input) { - auto sequence = SequenceTranslator(); - sequence->Process(input); - std::string command = "output.value='" + sequence->Output() + "';"; +void translate(const char* translator_name, const char* input) { + std::string translator_string = translator_name; + std::unique_ptr translator; + if (translator_string == "Example") + translator = ExampleTranslator(); + else if (translator_string == "Sequence") + translator = SequenceTranslator(); + else if (translator_string == "LineNumber") + translator = LineNumberTranslator(); + else + return; + + translator->Process(input); + std::string command = "output.value='" + translator->Output() + "';"; replaceAll(command, "\n", "\\n"); emscripten_run_script(command.c_str()); } diff --git a/src/style.css b/src/style.css index 7f4f29a..fd3b259 100644 --- a/src/style.css +++ b/src/style.css @@ -1,23 +1,73 @@ -#input { - position:absolute; - left:0; - top:0; - width:50%; - height:50%; +/*#input {*/ + /*width:100%;*/ + /*[>position:absolute;<]*/ + /*[>left:0;<]*/ + /*[>top:0;<]*/ + /*[>width:50%;<]*/ + /*[>height:50%;<]*/ +/*}*/ + +/*#output {*/ + /*width:100%;*/ + /*[>position:absolute;<]*/ + /*[>right:0;<]*/ + /*[>top:0;<]*/ + /*[>width:50%;<]*/ + /*[>height:50%;<]*/ +/*}*/ + +/*#errors {*/ + /*width:100%;*/ + /*[>position:absolute;<]*/ + /*[>left:0;<]*/ + /*[>bottom:0;<]*/ + /*[>width:100%;<]*/ + /*[>height:50%;<]*/ +/*}*/ + +html, body { + margin:0; + padding:0; } -#output { - position:absolute; - right:0; - top:0; - width:50%; - height:50%; +.gutter { + min-width:12px; + min-height:12px; } -#errors { - position:absolute; - left:0; - bottom:0; +.container.vertical { + display: flex; + flex-direction: column; +} + +.container.horizontal { + display: flex; + flex-direction: row; +} + +#code_area { width:100%; - height:50%; + height:100vh; + margin:0; + padding:0; +} + +#input, #output, #errors { + display:block; + padding:10px; + background-color:#EEE; + border:solid 1px black; +} + +#input, #output { + flex:1; +} + +#inputoutput { + flex:1; +} + +.fill_space { + width:100%; + height:100%; } diff --git a/src/translator/Translator.h b/src/translator/Translator.h index 2c8d042..206dc18 100644 --- a/src/translator/Translator.h +++ b/src/translator/Translator.h @@ -13,6 +13,7 @@ class Translator { std::unique_ptr ExampleTranslator(); std::unique_ptr SequenceTranslator(); +std::unique_ptr LineNumberTranslator(); #endif /* end of include guard: TRANSLATOR_TRANSLATOR */ diff --git a/src/translator/line_number/CMakeLists.txt b/src/translator/line_number/CMakeLists.txt new file mode 100644 index 0000000..987cb07 --- /dev/null +++ b/src/translator/line_number/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(translator_line_number STATIC + LineNumber.cpp +) diff --git a/src/translator/line_number/LineNumber.cpp b/src/translator/line_number/LineNumber.cpp new file mode 100644 index 0000000..1b9ea02 --- /dev/null +++ b/src/translator/line_number/LineNumber.cpp @@ -0,0 +1,57 @@ +#include +#include + +#include "screen/Screen.h" +#include "translator/Translator.h" + +class LineNumber : public Translator { + public: + virtual ~LineNumber() = default; + virtual void Process(const std::string& input) { + // cut by lines. + std::stringstream ss(input); + std::vector lines; + std::string line; + while (std::getline(ss, line)) { + lines.push_back(to_wstring(line)); + } + + int number_length = 0; + int max_number = 1; + while (max_number <= (int)lines.size()) { + max_number *= 10; + number_length++; + } + + int text_max_width = 0; + for (const auto& line : lines) { + text_max_width = std::max(text_max_width, (int)line.size()); + } + + int width = number_length + text_max_width + 3; + int height = lines.size() + 2; + Screen screen(width, height); + + int y = 1; + for (const auto& line : lines) { + screen.DrawText(number_length + 2, y, line); + screen.DrawText(1, y, to_wstring(std::to_string(y))); + ++y; + } + + screen.DrawBox(0, 0, width, height); + screen.DrawVerticalLine(1, height - 2, number_length + 1); + screen.DrawPixel(number_length + 1, 0, U'┬'); + screen.DrawPixel(number_length + 1, height - 1, U'┴'); + + output_ = screen.ToString(); + } + virtual std::string Output() { return output_; } + + private: + std::string output_; +}; + +std::unique_ptr LineNumberTranslator() { + return std::make_unique(); +} diff --git a/src/translator/sequence/Sequence.cpp b/src/translator/sequence/Sequence.cpp index bd6a9c0..c06377f 100644 --- a/src/translator/sequence/Sequence.cpp +++ b/src/translator/sequence/Sequence.cpp @@ -84,8 +84,8 @@ void Sequence::UniformizeMessageID() { for (auto& message : messages) { if (message.id != -1) { if (used.count(message.id)) { + std::cout << "Found two messages with the same id: " << message.id << std::endl; message.id = -1; - // TODO(arthursonzogni): Add warning. } else { used.insert(message.id); } @@ -107,12 +107,25 @@ void Sequence::UniformizeMessageID() { for (int id : {dependency.from, dependency.to}) { auto it = message_index.find(id); if (it == message_index.end()) { + std::wcout << "* Ignored dependency: \"" << actor.name << ": " + << dependency.from << " < " << dependency.to << "\"." + << std::endl; + std::wcout << " It cannot be used because the message ID \"" << id + << "\" doesn't exist" << std::endl; return true; } const Message& message = messages[it->second]; if (actor.name != message.from && // actor.name != message.to) { + std::wcout << "* Ignored dependency: \"" << actor.name << ": " + << dependency.from << " < " << dependency.to << "\"." + << std::endl; + std::wcout << " It cannot be used because the message \"" + << message.from << " -> " << message.to << ": " + << message.messages[0] + << "\" has nothing to do with actor " << actor.name + << std::endl; return true; } } @@ -291,8 +304,10 @@ void Sequence::LayoutComputeActorsPositions() { modified = true; } } - if (i++ > 500) + if (i++ > 500) { + std::cout << "Something goes wrong!" << std::endl; break; + } } for (auto& actor : actors) { @@ -502,8 +517,10 @@ std::vector FindTopologicalOrder(const Graph& graph) { } iteration++; - if (iteration >= 1000) + if (iteration >= 1000) { + std::cout << "There are cycles" << std::endl; break; + } } std::vector nodes; diff --git a/testing/sequence/basic/input b/test/sequence/basic/input similarity index 100% rename from testing/sequence/basic/input rename to test/sequence/basic/input diff --git a/testing/sequence/basic/output b/test/sequence/basic/output similarity index 100% rename from testing/sequence/basic/output rename to test/sequence/basic/output diff --git a/testing/sequence/change_author_order/input b/test/sequence/change_author_order/input similarity index 100% rename from testing/sequence/change_author_order/input rename to test/sequence/change_author_order/input diff --git a/testing/sequence/change_author_order/output b/test/sequence/change_author_order/output similarity index 100% rename from testing/sequence/change_author_order/output rename to test/sequence/change_author_order/output diff --git a/testing/sequence/change_message_order_1/input b/test/sequence/change_message_order_1/input similarity index 100% rename from testing/sequence/change_message_order_1/input rename to test/sequence/change_message_order_1/input diff --git a/testing/sequence/change_message_order_1/output b/test/sequence/change_message_order_1/output similarity index 100% rename from testing/sequence/change_message_order_1/output rename to test/sequence/change_message_order_1/output diff --git a/testing/sequence/change_message_order_2/input b/test/sequence/change_message_order_2/input similarity index 100% rename from testing/sequence/change_message_order_2/input rename to test/sequence/change_message_order_2/input diff --git a/testing/sequence/change_message_order_2/output b/test/sequence/change_message_order_2/output similarity index 100% rename from testing/sequence/change_message_order_2/output rename to test/sequence/change_message_order_2/output diff --git a/test/sequence/detect_cycles/input b/test/sequence/detect_cycles/input new file mode 100644 index 0000000..4f90218 --- /dev/null +++ b/test/sequence/detect_cycles/input @@ -0,0 +1,7 @@ +1) A -> B: message 1 +3) A -> B: message 4 +2) A -> B: message 2 +4) A -> B: message 4 + +A: 1<2, 3<4 +B: 2<1, 4<3 diff --git a/test/sequence/detect_cycles/output b/test/sequence/detect_cycles/output new file mode 100644 index 0000000..15d46a3 --- /dev/null +++ b/test/sequence/detect_cycles/output @@ -0,0 +1,20 @@ +┌─┐ ┌─┐ +│A│ │B│ +└┬┘ └┬┘ + │ │ + │──┐ │ + │message 2│ + │────────>│ + │ │ │ + │message 1│ + │ └─────>│ + │ │ + │──┐ │ + │message 4│ + │────────>│ + │ │ │ + │message 4│ + │ └─────>│ +┌┴┐ ┌┴┐ +│A│ │B│ +└─┘ └─┘ diff --git a/testing/sequence/interleaved/input b/test/sequence/interleaved/input similarity index 100% rename from testing/sequence/interleaved/input rename to test/sequence/interleaved/input diff --git a/testing/sequence/interleaved/output b/test/sequence/interleaved/output similarity index 100% rename from testing/sequence/interleaved/output rename to test/sequence/interleaved/output diff --git a/testing/sequence/interleaved_complex/input b/test/sequence/interleaved_complex/input similarity index 100% rename from testing/sequence/interleaved_complex/input rename to test/sequence/interleaved_complex/input diff --git a/testing/sequence/interleaved_complex/output b/test/sequence/interleaved_complex/output similarity index 100% rename from testing/sequence/interleaved_complex/output rename to test/sequence/interleaved_complex/output diff --git a/testing/sequence/interleaved_complex_2/input b/test/sequence/interleaved_complex_2/input similarity index 100% rename from testing/sequence/interleaved_complex_2/input rename to test/sequence/interleaved_complex_2/input diff --git a/testing/sequence/interleaved_complex_2/output b/test/sequence/interleaved_complex_2/output similarity index 100% rename from testing/sequence/interleaved_complex_2/output rename to test/sequence/interleaved_complex_2/output diff --git a/testing/sequence/layout_actor_multiple_actors_1/input b/test/sequence/layout_actor_multiple_actors_1/input similarity index 100% rename from testing/sequence/layout_actor_multiple_actors_1/input rename to test/sequence/layout_actor_multiple_actors_1/input diff --git a/testing/sequence/layout_actor_multiple_actors_1/output b/test/sequence/layout_actor_multiple_actors_1/output similarity index 100% rename from testing/sequence/layout_actor_multiple_actors_1/output rename to test/sequence/layout_actor_multiple_actors_1/output diff --git a/testing/sequence/layout_actor_multiple_actors_2/input b/test/sequence/layout_actor_multiple_actors_2/input similarity index 100% rename from testing/sequence/layout_actor_multiple_actors_2/input rename to test/sequence/layout_actor_multiple_actors_2/input diff --git a/testing/sequence/layout_actor_multiple_actors_2/output b/test/sequence/layout_actor_multiple_actors_2/output similarity index 100% rename from testing/sequence/layout_actor_multiple_actors_2/output rename to test/sequence/layout_actor_multiple_actors_2/output diff --git a/testing/sequence/layout_actor_space/input b/test/sequence/layout_actor_space/input similarity index 100% rename from testing/sequence/layout_actor_space/input rename to test/sequence/layout_actor_space/input diff --git a/testing/sequence/layout_actor_space/output b/test/sequence/layout_actor_space/output similarity index 100% rename from testing/sequence/layout_actor_space/output rename to test/sequence/layout_actor_space/output diff --git a/testing/sequence/layout_actor_space_with_messages/input b/test/sequence/layout_actor_space_with_messages/input similarity index 100% rename from testing/sequence/layout_actor_space_with_messages/input rename to test/sequence/layout_actor_space_with_messages/input diff --git a/testing/sequence/layout_actor_space_with_messages/output b/test/sequence/layout_actor_space_with_messages/output similarity index 100% rename from testing/sequence/layout_actor_space_with_messages/output rename to test/sequence/layout_actor_space_with_messages/output diff --git a/test/sequence/reorder_dependencies/input b/test/sequence/reorder_dependencies/input new file mode 100644 index 0000000..b383d98 --- /dev/null +++ b/test/sequence/reorder_dependencies/input @@ -0,0 +1,14 @@ +7) B<-C: message 7 +2) B->C: message 2 +5) D<-E: message 5 +8) A<-B: message 8 +1) A->B: message 1 +3) C->D: message 3 +4) D->E: message 4 +6) C<-D: message 6 + +A: +B: 1<2, 7<8 +C: 2<3, 6<7 +D: 3<4, 5<6 +E: 4<5 diff --git a/test/sequence/reorder_dependencies/output b/test/sequence/reorder_dependencies/output new file mode 100644 index 0000000..a991a18 --- /dev/null +++ b/test/sequence/reorder_dependencies/output @@ -0,0 +1,30 @@ +┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ +│A│ │B│ │C│ │D│ │E│ +└┬┘ └┬┘ └┬┘ └┬┘ └┬┘ + │ │ │ │ │ + │message 1│ │ │ │ + │────────>│ │ │ │ + │ │ │ │ │ + │ │message 2│ │ │ + │ │────────>│ │ │ + │ │ │ │ │ + │ │ │message 3│ │ + │ │ │────────>│ │ + │ │ │ │ │ + │ │ │ │message 4│ + │ │ │ │────────>│ + │ │ │ │ │ + │ │ │ │message 5│ + │ │ │ │<────────│ + │ │ │ │ │ + │ │ │message 6│ │ + │ │ │<────────│ │ + │ │ │ │ │ + │ │message 7│ │ │ + │ │<────────│ │ │ + │ │ │ │ │ + │message 8│ │ │ │ + │<────────│ │ │ │ +┌┴┐ ┌┴┐ ┌┴┐ ┌┴┐ ┌┴┐ +│A│ │B│ │C│ │D│ │E│ +└─┘ └─┘ └─┘ └─┘ └─┘