-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.cpp
executable file
·168 lines (139 loc) · 3.48 KB
/
tokenizer.cpp
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
#include <string>
#include <iostream>
#include "tokendefs.h"
#include "datetypes.h"
#include "tokenizer.h"
#include "exceptions.h"
using namespace std;
void Tokenizer::tokenizeString(string s) {
this->pos = 0;
this->repr = s;
}
token Tokenizer::nextToken(string & tokRepr) {
token t = nextToken2(tokRepr);
// cout <<t<<endl;
return t;
}
token Tokenizer::nextToken2(string & tokRepr) {
this->skipWhitespaces();
char ch = this->repr[this->pos];
//Unarni minus:
// if (ch=='-'){
// this->getChar();
// tokRepr="-";
// tokRepr+=this->readNum();
// return INT;
// }
if (isdigit(ch)) { //prvni je cislovka
string num = this->readNum();
if (this->repr[this->pos] == '.') { // float:
// this->pos++; //za tecku
this->getChar(); //za tecku
string frac = this->readNum();
tokRepr = num + "." + frac;
return DOUBLE;
} else { // INTEGER
tokRepr = num;
return INT;
}
} else if (ch == '(') { //zacatek seznamu
this->getChar();
return LIST_BEGIN;
} else if (ch == ')') { //konec seznamu
this->getChar();
return LIST_END;
} else if (ch == '"') { //zacatek stringu
tokRepr = this->readText();
return STRING_BEGIN;
} else if (!isdigit(ch) && isgraph(ch)) { //symbol
tokRepr = this->readSymbol();
return SYMBOL;
}
return 0;
}
void Tokenizer::skipWhitespaces() {
while (this->pos < (int) this->repr.length()) {
// int chr;
int chr = this->getChar();
if (chr == '#') { //přeskakování komentářů
do {
chr = this->getChar();
} while (chr != -1 && chr != '\n');
this->ungetChar();
}
if (!isspace(chr)) {
this->ungetChar();
break;
}
}
}
string Tokenizer::readNum() {
string s;
char chr;
while (isdigit(chr = this->getChar())) {
s.push_back(chr);
}
this->ungetChar();
return s;
}
//! je validni znak pro symbol
bool ischar(int chr) {
if (!isspace(chr) && chr != '(' && chr != ')') {
return true;
} else {
return false;
}
}
string Tokenizer::readSymbol() {
string s;
char chr;
while (ischar(chr = this->getChar())) {
s.push_back(chr);
}
// this->pos--;
this->ungetChar();
return s;
}
string Tokenizer::readText() {
string s;
this->getChar(); // prvni '"'
int chr = this->getChar();
while (chr != '"' && chr != -1) {
s.push_back(chr);
chr = this->getChar();
}
if (chr == -1) { //neuzavřený řetězec
SyntaxException e;
e.desc = "Syntax error: Unexpected EOF inside of string";
e.line = this->line;
e.col = this->col;
throw e;
}
return s;
}
int Tokenizer::getChar() {
if (this->pos >= (int) this->repr.length()) {
return -1;
}
// cout <<"li: "<<this->line <<endl;
int chr = this->repr[this->pos];
this->pos++;
if (chr == '\n') {
this->line++;
this->col = 0;
} else {
this->col++;
}
return chr;
}
void Tokenizer::ungetChar() {
this->pos--;
int chr = this->repr[this->pos];
if (chr == '\n') {
this->line--;
//TODO: vlozit misto 0 pozici konce radku:
this->col = 0;
} else {
this->col--;
}
}