-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetToken.cpp
107 lines (94 loc) · 1.96 KB
/
GetToken.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
#include "Reader.h"
char * Reader::getToken()
{
// Skip first spaces
while (1) {
if (!rawData[0]) return nullptr;
if (getCharType(rawData[0]) == CT_UNKNOWN)
Error("Unknown character '%c'.", rawData[0]);
if (getCharType(rawData[0]) != CT_SKIP) break;
rawData++;
}
char tmp[256];
char* result = NULL;
int count = 0;
// String constant token
if (rawData[0] == '\"') {
bool skip = true;
while (rawData[0]) {
tmp[++count - 1] = rawData[0];
rawData++;
if (tmp[count - 1] == '\"' && !skip)
break;
if (skip) {
skip = false;
if (count >= 2) {
char p = tmp[count - 1];
tmp[count - 2] = '\"';
tmp[count - 1] = ',';
switch (p)
{
case 'n': {
tmp[++count - 1] = '1';
tmp[++count - 1] = '3';
tmp[++count - 1] = ',';
tmp[++count - 1] = '1';
tmp[++count - 1] = '0';
tmp[++count - 1] = ',';
}break;
case '\"': {
tmp[++count - 1] = 34;
tmp[++count - 1] = ',';
}break;
default:
break;
}
tmp[++count - 1] = '\"';
}
}
else
if (tmp[count - 1] == '\\')
skip = true;
}
}
else
// Char constant token
if (rawData[0] == '\'') {
bool skip = true;
while (rawData[0]) {
count++;
tmp[count - 1] = rawData[0];
rawData++;
if (tmp[count - 1] == '\'' && !skip)
break;
if (skip)
skip = false;
else
if (tmp[count - 1] == '\\')
skip = true;
}
}
else
// Square Token
if (getCharType(rawData[0]) == CT_SQUARE) {
count = 1;
tmp[0] = rawData[0];
rawData++;
}
else
// Standard token
while (rawData[0]) {
int t = getCharType(rawData[0]);
if (t == CT_UNKNOWN)
Error("Undefined character");
if (count > 0 && t != getCharType(tmp[count - 1]))
break;
count++;
tmp[count - 1] = rawData[0];
rawData++;
}
// Return token result
result = (char*)calloc(1, count + 1);
memcpy(result, tmp, count);
return result;
}