-
Notifications
You must be signed in to change notification settings - Fork 0
/
holyc-highlighter.js
186 lines (165 loc) · 4.35 KB
/
holyc-highlighter.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
const syntax_colors = {
black: "#000000",
blue: "#0000aa",
lightblue: "#00aaaa",
purple: "#5555ff",
orange: "#aa5500",
lightorange: "#aa5500",
green: "#00aa00",
};
const is_blue_keyword = (str) => {
return str === "if" ||
str === "for" ||
str === "else" ||
str === "return" ||
str === "I0" ||
str === "U0" ||
str === "I8" ||
str === "U8" ||
str === "I16" ||
str === "I32" ||
str === "F64" ||
str === "class" ||
str === "#define" ||
str === "#include" ||
str === "js"
? true
: false;
};
const is_lightblue_keyword = (str) => {
return str === "TRUE" || str === "FALSE" ? true : false;
};
const is_purple_keyword = (str) => {
return str === "I64" || str === "U64" || str === "U32" || str === "U16"
? true
: false;
};
const element = (str, color) => {
const el = document.createElement("span");
el.appendChild(document.createTextNode(str));
el.style.color = color;
return el;
};
const lex_is_digit = (val) => {
return !isNaN(val);
};
const lex_is_alpha = (val) => {
return /^[A-Z0-9_#]$/i.test(val);
};
const is_next = (str, i, expected) => {
for (; i < str.length; ++i) {
if (str[i] === " " || str[i] === "\n") continue;
return str[i] === expected ? true : false;
}
return false;
};
const jshlchl_lex = (str) => {
if (!str) return;
let elements = [];
for (let i = 0; i < str.length; ++i) {
if (str[i] === "/" && str[i + 1] === "/") {
let comment = "";
while (1) {
comment += str[i++];
if (str[i] === "\n" || i === str.length) break;
}
elements.push(element(comment, syntax_colors.green));
}
if (str[i] === "\n") {
while (str[i++] === "\n") {
elements.push(document.createElement("br"));
}
i--;
}
if (str[i] === " ") {
let space = "";
while (str[i++] === " ") {
space += " ";
}
i--;
elements.push(element(space, ""));
}
if (lex_is_digit(str[i])) {
let number = "";
while (lex_is_digit(str[i])) {
number += str[i++];
}
i--;
elements.push(element(number, syntax_colors.blue));
}
if (lex_is_alpha(str[i]) && !lex_is_digit(str[i])) {
let text = "";
while (lex_is_alpha(str[i])) {
text += str[i++];
}
let color;
if (is_blue_keyword(text)) {
color = syntax_colors.blue;
} else if (is_lightblue_keyword(text)) {
color = syntax_colors.lightblue;
} else if (is_purple_keyword(text)) {
color = syntax_colors.purple;
} else if (is_next(str, i, "(")) {
color = syntax_colors.black;
} else {
color = syntax_colors.black;
}
i--;
elements.push(element(text, color));
}
if (str[i] === '"' || str[i] === "'") {
let string = "";
let strsymbol = str[i];
while (str[i]) {
if (str[i] === "\\") {
elements.push(element(string, syntax_colors.orange));
elements.push(element(str[i] + str[++i], syntax_colors.lightorange));
string = "";
i++;
if (str[i] === strsymbol || i === str.length) break;
}
string += str[i++];
if (str[i] === strsymbol || i === str.length) break;
}
if (str[i] === '"') string += '"';
else if (str[i] === "'") string += "'";
elements.push(element(string, syntax_colors.orange));
}
if (
str[i] !== "\n" &&
str[i] !== " " &&
str[i] !== '"' &&
str[i] !== "'" &&
!lex_is_alpha(str[i]) &&
!lex_is_digit(str[i]) &&
str[i]
) {
if (str[i] === "/" && str[i + 1] === "/") {
i--;
continue;
}
elements.push(element(str[i], "black"));
}
}
return elements;
};
export const set_scroll = () => {
const stdin = document.getElementById("stdin");
const hl = document.getElementById("hl");
hl.scrollLeft = stdin.scrollLeft;
hl.scrollTop = stdin.scrollTop;
};
export const highlight = (e) => {
const hl = document.getElementById("hl");
hl.innerHTML = "";
const elements = jshlchl_lex(e);
if (!elements) return;
for (let i = 0; i < elements.length; ++i) {
hl.appendChild(elements[i]);
}
// fix break line if it is in last element
if (elements[elements.length - 1].isEqualNode(document.createElement("br"))) {
hl.appendChild(document.createElement("br"));
}
set_scroll();
};