tan  0.0.1
token.h
1 #ifndef TAN_LEXDEF_H
2 #define TAN_LEXDEF_H
3 
4 #include "base.h"
5 #include <array>
6 
7 namespace tanlang {
8 
9 enum class TokenType {
10  END = -1, /// EOF
11  COMMENTS, ///
12  KEYWORD, ///
13  INT, ///
14  FLOAT, ///
15  ID, /// identifier
16  CHAR, /// character
17  STRING, /// string literal
18  PUNCTUATION, /// , ; . ( ) { } etc.
19  RELOP, /// relational operator
20  UOP, /// unary operator
21  BOP, /// binary operator
22 };
23 
24 extern umap<TokenType, str> token_type_names;
25 extern const vector<str> KEYWORDS;
26 extern const vector<char> PUNCTUATIONS;
27 extern const vector<str> TERMINAL_TOKENS;
28 // any symbol in OP can both be an operator itself or the start of an operator
29 extern const vector<char> OP;
30 extern const vector<str> OP_ALL;
31 extern umap<str, TokenType> OPERATION_VALUE_TYPE_MAP;
32 
33 class Token {
34 public:
35  static SourceSpan GetSourceSpan(const Token &start, const Token &end);
36  static SrcLoc GetSrcLoc(const Token *tok);
37 
38 public:
39  Token() = delete;
40  ~Token() = default;
41  Token(TokenType tokenType, uint32_t line, uint32_t col, str value, SourceFile *src);
42  [[nodiscard]] TokenType get_type() const;
43  void set_type(TokenType type);
44  [[nodiscard]] const str &get_value() const;
45  [[nodiscard]] str get_source_line() const;
46  [[nodiscard]] bool is_unsigned() const;
47  void set_is_unsigned(bool is_unsigned);
48  [[nodiscard]] uint32_t get_line() const;
49  [[nodiscard]] uint32_t get_col() const;
50  SourceFile *src() const;
51 
52 private:
53  TokenType _type = TokenType::END;
54  str _value{};
55  uint32_t _line = 0;
56  uint32_t _col = 0;
57  bool _is_unsigned = false;
58  SourceFile *_src;
59 };
60 
61 } // namespace tanlang
62 
63 #endif /*TAN_LEXDEF_H*/
A span of source code tokens, inclusive on both ends.
Definition: source_file.h:96