Repository navigation
Expand file tree
/
Copy pathst.js
More file actions
198 lines (189 loc) · 5.84 KB
/
Copy pathst.js
File metadata and controls
198 lines (189 loc) · 5.84 KB
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
(function (global) {
'use strict';
const KEYWORDS = new Set([
'if', 'then', 'elsif', 'else', 'end_if',
'case', 'of', 'end_case',
'for', 'to', 'by', 'do', 'end_for',
'while', 'end_while',
'repeat', 'until', 'end_repeat',
'exit', 'return',
'and', 'or', 'xor', 'not', 'mod',
'true', 'false'
]);
const TYPES = new Set([
'bool', 'sint', 'int', 'dint', 'lint',
'usint', 'uint', 'udint', 'ulint',
'real', 'lreal', 'string'
]);
const COLORS = {
kw: '#c586c0',
type: '#4ec9b0',
fn: '#dcdcaa',
num: '#b5cea8',
str: '#ce9178',
cm: '#6a9955',
op: '#d4d4d4',
id: '#9cdcfe',
punct: '#d4d4d4',
text: '#d4d4d4'
};
function esc(s) {
return String(s ?? '').replace(/[&<>"']/g, c => ({
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
}[c]));
}
function tokenize(src) {
const s = String(src ?? '');
const out = [];
let i = 0;
const n = s.length;
function push(type, text) { if (text) out.push({ type, text }); }
while (i < n) {
const c = s[i];
if (c === '\r') { i++; continue; }
if (c === '\n') { push('nl', '\n'); i++; continue; }
if (c === ' ' || c === '\t') {
let j = i + 1;
while (j < n && (s[j] === ' ' || s[j] === '\t')) j++;
push('ws', s.slice(i, j));
i = j;
continue;
}
if (c === '/' && s[i + 1] === '/') {
let j = i + 2;
while (j < n && s[j] !== '\n') j++;
push('cm', s.slice(i, j));
i = j;
continue;
}
if (c === '/' && s[i + 1] === '*') {
let j = i + 2;
while (j < n - 1 && !(s[j] === '*' && s[j + 1] === '/')) j++;
j = Math.min(n, j + 2);
push('cm', s.slice(i, j));
i = j;
continue;
}
if (c === '(' && s[i + 1] === '*') {
let j = i + 2;
while (j < n - 1 && !(s[j] === '*' && s[j + 1] === ')')) j++;
j = Math.min(n, j + 2);
push('cm', s.slice(i, j));
i = j;
continue;
}
if (c === "'") {
let j = i + 1;
while (j < n) {
if (s[j] === "'" && s[j + 1] === "'") { j += 2; continue; }
if (s[j] === "'") { j++; break; }
if (s[j] === '\n') break;
j++;
}
push('str', s.slice(i, j));
i = j;
continue;
}
if (c === '[' && s.slice(i, i + 4) === '[:=]') {
push('op', '[:=]');
i += 4;
continue;
}
if (s.slice(i, i + 2) === ':=' || s.slice(i, i + 2) === '<>' || s.slice(i, i + 2) === '<=' || s.slice(i, i + 2) === '>=' || s.slice(i, i + 2) === '**') {
push('op', s.slice(i, i + 2));
i += 2;
continue;
}
if (/[0-9]/.test(c) || (c === '.' && /[0-9]/.test(s[i + 1] || ''))) {
let j = i;
while (j < n && /[0-9]/.test(s[j])) j++;
if (s[j] === '#') {
j++;
while (j < n && /[0-9A-Za-z_]/.test(s[j])) j++;
} else {
if (s[j] === '.' && /[0-9]/.test(s[j + 1] || '')) {
j++;
while (j < n && /[0-9_]/.test(s[j])) j++;
}
if (s[j] === 'e' || s[j] === 'E') {
j++;
if (s[j] === '+' || s[j] === '-') j++;
while (j < n && /[0-9]/.test(s[j])) j++;
}
}
push('num', s.slice(i, j));
i = j;
continue;
}
if (/[A-Za-z_\u00C0-\u024F]/.test(c)) {
let j = i + 1;
while (j < n && /[A-Za-z0-9_\u00C0-\u024F]/.test(s[j])) j++;
const raw = s.slice(i, j);
const low = raw.toLowerCase();
let k = j;
while (k < n && (s[k] === ' ' || s[k] === '\t')) k++;
if (KEYWORDS.has(low)) push('kw', raw);
else if (TYPES.has(low)) push('type', raw);
else if (s[k] === '(') push('fn', raw);
else push('id', raw);
i = j;
continue;
}
if ('=<>+-*/&,;:.()[]'.includes(c)) {
push('op', c);
i++;
continue;
}
push('text', c);
i++;
}
return out;
}
const BLANK_RE = /[\s\u00A0\u1680\u2000-\u200D\u2028\u2029\u202F\u205F\u3000\uFEFF]/g;
function isBlankLine(l) {
return !String(l || '').replace(BLANK_RE, '');
}
function compactSource(src) {
return String(src || '')
.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n')
.split('\n')
.filter(l => !isBlankLine(l))
.map(l => l.replace(/[ \t\u00A0]+$/g, ''))
.join('\n');
}
function tokenHtml(tok) {
if (tok.type === 'nl') return '';
if (tok.type === 'ws') return esc(tok.text);
const cls = tok.type;
return `<span class="st-${cls}">${esc(tok.text)}</span>`;
}
function highlightHtml(src) {
const tokens = tokenize(compactSource(src));
const lines = [[]];
tokens.forEach(tok => {
if (tok.type === 'nl') lines.push([]);
else lines[lines.length - 1].push(tok);
});
const kept = lines.filter(toks => !isBlankLine(toks.map(t => t.text).join('')));
const rows = kept.length ? kept : [[]];
const width = String(Math.max(1, rows.length)).length;
return rows.map((toks, i) => {
const ln = String(i + 1).padStart(width, ' ');
const code = toks.map(tokenHtml).join('') || ' ';
return `<div class="st-line"><span class="st-ln">${ln}</span><span class="st-code">${code}</span></div>`;
}).join('');
}
function svgFill(type) {
return COLORS[type] || COLORS.text;
}
function svgTspans(src) {
return tokenize(src).filter(t => t.type !== 'nl').map(t => {
if (t.type === 'ws') return esc(t.text);
return `<tspan fill="${svgFill(t.type)}">${esc(t.text)}</tspan>`;
}).join('');
}
const api = { tokenize, highlightHtml, svgTspans, compactSource, isBlankLine, COLORS };
global.L5XST = api;
if (typeof module !== 'undefined' && module.exports) module.exports = api;
})(typeof window !== 'undefined' ? window : globalThis);