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