[frontend] Add parser generation based on structure.json file definition
@@ -234,16 +234,15 @@ const listDir = folder =>
});
});
-const findParser = (fileIndex, folder, sharedFiles, autocomplete) => {
- const prefix = autocomplete ? 'autocomplete' : 'syntax';
- if (fileIndex[prefix + '_header.jison'] && fileIndex[prefix + '_footer.jison']) {
- const parserName = folder + (autocomplete ? 'AutocompleteParser' : 'SyntaxParser');
+const addParserDefinition = (sources, dialect, autocomplete, lexer) => {
+ const parserName = dialect + (autocomplete ? 'AutocompleteParser' : 'SyntaxParser');
+
const parserDefinition = {
- sources: ['sql/' + folder + '/' + prefix + '_header.jison'].concat(sharedFiles),
- lexer: 'sql/' + folder + '/sql.jisonlex',
- target: 'sql/' + folder + '/' + parserName + '.jison',
+ sources: sources,
+ lexer: 'sql/' + dialect + '/' + lexer,
+ target: 'sql/' + dialect + '/' + parserName + '.jison',
sqlParser: autocomplete ? 'AUTOCOMPLETE' : 'SYNTAX',
- outputFolder: OUTPUT_FOLDER + 'sql/' + folder + '/',
+ outputFolder: OUTPUT_FOLDER + 'sql/' + dialect + '/',
afterParse: contents =>
new Promise(resolve => {
resolve(
@@ -252,7 +251,7 @@ const findParser = (fileIndex, folder, sharedFiles, autocomplete) => {
.replace(
'var ' + parserName + ' = ',
"import SqlParseSupport from 'parse/sql/" +
- folder +
+ dialect +
"/sqlParseSupport';\n\nvar " +
parserName +
' = '
@@ -268,8 +267,37 @@ const findParser = (fileIndex, folder, sharedFiles, autocomplete) => {
})
};
- parserDefinition.sources.push('sql/' + folder + '/' + prefix + '_footer.jison');
parserDefinitions[parserName] = parserDefinition;
+};
+
+const addParsersFromStructure = (structure, dialect) => {
+ addParserDefinition(
+ structure.autocomplete.map(source => 'sql/' + dialect + '/' + source),
+ dialect,
+ true,
+ structure.lexer
+ );
+ addParserDefinition(
+ structure.syntax.map(source => 'sql/' + dialect + '/' + source),
+ dialect,
+ false,
+ structure.lexer
+ );
+};
+
+const findParser = (fileIndex, folder, sharedFiles, autocomplete) => {
+ const prefix = autocomplete ? 'autocomplete' : 'syntax';
+ if (fileIndex[prefix + '_header.jison'] && fileIndex[prefix + '_footer.jison']) {
+ addParserDefinition(
+ [
+ 'sql/' + folder + '/' + prefix + '_header.jison',
+ ...sharedFiles,
+ 'sql/' + folder + '/' + prefix + '_footer.jison'
+ ],
+ folder,
+ autocomplete,
+ 'sql.jisonlex'
+ );
} else {
console.log(
"Warn: Could not find '" +
@@ -291,7 +319,13 @@ const identifySqlParsers = () =>
const promises = [];
files.forEach(folder => {
promises.push(
- listDir(JISON_FOLDER + 'sql/' + folder).then(jisonFiles => {
+ listDir(JISON_FOLDER + 'sql/' + folder).then(async jisonFiles => {
+ if (jisonFiles.find(fileName => fileName === 'structure.json')) {
+ const structure = JSON.parse(
+ await readFile(JISON_FOLDER + 'sql/' + folder + '/structure.json')
+ );
+ addParsersFromStructure(structure, folder);
+ } else {
const fileIndex = {};
jisonFiles.forEach(jisonFile => {
fileIndex[jisonFile] = true;
@@ -314,6 +348,7 @@ const identifySqlParsers = () =>
"Warn: Could not find 'sql.jisonlex' in " + JISON_FOLDER + 'sql/' + folder + '/'
);
}
+ }
})
);
});
