move hs2js package here + improve readme
This commit is contained in:
parent
ed506cabb5
commit
c232fb7dd5
12 changed files with 1489 additions and 35 deletions
37
packages/hs2js/src/parser.mjs
Normal file
37
packages/hs2js/src/parser.mjs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import Parser from "web-tree-sitter";
|
||||
|
||||
let base = "/";
|
||||
export function setBase(path) {
|
||||
base = path;
|
||||
}
|
||||
|
||||
async function _loadParser() {
|
||||
await Parser.init({
|
||||
locateFile(scriptName, scriptDirectory) {
|
||||
return `${base}${scriptName}`;
|
||||
},
|
||||
});
|
||||
const parser = new Parser();
|
||||
const Lang = await Parser.Language.load(`${base}tree-sitter-haskell.wasm`);
|
||||
parser.setLanguage(Lang);
|
||||
return parser;
|
||||
}
|
||||
|
||||
let parserLoaded;
|
||||
export function loadParser() {
|
||||
if (!parserLoaded) {
|
||||
parserLoaded = _loadParser();
|
||||
}
|
||||
return parserLoaded;
|
||||
}
|
||||
|
||||
export async function parse(code) {
|
||||
const parser = await loadParser();
|
||||
// for some reason, the parser doesn't like new lines..
|
||||
return parser.parse(
|
||||
code
|
||||
.replaceAll("\n\n", "~~~~")
|
||||
.replaceAll("\n", "")
|
||||
.replaceAll("~~~~", "\n")
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue