aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/v2script/SimpleGrammar.hpp
blob: 9bddabd0ab25b4c65fd54327ae6503201810b4b8 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once

#include <memory>
#include <string>
#include <vector>

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

#include <lexy/callback.hpp>
#include <lexy/dsl.hpp>

#include "detail/LexyLitRange.hpp"

// Grammar Definitions //
/* REQUIREMENTS:
 * DAT-626
 * DAT-627
 * DAT-628
 * DAT-636
 * DAT-641
 * DAT-642
 * DAT-643
 */
namespace ovdl::v2script::grammar {
   struct ParseOptions {
      /// @brief Makes string parsing avoid string escapes
      bool NoStringEscape;
   };

   static constexpr ParseOptions NoStringEscapeOption = ParseOptions { true };
   static constexpr ParseOptions StringEscapeOption = ParseOptions { false };

   template<ParseOptions Options>
   struct StatementListBlock;
   template<ParseOptions Options>
   struct AssignmentStatement;

   /* REQUIREMENTS: DAT-630 */
   static constexpr auto whitespace_specifier = lexy::dsl::ascii::blank / lexy::dsl::ascii::newline;
   /* REQUIREMENTS: DAT-631 */
   static constexpr auto comment_specifier = LEXY_LIT("#") >> lexy::dsl::until(lexy::dsl::newline).or_eof();

   /* REQUIREMENTS:
    * DAT-632
    * DAT-635
    */
   static constexpr auto data_specifier =
      lexy::dsl::ascii::alpha_digit_underscore / LEXY_ASCII_ONE_OF("+:@%&'-.") /
      lexy::dsl::lit_b<0x8A> / lexy::dsl::lit_b<0x8C> / lexy::dsl::lit_b<0x8E> /
      lexy::dsl::lit_b<0x92> / lexy::dsl::lit_b<0x97> / lexy::dsl::lit_b<0x9A> / lexy::dsl::lit_b<0x9C> /
      detail::lexydsl::make_range<0x9E, 0x9F>() /
      detail::lexydsl::make_range<0xC0, 0xD6>() /
      detail::lexydsl::make_range<0xD8, 0xF6>() /
      detail::lexydsl::make_range<0xF8, 0xFF>();

   static constexpr auto data_char_class = LEXY_CHAR_CLASS("DataSpecifier", data_specifier);

   static constexpr auto escaped_symbols = lexy::symbol_table<char> //
                                    .map<'"'>('"')
                                    .map<'\''>('\'')
                                    .map<'\\'>('\\')
                                    .map<'/'>('/')
                                    .map<'b'>('\b')
                                    .map<'f'>('\f')
                                    .map<'n'>('\n')
                                    .map<'r'>('\r')
                                    .map<'t'>('\t');

