-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathindex.js
236 lines (220 loc) · 7.45 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const antlr4 = require('antlr4');
const ECMAScriptLexer = require('./lib/antlr/ECMAScriptLexer.js');
const ECMAScriptParser = require('./lib/antlr/ECMAScriptParser.js');
const JavascriptANTLRVisitor =
require('./lib/antlr/ECMAScriptVisitor').ECMAScriptVisitor;
const ErrorListener = require('./codegeneration/ErrorListener.js');
const {
BsonTranspilersInternalError,
BsonTranspilersUnimplementedError,
} = require('./helper/error');
const yaml = require('js-yaml');
const getCodeGenerationVisitor = require('./codegeneration/CodeGenerationVisitor');
const getJavascriptVisitor = require('./codegeneration/javascript/Visitor');
const getShellVisitor = require('./codegeneration/shell/Visitor');
const getJavaGenerator = require('./codegeneration/java/Generator');
const getPythonGenerator = require('./codegeneration/python/Generator');
const getCsharpGenerator = require('./codegeneration/csharp/Generator');
const getJavascriptGenerator = require('./codegeneration/javascript/Generator');
const getObjectGenerator = require('./codegeneration/object/Generator');
const getRubyGenerator = require('./codegeneration/ruby/Generator.js');
const getGoGenerator = require('./codegeneration/go/Generator.js');
const getRustGenerator = require('./codegeneration/rust/Generator.js');
const getPhpGenerator = require('./codegeneration/php/Generator.js');
const shelljavasymbols = require('./lib/symbol-table/shelltojava');
const shellpythonsymbols = require('./lib/symbol-table/shelltopython');
const shellcsharpsymbols = require('./lib/symbol-table/shelltocsharp');
const shelljavascriptsymbols = require('./lib/symbol-table/shelltojavascript');
const shellobjectsymbols = require('./lib/symbol-table/shelltoobject');
const shellrubysymbols = require('./lib/symbol-table/shelltoruby');
const shellgosymbols = require('./lib/symbol-table/shelltogo');
const shellrustsymbols = require('./lib/symbol-table/shelltorust');
const shellphpsymbols = require('./lib/symbol-table/shelltophp');
/**
* Constructs the parse tree from the JS or Shell code given by the user.
*
* @param {String} input - the input code.
* @param {String} start - the name of the start node. Always defined in the
* language-specific visitor.
* @return {antlr4.ParserRuleContext} - The parse tree.
*/
const loadJSTree = (input, start) => {
const chars = new antlr4.InputStream(input);
const lexer = new ECMAScriptLexer.ECMAScriptLexer(chars);
lexer.strictMode = false;
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new ECMAScriptParser.ECMAScriptParser(tokens);
parser.buildParseTrees = true;
const listener = new ErrorListener();
parser.removeErrorListeners(); // Remove the default ConsoleErrorListener
parser.addErrorListener(listener); // Add back a custom error listener
return parser[start]();
};
const getTranspiler = (loadTree, visitor, generator, symbols) => {
const Transpiler = generator(visitor);
const transpiler = new Transpiler();
const doc = yaml.load(symbols);
/* Object validation. If the symbol table is missing any of these elements,
* then an error should be thrown. Can be empty, but must exist. */
[
'BasicTypes',
'BsonTypes',
'NativeTypes',
'SymbolTypes',
'BsonTypes',
'BsonSymbols',
'NativeSymbols',
'SymbolTypes',
].map((k) => {
if (!(k in doc)) {
throw new BsonTranspilersInternalError(
`Invalid Symbol Table: missing ${k}`
);
}
});
Object.assign(transpiler, {
SYMBOL_TYPE: doc.SymbolTypes,
BsonTypes: doc.BsonTypes,
Symbols: Object.assign({}, doc.BsonSymbols, doc.NativeSymbols),
Types: Object.assign({}, doc.BasicTypes, doc.BsonTypes, doc.NativeTypes),
Syntax: doc.Syntax,
Imports: doc.Imports,
});
const compile = (input, idiomatic, driverSyntax) => {
try {
const tree = loadTree(input, transpiler.startRule);
transpiler.idiomatic =
idiomatic === undefined ? transpiler.idiomatic : idiomatic;
if (!driverSyntax) {
transpiler.clearImports();
transpiler.clearDeclarations();
}
return transpiler.start(tree, !driverSyntax);
} catch (e) {
if (e.code && e.code.includes('BSONTRANSPILERS')) {
throw e;
}
throw new BsonTranspilersInternalError(e.message, e);
} finally {
transpiler.idiomatic = true;
}
};
return {
compileWithDriver: (input, idiomatic) => {
transpiler.clearImports();
transpiler.clearDeclarations();
const result = {};
Object.keys(input).map((k) => {
result[k] =
k === 'options' || k === 'exportMode'
? input[k]
: compile(input[k], idiomatic, true);
});
if (
!('options' in result) ||
!('uri' in result.options) ||
!('database' in result.options) ||
!('collection' in result.options)
) {
throw new BsonTranspilersInternalError(
'Missing required metadata to generate drivers syntax'
);
}
if (!('aggregation' in result) && !('filter' in result)) {
throw new BsonTranspilersInternalError(
"Need to pass 'aggregation' or 'filter' when compiling with driver syntax"
);
}
if (!transpiler.Syntax.driver) {
throw new BsonTranspilersUnimplementedError(
'Generating driver syntax not implemented for current language'
);
}
return transpiler.Syntax.driver.bind(transpiler.getState())(result);
},
compile: compile,
getImports: (mode, driverSyntax) => {
return transpiler.getImports(mode, driverSyntax);
},
};
};
module.exports = {
shell: {
java: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getJavaGenerator,
shelljavasymbols
),
python: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getPythonGenerator,
shellpythonsymbols
),
csharp: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getCsharpGenerator,
shellcsharpsymbols
),
javascript: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getJavascriptGenerator,
shelljavascriptsymbols
),
object: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getObjectGenerator,
shellobjectsymbols
),
ruby: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getRubyGenerator,
shellrubysymbols
),
go: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getGoGenerator,
shellgosymbols
),
rust: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getRustGenerator,
shellrustsymbols
),
php: getTranspiler(
loadJSTree,
getShellVisitor(
getJavascriptVisitor(getCodeGenerationVisitor(JavascriptANTLRVisitor))
),
getPhpGenerator,
shellphpsymbols
),
},
getTree: {
shell: loadJSTree,
},
};