sammine-lang
Loading...
Searching...
No Matches
Token.h
Go to the documentation of this file.
1#pragma once
2#include "util/Utilities.h"
3#include <map>
4#include <memory>
5#include <string>
6
9
10namespace sammine_lang {
11enum TokenType {
12 // TODO: Add void keyword for return
13 TokADD, // +
14 TokSUB, // -
15 TokMUL, // *
16 TokDIV, // /
17 TokMOD, // %
18
19 TokAddAssign, // +=
20 TokAddIncr, // ++
21 TokSubAssign, // -=
22 TokSubDecr, // --
23 TokMulAssign, // *=
24 TokDivAssign, // /=
25
26 TokAND, // &&
27 TokAndLogical, // &
28 TokOR, // ||
29 TokORLogical, // |
30 TokXOR, // ^
31 TokSHL, // <<
32 TokSHR, // >>
33
34 TokEQUAL, // ==
35 TokLESS, // <
36 TokLessEqual, // <=
37
38 TokGREATER, // >
39 TokGreaterEqual, // >=
40
41 TokASSIGN, // =
42 TokNOT, // !
43
44 // TokEXP AND FloorDiv
45 TokEXP, // **
46 TokFloorDiv, // /_
47 TokCeilDiv, // /^
48
49 // TokPAREN
50 TokLeftParen, // (
51 TokRightParen, // )
52 TokLeftCurly, // {
53 TokRightCurly, // }
54
55 // Comma and colons and all that
56 TokComma, // ,
57 TokDot, // .
58 TokSemiColon, // ;
59 TokColon, // :
60 TokDoubleColon, // ::
61
62 // TokFunction
63 TokReturn,
64 TokFunc, // fn
65 TokRecord, // record
66 TokArrow, // ->
67 TokLet, // let
68 TokExtern, // extern
69 // TokID
70 TokID, // Representing an identifier
71 TokStr, // Representing a string
72 // TokNum
73 TokNum, // Representing a number
74 TokTrue, // Representing a boolean true
75 TokFalse, // Representing a boolean false
76 // TokIf
77 TokIf, // if
78 TokElse, // else
79
80 // TokCOMMENTS
81 TokSingleComment, //
82 TokEOF,
83 TokINVALID,
84};
85
86static const std::map<TokenType, std::string> TokenMap = {
87 {TokADD, "+"},
88 {TokSUB, "-"},
89 {TokMUL, "*"},
90 {TokDIV, "/"},
91 {TokMOD, "%"},
92
93 {TokAddAssign, "+="},
94 {TokAddIncr, "++"},
95 {TokSubAssign, "-="},
96 {TokSubDecr, "--"},
97 {TokMulAssign, "*="},
98 {TokDivAssign, "/="},
99
100 {TokAND, "&&"},
101 {TokAndLogical, "&"},
102 {TokOR, "||"},
103 {TokORLogical, "|"},
104 {TokXOR, "^"},
105 {TokSHL, ">>"},
106 {TokSHR, "<<"},
107 {TokEQUAL, "=="},
108 {TokLESS, "<"},
109 {TokLessEqual, "<="},
110
111 {TokGREATER, ">"},
112 {TokGreaterEqual, ">="},
113
114 {TokASSIGN, "="},
115 {TokNOT, "!"},
116 {TokEXP, "**"},
117 {TokFloorDiv, "/_"},
118 {TokCeilDiv, "/^"},
119
120 {TokLeftParen, "("},
121 {TokRightParen, ")"},
122 {TokLeftCurly, "{"},
123 {TokRightCurly, "}"},
124
125 {TokComma, ","},
126 {TokDot, "."},
127 {TokSemiColon, ";"},
128 {TokColon, ":"},
129 {TokDoubleColon, "::"},
130
131 // TokFunction
132 {TokReturn, "return"},
133 {TokFunc, "fn"},
134 {TokArrow, "->"},
135 {TokLet, "let"},
136 {TokRecord, "Record"},
137 {TokID, "identifier"},
138
139 {TokNum, "number"},
140
141 {TokIf, "if"},
142 {TokElse, "else"},
143
144 // TokCOMMENTS
145 {TokSingleComment, "#"},
146 {TokEOF, "EOF"},
147 {TokINVALID, "UNRECOGNIZED"},
148};
149
152
155class Token {
156 using Location = sammine_util::Location;
157
158public:
159 TokenType tok_type;
160 std::string lexeme;
161 Location location;
162 Token() = delete;
163 Token(TokenType type, std::string lexeme, Location location)
164 : tok_type(type), lexeme(std::move(lexeme)), location(location) {}
165 bool is_comparison() {
166 return tok_type == TokLESS || tok_type == TokGreaterEqual ||
167 tok_type == TokLessEqual || tok_type == TokGREATER ||
168 tok_type == TokEQUAL;
169 }
170 bool is_logical() { return tok_type == TokOR || tok_type == TokAND; }
171 Location get_location() const { return this->location; }
172};
173
175
178class TokenStream {
179 std::vector<std::shared_ptr<Token>> TokStream;
180 size_t current_index;
181 bool error;
182
183public:
184 std::vector<std::shared_ptr<Token>> ErrStream;
185
186 TokenStream() : TokStream(), current_index(0), error(false) {}
187
188 void push_back(const std::shared_ptr<Token> &token) {
189 if (token->tok_type == TokINVALID) {
190 error = true;
191 ErrStream.push_back(token);
192 } else {
193 TokStream.push_back(token);
194 }
195 }
196
197 bool hasErrors() { return error; }
198
199 void push_back(const Token &token) {
200 this->push_back(std::make_shared<Token>(token));
201 }
202
203 std::shared_ptr<Token> &exhaust_until(TokenType tokType) {
204 if (tokType == TokenType::TokEOF) {
205 current_index = TokStream.size() - 1;
206 return TokStream.back();
207 }
208 while (!isEnd()) {
209 if (TokStream[current_index]->tok_type == tokType)
210 return TokStream[current_index++];
211 else
212 current_index++;
213 }
214
215 return TokStream.back();
216 }
217
218 bool isEnd() { return current_index >= (TokStream.size() - 1); }
219 std::shared_ptr<Token> peek() { return TokStream[current_index]; };
220 std::shared_ptr<Token> consume() {
221 auto token = peek();
222 current_index = std::min(TokStream.size() - 1, current_index + 1);
223 return token;
224 }
225
226 sammine_util::Location currentLocation() {
227 if (!TokStream.empty()) {
228 return TokStream[current_index]->get_location();
229 }
230 return {};
231 }
232};
233} // namespace sammine_lang
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
Definition Token.h:155
Definition Utilities.h:51