summaryrefslogtreecommitdiff
path: root/src/parser.hh
blob: 5d1cdb45c569689a9a85224b3110955131f548f9 (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
// This file is part of Cait <https://github.com/Fuwn/cait>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

#ifndef PARSER_HH
#define PARSER_HH

#include <exception>
#include <stdexcept>

#include "lexer.hh"
#include "node.hh"

namespace cait {

using node_tree = std::vector<node::node_set>;

class parser_exception : public std::runtime_error {
public:
  explicit parser_exception(const std::string &);
};

struct token_bundle {
private:
  using token_t = token;

public:
  std::size_t &j;
  token_t &token;
  std::vector<token_t> &parsed_token_line;
  std::vector<token_t> &token_line;

  token_bundle(std::size_t &, token_t &, std::vector<token_t> &,
               std::vector<token_t> &);
};

// https://stackoverflow.com/a/25652548/14452787
template <typename T> struct range_t {
private:
  T _begin, _end;

public:
  range_t(T, T);
  T begin();
  T end();
};

// https://stackoverflow.com/a/25652548/14452787
template <typename T> range_t<T> range(T, T);

class parser {
private:
  token_tree _tree;
  node_tree _nodes;

  auto fix_tree(token_tree &) -> void;
  auto prune_empty_token_lines() -> void;
  static auto token_parse_build_declaration(token_bundle) -> void;
  static auto token_parse_rule_variable(token_bundle) -> void;
  static auto token_parse_include_subninja_declaration(token_bundle) -> void;
  static auto token_parse_default_declaration(token_bundle) -> void;
  static auto token_parse_rule_pool_declaration(token_bundle) -> void;
  static auto token_parse_assignment_operator(token_bundle) -> void;

public:
  explicit parser(token_tree &);
  [[nodiscard]] auto tree() const noexcept -> const token_tree &;
  [[nodiscard]] auto nodes() const noexcept -> const node_tree &;
  auto generate_nodes(const std::string &,
                      const std::vector<std::string> &) noexcept(false) -> void;
};

} // namespace cait

#endif // PARSER_HH