summaryrefslogtreecommitdiff
path: root/src/token.hh
blob: 0724bc7b1875a00ea188eec87cb84fb794b327b8 (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
// 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 TOKEN_HH
#define TOKEN_HH

#include <string>
#include <vector>

#include "utility.hh"

namespace cait {

struct token;

using token_tree = std::vector<std::vector<token>>;

// https://stackoverflow.com/a/53284026/14452787
class token_type {
public:
  enum token_type_value {
    rule_declaration,
    rule_command,
    rule_depfile,
    rule_deps,
    rule_msvc_deps_prefix,
    rule_description,
    rule_dyndep,
    rule_generator,
    rule_in,
    rule_in_newline,
    rule_out,
    rule_restat,
    rule_rspfile,
    rule_rspfile_content,
    build_declaration,
    build_input,
    build_rule,
    build_output,
    default_declaration,
    default_target,
    string_literal,
    variable_declaration,
    variable_value,
    assignment_operator,
    identifier,
    pool_declaration,
    pool_depth,
    subninja_declaration,
    include_declaration
  };

private:
  token_type_value value;

public:
  token_type() = default;
  constexpr token_type(token_type_value _value) : value(_value) {}
  [[nodiscard]] constexpr std::string string() const {
    switch (this->value) {
    case rule_declaration:
      return "rule_declaration";
    case rule_command:
      return "rule_command";
    case rule_depfile:
      return "rule_depfile";
    case rule_deps:
      return "rule_deps";
    case rule_msvc_deps_prefix:
      return "rule_msvc_deps_prefix";
    case rule_description:
      return "rule_description";
    case rule_dyndep:
      return "rule_dyndep";
    case rule_generator:
      return "rule_generator";
    case rule_in:
      return "rule_in";
    case rule_in_newline:
      return "rule_in_newline";
    case rule_out:
      return "rule_out";
    case rule_restat:
      return "rule_restat";
    case rule_rspfile:
      return "rule_rspfile";
    case rule_rspfile_content:
      return "rule_rspfile_content";
    case build_declaration:
      return "build_declaration";
    case build_input:
      return "build_input";
    case build_rule:
      return "build_rule";
    case default_declaration:
      return "default_declaration";
    case default_target:
      return "default_target";
    case string_literal:
      return "string_literal";
    case variable_declaration:
      return "variable_declaration";
    case variable_value:
      return "variable_value";
    case assignment_operator:
      return "assignment_operator";
    case identifier:
      return "identifier";
    case build_output:
      return "build_output";
    case pool_declaration:
      return "pool_declaration";
    case pool_depth:
      return "pool_depth";
    case subninja_declaration:
      return "subninja_declaration";
    case include_declaration:
      return "include_declaration";
    }
  }
  constexpr operator token_type_value() const { return this->value; }
};

struct token {
  std::size_t line;
  [[maybe_unused]] std::size_t begin;
  [[maybe_unused]] std::size_t end;
  std::string word;
  token_type type{};

  token(std::size_t, std::size_t, std::size_t, const std::string &);
  auto with_type(token_type) -> token;
};

} // namespace cait

#endif // TOKEN_HH