generate.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { buildParserFile } = require('@lezer/generator')
  2. const { writeFileSync, readFileSync } = require('fs')
  3. const path = require('path')
  4. const grammars = [
  5. {
  6. grammarPath: path.resolve(
  7. __dirname,
  8. '../../frontend/js/features/source-editor/lezer-latex/latex.grammar'
  9. ),
  10. parserOutputPath: path.resolve(
  11. __dirname,
  12. '../../frontend/js/features/source-editor/lezer-latex/latex.mjs'
  13. ),
  14. termsOutputPath: path.resolve(
  15. __dirname,
  16. '../../frontend/js/features/source-editor/lezer-latex/latex.terms.mjs'
  17. ),
  18. },
  19. {
  20. grammarPath: path.resolve(
  21. __dirname,
  22. '../../frontend/js/features/source-editor/lezer-bibtex/bibtex.grammar'
  23. ),
  24. parserOutputPath: path.resolve(
  25. __dirname,
  26. '../../frontend/js/features/source-editor/lezer-bibtex/bibtex.mjs'
  27. ),
  28. termsOutputPath: path.resolve(
  29. __dirname,
  30. '../../frontend/js/features/source-editor/lezer-bibtex/bibtex.terms.mjs'
  31. ),
  32. },
  33. ]
  34. function compile(grammar) {
  35. const { grammarPath, termsOutputPath, parserOutputPath } = grammar
  36. const moduleStyle = 'es'
  37. console.info(`Compiling ${grammarPath}`)
  38. const grammarText = readFileSync(grammarPath, 'utf8')
  39. console.info(`Loaded grammar from ${grammarPath}`)
  40. const { parser, terms } = buildParserFile(grammarText, {
  41. fileName: grammarPath,
  42. moduleStyle,
  43. })
  44. console.info(`Built parser`)
  45. writeFileSync(parserOutputPath, parser)
  46. console.info(`Wrote parser to ${parserOutputPath}`)
  47. writeFileSync(termsOutputPath, terms)
  48. console.info(`Wrote terms to ${termsOutputPath}`)
  49. console.info('Done!')
  50. }
  51. module.exports = { compile, grammars }
  52. if (require.main === module) {
  53. try {
  54. grammars.forEach(compile)
  55. process.exit(0)
  56. } catch (err) {
  57. console.error(err)
  58. process.exit(1)
  59. }
  60. }