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
|
// the pretty-printer: it never changes a program's tokens, it is stable (formatting twice is the same as once),
// it follows .zcformat and the editor's tab settings, and it leaves asm blocks, strings and comments alone.
// usage: node tests/drivers/format_test.js path/to/zclsp (run from the repository root)
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const server = process.argv[2];
const root = path.resolve(__dirname, '..', '..');
const format = (text, args = []) => cp.spawnSync(server, ['--format', ...args], { input: text, cwd: root, maxBuffer: 1 << 28 }).stdout.toString();
// tokens of a source, white space dropped; the lines of a block comment are compared without their indent
function tokens(src) {
const out = [];
let i = 0;
const n = src.length;
while (i < n) {
const c = src[i];
if (/\s/.test(c)) { i++; continue; }
if (c === '`') { out.push(src.slice(i)); break; }
if (c === '/' && src[i + 1] === '/') { let j = src.indexOf('\n', i); if (j < 0) j = n; out.push(src.slice(i, j).trimEnd()); i = j; continue; }
if (c === '/' && src[i + 1] === '*') { let j = src.indexOf('*/', i + 2); j = j < 0 ? n : j + 2; out.push(src.slice(i, j).split('\n').map(l => l.trim()).join('\n')); i = j; continue; }
if (c === '"' || c === "'") { let j = i + 1; while (j < n && src[j] !== c && src[j] !== '\n') { if (src[j] === '\\') j++; j++; } out.push(src.slice(i, j + 1)); i = j + 1; continue; }
if (/[A-Za-z0-9_\u0080-]/.test(c)) { let j = i; while (j < n && /[A-Za-z0-9_\u0080-]/.test(src[j])) j++; out.push(src.slice(i, j)); i = j; continue; }
out.push(c); i++;
}
// adjacent symbol characters are compared one by one, so merged or split operators still match
return out;
}
const files = [];
for (const dir of ['examples', 'examples/learn', 'tests/programs', 'tests/errors', 'tests/typeerrors', 'tests/langerrors', 'src/lib', 'src/compiler', 'src/runtime', 'src/lsp', 'src/tools'])
for (const f of fs.readdirSync(path.join(root, dir))) if (f.endsWith('.zc')) files.push(path.join(dir, f));
assert.ok(files.length > 100, 'the corpus: ' + files.length);
let checked = 0;
for (const f of files) {
const src = fs.readFileSync(path.join(root, f), 'utf8');
const once = format(src);
assert.deepStrictEqual(tokens(once), tokens(src), f + ': formatting changed the tokens');
assert.strictEqual(format(once), once, f + ': formatting is not stable');
assert.ok(once === '' || once.endsWith('\n'), f + ': ends with a line break');
if (!src.includes('`')) assert.ok(!/[ \t]+$/m.test(once), f + ': trailing white space');
checked++;
}
// the behaviour
const sample = 'fn main(){\nint x=1+2 ;\n if(x>1){\n put_n( x );\n}\n\n\n\nreturn 0;}\n';
assert.strictEqual(format(sample), 'fn main() {\n\tint x = 1+2;\n\tif (x>1) {\n\t\tput_n(x);\n\t}\n\n\treturn 0; }\n');
assert.strictEqual(format('fn f(a,b){return a*b;}\n'), 'fn f(a, b) { return a*b; }\n');
assert.strictEqual(format('fn main() {\n\tswitch (k) {\n\tcase 1: return 1;\n\tdefault:\n\t\treturn 2;\n\t}\n}\n'), 'fn main() {\n\tswitch (k) {\n\t\tcase 1: return 1;\n\t\tdefault:\n\t\t\treturn 2;\n\t}\n}\n');
assert.strictEqual(format('fn main() {\nlet a = 1 +\n2;\nif (a)\nreturn 1;\nreturn 0;\n}\n'), 'fn main() {\n\tlet a = 1 +\n\t\t2;\n\tif (a)\n\t\treturn 1;\n\treturn 0;\n}\n');
assert.strictEqual(format('fn put_f(x) { asm { x @ #1220 } return 0; }\n'), 'fn put_f(x) { asm { x @ #1220 } return 0; }\n');
assert.strictEqual(format('fn k() { asm {\n 0 , ?\n} return 1; }\n'), 'fn k() { asm {\n\t\t0 , ?\n\t} return 1; }\n');
assert.strictEqual(format('/* a\n * b\n */\nint g = 1; // note\nint h;\n'), '/* a\n * b\n */\nint g = 1; // note\nint h;\n');
assert.strictEqual(format('fn main() {\n\tif (x)\n\t{\n\t\ty();\n\t}\n\telse\n\t{\n\t\tz();\n\t}\n}'), 'fn main() {\n\tif (x)\n\t{\n\t\ty();\n\t}\n\telse\n\t{\n\t\tz();\n\t}\n}\n');
assert.strictEqual(format('fn main() { return 0; }\n`data stays\n as it is\n'), 'fn main() { return 0; }\n`data stays\n as it is\n');
// .zcformat next to a document: found from the document's directory upward; the editor's tab settings apply without one
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'zcfmt-'));
fs.mkdirSync(path.join(dir, 'a', 'b'), { recursive: true });
fs.writeFileSync(path.join(dir, '.zcformat'), '# comment\nUseTab: false\nIndentWidth: 2\nMaxEmptyLines: 0\nNormalizeSpacing: false\n');
const frame = m => { const b = Buffer.from(JSON.stringify(m)); return Buffer.concat([Buffer.from('Content-Length: ' + b.length + '\r\n\r\n'), b]); };
function lsp(uri, text, options) {
const input = Buffer.concat([
{ jsonrpc: '2.0', id: 1, method: 'initialize', params: {} },
{ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, version: 1, languageId: 'zc', text } } },
{ jsonrpc: '2.0', id: 2, method: 'textDocument/formatting', params: { textDocument: { uri }, options } },
{ jsonrpc: '2.0', method: 'exit' }].map(frame));
const r = cp.spawnSync(server, process.argv.slice(3), { input, cwd: root, maxBuffer: 1 << 26 });
let d = r.stdout, caps, res;
while (d.length) {
const e = d.indexOf('\r\n\r\n'), n = Number(/Content-Length:\s*(\d+)/i.exec(d.subarray(0, e).toString())[1]);
const m = JSON.parse(d.subarray(e + 4, e + 4 + n)); d = d.subarray(e + 4 + n);
if (m.id === 1) caps = m.result.capabilities;
if (m.id === 2) res = m.result;
}
return { caps, res };
}
const messy = 'fn main(){\nif(1){\nput_n( 1 );\n\n\nput_c(10);}\nreturn 0;}\n';
let r = lsp('file://' + path.join(dir, 'a', 'b', 'x.zc'), messy, { tabSize: 8, insertSpaces: false });
assert.ok(r.caps.documentFormattingProvider, 'formatting is advertised');
assert.strictEqual(r.res.length, 1);
assert.strictEqual(r.res[0].newText, 'fn main(){\n if(1){\n put_n( 1 );\n put_c(10);}\n return 0;}\n', '.zcformat from two directories up wins over the editor');
assert.deepStrictEqual(r.res[0].range.start, { line: 0, character: 0 });
fs.rmSync(path.join(dir, '.zcformat'));
r = lsp('file://' + path.join(dir, 'a', 'y.zc'), messy, { tabSize: 3, insertSpaces: true });
assert.strictEqual(r.res[0].newText, 'fn main() {\n if (1) {\n put_n(1);\n\n put_c(10); }\n return 0; }\n', 'the editor settings without a .zcformat');
r = lsp('file://' + path.join(dir, 'a', 'y.zc'), 'fn main() {\n\treturn 0;\n}\n', { tabSize: 4, insertSpaces: false });
assert.deepStrictEqual(r.res, [], 'a formatted document gets no edit');
fs.rmSync(dir, { recursive: true });
console.log('ok formatter: ' + checked + ' files keep their tokens and format stably; options and edits');
|