sapphirec  0.1
The Sapphire compiler.
Token.h
1 #pragma once
2 #include "Location.h"
3 #include "sapphirepch.h"
4 
5 /* All current token types. Will change in the future. */
6 enum TokenType
7 {
8  TT_Identifier,
9  TT_Number,
10  TT_Float,
11  TT_Keyword,
12  TT_String,
13  TT_Plus,
14  TT_Minus,
15  TT_Mul,
16  TT_Div,
17  TT_At,
18  TT_Equal,
19  TT_LessThan,
20  TT_GreaterThan,
21  TT_LParen,
22  TT_RParen,
23  TT_LBracket,
24  TT_RBracket,
25  TT_Semicolon,
26  TT_LoadedString,
27  TT_EOF,
28  TT_Null,
29  TT_Equals,
30  TT_GTE,
31  TT_LTE,
32  TT_Period,
33  TT_Comma,
34  TT_Path,
35  TT_Exclamation,
36  TT_NEqual,
37  TT_LSQB,
38  TT_RSQB,
39  TT_Type,
40  TT_Import,
41  TT_Syscall0,
42  TT_Syscall1,
43  TT_Syscall2,
44  TT_Syscall3,
45  TT_Syscall4,
46  TT_Syscall5,
47  TT_CompilerMacro
48 };
49 
50 extern const std::string token_strings[];
51 
52 /* Struct to represent tokens generated by the Lexer. */
53 struct Token
54 {
55  TokenType tk_type;
56 
57  int int_value;
58  std::string string_value;
59  float float_value;
60 
61  Location loc;
62 
63  Token(const TokenType& type);
64 
65  Token(const TokenType& type, const Location& location);
66 
67  Token(const TokenType& type, const int& val, const Location& location);
68 
69  Token(const TokenType& type, const std::string& val, const Location& location);
70 
71  Token(const TokenType& type, const std::string& val);
72 
73  Token(const TokenType& type, const float& val, const Location& location);
74 
75  ~Token();
76 
77  /* Return a string representation of the Token's contents. */
78  std::string to_string() const;
79 
80  /* Return the contents of the line where the Token was located. */
81  std::string line() const;
82 
83  /* Return a copy of the original token, but adding the contents of the line where
84  the token was located. */
85  static Token make_with_line(const Token& origin, const std::string& line_text);
86 
87  void operator=(const Token& other);
88 
89  /* Convert the Token into a blank token (does not delete it), so that the Normalizer can remove it afterwards.
90  This is to not alter vectors while iterating over them. */
91  static void erase(Token& tk);
92 
93  /* Return a copy of this Token, but with its TokenType changed. */
94  Token copy_with_new_type(const TokenType& type);
95 
96  /* Iterate over two vectors of Tokens, starting from count for vector A, starting from 0 for vector B, checking if
97  the current Tokens' types match. If at any point they don't, return false. Else, return true. */
98  static bool match_token_types(const std::vector<Token>& a, const std::vector<Token>& b, int count);
99 
100  private:
101  std::string line_text;
102 };
103 
104 /* typedef to make it easier to see a what a std::vector of tokens is being used for. */
105 typedef std::vector<Token> TokenStream;
Definition: Location.h:6
Definition: Token.h:54