Skip to content

Commit ed3ea32

Browse files
committed
add code tokens
1 parent 4f63969 commit ed3ea32

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

wast/app.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace wast {
44

5+
const whitespace: string = " "
6+
57
export function highlight(wast: string) {
68
if (TypeScript.logging.outputEverything) {
79
console.log(wast);
@@ -28,6 +30,16 @@ namespace wast {
2830
row.appendChild(codeBlock);
2931
}
3032

33+
let startWith = function (token: string): boolean {
34+
for (var i = 0; i < token.length; i++) {
35+
if (buffer[i] != token.charAt(i)) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
3143
while (!code.EndRead) {
3244
c = code.Next;
3345

@@ -41,6 +53,45 @@ namespace wast {
4153
} else {
4254
buffer.push(c);
4355
}
56+
} else if (escape.string) {
57+
if (c = "\"") {
58+
escape.string = false;
59+
buffer.push(c);
60+
token = $ts("<span>", { class: "string" }).display(buffer.join(""));
61+
buffer = [];
62+
line.push(token);
63+
} else {
64+
buffer.push(c);
65+
}
66+
} else {
67+
if (startWith(";;")) {
68+
escape.comment = true;
69+
} else if (c == "\"") {
70+
escape.string = true;
71+
} else if (c == " ") {
72+
if (buffer.length > 0) {
73+
// split a code token
74+
token = $ts("<span>", { class: "code" }).display(buffer.join(" "));
75+
buffer = []
76+
line.push(token);
77+
}
78+
// add current token delimiter whitespace
79+
line.push($ts("<span>").display(whitespace));
80+
} else if (c == "(" || c == ")") {
81+
// s-expression delimiter
82+
// using strong text style
83+
if (buffer.length > 0) {
84+
// split a code token
85+
token = $ts("<span>", { class: "code" }).display(buffer.join(" "));
86+
buffer = []
87+
line.push(token);
88+
}
89+
90+
// add current S-expression token delimiter
91+
line.push($ts("<span>", { style: "font-style: strong;" }).display(c));
92+
} else {
93+
buffer.push(c);
94+
}
4495
}
4596
}
4697
}

0 commit comments

Comments
 (0)