From e620291b9a4fc10d811cdb6a00465d1fec8dde71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Szabo?= Date: Wed, 22 Feb 2023 01:22:38 -0300 Subject: [PATCH] Adding a WASMTreeSitterGrammar --- src/wasm-tree-sitter-grammar.js | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/wasm-tree-sitter-grammar.js diff --git a/src/wasm-tree-sitter-grammar.js b/src/wasm-tree-sitter-grammar.js new file mode 100644 index 000000000..9759ce0fd --- /dev/null +++ b/src/wasm-tree-sitter-grammar.js @@ -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; +}