-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cpp
143 lines (128 loc) · 4.92 KB
/
Lexer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Created by nisal on 7/19/2023.
//
#include "Lexer.h"
std::unordered_set<std::string> keywords = {
"let", "where", "within", "aug", "fn", "in"};
std::unordered_set<std::string> operators = {
"and", "or", "not", "gr", "ge", "ls", "le", "eq", "ne"};
std::unordered_set<std::string> booleanValues = {
"true", "false"};
// NOLINTNEXTLINE
Token Lexer::getNextToken() {
skipWhitespace();
if (currentPosition >= input.length()) {
// Check if it is the last empty line or end of input
if (currentPosition == input.length())
return {token_type::END_OF_FILE, ""};
else
return {token_type::DELIMITER, ""};
}
char currentChar = input[currentPosition++];
if (isalpha(currentChar)) {
std::stringstream ss;
ss << currentChar;
while (currentPosition < input.length() && (isalnum(input[currentPosition]) || input[currentPosition] == '_')) {
ss << input[currentPosition++];
}
std::string identifier = ss.str();
// Check if the identifier is a keyword
if (keywords.count(identifier) > 0) {
return {token_type::KEYWORD, identifier};
} else if (operators.count(identifier) > 0) {
return {token_type::OPERATOR, identifier};
} else if (booleanValues.count(identifier) > 0) {
if (identifier == "true")
return {token_type::INTEGER, "1"};
else
return {token_type::INTEGER, "0"};
}
return {token_type::IDENTIFIER, identifier};
} else if (isdigit(currentChar)) {
std::stringstream ss;
ss << currentChar;
while (currentPosition < input.length() && isdigit(input[currentPosition])) {
ss << input[currentPosition++];
}
std::string number = ss.str();
return {token_type::INTEGER, number};
} else if (currentChar == '/') {
if (currentPosition < input.length() && input[currentPosition] == '/') {
// Skip single-line comment
while (currentPosition < input.length() && input[currentPosition] != '\n') {
currentPosition++;
}
// Recursively call getNextToken to get the next valid token
return getNextToken();
} else if (isOperatorSymbol(currentChar)) {
std::stringstream ss;
ss << currentChar;
while (currentPosition < input.length() && isOperatorSymbol(input[currentPosition])) {
ss << input[currentPosition++];
}
std::string op = ss.str();
return {token_type::OPERATOR, op};
} else {
std::cerr << "Error: Unknown token encountered" << std::endl;
return {token_type::END_OF_FILE, ""};
}
} else if (isOperatorSymbol(currentChar)) {
std::stringstream ss;
ss << currentChar;
if (ss.str() == ",") {
return {token_type::OPERATOR, ss.str()};
}
while (currentPosition < input.length() && isOperatorSymbol(input[currentPosition])) {
ss << input[currentPosition++];
}
std::string op = ss.str();
return {token_type::OPERATOR, op};
} else if (currentChar == '\'' || currentChar == '"') {
bool isSingleQuote = currentChar == '\'';
std::stringstream ss;
while (currentPosition < input.length()) {
currentChar = input[currentPosition++];
if (currentChar == '\'' && isSingleQuote || currentChar == '"' && !isSingleQuote) {
break;
} else if (currentChar == '\\') {
currentChar = input[currentPosition++];
switch (currentChar) {
case 't':
ss << '\t';
break;
case 'n':
ss << '\n';
break;
case '\\':
ss << '\\';
break;
case '\'':
ss << '\'';
break;
default:
ss << '\\' << currentChar;
break;
}
} else {
ss << currentChar;
}
}
std::string str = ss.str();
return {token_type::STRING, str};
} else if (currentChar == '(' || currentChar == ')') {
std::string delimiter(1, currentChar);
return {token_type::DELIMITER, delimiter};
} else {
std::cerr << "Error: Unknown token encountered" << std::endl;
return {token_type::END_OF_FILE, ""};
}
}
void Lexer::skipWhitespace() {
while (currentPosition < input.length() && isspace(input[currentPosition])) {
currentPosition++;
}
}
bool Lexer::isOperatorSymbol(char c) {
static const std::string operators = "+-*<>&.@/:=~|$!#%^_[}{?,";
return operators.find(c) != std::string::npos;
}