|
| 1 | +// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com> |
| 2 | +// This file is subject to the license terms in the LICENSE file |
| 3 | +// found in the top-level directory of this distribution. |
| 4 | + |
| 5 | +#ifndef CPPAST_CPP_TOKEN_HPP_INCLUDED |
| 6 | +#define CPPAST_CPP_TOKEN_HPP_INCLUDED |
| 7 | + |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include <type_safe/reference.hpp> |
| 12 | + |
| 13 | +namespace cppast |
| 14 | +{ |
| 15 | + /// The kinds of C++ tokens. |
| 16 | + enum class cpp_token_kind |
| 17 | + { |
| 18 | + identifier, //< Any identifier. |
| 19 | + keyword, //< Any keyword. |
| 20 | + literal, //< Any literal. |
| 21 | + punctuation, //< Any other punctuation. |
| 22 | + |
| 23 | + unknown, //< An unknown token. |
| 24 | + }; |
| 25 | + |
| 26 | + /// A C++ token. |
| 27 | + struct cpp_token |
| 28 | + { |
| 29 | + std::string spelling; |
| 30 | + cpp_token_kind kind; |
| 31 | + |
| 32 | + cpp_token(cpp_token_kind kind, std::string spelling) |
| 33 | + : spelling(std::move(spelling)), kind(kind) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + friend bool operator==(const cpp_token& lhs, const cpp_token& rhs) noexcept |
| 38 | + { |
| 39 | + return lhs.spelling == rhs.spelling; |
| 40 | + } |
| 41 | + |
| 42 | + friend bool operator!=(const cpp_token& lhs, const cpp_token& rhs) noexcept |
| 43 | + { |
| 44 | + return !(rhs == lhs); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + /// A combination of multiple C++ tokens. |
| 49 | + class cpp_token_string |
| 50 | + { |
| 51 | + public: |
| 52 | + /// Builds a token string. |
| 53 | + class builder |
| 54 | + { |
| 55 | + public: |
| 56 | + builder() = default; |
| 57 | + |
| 58 | + /// \effects Adds a token. |
| 59 | + void add_token(cpp_token tok) |
| 60 | + { |
| 61 | + tokens_.push_back(std::move(tok)); |
| 62 | + } |
| 63 | + |
| 64 | + /// \effects Converts a trailing `>>` to `>` token. |
| 65 | + void unmunch(); |
| 66 | + |
| 67 | + /// \returns The finished string. |
| 68 | + cpp_token_string finish() |
| 69 | + { |
| 70 | + return cpp_token_string(std::move(tokens_)); |
| 71 | + } |
| 72 | + |
| 73 | + private: |
| 74 | + std::vector<cpp_token> tokens_; |
| 75 | + }; |
| 76 | + |
| 77 | + /// \effects Creates it from a sequence of tokens. |
| 78 | + cpp_token_string(std::vector<cpp_token> tokens) : tokens_(std::move(tokens)) {} |
| 79 | + |
| 80 | + /// \effects Creates from a string. |
| 81 | + /// \notes This does not do tokenization, it will only store a single, unknown token! |
| 82 | + static cpp_token_string from_string(std::string str) |
| 83 | + { |
| 84 | + return cpp_token_string({cpp_token(cpp_token_kind::unknown, std::move(str))}); |
| 85 | + } |
| 86 | + |
| 87 | + /// \exclude target |
| 88 | + using iterator = std::vector<cpp_token>::const_iterator; |
| 89 | + |
| 90 | + /// \returns An iterator to the first token. |
| 91 | + iterator begin() const noexcept |
| 92 | + { |
| 93 | + return tokens_.begin(); |
| 94 | + } |
| 95 | + |
| 96 | + /// \returns An iterator one past the last token. |
| 97 | + iterator end() const noexcept |
| 98 | + { |
| 99 | + return tokens_.end(); |
| 100 | + } |
| 101 | + |
| 102 | + /// \returns Whether or not the string is empty. |
| 103 | + bool empty() const noexcept |
| 104 | + { |
| 105 | + return tokens_.empty(); |
| 106 | + } |
| 107 | + |
| 108 | + /// \returns A reference to the first token. |
| 109 | + const cpp_token& front() const noexcept |
| 110 | + { |
| 111 | + return tokens_.front(); |
| 112 | + } |
| 113 | + |
| 114 | + /// \returns A reference to the last token. |
| 115 | + const cpp_token& back() const noexcept |
| 116 | + { |
| 117 | + return tokens_.back(); |
| 118 | + } |
| 119 | + |
| 120 | + /// \returns The string representation of the tokens, without any whitespace. |
| 121 | + std::string as_string() const; |
| 122 | + |
| 123 | + private: |
| 124 | + std::vector<cpp_token> tokens_; |
| 125 | + |
| 126 | + friend bool operator==(const cpp_token_string& lhs, const cpp_token_string& rhs); |
| 127 | + }; |
| 128 | + |
| 129 | + bool operator==(const cpp_token_string& lhs, const cpp_token_string& rhs); |
| 130 | + |
| 131 | + inline bool operator!=(const cpp_token_string& lhs, const cpp_token_string& rhs) |
| 132 | + { |
| 133 | + return !(lhs == rhs); |
| 134 | + } |
| 135 | +} // namespace cppast |
| 136 | + |
| 137 | +#endif // CPPAST_CPP_TOKEN_HPP_INCLUDED |
0 commit comments