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
|
#include "html_utils.h"
#include "lexconst.h"
namespace HTMLUtils {
std::string escapeHTML(const std::string &lexeme) {
std::string result;
for (auto c : lexeme) {
switch(c) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
default:
result.push_back(c);
break;
}
}
return result;
}
bool writeToOutput(FILE *out, Token token) {
if (token.type == TokenClass::Eof) {
printf("End of file!\n");
fputc('\n', out);
return false;
}
if (token.type == TokenClass::Skip) {
if (fputs(token.lexeme.c_str(), out) == EOF) {
printf("Error while writing to output");
return false;
}
return true;
}
printf("Token of type %s at line %d: ", LexCer::tokenToClass.at(token.type).c_str(), token.line);
if (token.type == TokenClass::Number)
printf("%f\n", token.number);
else
printf("%s\n", token.lexeme.c_str());
auto lexeme = escapeHTML(token.lexeme);
std::string output;
output.append(LexCer::spanBegin).append(LexCer::tokenToClass.at(token.type)).append("\">").append(lexeme).append(LexCer::spanEnd);
if (fputs(output.c_str(), out) == EOF) {
printf("Error while writing to output");
return false;
}
return true;
}
std::string getOutputFileName(const std::string &name) {
auto pos = name.find_last_of('.');
if (pos == std::string::npos) {
return name + ".html";
}
return name.substr(0, pos) + ".html";
}
void lexCit(FILE *in, FILE *out) {
Tokenizer hl {in, out};
std::string lexeme;
double number;
TokenClass type;
Token token;
do {
token = hl.nextToken(&lexeme, &number);
type = token.type;
if (!writeToOutput(out, token))
break;
lexeme = "";
number = 0.0;
} while(type != TokenClass::Eof || type != TokenClass::Invalid);
}
} /* Namespace HTMLUtils */
|