CLI tool for creating and testing tree-sitter parsers
  • JavaScript 91.9%
  • C++ 6.2%
  • EJS 1.3%
  • Python 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Max Brunsfeld 44ba293422
Merge pull request #57 from tree-sitter/deprecate-repo
Add notice that the repo is deprecated
2019-02-05 17:12:46 -08:00
lib Allow property sheets to use repeated keys to represent arrays 2018-12-11 17:28:49 -08:00
src Compile property sheets to state machines (#45) 2018-10-15 23:18:56 -07:00
test Allow property sheets to use repeated keys to represent arrays 2018-12-11 17:28:49 -08:00
vendor ⬆️ tree-sitter for msvc fix in gyp file 2018-12-12 16:08:18 -08:00
.gitignore ⬆️ tree-sitter, rework the tests for the new API 2018-05-14 17:48:33 -07:00
.gitmodules Initial commit 2014-07-23 23:29:54 -07:00
.npmignore Fix exclusion of unneeded tree-sitter files in npmignore 2018-07-18 10:01:57 -07:00
.travis.yml ⬆️ Tree-sitter, add test for isMissing() 2017-12-30 22:01:23 -08:00
appveyor.yml Use node 8 on CI 2017-11-21 12:24:50 -08:00
binding.gyp Rename compile -> generate 2016-10-09 22:29:58 -07:00
cli.js Merge pull request #44 from tree-sitter/add-build-subcommand 2018-11-20 21:36:16 -08:00
LICENSE Initial commit 2014-07-23 23:29:54 -07:00
package.json 0.13.16 2019-01-08 13:29:54 -08:00
README.md Add notice that the repo is deprecated 2019-02-05 17:09:31 -08:00

tree-sitter-cli

Incremental parsers for node

Build Status Build status


⚠️ This repository is deprecated. ⚠️

The source code for the Tree-sitter CLI is now part of the main Tree-sitter repository.


Installation

npm install tree-sitter-cli

Creating a language

Create a grammar.js in the root directory of your module. This file should create and export a grammar object using tree-sitter's helper functions:

module.exports = grammar({
  name: "arithmetic",

  extras: $ => [$.comment, /\s/],

  rules: {
    program: $ => repeat(choice(
      $.assignment_statement,
      $.expression_statement
    )),

    assignment_statement: $ => seq(
      $.variable, "=", $.expression, ";"
    ),

    expression_statement: $ => seq(
      $.expression, ";"
    ),

    expression: $ => choice(
      $.variable,
      $.number,
      prec.left(1, seq($.expression, "+", $.expression)),
      prec.left(1, seq($.expression, "-", $.expression)),
      prec.left(2, seq($.expression, "*", $.expression)),
      prec.left(2, seq($.expression, "/", $.expression)),
      prec.left(3, seq($.expression, "^", $.expression))
    ),

    variable: $ => /\a\w*/,

    number: $ => /\d+/,

    comment: $ => /#.*/
  }
});

Run tree-sitter generate. This will generate a C function for parsing your language, a C++ function that exposes the parser to javascript, and a binding.gyp file for compiling these sources into a native node module.

Grammar syntax

The grammar function takes an object with the following keys:

  • name - the name of the language.
  • rules - an object whose keys are rule names and whose values are Grammar Rules. The first key in the map will be the start symbol. See the 'Rules' section for how to construct grammar rules.
  • extras - an array of Grammar Rules which may appear anywhere in a document. This construct is used to useful for things like whitespace and comments in programming languages.
  • conflicts - an array of arrays of Grammar Rules which are known to conflict with each other in an LR(1) parser. You'll need to use this if writing a grammar that is not LR(1).

Rules

  • $.property references - match another rule with the given name.
  • String literals - match exact strings.
  • RegExp literals - match strings according to ECMAScript regexp syntax. Assertions (e.g. ^, $) are not yet supported.
  • choice(rule...) - matches any one of the given rules.
  • repeat(rule) - matches any number of repetitions of the given rule.
  • seq(rule...) - matches each of the given rules in sequence.
  • blank() - matches the empty string.
  • optional(rule) - matches the given rule or the empty string.