summaryrefslogtreecommitdiff
path: root/src/grammar.cc
blob: c737408378fc907ab57014a3861cd6ccfb686983 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "grammar.hh"

#include "errors.hh"
#include "line.hh"
#include "location.hh"
#include "str.hh"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <format>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace grammar {

namespace {

class GrammarImpl : public Grammar {
 public:
  explicit GrammarImpl(std::vector<std::unique_ptr<Element>> elements)
      : elements_(std::move(elements)) {}

  [[nodiscard]]
  Element const& root() const override {
    return *elements_[0];
  }

 private:
  std::vector<std::unique_ptr<Element>> elements_;
};

struct FirstPassElement {
  src::Location loc;
  std::vector<std::string> definitions;

  explicit FirstPassElement(src::Location loc) : loc(loc) {}
};

class GrammarLoader {
 public:
  GrammarLoader(std::unique_ptr<io::Reader> reader,
                std::vector<std::string> const& character_classes,
                src::Errors& errors)
      : reader_(line::open(std::move(reader))),
        character_classes_(character_classes),
        errors_(errors) {}

  std::unique_ptr<Grammar> load() {
    // Read whole file in a first pass, before parsing definitions.
    std::map<std::string, FirstPassElement> first_pass_elements;
    FirstPassElement* last_element = nullptr;

    std::map<std::string_view, uint8_t, std::less<>> cc_lookup;
    for (uint8_t i = 0; i < static_cast<uint8_t>(character_classes_.size());
         ++i) {
      cc_lookup.emplace(character_classes_[i], i);
    }

    while (true) {
      auto line = reader_->read();
      auto loc = src::Location(reader_->number(), 0);
      if (!line.has_value()) {
        if (line.error() == io::ReadError::Eof)
          break;
        errors_.err(loc, "Error reading");
        return nullptr;
      }
      if (line.value().empty() || line.value().front() == '#')
        continue;
      if (line.value().front() == ' ') {
        // Continue on last element
        size_t i = 1;
        while (i < line.value().size() && line.value()[i] == ' ')
          ++i;
        if (i == line.value().size()) {
          errors_.err(loc, "Unexpected line, only spaces");
          continue;
        }
        if (last_element == nullptr) {
          errors_.err(loc, "Expected element before indented lines");
          continue;
        }
        last_element->definitions.emplace_back(line.value().substr(i));
      } else {
        // New element
        if (line.value().back() != ':') {
          errors_.err(
              loc,
              "Unexpected line, not indented but also not ending with a ':'");
          continue;
        }

        auto name = line.value().substr(0, line.value().size() - 1);
        if (cc_lookup.contains(name)) {
          errors_.warn(
              loc, std::format("Element {} overrides character class", name));
        }
        auto pair = first_pass_elements.emplace(name, FirstPassElement(loc));
        if (!pair.second) {
          errors_.err(loc, std::format("Duplicate element {}", name));
        }
        last_element = &pair.first->second;
      }
    }

    if (first_pass_elements.empty()) {
      errors_.err(src::Location(reader_->number(), 0), "No elements found");
      return nullptr;
    }

    std::vector<std::unique_ptr<Element>> second_pass_elements;
    std::map<std::string_view, size_t, std::less<>> second_pass_lookup;
    for (auto const& pair : first_pass_elements) {
      auto element = std::make_unique<Element>();
      element->name = pair.first;
      second_pass_lookup.emplace(element->name, second_pass_elements.size());
      second_pass_elements.emplace_back(std::move(element));
    }

    size_t i = 0;
    for (auto const& pair : first_pass_elements) {
      for (auto const& in_definition : pair.second.definitions) {
        auto out_symbols =
            parse_definition(pair.second.loc, second_pass_elements,
                             second_pass_lookup, cc_lookup, in_definition);

        if (out_symbols.empty()) {
          errors_.err(pair.second.loc, "no symbols found in definition");
          continue;
        }

        second_pass_elements[i]->definitions.emplace_back(
            Definition{.symbols = std::move(out_symbols)});
      }
      ++i;
    }

    // Find root and move it first (if needed)
    std::vector<size_t> used(second_pass_elements.size(), 0);
    for (auto const& element : second_pass_elements) {
      for (auto const& definition : element->definitions) {
        for (auto const& symbol : definition.symbols) {
          switch (symbol.type) {
            case Symbol::Type::kTerminal:
            case Symbol::Type::kCharacterClass:
              break;
            case Symbol::Type::kNonTerminal:
              if (!symbol.element->name.empty())
                used[second_pass_lookup.find(symbol.element->name)->second]++;
              break;
          }
        }
      }
    }

    std::optional<size_t> root_index;
    for (size_t i = 0; i < used.size(); ++i) {
      if (used[i] == 0 && !second_pass_elements[i]->name.empty()) {
        if (root_index.has_value()) {
          errors_.warn(first_pass_elements.find(second_pass_elements[i]->name)
                           ->second.loc,
                       std::format("{} is not referenced but also not root",
                                   second_pass_elements[i]->name));
        } else {
          root_index = i;
        }
      }
    }

    if (root_index.has_value()) {
      if (root_index.value() != 0) {
        std::swap(second_pass_elements[0],
                  second_pass_elements[root_index.value()]);
      }
    } else {
      errors_.err(
          first_pass_elements.find(second_pass_elements[0]->name)->second.loc,
          "No root element found");
    }

    optimize(second_pass_elements);

    return std::make_unique<GrammarImpl>(std::move(second_pass_elements));
  }

 private:
  static void optimize(std::vector<std::unique_ptr<Element>> const& elements) {
    merge_terminals(elements);
  }

