-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.js
More file actions
executable file
·66 lines (60 loc) · 1.9 KB
/
Copy pathhighlight.js
File metadata and controls
executable file
·66 lines (60 loc) · 1.9 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
#!/usr/bin/env node
// Simple regex-based syntax highlighter for Inference language
const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
if (args.length !== 1) {
console.error(`Usage: node ${path.basename(process.argv[1])} <file.inf>`);
process.exit(1);
}
const file = args[0];
let src;
try {
src = fs.readFileSync(file, 'utf8');
} catch (e) {
console.error(`Error reading file ${file}:`, e.message);
process.exit(1);
}
// Escape HTML
function escapeHtml(s) {
return s.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
// Define token patterns and classes
const patterns = [
{cls: 'comment', re: /^\/\/\/.*(?:\r?\n|$)/},
{cls: 'string', re: /^"(?:\\.|[^"\\])*"/},
{cls: 'type', re: /^(?:\b(?:i8|i16|i32|i64|u8|u16|u32|u64|bool)\b|\(\))/},
{cls: 'keyword', re: /^(?:\b(?:fn|forall|exists|assume|unique|loop|if|else|break|return|let|const|type|enum|struct|use|spec|external)\b)/},
{cls: 'boolean', re: /^(?:\b(?:true|false)\b)/},
{cls: 'number', re: /^-?\d+(?:\.\d+)?/},
{cls: 'operator', re: /^(?:->|::|\*\*|==|!=|<=|>=|&&|\|\||<<|>>|[=+\-*/%&|^<>])/},
{cls: 'punctuation', re: /^[{}\[\]();,]/},
{cls: 'identifier', re: /^\b[A-Za-z_]\w*\b/},
{cls: 'whitespace', re: /^\s+/},
{cls: 'text', re: /^./}
];
// Highlight
let out = '';
let pos = 0;
while (pos < src.length) {
let matched = false;
for (const {cls, re} of patterns) {
const m = re.exec(src.slice(pos));
if (!m) continue;
const tok = m[0];
const esc = escapeHtml(tok);
if (cls === 'whitespace' || cls === 'text') {
out += esc;
} else {
out += `<span class="${cls}">${esc}</span>`;
}
pos += tok.length;
matched = true;
break;
}
if (!matched) { out += escapeHtml(src[pos]); pos++; }
}
// Wrap in pre/code
process.stdout.write('<pre><code>' + out + '</code></pre>');