mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-10-27 03:32:44 +03:00
Adding a WASMTreeSitterGrammar
This commit is contained in:
parent
ef24d20b2f
commit
e620291b9a
43
src/wasm-tree-sitter-grammar.js
Normal file
43
src/wasm-tree-sitter-grammar.js
Normal file
@ -0,0 +1,43 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Parser = require('web-tree-sitter');
|
||||
|
||||
module.exports = class WASMTreeSitterGrammar {
|
||||
constructor(registry, grammarPath, params) {
|
||||
this.scopeName = params.scopeName
|
||||
const dirName = path.dirname(grammarPath)
|
||||
const qPath = path.join(dirName, params.treeSitter.syntaxQuery)
|
||||
this.syntaxQuery = fs.readFileSync(qPath, 'utf-8')
|
||||
if(params.treeSitter.localsQuery) {
|
||||
const lPath = path.join(dirName, params.treeSitter.localsQuery)
|
||||
this.localsQuery = fs.readFileSync(lPath, 'utf-8')
|
||||
}
|
||||
this.grammarPath = path.join(dirName, params.treeSitter.grammar)
|
||||
this.contentRegex = buildRegex(params.contentRegex);
|
||||
this.firstLineRegex = buildRegex(params.firstLineRegex);
|
||||
this.fileTypes = params.fileTypes || [];
|
||||
this.registry = registry
|
||||
}
|
||||
|
||||
// TODO: Why is this here?
|
||||
activate() {
|
||||
this.registration = this.registry.addGrammar(this);
|
||||
}
|
||||
|
||||
// TODO: Why is this here?
|
||||
deactivate() {
|
||||
this.registration?.dispose();
|
||||
}
|
||||
|
||||
inspect() {
|
||||
return `TreeSitterGrammar {scopeName: ${this.scopeName}}`;
|
||||
}
|
||||
}
|
||||
|
||||
function buildRegex(value) {
|
||||
// Allow multiple alternatives to be specified via an array, for
|
||||
// readability of the grammar file
|
||||
if (Array.isArray(value)) value = value.map(_ => `(${_})`).join('|');
|
||||
if (typeof value === 'string') return new RegExp(value);
|
||||
return null;
|
||||
}
|
Loading…
Reference in New Issue
Block a user