2023-04-13 04:21:25 -04:00
|
|
|
const { buildParserFile } = require('@lezer/generator')
|
|
|
|
const { writeFileSync, readFileSync } = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
2023-07-03 06:18:27 -04:00
|
|
|
const grammars = [
|
|
|
|
{
|
|
|
|
grammarPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-latex/latex.grammar'
|
|
|
|
),
|
|
|
|
parserOutputPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-latex/latex.mjs'
|
|
|
|
),
|
|
|
|
termsOutputPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-latex/latex.terms.mjs'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
grammarPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-bibtex/bibtex.grammar'
|
|
|
|
),
|
|
|
|
parserOutputPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-bibtex/bibtex.mjs'
|
|
|
|
),
|
|
|
|
termsOutputPath: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../frontend/js/features/source-editor/lezer-bibtex/bibtex.terms.mjs'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
2023-04-13 04:21:25 -04:00
|
|
|
|
2023-07-03 06:18:27 -04:00
|
|
|
function compile(grammar) {
|
|
|
|
const { grammarPath, termsOutputPath, parserOutputPath } = grammar
|
2023-04-13 04:21:25 -04:00
|
|
|
const moduleStyle = 'es'
|
|
|
|
console.info(`Compiling ${grammarPath}`)
|
|
|
|
|
|
|
|
const grammarText = readFileSync(grammarPath, 'utf8')
|
|
|
|
console.info(`Loaded grammar from ${grammarPath}`)
|
|
|
|
|
|
|
|
const { parser, terms } = buildParserFile(grammarText, {
|
|
|
|
fileName: grammarPath,
|
|
|
|
moduleStyle,
|
|
|
|
})
|
|
|
|
console.info(`Built parser`)
|
|
|
|
|
|
|
|
writeFileSync(parserOutputPath, parser)
|
|
|
|
console.info(`Wrote parser to ${parserOutputPath}`)
|
|
|
|
|
|
|
|
writeFileSync(termsOutputPath, terms)
|
|
|
|
console.info(`Wrote terms to ${termsOutputPath}`)
|
|
|
|
|
|
|
|
console.info('Done!')
|
|
|
|
}
|
|
|
|
|
2023-07-03 06:18:27 -04:00
|
|
|
module.exports = { compile, grammars }
|
2023-04-13 04:21:25 -04:00
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
try {
|
2023-07-03 06:18:27 -04:00
|
|
|
grammars.forEach(compile)
|
2023-04-13 04:21:25 -04:00
|
|
|
process.exit(0)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|