-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsyntaxHighlighterForAEC.js
233 lines (233 loc) · 8.58 KB
/
syntaxHighlighterForAEC.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* This is a NodeJS script which reads an AEC file and outputs a syntax
* highlighted code in form of a HTML file. Might come useful if I need
* to publish a paper including code in the new version of AEC. The
* same code is used in the script in "AEC_specification.html", so it's
* written to be compatible with as many browsers as possible, rather
* than to follow the best software engineering practices.
*/
"use strict";
function highlightedToken(token) {
if (token == "<")
token = "<";
if (token == ">")
token = ">";
if (token.substr(0, 2) == "//" || token.substr(0, 2) == "/*")
return '<span class="Comment">' + token + "</span>";
if (token.charAt(0) == '"' || token.charAt(0) == "'")
return ('<span class="String">' +
token.replace(/\\n/g, "<b>\\<i>n</i></b>")
.replace(/\\\\/g, "<b>\\<i>\\</i></b>")
.replace(/\\t/g, "<b>\\<i>t</i></b>")
.replace(/\\\"/g, "<b>\\<i>\"</i></b>") +
"</span>");
if (token.substr(0, 2) == 'R"')
return ('<span class="String">' +
token.replace(/\n/g, '</span>\n<span class="String">') + "</span>");
if (token == "Character" || token == "CharacterPointer" ||
token == "Integer16" || token == "Integer16Pointer" ||
token == "Integer32" || token == "Integer32Pointer" ||
token == "Integer64" || token == "Integer64Pointer" ||
token == "Decimal32" || token == "Decimal64" ||
token == "Decimal32Pointer" || token == "Decimal64Pointer" ||
token == "CharacterPointerPointer" || token == "DataType" ||
token == "Nothing" || token == "InstantiateStructure" ||
/Pointer$/.test(token) || /^PointerTo/.test(token))
return '<span class="Type">' + token + "</span>";
if (token.charAt(0) >= "0" && token.charAt(0) <= "9")
return '<span class="Constant">' + token + "</span>";
if (token == "Function" || token == "Which" || token == "Returns" ||
token == "Is" || token == "External" || token == "Declared" ||
token == "Does" || token == "EndFunction" || token == "Return" ||
token == "If" || token == "ElseIf" || token == "Else" ||
token == "EndIf" || token == "Then" || token == "While" ||
token == "EndWhile" || token == "Loop" || token == ";" ||
token == "Structure" || token == "Consists" || token == "Of" ||
token == "EndStructure" || token == "asm" || token == "asm_i32" ||
token == "asm_i64" || token == "asm_f32" || token == "asm_f64")
return '<span class="Keyword">' + token + "</span>";
if (token == "and" || token == "or" || token == "mod" ||
token == "AddressOf" || token == "ValueAt" || token == "," ||
token == "+" || token == "-" || token == "/" || token == "*" ||
token == "?" || token == ":" || token == "=" || token == "<" ||
token == ">" || token == "not" || token == "invertBits" ||
token == "SizeOf")
return '<span class="Operator">' + token + "</span>";
if (token == "{" || token == "}" || token == "(" || token == ")" ||
token == "[" || token == "]")
return '<span class="Parenthesis">' + token + "</span>";
return token;
}
function highlightAEC(sourceCode) {
var currentToken = "", areWeInAString = false, areWeInAComment = false,
howTheCommentStarted, howTheStringStarted, highlightedCode = "";
for (var i = 0; i < sourceCode.length; i++) {
if (!areWeInAComment && !areWeInAString &&
(sourceCode.substr(i, 2) == "//" || sourceCode.substr(i, 2) == "/*")) {
areWeInAComment = true;
howTheCommentStarted = sourceCode.substr(i, 2);
highlightedCode += highlightedToken(currentToken);
currentToken = howTheCommentStarted;
i++;
continue;
}
if (areWeInAComment &&
((howTheCommentStarted == "//" && sourceCode.charAt(i) == "\n") ||
(howTheCommentStarted == "/*" && sourceCode.substr(i, 2) == "*/"))) {
areWeInAComment = false;
if (howTheCommentStarted == "//")
currentToken += "\n";
else
currentToken += "*/";
if (howTheCommentStarted == "/*")
i++;
highlightedCode += highlightedToken(currentToken);
currentToken = "";
continue;
}
if (areWeInAComment) {
currentToken += sourceCode.charAt(i);
continue;
}
if (!areWeInAString &&
(sourceCode.charAt(i) == '"' || sourceCode.charAt(i) == "'" ||
sourceCode.substr(i, 2) == 'R"')) {
areWeInAString = true;
if (sourceCode.substr(i, 2) == 'R"') {
howTheStringStarted = "";
var whereIsTheOpenParenthesis = i + 2;
while (sourceCode.charAt(whereIsTheOpenParenthesis) != '(') {
howTheStringStarted += sourceCode.charAt(whereIsTheOpenParenthesis);
whereIsTheOpenParenthesis++;
}
howTheStringStarted = ')' + howTheStringStarted + '"';
} else
howTheStringStarted = sourceCode.charAt(i);
highlightedCode += highlightedToken(currentToken);
currentToken = sourceCode.charAt(i);
continue;
}
if (areWeInAString &&
(currentToken.charAt(currentToken.length - 1) != "\\" ||
(currentToken.charAt(currentToken.length - 2) == "\\" &&
currentToken.charAt(currentToken.length - 3) != "\\")) &&
sourceCode.substr(i, howTheStringStarted.length) ==
howTheStringStarted) {
areWeInAString = false;
currentToken += howTheStringStarted;
i += howTheStringStarted.length - 1;
highlightedCode += highlightedToken(currentToken);
currentToken = "";
continue;
}
if (areWeInAString) {
currentToken += sourceCode.charAt(i);
continue;
}
if ((sourceCode.charAt(i) >= "0" && sourceCode.charAt(i) <= "9") ||
(sourceCode.charAt(i) >= "a" && sourceCode.charAt(i) <= "z") ||
(sourceCode.charAt(i) >= "A" && sourceCode.charAt(i) <= "Z") ||
sourceCode.charAt(i) == "_" || sourceCode.charAt(i) == ".") {
currentToken += sourceCode.charAt(i);
continue;
}
highlightedCode +=
highlightedToken(currentToken) + highlightedToken(sourceCode.charAt(i));
currentToken = "";
}
return highlightedCode + highlightedToken(currentToken);
}
if (typeof require == "undefined")
(print || console.log)("Please run this program in NodeJS!");
else if (process.argv.length != 3 || !/\.AEC$/i.test(process.argv[2])) {
console.log(`Please invoke this program as follows:
"${process.argv[0]}" "${process.argv[1]}" name_of_the_AEC_program.aec
`);
} else {
const FileSystem = require("fs");
let originalCode;
try {
originalCode =
FileSystem.readFileSync(process.argv[2], "utf8").replace(/\r/g, "");
} catch (error) {
console.log(`Can't open the file "${process.argv[2]}" for reading! `,
error.message);
}
if (originalCode != undefined) {
// If reading the input file succeeded.
const bodyWithoutLineNumbers = highlightAEC(originalCode);
const bodySplitByLines = bodyWithoutLineNumbers.split("\n");
let body = "";
for (let i = 0; i < bodySplitByLines.length; i++) {
if (i + 1 < 10)
body += " " +
'<span class="LineNumber">' + (i + 1) + "</span> " +
bodySplitByLines[i] + "\n";
else if (i + 1 < 100)
body += " " +
'<span class="LineNumber">' + (i + 1) + "</span> " +
bodySplitByLines[i] + "\n";
else if (i + 1 < 1000)
body += " " +
'<span class="LineNumber">' + (i + 1) + "</span> " +
bodySplitByLines[i] + "\n";
else
body += '<span class="LineNumber">' + (i + 1) + "</span> " +
bodySplitByLines[i] + "\n";
}
const html = `
<!doctype html>
<html>
<head>
<title>${process.argv[2]} - Syntax Highlighted in NodeJS</title>
<style type="text/css">
pre {
font-size: 13px;
}
.LineNumber {
color: #772200;
font-weight: normal;
}
.Type {
color: #775500;
font-weight: bold;
}
.Keyword {
color: #770000;
font-weight: bold;
}
.Comment {
color: #004444;
font-weight: bold;
}
.Parenthesis {
color: #333322;
font-weight: bold;
}
.Operator {
color: #770077;
font-weight: bold;
}
.Constant {
color: #006600;
font-weight: bold;
}
.String {
color: #003377;
}
</style>
</head>
<body>
<pre>${body}</pre>
</body>
</html>
`;
try {
FileSystem.writeFileSync(process.argv[2] + ".html", html);
} catch (error) {
console.log('Unable to open the file "' + process.argv[2] +
'.html" for output! ',
error.message);
}
}
}