sapphirec
The Sapphire documentation
Token.h
Go to the documentation of this file.
1 #pragma once
2 #include "Location.h"
3 #include "sapphirepch.h"
4 
5 /* All current token types. Will change in the future. */
7 {
48 };
49 
50 extern const std::string token_strings[];
51 
52 /* Struct to represent tokens generated by the Lexer. */
53 struct Token
54 {
56 
57  int int_value;
58  std::string string_value;
59  float float_value;
60 
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;
const std::string token_strings[]
Definition: Token.cpp:4
std::vector< Token > TokenStream
Definition: Token.h:105
TokenType
Definition: Token.h:7
@ TT_Syscall5
Definition: Token.h:46
@ TT_LBracket
Definition: Token.h:23
@ TT_LessThan
Definition: Token.h:19
@ TT_RParen
Definition: Token.h:22
@ TT_Semicolon
Definition: Token.h:25
@ TT_Period
Definition: Token.h:32
@ TT_Equal
Definition: Token.h:18
@ TT_Minus
Definition: Token.h:14
@ TT_Syscall2
Definition: Token.h:43
@ TT_Keyword
Definition: Token.h:11
@ TT_Syscall4
Definition: Token.h:45
@ TT_NEqual
Definition: Token.h:36
@ TT_RBracket
Definition: Token.h:24
@ TT_GreaterThan
Definition: Token.h:20
@ TT_GTE
Definition: Token.h:30
@ TT_At
Definition: Token.h:17
@ TT_Type
Definition: Token.h:39
@ TT_Div
Definition: Token.h:16
@ TT_LSQB
Definition: Token.h:37
@ TT_Path
Definition: Token.h:34
@ TT_Mul
Definition: Token.h:15
@ TT_Syscall1
Definition: Token.h:42
@ TT_Plus
Definition: Token.h:13
@ TT_Identifier
Definition: Token.h:8
@ TT_LoadedString
Definition: Token.h:26
@ TT_Syscall3
Definition: Token.h:44
@ TT_LParen
Definition: Token.h:21
@ TT_Syscall0
Definition: Token.h:41
@ TT_Equals
Definition: Token.h:29
@ TT_Null
Definition: Token.h:28
@ TT_EOF
Definition: Token.h:27
@ TT_Exclamation
Definition: Token.h:35
@ TT_LTE
Definition: Token.h:31
@ TT_Import
Definition: Token.h:40
@ TT_String
Definition: Token.h:12
@ TT_Comma
Definition: Token.h:33
@ TT_CompilerMacro
Definition: Token.h:47
@ TT_Float
Definition: Token.h:10
@ TT_Number
Definition: Token.h:9
@ TT_RSQB
Definition: Token.h:38
Definition: Location.h:6
Definition: Token.h:54
Token copy_with_new_type(const TokenType &type)
Definition: Token.cpp:44
int int_value
Definition: Token.h:57
static bool match_token_types(const std::vector< Token > &a, const std::vector< Token > &b, int count)
Definition: Token.cpp:189
std::string string_value
Definition: Token.h:58
static void erase(Token &tk)
Definition: Token.cpp:184
Location loc
Definition: Token.h:61
~Token()
Definition: Token.cpp:40
std::string line() const
Definition: Token.cpp:157
std::string to_string() const
Definition: Token.cpp:57
static Token make_with_line(const Token &origin, const std::string &line_text)
Definition: Token.cpp:162
float float_value
Definition: Token.h:59
void operator=(const Token &other)
Definition: Token.cpp:175
Token(const TokenType &type)
Definition: Token.cpp:13
TokenType tk_type
Definition: Token.h:55