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
186
187
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using JSCompiler.Core;
namespace JSCompiler.SyntaxHighlighter
{
public static class SyntaxHighlighter
{
private static readonly string HTML =
@"<!DOCTYPE html>
<html>
<head>
{0}
<title> {1} </title>
</head>
<body> {2} </body></html>";
private static readonly string DEFAULT_CSS =
@"<style>
body {
font-size: 20px;
font-family: ""Verdana"";
}
.number {
color: Orange;
}
.string {
color: Gray;
white-space: pre-wrap;
}
.keyword {
color: Blue;
}
.identifier {
color: Black;
}
.operator {
color: DarkBlue;
}
.comment {
color: Green;
font-style: italic;
white-space: pre-wrap;
}
.reserved {
color: MediumVioletRed;
}
.undefined{
color: Black;
}
.default {
color: DarkBlue;
}
</style>";
private static IEnumerable<TokenType> KEYWORDS = ReservedStrings.KeywordsAsMap().Values;
private static IEnumerable<TokenType> OTHER_RESERVED = ReservedStrings.OthersAsMap().Values;
private static IEnumerable<TokenType> ONE_SYMBOL_OPERATORS = new List<TokenType>
{
TokenType.Minus, // -
TokenType.Plus, // +
TokenType.Star, // *
TokenType.Division, // /
TokenType.Modulo, // %
TokenType.BitwiseNot, // ~
TokenType.BitwiseAnd, // &
TokenType.BitwiseXor, // ^
TokenType.BitwiseOr, // |
TokenType.QuestionMark, // ?
TokenType.Bang, // !
TokenType.Equal, // =
TokenType.Greater, // >
TokenType.Less, // <
TokenType.Backslash, // \
};
private static IEnumerable<TokenType> MULT_SYMBOL_OPERATORS = new List<TokenType>
{
TokenType.NotEqual, // !=
TokenType.EqualEqual, // ==
TokenType.StrictEqual, // ===
TokenType.StrictNotEqual, // !==
TokenType.GreaterEqual, // >=
TokenType.LessEqual, // <=
TokenType.MinusMinus, // --
TokenType.PlusPlus, // ++
TokenType.LeftShift, // <<
TokenType.RightShift, // >>
TokenType.LogicalAnd, // &&
TokenType.LogicalOr, // ||
TokenType.StarEqual, // *=
TokenType.DivideEqual, // /=
TokenType.ModuloEqual, // %=
TokenType.PlusEqual, // +=
TokenType.MinusEqual, // -=
TokenType.LeftShiftEqual, // <<=
TokenType.RightShiftEqual, // >>=
TokenType.BitwiseAndEqual, // &=
TokenType.BitwiseXorEqual, // ^=
TokenType.BitwiseOrEqual, // |=
};
public static void GenerateHtml(IEnumerable<Token> tokens, string fileName, string css = null)
{
if (css == null) css = DEFAULT_CSS;
string code = "";
IEnumerator<Token> tokenEnum = tokens.GetEnumerator();
if (!tokenEnum.MoveNext()) return;
Token cur;
do
{
cur = tokenEnum.Current;
TokenType type = cur.Type;
switch (type)
{
case TokenType.Whitespace:
code += " ";
break;
case TokenType.Tab:
code += " " + " " + " " + " ";
break;
case TokenType.NewLine:
code += "<br>";
break;
case TokenType.Number:
code += "<span class=\"number\">" + ((NumToken)cur).Num + "</span>";
break;
case TokenType.String:
code += "<span class=\"string\">" + ((WordToken)cur).Lexeme + "</span>";
break;
case TokenType.Identifier:
code += "<span class=\"identifier\">" + ((WordToken)cur).Lexeme + "</span>";
break;
case TokenType.Comment:
code += "<span class=\"comment\">" + ((WordToken)cur).Lexeme + "</span>";
break;
case TokenType.Error:
code += "<span class=\"undefined\">" + ((WordToken)cur).Lexeme + "</span>";
break;
default:
if (KEYWORDS.Contains(type))
{
code += "<span class=\"keyword\">" + ((WordToken)cur).Lexeme + "</span>";
}
else if(OTHER_RESERVED.Contains(type))
{
code += "<span class=\"reserved\">" + ((WordToken)cur).Lexeme + "</span>";
}
else if (ONE_SYMBOL_OPERATORS.Contains(type))
{
code += "<span class=\"operator\">" + cur.ToString() + "</span>";
}
else if (MULT_SYMBOL_OPERATORS.Contains(type))
{
code += "<span class=\"operator\">" + ((WordToken)cur).Lexeme + "</span>";
}
else
{
code += "<span class=\"default\">" + cur.ToString() + "</span>";
break;
}
break;
}
}
while (tokenEnum.MoveNext() && tokenEnum.Current.Type != TokenType.EOF);
string shouldWrite = string.Format(HTML, css, fileName, code);
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.Write(shouldWrite);
}
}
}
}
|