-
Notifications
You must be signed in to change notification settings - Fork 2
/
show-pgml-tree.js
executable file
·139 lines (127 loc) · 3.33 KB
/
show-pgml-tree.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
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { format } from 'prettier';
import { fileTests } from '@lezer/generator/dist/test';
import { pgmlLanguage } from './dist/index.js';
const argv = yargs(hideBin(process.argv))
.usage('$0 <file>')
.version(false)
.option('file', {
alias: 'f',
description: 'Show the tree for code in a given Perl file.',
type: 'string'
})
.option('test-suite', {
alias: 's',
description: 'Show the tree for code in the specified test suite.',
type: 'string'
})
.option('test', {
alias: 't',
description: 'Show the tree for only the specified test.',
type: 'string'
})
.alias('help', 'h')
.conflicts('file', 'test-suite')
.check((argv) => (argv.f || argv.s ? true : 'Either the file or test-suite option is required.'))
.parse();
const sources = [];
try {
if (argv.f) {
sources.push({ name: argv.f, text: readFileSync(argv.f, 'utf8') });
} else if (argv.s) {
const tests = fileTests(
readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'test', `${argv.s}.txt`), 'utf8'),
`${argv.s}.txt`
);
for (const { name, text } of tests) {
if (argv.t && argv.t !== name) continue;
sources.push({ name, text });
}
}
} catch (e) {
console.log(e.message);
process.exit();
}
const keywords = [
'await',
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'implements',
'import',
'in',
'instanceof',
'interface',
'let',
'new',
'null',
'package',
'private',
'protected',
'public',
'require',
'return',
'static',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield'
];
for (const { name, text } of sources) {
console.log(`Test: \x1b[1m\x1b[34m${name}\x1b[0m\n`);
console.log(text.trim());
console.log('\n\x1b[1m\x1b[32m==>\x1b[0m\n');
const tree = pgmlLanguage.parser.parse(text);
try {
let adjustedSource = tree.toString().replaceAll('⚠', '"⚠"');
for (const keyword of keywords) {
const keywordRegexp = new RegExp(`\\b(${keyword})\\b`, 'g');
adjustedSource = adjustedSource.replace(keywordRegexp, '_$1_');
}
let formattedProgram = await format(adjustedSource, {
parser: 'babel',
semi: false,
printWidth: 120,
tabWidth: 4,
trailingComma: 'none'
});
formattedProgram = formattedProgram.replaceAll('"⚠"', '\x1b[41m\x1b[37m⚠\x1b[0m');
for (const keyword of keywords) {
const keywordRegexp = new RegExp(`\\b_(${keyword})_\\b`, 'g');
formattedProgram = formattedProgram.replace(keywordRegexp, '$1');
}
console.log(formattedProgram);
} catch (e) {
console.log(e.message);
console.log(tree.toString());
}
}