-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
77 lines (63 loc) · 1.7 KB
/
parser.js
File metadata and controls
77 lines (63 loc) · 1.7 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
import fs from 'fs';
import beautify from 'js-beautify';
import uglify from "uglify-js";
import grammarRules from './lib/grammar.js'
const args = {
files: process.argv.slice(2).filter(a => !a.startsWith('-')),
options: process.argv.slice(2).filter(a => a.startsWith('-')).map(a => a.substr(1)).join('')
}
const input = fs.readFileSync(args.files[0], 'utf-8');
const lines = input.split('\n').map(line => line);
let output = `import Discord from "discord.js";\n`;
if (!input)
throw new Error('input is empty');
let previousIndent = 0,
bracketClosers = [],
resetBrackets = () => output += bracketClosers.join(';'),
ignore = false;
for (let line of lines) {
line = line.split("//")[0];
if (!line.trim()) continue;
const indent = line.match(/^\s*/)[0].length;
let indentChange = indent - previousIndent;
previousIndent = indent;
if (indentChange < 0)
for (var i = 0; i < -indentChange; i++)
output += bracketClosers.shift() || '';
if (line.trim() == '#{') {
ignore = true;
continue;
} else
if (line.trim() == '}#') {
ignore = false;
continue;
} else
if (ignore) {
output += line;
continue;
}
let successLine = false;
for (let [regex, parser] of grammarRules) {
if (regex.test(line)) {
let parsed = parser(line.match(regex).groups || {});
if (Array.isArray(parsed)) {
bracketClosers.push(parsed[1] + ';');
parsed = parsed[0];
}
output += parsed + '\n';
successLine = true;
break;
};
}
if (!successLine)
console.log('Error:', line)
}
resetBrackets();
if (args.options.includes('m')) //beautify
output = uglify.minify(output).code;
else //beautify
output = beautify.js(output);
if (args.files[1])
fs.writeFileSync(args.files[1], output);
else
console.log(output);