forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typegen.js
112 lines (104 loc) · 3.48 KB
/
typegen.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
const fs = require('fs');
const path = require('path');
const peg = require('pegjs');
const syntax = fs.readFileSync(path.resolve(__dirname, './pegjs/postgresql.pegjs'), 'utf-8')
const ast = peg.parser.parse(syntax);
const built = [];
function checkCode(r, onName) {
const simple = /^[\s$]*\/\/\s*=>\s*([^$\r\n]+)$/m.exec(r.code);
if (simple) return simple[1].trim();
const complex = /^[\s$]*\/\*([^§]+)\*\//m.exec(r.code);
if (!complex) throw new Error('You must provide a type for code block: ' + onName);
const typecode = complex[1];
const at = typecode.lastIndexOf('=>');
if (at < 0) throw new Error('Wrong type code format for code block: ' + onName);
const toInsert = typecode.substr(0, at);
built.push(toInsert.trim());
return typecode.substr(at + 2).trim();
}
function buildExpression(r, onName) {
if (r.code) return checkCode(r, onName)
switch (r.type) {
case 'choice':
const ret = r.alternatives
.map(e => buildExpression(e, onName))
.map(e => e && e.trim())
return ret.some(x => !x)
? null
: [...new Set(ret.filter(e => e !== 'IGNORE'))].join(' | ').trim();
case 'rule_ref':
return r.name;
case 'literal':
return JSON.stringify(r.value);
case 'labeled': // mmmh.. weird. Remove ? See extract_filed
return buildExpression(r.expression, onName);
case 'zero_or_more':
case 'one_or_more':
const of = buildExpression(r.expression, onName);
return of && `(${of.trim()})[]`;
case 'simple_not':
case 'class':
case 'any':
case 'sequence':
return null;
default:
throw new Error('Unsupported node type: ' + r.type);
}
}
let parsed = 0;
const strings = new Set([
'single_quote_char',
'single_char',
'escape_char',
'line_terminator',
'int', // yea...
'frac',
'digits',
'digit',
'hexDigit',
'e',
'exp',
'ident'
])
for (const r of ast.rules) {
if (r.name === 'number') {
continue;
}
let value;
switch (r.name) {
case 'data_type':
value = `{
dataType: string;
length?: number;
suffix?: string;
scale?: number;
parentheses?: boolean;
expr?: expr_list;
}`;
break;
default:
value = strings.has(r.name) ? 'string'
: r.name.indexOf('KW_') === 0 ? null
: buildExpression(r.expression, r.name);
break;
}
if (value) {
built.push(`export type ${r.name} = ${value.trim()};`);
} else {
built.push(`type ${r.name} = never;`);
// console.log('Ignoring ' + r.name);
}
parsed++;
}
if (!fs.existsSync(path.resolve(__dirname, './ast'))) {
fs.mkdirSync(path.resolve(__dirname, './ast'));
}
fs.writeFileSync(path.resolve(__dirname, './ast/postgresql.ts'), `
/*
⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔
⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔ THIS FILE HAS BEEN AUTO-GENERATED DO NOT EDIT IT ! ⛔⛔⛔⛔⛔⛔⛔⛔
⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔⛔
*/
`
+ built.join('\n\n'));
console.log(`Parsed ${parsed}/${ast.rules.length} rules !`);