aboutsummaryrefslogtreecommitdiff
path: root/tests/src/v2script/AbstractSyntaxTree.cpp
blob: c06da08913b2b49ddebcbd0433da091d59ab18f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <string_view>
#include <type_traits>

#include <openvic-dataloader/NodeLocation.hpp>
#include <openvic-dataloader/detail/SymbolIntern.hpp>
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>

#include <dryad/node_map.hpp>
#include <dryad/tree.hpp>

#include "Helper.hpp"
#include <range/v3/iterator/operations.hpp>
#include <range/v3/view/enumerate.hpp>
#include <range/v3/view/zip.hpp>
#include <snitch/snitch.hpp>

using namespace ovdl;
using namespace ovdl::v2script::ast;
using namespace std::string_view_literals;

struct Ast : SymbolIntern {
   dryad::tree<FileTree> tree;
   dryad::node_map<const Node, NodeLocation> map;
   symbol_interner_type symbol_interner;

   template<typename T, typename... Args>
   T* create(Args&&... args) {
      auto node = tree.template create<T>(DRYAD_FWD(args)...);
      return node;
   }

   template<typename T, typename... Args>
   T* create_with_intern(std::string_view view, Args&&... args) {
      auto intern = symbol_interner.intern(view.data(), view.size());
      auto node = tree.template create<T>(intern, DRYAD_FWD(args)...);
      return node;
   }

   template<typename T, typename... Args>
   T* create_with_loc(NodeLocation loc, Args&&... args) {
      auto node = tree.template create<T>(DRYAD_FWD(args)...);
      set_location(node, loc);
      return node;
   }

   template<typename T, typename... Args>
   T* create_with_loc_and_intern(NodeLocation loc, std::string_view view, Args&&... args) {
      auto intern = symbol_interner.intern(view.data(), view.size());
      auto node = tree.template create<T>(intern, DRYAD_FWD(args)...);
      set_location(node, loc);
      return node;
   }

   void set_location(const Node* n, NodeLocation loc) {
      map.insert(n, loc);
   }

   NodeLocation location_of(const Node* n) const {
      auto result = map.lookup(n);
      if (result == nullptr)
         return {};
      return *result;
   }
};

TEST_CASE("V2Script Nodes", "[v2script-nodes]") {
   Ast ast;

   auto* id = ast.create_with_intern<IdentifierValue>("id");
   CHECK_IF(id) {
      CHECK(id->kind() == NodeKind::IdentifierValue);
      CHECK(id->value(ast.symbol_interner) == "id"sv);
   }

   auto* str = ast.create_with_intern<StringValue>("str");
   CHECK_IF(str) {
      CHECK(str->kind() == NodeKind::StringValue);
      CHECK(str->value(ast.symbol_interner) == "str"sv);
   }

   auto* list = ast.create<ListValue>();
   CHECK_IF(list) {
      CHECK(list->kind() == NodeKind::ListValue);
   }

   auto* null = ast.create<NullValue>();
   CHECK_IF(null) {
      CHECK(null->kind() == NodeKind::NullValue);
   }

   auto* event = ast.create<EventStatement>(false, list);
   CHECK_IF(event) {
      CHECK(event->kind() == NodeKind::EventStatement);
      CHECK(!event->is_province_event());
      CHECK(event->right() == list);
   }

   auto* assign = ast.create<AssignStatement>(id, str);
   CHECK_IF(assign) {
      CHECK(assign->kind() == NodeKind::AssignStatement);
      CHECK(assign->left() == id);
      CHECK(assign->right() == str);
   }

   auto* value_statement = ast.create<ValueStatement>(null);
   CHECK_IF(value_statement) {
      CHECK(value_statement->kind() == NodeKind::ValueStatement);
      CHECK(value_statement->value() == null);
   }

   if (!assign || !value_statement || !event || !null || !list || !str || !id) {
      return;
   }

   StatementList statement_list {};
   statement_list.push_back(assign);
   statement_list.push_back(value_statement);
   statement_list.push_back(event);
   CHECK_FALSE(statement_list.empty());
   CHECK(ranges::distance(statement_list) == 3);

   for (const auto [statement_list_index, statement] : statement_list | ranges::views::enumerate) {
      CAPTURE(statement_list_index);
      CHECK_OR_CONTINUE(statement);
      switch (statement_list_index) {
         case 0: CHECK_OR_CONTINUE(statement == assign); break;
         case 1: CHECK_OR_CONTINUE(statement == value_statement); break;
         case 2: CHECK_OR_CONTINUE(statement == event); break;
         default: CHECK_OR_CONTINUE(false); break;
      }
   }

   auto* file_tree = ast.create<FileTree>(statement_list);
   CHECK_IF(file_tree) {
      CHECK(file_tree->kind() == NodeKind::FileTree);

      const auto statements = file_tree->statements();
      CHECK_FALSE(statements.empty());
      CHECK(ranges::distance(statements) == 3);

      for (const auto [statement, list_statement] : ranges::views::zip(statements, statement_list)) {
         CHECK_OR_CONTINUE(list_statement);
         CHECK_OR_CONTINUE(statement);
         CHECK_OR_CONTINUE(statement == list_statement);
      }
   }

   ast.tree.set_root(file_tree);
   CHECK(ast.tree.has_root());
   CHECK(ast.tree.root() == file_tree);

   ast.tree.clear();
   CHECK_FALSE(ast.tree.has_root());
   CHECK(ast.tree.root() != file_tree);
}

TEST_CASE("V2Script Nodes Location", "[v2script-nodes-location]") {
   Ast ast;

   constexpr auto fake_buffer = "id"sv;

   auto* id = ast.create_with_loc_and_intern<IdentifierValue>(NodeLocation::make_from(&fake_buffer[0], &fake_buffer[1]), "id");

   CHECK_IF(id) {
      CHECK(id->value(ast.symbol_interner) == "id"sv);

      auto location = ast.location_of(id);
      CHECK_FALSE(location.is_synthesized());
      CHECK(location.begin() == &fake_buffer[0]);
      CHECK(location.end() - 1 == &fake_buffer[1]);
   }
}