   template<ParseOptions Options>
   struct Identifier {
      static constexpr auto rule = lexy::dsl::identifier(data_char_class);
      static constexpr auto value = lexy::callback<ast::NodePtr>(
         [](auto lexeme) {
            std::string str(lexeme.data(), lexeme.size());
            return ast::make_node_ptr<ast::IdentifierNode>(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str));
         });
   };

   /* REQUIREMENTS:
    * DAT-633
    * DAT-634
    */
   template<ParseOptions Options>
   struct StringExpression {
      static constexpr auto rule = [] {
         // Arbitrary code points that aren't control characters.
         auto c = ovdl::detail::lexydsl::make_range<0x20, 0xFF>() - lexy::dsl::ascii::control;

         if constexpr (Options.NoStringEscape) {
            return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c);
         } else {
            // Escape sequences start with a backlash.
            // They either map one of the symbols,
            // or a Unicode code point of the form uXXXX.
            auto escape = lexy::dsl::backslash_escape //
                          .symbol<escaped_symbols>();
            return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c, escape);
         }
      }();

      static constexpr auto value =
         lexy::as_string<std::string> >>
         lexy::callback<ast::NodePtr>(
            [](const char* begin, auto&& str, const char* end) {
               return ast::make_node_ptr<ast::StringNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(str));
            });
   };

   /* REQUIREMENTS: DAT-638 */
   template<ParseOptions Options>
   struct ValueExpression {
      static constexpr auto rule = lexy::dsl::p<Identifier<Options>> | lexy::dsl::p<StringExpression<Options>>;
      static constexpr auto value = lexy::forward<ast::NodePtr>;
   };

   template<ParseOptions Options>
   struct SimpleAssignmentStatement {
      static constexpr auto rule =
         lexy::dsl::position(lexy::dsl::p<Identifier<Options>>) >>
         (lexy::dsl::equal_sign +
            (lexy::dsl::p<ValueExpression<Options>> | lexy::dsl::recurse_branch<StatementListBlock<Options>>));

      static constexpr auto value = lexy::callback<ast::NodePtr>(
         [](const char* pos, auto name, auto&& initalizer) {
            return ast::make_node_ptr<ast::AssignNode>(pos, LEXY_MOV(name), LEXY_MOV(initalizer));
         });
   };

   /* REQUIREMENTS: DAT-639 */
   template<ParseOptions Options>
   struct AssignmentStatement {
      static constexpr auto rule =
         lexy::dsl::position(lexy::dsl::p<Identifier<Options>>) >>
            (lexy::dsl::equal_sign >>
                  (lexy::dsl::p<ValueExpression<Options>> | lexy::dsl::recurse_branch<StatementListBlock<Options>>) |
               lexy::dsl::else_ >> lexy::dsl::return_) |
         lexy::dsl::p<StringExpression<Options>> |
         lexy::dsl::recurse_branch<StatementListBlock<Options>>;

      static constexpr auto value = lexy::callback<ast::NodePtr>(
         [](const char* pos, auto name, lexy::nullopt = {}) {
            return LEXY_MOV(name);
         },
         [](auto name, lexy::nullopt = {}) {
            return LEXY_MOV(name);
         },
         [](const char* pos, auto name, auto&& initalizer) {
            return ast::make_node_ptr<ast::AssignNode>(pos, LEXY_MOV(name), LEXY_MOV(initalizer));
         });
   };

   /* REQUIREMENTS: DAT-640 */
   template<ParseOptions Options>
   struct StatementListBlock {
      static constexpr auto rule =
         lexy::dsl::position(lexy::dsl::curly_bracketed.open()) >>
         (lexy::dsl::opt(lexy::dsl::list(lexy::dsl::recurse_branch<AssignmentStatement<Options>>)) +
            lexy::dsl::opt(lexy::dsl::semicolon)) >>
         lexy::dsl::position(lexy::dsl::curly_bracketed.close());

      static constexpr auto value =
         lexy::as_list<std::vector<ast::NodePtr>> >>
         lexy::callback<ast::NodePtr>(
            [](const char* begin, lexy::nullopt, const char* end) {
               return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end));
            },
            [](const char* begin, auto&& list, const char* end) {
               return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list));
            },
            [](const char* begin, lexy::nullopt, lexy::nullopt, const char* end) {
               return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end));
            },
            [](const char* begin, auto&& list, lexy::nullopt, const char* end) {
               return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list));
            },
            [](const char* begin, auto& list, const char* end) {
               return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), list);
            });
   };

   template<ParseOptions Options>
   struct File {
      // Allow arbitrary spaces between individual tokens.
      static constexpr auto whitespace = whitespace_specifier | comment_specifier;

      static constexpr auto rule = lexy::dsl::position + lexy::dsl::terminator(lexy::dsl::eof).opt_list(lexy::dsl::p<AssignmentStatement<Options>>);

      static constexpr auto value = lexy::as_list<std::vector<ast::NodePtr>> >> lexy::new_<ast::FileNode, ast::NodePtr>;
   };
}