generate.js 1.3 KB

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