-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTokenizer.cpp
More file actions
63 lines (55 loc) · 1.09 KB
/
Copy pathTokenizer.cpp
File metadata and controls
63 lines (55 loc) · 1.09 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
#include "Tokenizer.h"
#include "Expression.h"
#include <cctype>
Tokenizer::Tokenizer(char* data) : str(std::string(data)) {
}
NumberExpression* Tokenizer::number()
{
this->skipWhiteSpace();
char ch = str.peek();
if (std::isdigit(ch)) {
double x;
str >> x;
return new NumberExpression(x);
}
return nullptr;
}
VariableExpression* Tokenizer::variable() {
this->skipWhiteSpace();
char ch = str.peek();
std::string s;
while (std::isalpha(ch)) {
ch = str.get();
s += ch;
ch = str.peek();
}
if (s.length() > 0)
return new VariableExpression(s);
return nullptr;
}
bool Tokenizer::character(char expected) {
this->skipWhiteSpace();
char ch = str.peek();
if (ch == expected) {
ch = str.get();
return true;
}
return false;
}
int Tokenizer::mark() {
return str.tellg();
}
bool Tokenizer::atEnd() {
return str.eof();
}
void Tokenizer::reset(int mark) {
str.clear();
str.seekg(mark);
}
void Tokenizer::skipWhiteSpace() {
char ch = str.peek();
while (ch == ' ' || ch == '\t') {
ch = str.get();
ch = str.peek();
}
}