Repository navigation
Expand file tree
/
Copy pathLexer.cpp
More file actions
88 lines (73 loc) · 1.99 KB
/
Copy pathLexer.cpp
File metadata and controls
88 lines (73 loc) · 1.99 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
#include "Lexer.h"
#include <print>
Lexer::Lexer(std::string &content) : content(content) {
this->start = 0;
this->currentPos = 0;
this->terms = {};
this->terms.reserve(content.size() / 2);
}
void Lexer::scanNumber() {
while (isNumber(peek())) currentPos++;
if (peek() == '.' && this->isNumber(peekNext())) {
currentPos++;
while (isNumber(peek())) currentPos++;
}
this->terms.emplace_back(content.substr(start, currentPos - start));
}
void Lexer::scanWord() {
while (isAlphabet(peek()) || isNumber(peek()) || peek() == '_' ||
peek() == '\'')
currentPos++;
this->terms.emplace_back(content.substr(start, currentPos - start));
}
bool Lexer::isAtEOF() {
if (this->content[currentPos] == '\0') return true;
return false;
}
char Lexer::peek() {
if (this->isAtEOF()) return '\0';
return this->content[currentPos];
}
char Lexer::peekNext() {
if (this->isAtEOF()) return '\0';
return this->content[currentPos + 1];
}
bool Lexer::isNumber(const char &c) {
if (c >= '0' && c <= '9') return true;
return false;
}
bool Lexer::isAlphabet(const char &c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true;
return false;
}
std::vector<std::string> Lexer::process() {
while (!this->isAtEOF()) {
this->start = this->currentPos;
char c = content[this->currentPos++];
switch (c) {
// Remove spaces
case ' ':
case '\n':
case '\r':
case '\t':
break;
// Remove common punctuations
case '.':
case '/':
case ',':
case ';':
break;
default: {
if (isNumber(c)) {
scanNumber();
break;
}
if (isAlphabet(c)) {
scanWord();
break;
}
}
}
}
return this->terms;
}