tlaplus-community / tree-sitter-tlaplus

A tree-sitter grammar for TLA⁺ and PlusCal
MIT License
57 stars 10 forks source link

Instructions to run with tree-sitter-cli and with nodejs #101

Closed lemmy closed 8 months ago

lemmy commented 8 months ago

Sharing in case somebody finds it useful:

npm install tree-sitter-cli
git clone https://github.com/tlaplus-community/tree-sitter-tlaplus.git
cd tree-sitter-tlaplus/
tree-sitter parse /path/to/examples/specifications/DieHard/DieHard.tla

## Parse to XML and pretty-print with xq (jq sibling)
tree-sitter parse -x /path/to/examples/specifications/DieHard/DieHard.tla | xq

package.json

{
  "dependencies": {
    "@tlaplus/tree-sitter-tlaplus": "^1.0.8",
    "tree-sitter": "^0.20.6"
  }
}
const Parser = require('tree-sitter');
const TLA = require('@tlaplus/tree-sitter-tlaplus');
const fs = require('fs');

const parser = new Parser();
parser.setLanguage(TLA);
console.log(parser.getLanguage());

const sourceCode = fs.readFileSync('/path/to/examples/specifications/DieHard/DieHard.tla', "utf8");

const tree = parser.parse(sourceCode);

const callExpression = tree.rootNode.toString();
console.log(callExpression)
ahelwer commented 8 months ago

Would be good to add to https://github.com/tlaplus-community/tlaplus-tool-dev-examples!

ahelwer commented 7 months ago

A modified version of this example is now included in the repo and tested in the CI, along with examples for several other languages: https://github.com/tlaplus-community/tree-sitter-tlaplus/tree/main/test/consumers