  static void merge_terminals(
      std::vector<std::unique_ptr<Element>> const& elements) {
    for (auto const& element : elements) {
      for (auto& definition : element->definitions) {
        auto it = definition.symbols.begin();
        while (it != definition.symbols.end()) {
          if (it->type != Symbol::Type::kTerminal) {
            ++it;
            continue;
          }

          auto it2 = it + 1;
          if (it2 == definition.symbols.end())
            break;
          if (it2->type != Symbol::Type::kTerminal ||
              it->optional != it2->optional) {
            ++it;
            continue;
          }

          it->value += it2->value;
          definition.symbols.erase(it2);
        }
      }
    }
  }

  std::vector<Symbol> parse_definition(
      src::Location const& loc,
      std::vector<std::unique_ptr<Element>>& non_terminal,
      std::map<std::string_view, size_t, std::less<>>& non_terminal_lookup,
      std::map<std::string_view, uint8_t, std::less<>> const& cc_lookup,
      std::string_view in_definition) {
    std::vector<Symbol> out_symbols;
    bool exclude = false;
    bool expect_not = false;
    while (!in_definition.empty()) {
      auto symbol = parse_symbol(loc, non_terminal, non_terminal_lookup,
                                 cc_lookup, in_definition, exclude, expect_not);
      if (symbol.has_value()) {
        out_symbols.push_back(std::move(symbol.value()));
      }
    }
    return out_symbols;
  }

  std::optional<Symbol> parse_symbol(
      src::Location const& loc,
      std::vector<std::unique_ptr<Element>>& non_terminal,
      std::map<std::string_view, size_t, std::less<>>& non_terminal_lookup,
      std::map<std::string_view, uint8_t, std::less<>> const& cc_lookup,
      std::string_view& input, bool& exclude, bool& expect_not,
      char extra_terminator = '\0') {
    Symbol out_symbol;

    if ((input[0] == '{' || input[0] == '[') && input.size() > 1 &&
        input[1] != ' ') {
      char end = input[0] == '{' ? '}' : ']';
      std::vector<Symbol> inner_symbols;
      bool inner_exclude = false;
      bool inner_expect_not = false;
      input = input.substr(1);
      while (!input.empty() && input.front() != end) {
        auto symbol =
            parse_symbol(loc, non_terminal, non_terminal_lookup, cc_lookup,
                         input, inner_exclude, inner_expect_not, end);
        if (symbol.has_value()) {
          inner_symbols.push_back(std::move(symbol.value()));
        }
      }

      if (input.empty()) {
        errors_.err(loc, "unclosed sub-symbol");
        return std::nullopt;
      }
      if (input.size() == 1 || input[1] != ' ') {
        input = input.substr(1);
      } else {
        input = input.substr(2);
      }

      if (inner_symbols.empty()) {
        errors_.err(loc, "empty sub-symbol");
        return std::nullopt;
      }
      if (inner_symbols.size() == 1) {
        out_symbol = inner_symbols[0];
      } else {
        auto anon_element = std::make_unique<Element>();
        anon_element->definitions.emplace_back(
            Definition{.symbols = std::move(inner_symbols)});
        non_terminal.push_back(std::move(anon_element));
        out_symbol.type = Symbol::Type::kNonTerminal;
        out_symbol.element = non_terminal.back().get();
      }

      if (exclude) {
        errors_.err(loc, "Optional and exclude doesn't work together");
      } else if (end == '}') {
        out_symbol.optional = Symbol::Optional::kZeroOrMore;
      } else /* if (end == '[') */ {
        out_symbol.optional = Symbol::Optional::kZeroOrOne;
      }

      return out_symbol;
    }

    std::string_view::size_type end;
    if (extra_terminator) {
      char tmp[2];
      tmp[0] = extra_terminator;
      tmp[1] = ' ';
      end = input.find_first_of(std::string_view{tmp, 2});
    } else {
      end = input.find(' ');
    }
    std::string_view in_symbol;
    if (end == std::string_view::npos) {
      in_symbol = input;
      input = std::string_view{};
    } else {
      in_symbol = input.substr(0, end);
      if (input[end] == ' ') {
        input = input.substr(end + 1);
      } else {
        input = input.substr(end);
      }
    }

    if (exclude) {
      if (in_symbol == "or")
        return std::nullopt;
      out_symbol.optional = Symbol::Optional::kExcluded;
    } else if (expect_not) {
      expect_not = false;
      if (in_symbol == "not") {
        exclude = true;
      } else {
        errors_.err(loc, "but is not followed by not");
      }
      return std::nullopt;
    }
    if (in_symbol == "but") {
      expect_not = true;
      return std::nullopt;
    }
    auto it2 = non_terminal_lookup.find(in_symbol);
    if (it2 != non_terminal_lookup.end()) {
      out_symbol.type = Symbol::Type::kNonTerminal;
      out_symbol.element = non_terminal[it2->second].get();
    } else {
      auto it3 = cc_lookup.find(in_symbol);
      if (it3 != cc_lookup.end()) {
        out_symbol.type = Symbol::Type::kCharacterClass;
        out_symbol.char_class = it3->second;
      } else {
        out_symbol.type = Symbol::Type::kTerminal;
        out_symbol.value = in_symbol;
      }
    }
    return out_symbol;
  }

  std::unique_ptr<line::Reader> reader_;
  std::vector<std::string> const& character_classes_;
  src::Errors& errors_;
};

}  // namespace

std::unique_ptr<Grammar> load(std::unique_ptr<io::Reader> reader,
                              std::vector<std::string> const& character_classes,
                              src::Errors& errors) {
  GrammarLoader loader(std::move(reader), character_classes, errors);
  return loader.load();
}

}  // namespace grammar