-
Notifications
You must be signed in to change notification settings - Fork 0
/
pptoken.cpp
186 lines (164 loc) · 3.61 KB
/
pptoken.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdexcept>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#include "IPPTokenStream.h"
#include "DebugPPTokenStream.h"
// Translation features you need to implement:
// - utf8 decoder
// - utf8 encoder
// - universal-character-name decoder
// - trigraphs
// - line splicing
// - newline at eof
// - comment striping (can be part of whitespace-sequence)
// EndOfFile: synthetic "character" to represent the end of source file
constexpr int EndOfFile = -1;
// given hex digit character c, return its value
int HexCharToValue(int c)
{
switch (c)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'a': return 10;
case 'B': return 11;
case 'b': return 11;
case 'C': return 12;
case 'c': return 12;
case 'D': return 13;
case 'd': return 13;
case 'E': return 14;
case 'e': return 14;
case 'F': return 15;
case 'f': return 15;
default: throw logic_error("HexCharToValue of nonhex char");
}
}
// See C++ standard 2.11 Identifiers and Appendix/Annex E.1
const vector<pair<int, int>> AnnexE1_Allowed_RangesSorted =
{
{0xA8,0xA8},
{0xAA,0xAA},
{0xAD,0xAD},
{0xAF,0xAF},
{0xB2,0xB5},
{0xB7,0xBA},
{0xBC,0xBE},
{0xC0,0xD6},
{0xD8,0xF6},
{0xF8,0xFF},
{0x100,0x167F},
{0x1681,0x180D},
{0x180F,0x1FFF},
{0x200B,0x200D},
{0x202A,0x202E},
{0x203F,0x2040},
{0x2054,0x2054},
{0x2060,0x206F},
{0x2070,0x218F},
{0x2460,0x24FF},
{0x2776,0x2793},
{0x2C00,0x2DFF},
{0x2E80,0x2FFF},
{0x3004,0x3007},
{0x3021,0x302F},
{0x3031,0x303F},
{0x3040,0xD7FF},
{0xF900,0xFD3D},
{0xFD40,0xFDCF},
{0xFDF0,0xFE44},
{0xFE47,0xFFFD},
{0x10000,0x1FFFD},
{0x20000,0x2FFFD},
{0x30000,0x3FFFD},
{0x40000,0x4FFFD},
{0x50000,0x5FFFD},
{0x60000,0x6FFFD},
{0x70000,0x7FFFD},
{0x80000,0x8FFFD},
{0x90000,0x9FFFD},
{0xA0000,0xAFFFD},
{0xB0000,0xBFFFD},
{0xC0000,0xCFFFD},
{0xD0000,0xDFFFD},
{0xE0000,0xEFFFD}
};
// See C++ standard 2.11 Identifiers and Appendix/Annex E.2
const vector<pair<int, int>> AnnexE2_DisallowedInitially_RangesSorted =
{
{0x300,0x36F},
{0x1DC0,0x1DFF},
{0x20D0,0x20FF},
{0xFE20,0xFE2F}
};
// See C++ standard 2.13 Operators and punctuators
const unordered_set<string> Digraph_IdentifierLike_Operators =
{
"new", "delete", "and", "and_eq", "bitand",
"bitor", "compl", "not", "not_eq", "or",
"or_eq", "xor", "xor_eq"
};
// See `simple-escape-sequence` grammar
const unordered_set<int> SimpleEscapeSequence_CodePoints =
{
'\'', '"', '?', '\\', 'a', 'b', 'f', 'n', 'r', 't', 'v'
};
// Tokenizer
struct PPTokenizer
{
IPPTokenStream& output;
PPTokenizer(IPPTokenStream& output)
: output(output)
{}
void process(int c)
{
// TODO: Your code goes here.
// 1. do translation features
// 2. tokenize resulting stream
// 3. call an output.emit_* function for each token.
if (c == EndOfFile)
{
output.emit_identifier("not_yet_implemented");
output.emit_eof();
}
// TIP: Reference implementation is about 1000 lines of code.
// It is a state machine with about 50 states, most of which
// are simple transitions of the operators.
}
};
int main()
{
try
{
ostringstream oss;
oss << cin.rdbuf();
string input = oss.str();
DebugPPTokenStream output;
PPTokenizer tokenizer(output);
for (char c : input)
{
unsigned char code_unit = c;
tokenizer.process(code_unit);
}
tokenizer.process(EndOfFile);
}
catch (exception& e)
{
cerr << "ERROR: " << e.what() << endl;
return EXIT_FAILURE;
}
}