mpen
mpen

Reputation: 282895

node-tree-sitter always outputs error despite CLI working

I'm trying to use tree-sitter to parse my little test language.

Here's the code:

#!node
const Parser = require('tree-sitter');
const Kram = require('.');

const parser = new Parser();
parser.setLanguage(Kram);

const sourceCode = `
func foo(): bool {
    return 5;
}
`
const tree = parser.parse(sourceCode);

console.log(tree.rootNode.toString());

It always outputs

(ERROR (UNEXPECTED 'f'))

Indicating it's failing to parse the first character.

However, when I run

npm run tree-sitter parse examples/hello.kram

Where hello.kram contains the exact same source code, it parses fine:

(source_file [2, 0] - [5, 0]
  (function_definition [2, 0] - [4, 1]
    (identifier [2, 5] - [2, 8])
    (parameter_list [2, 8] - [2, 10])
    (block [2, 17] - [4, 1]
      (return_statement [3, 4] - [3, 13]
        (number [3, 11] - [3, 12])))))

In my package.json I have main pointing to bindings/node which is the same thing that tree-sitter-javascript did.

So I'm not sure what's wrong. What else do I have to do to actually use my parser from JS?

Upvotes: 1

Views: 34

Answers (0)

Related Questions