docs: document token
This commit is contained in:
@@ -10,44 +10,48 @@
|
|||||||
namespace furc {
|
namespace furc {
|
||||||
namespace front {
|
namespace front {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Token type.
|
||||||
|
*/
|
||||||
enum class token_t {
|
enum class token_t {
|
||||||
None,
|
None, /**< None */
|
||||||
Identifier,
|
Identifier, /**< Identifier */
|
||||||
String,
|
String, /**< String */
|
||||||
Keyword,
|
Keyword, /**< Keyword */
|
||||||
Integer,
|
Integer, /**< Integer */
|
||||||
LParen,
|
|
||||||
RParen,
|
|
||||||
LBrace,
|
|
||||||
RBrace,
|
|
||||||
LBracket,
|
|
||||||
RBracket,
|
|
||||||
Semicolon,
|
|
||||||
Colon,
|
|
||||||
Comma,
|
|
||||||
Dot,
|
|
||||||
|
|
||||||
Plus,
|
LParen, /**< `(` */
|
||||||
Minus,
|
RParen, /**< `)` */
|
||||||
Star,
|
LBrace, /**< `{` */
|
||||||
Slash,
|
RBrace, /**< `}` */
|
||||||
Percent,
|
LBracket, /**< `[` */
|
||||||
DPlus,
|
RBracket, /**< `]` */
|
||||||
DMinus,
|
Semicolon, /**< `;` */
|
||||||
|
Colon, /**< `:` */
|
||||||
|
Comma, /**< `,` */
|
||||||
|
Dot, /**< `.` */
|
||||||
|
|
||||||
Eq,
|
Plus, /**< `+` */
|
||||||
PlusEq,
|
Minus, /**< `-` */
|
||||||
MinusEq,
|
Star, /**< `*` */
|
||||||
StarEq,
|
Slash, /**< `/` */
|
||||||
SlashEq,
|
Percent, /**< `%` */
|
||||||
PercentEq,
|
DPlus, /**< `++` */
|
||||||
|
DMinus, /**< `--` */
|
||||||
|
|
||||||
DEq,
|
Eq, /**< `=` */
|
||||||
NotEq,
|
PlusEq, /**< `+=` */
|
||||||
LessThan,
|
MinusEq, /**< `-=` */
|
||||||
GreaterThan,
|
StarEq, /**< `*=` */
|
||||||
LessEq,
|
SlashEq, /**< `/=` */
|
||||||
GreaterEq,
|
PercentEq, /**< `%=` */
|
||||||
|
|
||||||
|
DEq, /**< `==` */
|
||||||
|
NotEq, /**< `!=` */
|
||||||
|
LessThan, /**< `<` */
|
||||||
|
GreaterThan, /**< `>` */
|
||||||
|
LessEq, /**< `<=` */
|
||||||
|
GreaterEq, /**< `>=` */
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline std::ostream& operator<<(std::ostream& os, token_t type) {
|
static inline std::ostream& operator<<(std::ostream& os, token_t type) {
|
||||||
@@ -130,12 +134,15 @@ static inline std::string operator+(const std::string& str, token_t type) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Keyword token.
|
||||||
|
*/
|
||||||
enum class keyword_token {
|
enum class keyword_token {
|
||||||
None,
|
None, /**< None */
|
||||||
Func,
|
Func, /**< `func` */
|
||||||
Return,
|
Return, /**< `return` */
|
||||||
If,
|
If, /**< `if` */
|
||||||
Else,
|
Else, /**< `else` */
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
||||||
@@ -160,43 +167,130 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
using integer_token = std::uint64_t;
|
using integer_token = std::uint64_t; /**< Integer token. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Token.
|
||||||
|
*/
|
||||||
struct token {
|
struct token {
|
||||||
token_t type = token_t::None;
|
token_t type = token_t::None; /**< Token type. */
|
||||||
|
/**
|
||||||
|
* @brief Token's value.
|
||||||
|
*/
|
||||||
union value {
|
union value {
|
||||||
|
/**
|
||||||
|
* @brief Null value. For token_t::None, token_t::Plus, token_t::Minus, etc.
|
||||||
|
* @see token_t::None
|
||||||
|
*/
|
||||||
std::nullptr_t null;
|
std::nullptr_t null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief String value. For token_t::Identifier and token_t::String.
|
||||||
|
* @see token_t::Identifier
|
||||||
|
* @see token_t::String
|
||||||
|
*/
|
||||||
std::string_view string;
|
std::string_view string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Keyword value. For token_t::Keyword.
|
||||||
|
* @see token_t::Keyword.
|
||||||
|
*/
|
||||||
keyword_token keyword;
|
keyword_token keyword;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Integer value. For token_t::Integer.
|
||||||
|
* @see token_t::Integer
|
||||||
|
*/
|
||||||
integer_token integer;
|
integer_token integer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new null value.
|
||||||
|
*
|
||||||
|
* @param value Null value.
|
||||||
|
*/
|
||||||
value(std::nullptr_t value = nullptr)
|
value(std::nullptr_t value = nullptr)
|
||||||
: null(value) {}
|
: null(value) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new string value.
|
||||||
|
*
|
||||||
|
* @param value The string.
|
||||||
|
*/
|
||||||
value(std::string_view value)
|
value(std::string_view value)
|
||||||
: string(value) {}
|
: string(value) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new keyword value.
|
||||||
|
*
|
||||||
|
* @param value The keyword.
|
||||||
|
*/
|
||||||
value(keyword_token value)
|
value(keyword_token value)
|
||||||
: keyword(value) {}
|
: keyword(value) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new integer value.
|
||||||
|
*
|
||||||
|
* @param value The integer.
|
||||||
|
*/
|
||||||
value(integer_token value)
|
value(integer_token value)
|
||||||
: integer(value) {}
|
: integer(value) {}
|
||||||
} value;
|
} value; /**< Token value. */
|
||||||
|
|
||||||
token() = default;
|
token() = default;
|
||||||
|
|
||||||
token(token_t type, std::string_view value = {})
|
/**
|
||||||
|
* @brief Construct a new null token.
|
||||||
|
*
|
||||||
|
* @param type Token's type.
|
||||||
|
*/
|
||||||
|
token(token_t type)
|
||||||
|
: type(type) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new string token.
|
||||||
|
*
|
||||||
|
* @param type Token's type.
|
||||||
|
* @param value String value.
|
||||||
|
*/
|
||||||
|
token(token_t type, std::string_view value)
|
||||||
: type(type), value(value) {}
|
: type(type), value(value) {}
|
||||||
|
|
||||||
token(keyword_token keyword, std::string_view value = {})
|
/**
|
||||||
|
* @brief Construct a new keyword token.
|
||||||
|
*
|
||||||
|
* @param keyword Keyword value.
|
||||||
|
*/
|
||||||
|
token(keyword_token keyword)
|
||||||
: type(token_t::Keyword), value(keyword) {}
|
: type(token_t::Keyword), value(keyword) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new integer token.
|
||||||
|
*
|
||||||
|
* @param integer Integer value.
|
||||||
|
*/
|
||||||
token(integer_token integer)
|
token(integer_token integer)
|
||||||
: type(token_t::Integer), value(integer) {}
|
: type(token_t::Integer), value(integer) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns pointer to this token's value.
|
||||||
|
*
|
||||||
|
* @return Pointer to the token's value.
|
||||||
|
*/
|
||||||
union value* operator->() { return &value; }
|
union value* operator->() { return &value; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns constant pointer to this token's value.
|
||||||
|
*
|
||||||
|
* @return Pointer to the token's value.
|
||||||
|
*/
|
||||||
const union value* operator->() const { return &value; }
|
const union value* operator->() const { return &value; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compares two tokens for equality.
|
||||||
|
*
|
||||||
|
* @param rhs Token to compare against.
|
||||||
|
* @return true if the tokens are equal.
|
||||||
|
*/
|
||||||
bool operator==(const token& rhs) const {
|
bool operator==(const token& rhs) const {
|
||||||
if (type != rhs.type) return false;
|
if (type != rhs.type) return false;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -220,7 +314,7 @@ static inline std::ostream& operator<<(std::ostream& os, const token& token) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Error = std::string>
|
template <typename Error = std::string>
|
||||||
using token_handle = handle<token, Error>;
|
using token_handle = handle<token, Error>; /**< Alias to handle of token. */
|
||||||
|
|
||||||
} // namespace front
|
} // namespace front
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ token_handle<> lexer::next_token() {
|
|||||||
{ "else", keyword_token::Else },
|
{ "else", keyword_token::Else },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second, value };
|
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second };
|
||||||
return { location, token_t::Identifier, value };
|
return { location, token_t::Identifier, value };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user