-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.h
93 lines (79 loc) · 2.6 KB
/
Token.h
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
#ifndef TITANPLUSPLUS_TOKEN_H
#define TITANPLUSPLUS_TOKEN_H
/**
* @file Token.h
* @author Bryn McKerracher
* @date 8/10/2021
* @brief Contains the Token class header.
*/
#include <cstdint>
#include <string>
/**
* An object for holding and accessing information about a single lexeme.
*
* This is a utility class for the Scanner class's lexical parsing of
* Titan source code. Token objects are a midpoint between raw Titan
* source code and the VM's bytecode.
*
* @class Token
* @brief A utility class for lexical parsing.
*/
class Token {
public:
/**
* A comprehensive list of all tokens supported by the parser.
*
* @enum Type
* @brief A enumeration of all supported token types.
*/
enum Type {
LEFT_PAREN, RIGHT_PAREN,
LEFT_BRACE, RIGHT_BRACE,
COMMA, DOT, MINUS, PLUS,
SEMICOLON, SLASH, STAR,
// One or two character tokens.
BANG, BANG_EQUAL,
EQUAL, EQUAL_EQUAL,
GREATER, GREATER_EQUAL,
LESS, LESS_EQUAL,
// Literals.
IDENTIFIER, STRING, NUMBER, MATRIX,
// Keywords.
AND, CLASS, ELSE, FALSE,
FOR, FUN, IF, NIL, OR,
PRINT, RETURN, SUPER, THIS,
TRUE, VAR, WHILE,
//Errors
ERROR, END_OF_FILE,
//Number of types (Must be last)
SIZE,
};
Token::Type type = Token::Type::ERROR; ///< The type of token this object represents.
size_t start = 0; ///< Pointer to the start character of the token in the source code.
size_t length = 0; ///< The length of the token in ASCII characters.
int line = 0; ///< The line number of the token.
/**
* Creates a token object using the given index for the starting
* character of the token in the source code.
*
* @brief Creates a token object from the source code.
* @param index The index of the Token's starting character in the source code.
* @param titanSource The string of Titan source code to generate a token from.
*/
Token(size_t index, const std::string& titanSource);
/**
* Creates a token.
*
* @brief Constructs a token from the given type.
* @param type The type of token to be constructed.
* @param index The index of the Token's starting character in the source code.
* @param tokenLength The length of the token in ASCII characters.
* @param line The line number of the token.
*/
Token(Token::Type tokenType, size_t index, size_t tokenLength, int tokenLine);
/**
* @brief Default constructor.
*/
Token() = default;
};
#endif //TITANPLUSPLUS_TOKEN_H