generateParsers.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. const fs = require('fs');
  17. const exec = require('child_process').exec;
  18. const LICENSE =
  19. '// Licensed to Cloudera, Inc. under one\n' +
  20. '// or more contributor license agreements. See the NOTICE file\n' +
  21. '// distributed with this work for additional information\n' +
  22. '// regarding copyright ownership. Cloudera, Inc. licenses this file\n' +
  23. '// to you under the Apache License, Version 2.0 (the\n' +
  24. '// "License"); you may not use this file except in compliance\n' +
  25. '// with the License. You may obtain a copy of the License at\n' +
  26. '//\n' +
  27. '// http://www.apache.org/licenses/LICENSE-2.0\n' +
  28. '//\n' +
  29. '// Unless required by applicable law or agreed to in writing, software\n' +
  30. '// distributed under the License is distributed on an "AS IS" BASIS,\n' +
  31. '// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' +
  32. '// See the License for the specific language governing permissions and\n' +
  33. '// limitations under the License.\n';
  34. const SQL_STATEMENTS_PARSER_JSDOC =
  35. '/**\n' +
  36. ' * @param {string} input\n' +
  37. ' *\n' +
  38. ' * @return {SqlStatementsParserResult}\n' +
  39. ' */\n';
  40. const JISON_FOLDER = 'desktop/core/src/desktop/js/parse/jison/';
  41. const TARGET_FOLDER = 'desktop/core/src/desktop/js/parse/';
  42. const PARSERS = {
  43. globalSearchParser: {
  44. sources: ['globalSearchParser.jison'],
  45. target: 'globalSearchParser.jison',
  46. afterParse: contents =>
  47. new Promise(resolve => {
  48. resolve(
  49. LICENSE +
  50. contents.replace(
  51. 'var globalSearchParser = ',
  52. "import SqlParseSupport from 'parse/sqlParseSupport';\n\nvar globalSearchParser = "
  53. ) +
  54. '\nexport default globalSearchParser;\n'
  55. );
  56. })
  57. },
  58. solrFormulaParser: {
  59. sources: ['solrFormulaParser.jison'],
  60. target: 'solrFormulaParser.jison',
  61. afterParse: contents =>
  62. new Promise(resolve => {
  63. resolve(LICENSE + contents + 'export default solrFormulaParser;\n');
  64. })
  65. },
  66. solrQueryParser: {
  67. sources: ['solrQueryParser.jison'],
  68. target: 'solrQueryParser.jison',
  69. afterParse: contents =>
  70. new Promise(resolve => {
  71. resolve(LICENSE + contents + 'export default solrQueryParser;\n');
  72. })
  73. },
  74. sqlAutocompleteParser: {
  75. sources: [
  76. 'autocomplete_header.jison',
  77. 'sql_main.jison',
  78. 'sql_valueExpression.jison',
  79. 'sql_error.jison',
  80. 'sql_alter.jison',
  81. 'sql_analyze.jison',
  82. 'sql_create.jison',
  83. 'sql_drop.jison',
  84. 'sql_grant.jison',
  85. 'sql_insert.jison',
  86. 'sql_load.jison',
  87. 'sql_set.jison',
  88. 'sql_show.jison',
  89. 'sql_update.jison',
  90. 'sql_use.jison',
  91. 'autocomplete_footer.jison'
  92. ],
  93. target: 'sqlAutocompleteParser.jison',
  94. lexer: 'sql.jisonlex',
  95. afterParse: contents =>
  96. new Promise(resolve => {
  97. resolve(
  98. LICENSE +
  99. contents.replace(
  100. 'var sqlAutocompleteParser = ',
  101. "import SqlParseSupport from 'parse/sqlParseSupport';\n\nvar sqlAutocompleteParser = "
  102. ) +
  103. '\nexport default sqlAutocompleteParser;\n'
  104. );
  105. })
  106. },
  107. sqlStatementsParser: {
  108. sources: ['sqlStatementsParser.jison'],
  109. target: 'sqlStatementsParser.jison',
  110. afterParse: contents =>
  111. new Promise(resolve => {
  112. resolve(
  113. LICENSE +
  114. contents.replace(
  115. 'parse: function parse',
  116. SQL_STATEMENTS_PARSER_JSDOC + 'parse: function parse'
  117. ) +
  118. 'export default sqlStatementsParser;\n'
  119. );
  120. })
  121. },
  122. sqlSyntaxParser: {
  123. sources: [
  124. 'syntax_header.jison',
  125. 'sql_main.jison',
  126. 'sql_valueExpression.jison',
  127. 'sql_alter.jison',
  128. 'sql_analyze.jison',
  129. 'sql_create.jison',
  130. 'sql_drop.jison',
  131. 'sql_grant.jison',
  132. 'sql_insert.jison',
  133. 'sql_load.jison',
  134. 'sql_set.jison',
  135. 'sql_show.jison',
  136. 'sql_update.jison',
  137. 'sql_use.jison',
  138. 'syntax_footer.jison'
  139. ],
  140. target: 'sqlSyntaxParser.jison',
  141. lexer: 'sql.jisonlex',
  142. afterParse: contents =>
  143. new Promise(resolve => {
  144. resolve(
  145. LICENSE +
  146. contents
  147. .replace(
  148. 'var sqlSyntaxParser = ',
  149. "import SqlParseSupport from 'parse/sqlParseSupport';\n\nvar sqlSyntaxParser = "
  150. )
  151. .replace(
  152. 'loc: yyloc,',
  153. "loc: lexer.yylloc, ruleId: stack.slice(stack.length - 2, stack.length).join(''),"
  154. ) +
  155. '\nexport default sqlSyntaxParser;\n'
  156. );
  157. })
  158. }
  159. };
  160. const readFile = path =>
  161. new Promise((resolve, reject) => {
  162. fs.readFile(path, (err, buf) => {
  163. if (err) {
  164. reject();
  165. }
  166. resolve(buf.toString());
  167. });
  168. });
  169. const writeFile = (path, contents) =>
  170. new Promise((resolve, reject) => {
  171. fs.writeFile(path, contents, (err, data) => {
  172. if (err) {
  173. reject();
  174. }
  175. resolve();
  176. });
  177. });
  178. const deleteFile = path => {
  179. fs.unlinkSync(path);
  180. };
  181. const execCmd = cmd =>
  182. new Promise((resolve, reject) => {
  183. exec(cmd, (err, stdout, stderr) => {
  184. if (err) {
  185. reject(stderr);
  186. }
  187. resolve();
  188. });
  189. });
  190. const generateParser = parserName =>
  191. new Promise((resolve, reject) => {
  192. const parserConfig = PARSERS[parserName];
  193. const concatPromise = new Promise((resolve, reject) => {
  194. if (parserConfig.sources.length > 1 && parserConfig.target) {
  195. console.log('Concatenating files...');
  196. const promises = parserConfig.sources.map(fileName => readFile(JISON_FOLDER + fileName));
  197. Promise.all(promises)
  198. .then(contents => {
  199. writeFile(JISON_FOLDER + parserConfig.target, contents).then(() => {
  200. resolve(JISON_FOLDER + parserConfig.target);
  201. });
  202. })
  203. .catch(reject);
  204. } else if (parserConfig.sources.length === 1) {
  205. resolve(JISON_FOLDER + parserConfig.sources[0]);
  206. } else {
  207. reject('No jison source specified');
  208. }
  209. });
  210. concatPromise
  211. .then(targetPath => {
  212. let jisonCommand = 'jison ' + targetPath;
  213. if (parserConfig.lexer) {
  214. jisonCommand += ' ' + JISON_FOLDER + parserConfig.lexer;
  215. }
  216. jisonCommand += ' -m js';
  217. console.log('Generating parser...');
  218. execCmd(jisonCommand)
  219. .then(() => {
  220. if (parserConfig.sources.length > 1) {
  221. deleteFile(targetPath); // Remove concatenated file
  222. }
  223. console.log('Adjusting JS...');
  224. const generatedJsFileName = parserConfig.target.replace('.jison', '.js');
  225. readFile(generatedJsFileName)
  226. .then(contents => {
  227. parserConfig
  228. .afterParse(contents)
  229. .then(finalContents => {
  230. writeFile(TARGET_FOLDER + generatedJsFileName, finalContents)
  231. .then(() => {
  232. deleteFile(generatedJsFileName);
  233. console.log('Done!\n');
  234. resolve();
  235. })
  236. .catch(reject);
  237. })
  238. .catch(reject);
  239. })
  240. .catch(reject);
  241. })
  242. .catch(reject);
  243. })
  244. .catch(reject);
  245. });
  246. let parsersToGenerate = [];
  247. const invalid = [];
  248. let all = false;
  249. let appFound = false;
  250. process.argv.forEach(arg => {
  251. if (appFound) {
  252. if (arg === 'all') {
  253. all = true;
  254. } else if (PARSERS[arg]) {
  255. parsersToGenerate.push(arg);
  256. } else {
  257. invalid.push(arg);
  258. }
  259. } else if (arg.indexOf('generateParsers.js') !== -1) {
  260. appFound = true;
  261. }
  262. });
  263. if (all) {
  264. parsersToGenerate = Object.keys(PARSERS);
  265. }
  266. if (invalid.length) {
  267. console.log("No parser config found for: '" + invalid.join("', '") + "'");
  268. console.log(
  269. '\nPossible options are:\n ' + ['all'].concat(Object.keys(PARSERS)).join('\n ') + '\n'
  270. );
  271. return;
  272. }
  273. const parserCount = parsersToGenerate.length;
  274. let idx = 0;
  275. const generateRecursive = () => {
  276. idx++;
  277. if (parsersToGenerate.length) {
  278. const parserName = parsersToGenerate.pop();
  279. if (parserCount > 1) {
  280. console.log("Generating '" + parserName + "' (" + idx + '/' + parserCount + ')...');
  281. } else {
  282. console.log("Generating '" + parserName + "'...");
  283. }
  284. generateParser(parserName)
  285. .then(generateRecursive)
  286. .catch(error => {
  287. console.log(error);
  288. console.log('FAIL!');
  289. });
  290. }
  291. };
  292. generateRecursive();