Repository navigation
Expand file tree
/
Copy pathlexer.cpp
More file actions
48 lines (43 loc) · 1.65 KB
/
Copy pathlexer.cpp
File metadata and controls
48 lines (43 loc) · 1.65 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
#include "lexer.h"
#include <iostream>
std::set<std::string> symbols = {"|", ">", "<", ";"};
std::vector<Token> tokenize(std::string input){
LexState currentState = LexState::NORMAL;
std::string currentString;
std::vector<Token> tokens;
for(char character : input){
//logic for quoted strings
if(character == '\'' || character == '\"'){
if(currentState == LexState::NORMAL) currentState = LexState::QUOTE;
else currentState = LexState::NORMAL;
}
else if((character != ' ' && character != ';') || currentState == LexState::QUOTE){
//A = 65, Z = 90, add 32 to convert to lowercase
if((int) character >= 65 && (int) character <= 90){
character = (char) ((int) character + 32); //convert to lowercase
}
currentString += character;
}
else{
if(currentString != "" || character == ';'){
if(currentString != "") addToken(currentString, &tokens);
if(character == ';') addToken(";", &tokens);
currentString = "";
}
}
}
//If input doesn't end with space, the last lexeme won't be recorded. Fix this behavior
if(currentString != ""){
addToken(currentString, &tokens);
}
return tokens;
}
//does not replace currentstring with ""
void addToken(std::string currentString, std::vector<Token>* tokens){
if(symbols.count(currentString) != 0){
(*tokens).push_back({TokenType::SYMBOL, currentString});
}
else{
(*tokens).push_back({TokenType::WORD, currentString});
}
}