blob: 2472f6da372135488d6dfd7102bbc6dc2253a9a9 (
plain)
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 "parser.h"
#include <iostream>
#include <string>
#include <vector>
#include "token.h"
Parser::Parser() { }
bool isNumber(const std::string& str) {
for (const char& c : str) {
if (!std::isdigit(c)) {
return false;
}
}
return true;
}
std::vector<int> Parser::parseValues(const std::string& input) {
std::vector<int> res;
std::string tmp;
for (const char& c : input) {
if (!std::isspace(c)) {
tmp += c;
}
else {
if (isNumber(tmp)) {
res.push_back(std::stoi(tmp));
} else {
for (const char& c : tmp) {
res.push_back( static_cast<int>(c) );
}
}
tmp = "";
}
}
return res;
}
std::vector<Token> Parser::parseCode(const std::string& input) {
std::vector<Token> result;
bool isString = false;
std::string tmp;
for (auto& c : input) {
if (std::isspace(c)) {
continue;
}
if (c == '"' && !isString) {
isString = true;
continue;
}
if (c == '"' && isString) {
Token token(tmp, true);
result.push_back(token);
tmp = "";
isString = false;
continue;
}
if (!isString) {
std::string parsed(1, c);
Token token(parsed, false);
result.push_back(token);
}
else {
tmp += c;
}
}
return result;
}
void Parser::printTokens(const std::vector<Token>& tokens) {
for (auto& token : tokens) {
std::cout << "Token[" << token.getLexeme() << "] ";
}
std::cout << std::endl;
}
void Parser::printValues(const std::vector<int>& values) {
for (auto& value : values) {
std::cout << "Value[" << value << "] ";
}
std::cout << std::endl;
}
|