summaryrefslogtreecommitdiff
path: root/src/token.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/token.hh')
-rw-r--r--src/token.hh152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/token.hh b/src/token.hh
new file mode 100644
index 0000000..0724bc7
--- /dev/null
+++ b/src/token.hh
@@ -0,0 +1,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