-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.cc
143 lines (125 loc) · 2.89 KB
/
highlight.cc
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
#include "highlight.hh"
void highlight::put_curly(const std::string &s)
{
*out << "<span style=\"color: #FF0000\">" << s << "</span>";
if (s == "{")
++indent_;
if (s == "}") {
--indent_;
if (last_indent_ == indent_) {
out = adaptor_.create(NONE);
last_indent_ = 0;
}
}
}
void highlight::put_number(const std::string &s)
{
*out << "<span style=\"color: #993399\">" << s << "</span>";
}
void highlight::put_keyword(const std::string &s)
{
if (s == "pretty" && last_kw == ALGEBRA) {
put_text(s);
return;
}
if (s == "algebra") {
last_kw = ALGEBRA;
last_indent_ = indent_;
out = adaptor_.create(ALGEBRA);
}
if (s == "grammar") {
last_kw = GRAMMAR;
last_indent_ = indent_;
out = adaptor_.create(GRAMMAR);
}
if (s == "signature") {
last_kw = SIGNATURE;
last_indent_ = indent_;
out = adaptor_.create(SIGNATURE);
}
*out << "<span style=\"font-weight: bold; color: #0000FF\">"
<< s
<< "</span>";
}
void highlight::put_text(const std::string &s)
{
static const char types[][7] = { "int", "char", "string", "Rope", "double", "single" };
for (unsigned i = 0; i<6; ++i)
if (types[i] == s) {
*out << "<span style=\"color: #009900\">" << s << "</span>";
return;
}
if (last_kw)
adaptor_.set_name(s);
*out << s;
last_kw = NONE;
}
void highlight::put_comment_begin(const std::string &s)
{
*out << "<span style=\"font-style: italic; color: #9A1900\">"
<< s;
}
void highlight::put_comment_end(const std::string &s)
{
*out << s << "</span>";
}
void highlight::put_op(const std::string &s)
{
*out << "<span style=\"color: #990000\">";
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
switch (*i) {
case '<' :
*out << "<";
break;
case '>' :
*out << ">";
break;
default:
*out << *i;
break;
}
*out << "</span>";
}
void highlight::put_squote_begin(const std::string &s)
{
*out << "<span style=\"color: #FF0000\">"
<< s;
}
void highlight::put_squote_end(const std::string &s)
{
*out << s << "</span>";
}
void highlight::put_cquote_begin(const std::string &s)
{
*out << "<span style=\"color: #FF0000\">"
<< s;
}
void highlight::put_cquote_end(const std::string &s)
{
*out << s << "</span>";
}
void highlight::put_ws(const std::string &s)
{
*out << s;
}
typedef void* yyscan_t;
int hl_lex_init (yyscan_t* scanner);
int hl_lex_destroy (yyscan_t yyscanner );
int hl_lex (yyscan_t yyscanner);
void hl_set_extra (highlight *user_defined ,yyscan_t yyscanner );
void hl_set_in (FILE * in_str ,yyscan_t yyscanner );
#include <stdio.h>
#include <sstream>
void highlight::lex()
{
FILE *f = fopen(filename_.c_str(), "r");
if (!f)
return;
yyscan_t scanner;
hl_lex_init(&scanner);
hl_set_in(f, scanner);
hl_set_extra(this, scanner);
hl_lex(scanner);
hl_lex_destroy(scanner);
fclose(f);
}