-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.cpp
More file actions
186 lines (156 loc) · 5.5 KB
/
lex.cpp
File metadata and controls
186 lines (156 loc) · 5.5 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "lex.hpp"
#include <cctype>
#include <iostream>
#include <stdexcept>
std::unordered_set<char> Lexer::whitespaces = {' ', '\t', '\n', '\f', '\r', '\v'};
Lexer::Lexer(std::string content){
this->content = content;
this->position = this->content.begin();
tokens = { };
}
bool Lexer::positionValid(){
if (position < content.end() ){
return true;
}
return false;
}
void Lexer::parse(){
while (positionValid()){
if (! consumePredefinedTokenIfPresent() ){
switch(Token::identifyNonPredefinedTokenType(*position)){
case TokenType::IDENTIFER:
consumeIdentifier();
break;
case TokenType::INTEGER:
consumeInteger();
break;
case TokenType::CHAR:
consumeChar();
break;
case TokenType::STRING:
consumeString();
case TokenType::COMMENT_1:
consumeCommentOne();
break;
case TokenType::COMMENT_2:
consumeCommentTwo();
break;
default:
break;
}
}
consumeWhitespaceIfPresent();
}
}
void Lexer::consumeWhitespaceIfPresent(){
while (positionValid() && whitespaces.count(*position)){
position++ ;
}
}
bool Lexer::consumePredefinedTokenIfPresent(){
// Identify whether any of the predefined tokens match with the sequence of characters
// following the current pointer location
for (auto it: Token::predefined_tokens){
std::string token_value = it.second;
int token_length = token_value.size();
if (position+token_length < content.end()){
std::string content_slice (position, position+token_length);
if (content_slice == token_value){
// Now that the token has been recognized in the text,
// Check whether it's possibly a piece of an identifier isntead (eg- "orange" = "or"+"range")
// Condition - If token is all alphabetic, the next character also shouldn't be.
bool isIdentifier = true;
for (int i=0; i<token_length+1; ++i){
if (!isalpha(*(position+i))){
isIdentifier = false;
}
}
if (isIdentifier){
continue; // moves onto the next predefined token type
}
position += token_length;
tokens.push_back( Token(it.first) );
return true;
}
}
}
return false;
}
void Lexer::consumeIdentifier(){
// position iterator is on the first letter of the identifier, which was already checked
std::string::iterator temp { position };
position ++;
while (positionValid() && (isalnum(*position) || *position=='_' )){
position++;
}
tokens.push_back( Token(TokenType::IDENTIFER, std::string(temp, position)) );
}
void Lexer::consumeInteger(){
// position iterator is on the first letter of the integer, which was already checked
std::string::iterator temp { position };
position ++;
while (positionValid() && isdigit(*position) ){
position++;
}
tokens.push_back( Token(TokenType::INTEGER, std::string(temp, position)) );
}
void Lexer::consumeChar(){
// position iterator is on the first quote of the char, which was already checked
std::string::iterator temp { position };
position ++;
if (positionValid() && *position =='\''){
throw std::runtime_error("Invalid char token \n");
}
position ++;
if (positionValid() && *position !='\''){
throw std::runtime_error("Invalid char token \n");
}
if (!positionValid()){
throw std::runtime_error("EOF reached while parsing char \n");
}
tokens.push_back( Token(TokenType::CHAR, std::string(temp, temp+3)) );
position ++;
}
void Lexer::consumeString(){
// position iterator is on the first double quote of the string, which was already checked
std::string::iterator temp { position };
position ++;
while (positionValid() && *position!='"'){
position ++;
}
if (!positionValid()){
throw std::runtime_error("EOF reached while parsing string \n");
}
if (*position == '"'){
tokens.push_back( Token(TokenType::STRING, std::string(temp+1, position)) );
position++;
}
}
void Lexer::consumeCommentOne(){
// position iterator is on the first letter of the identifier, which was already checked
std::string::iterator temp { position };
position ++;
while (positionValid() && *position != '\n'){
position++;
}
// Whether it's now at EOF or a newline, the comment is complete
tokens.push_back( Token(TokenType::COMMENT_1, std::string(temp, position)) );
}
void Lexer::consumeCommentTwo(){
// position iterator is on the first letter of the identifier, which was already checked
std::string::iterator temp { position };
position ++;
while (positionValid() && *position!='}'){
position++;
}
if (!positionValid()){
throw std::runtime_error("EOF reached while parsing multiline comment \n");
}
if (*position == '}'){
tokens.push_back( Token(TokenType::COMMENT_2, std::string(temp, position+1)) );
position++;
}
}
std::vector<Token> Lexer::getTokenSequence(){
return tokens;
}