-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenizerHelper.hpp
152 lines (135 loc) · 4.34 KB
/
TokenizerHelper.hpp
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
#pragma once
#include "InputStream.hpp"
class TokenizerHelper {
public:
static bool IsWhitespace(char it) {
return it == '\t' || it == '\n' || it == '\v' || it == '\f' || it == '\r' || it == ' ';
}
static bool IsAlphanumeric(char it) {
return it >= 'a' && it <= 'z' || it >= 'A' && it <= 'Z' || it >= '0' && it <= '9' || it == '_';
}
static bool IsBinDigit(char it) {
return it == '0' || it == '1';
}
static bool IsOctDigit(char it) {
return it >= '0' && it <= '7';
}
static bool IsDecDigit(char it) {
return it >= '0' && it <= '9';
}
static bool IsHexDigit(char it) {
return IsDecDigit(it) || it >= 'a' && it <= 'f' || it >= 'A' && it <= 'F';
}
static char BinToInt(char it) {
if (it == '0') {
return 0;
} else if (it == '1') {
return 1;
}
throw std::exception();
}
static char OctToInt(char it) {
if (it >= '0' && it <= '7') {
return (it - '0');
}
throw std::exception();
}
static char DecToInt(char it) {
if (it >= '0' && it <= '9') {
return (it - '0');
}
throw std::exception();
}
static char HexToInt(char it) {
if (it >= 'a' && it <= 'f') {
return 10 + (it - 'a');
} else if (it >= 'A' && it <= 'F') {
return 10 + (it - 'A');
} else if (it >= '0' && it <= '9') {
return (it - '0');
}
throw std::exception();
}
static bool TryGetEscape(InputStream *stream, char *result) {
char c = stream->PeekChar(0);
if (c == '\\') {
c = stream->PeekChar(1);
stream->SkipChar(2);
if (c == '\'') {
*result = '\'';
} else if (c == '"') {
*result = '\"';
} else if (c == 'x') {
c = stream->PeekChar(0);
if (TokenizerHelper::IsOctDigit(c)) {
char symbol = TokenizerHelper::OctToInt(c);
c = stream->PeekChar(1);
stream->SkipChar(2);
if (TokenizerHelper::IsHexDigit(c)) {
symbol = (symbol * 16) + TokenizerHelper::HexToInt(c);
*result = symbol;
} else {
return false;
}
} else {
stream->SkipChar(1);
return false;
}
} else if (c == 'n') {
*result = '\n';
} else if (c == 'r') {
*result = '\r';
} else if (c == 't') {
*result = '\t';
} else if (c == '\\') {
*result = '\\';
} else if (c == '0') {
*result = '\0';
} else {
return false;
}
}
return true;
}
static bool TryGetByteEscape(InputStream *stream, uint8_t *result) {
char c = stream->PeekChar(0);
if (c == '\\') {
c = stream->PeekChar(1);
stream->SkipChar(2);
if (c == '\'') {
*result = '\'';
} else if (c == '"') {
*result = '\"';
} else if (c == 'x') {
c = stream->PeekChar(0);
if (TokenizerHelper::IsHexDigit(c)) {
uint8_t symbol = TokenizerHelper::HexToInt(c);
c = stream->PeekChar(1);
stream->SkipChar(2);
if (TokenizerHelper::IsHexDigit(c)) {
symbol = (symbol * 16) + TokenizerHelper::HexToInt(c);
*result = symbol;
} else {
return false;
}
} else {
stream->SkipChar(1);
return false;
}
} else if (c == 'n') {
*result = '\n';
} else if (c == 'r') {
*result = '\r';
} else if (c == 't') {
*result = '\t';
} else if (c == '\\') {
*result = '\\';
} else if (c == '0') {
*result = '\0';
} else {
return false;
}
}
return true;
}
};