Uh oh!
There was an error while loading. Please reload this page.
forked from taozhi8833998/node-sql-parser
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypegen.js
More file actions
Latest commit
112 lines (104 loc) · 3.48 KB
/
Copy pathtypegen.js
File metadata and controls
112 lines (104 loc) · 3.48 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
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
constfs=require('fs');
constpath=require('path');
constpeg=require('pegjs');
constsyntax=fs.readFileSync(path.resolve(__dirname,'./pegjs/postgresql.pegjs'),'utf-8')
constast=peg.parser.parse(syntax);
constbuilt=[];
functioncheckCode(r,onName){
constsimple=/^[\s$]*\/\/\s*=>\s*([^$\r\n]+)$/m.exec(r.code);
if(simple)returnsimple[1].trim();
constcomplex=/^[\s$]*\/\*([^§]+)\*\//m.exec(r.code);
if(!complex)thrownewError('You must provide a type for code block: '+onName);
consttypecode=complex[1];
constat=typecode.lastIndexOf('=>');
if(at<0)thrownewError('Wrong type code format for code block: '+onName);
consttoInsert=typecode.substr(0,at);
built.push(toInsert.trim());
returntypecode.substr(at+2).trim();
}
functionbuildExpression(r,onName){
if(r.code)returncheckCode(r,onName)
switch(r.type){
case'choice':
constret=r.alternatives
.map(e=>buildExpression(e,onName))
.map(e=>e&&e.trim())
returnret.some(x=>!x)
? null
: [...newSet(ret.filter(e=>e!=='IGNORE'))].join(' | ').trim();
case'rule_ref':
returnr.name;
case'literal':
returnJSON.stringify(r.value);
case'labeled': // mmmh.. weird. Remove ? See extract_filed
returnbuildExpression(r.expression,onName);
case'zero_or_more':
case'one_or_more':
constof=buildExpression(r.expression,onName);
returnof&&`(${of.trim()})[]`;
case'simple_not':
case'class':
case'any':
case'sequence':
returnnull;
default:
thrownewError('Unsupported node type: '+r.type);
}
}
letparsed=0;
conststrings=newSet([
'single_quote_char',
'single_char',
'escape_char',
'line_terminator',
'int',// yea...
'frac',
'digits',
'digit',
'hexDigit',
'e',
'exp',
'ident'
])
for(constrofast.rules){
if(r.name==='number'){
continue;
}
letvalue;
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 !`);