Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tree-sitter/tree-sitter-cli
CLI tool for creating and testing tree-sitter parsers
https://github.com/tree-sitter/tree-sitter-cli
cli parser-generator tree-sitter
Last synced: about 2 months ago
JSON representation
CLI tool for creating and testing tree-sitter parsers
- Host: GitHub
- URL: https://github.com/tree-sitter/tree-sitter-cli
- Owner: tree-sitter
- License: mit
- Archived: true
- Created: 2014-07-24T06:29:10.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-02-06T01:12:50.000Z (almost 6 years ago)
- Last Synced: 2024-09-22T16:03:01.780Z (about 2 months ago)
- Topics: cli, parser-generator, tree-sitter
- Language: JavaScript
- Homepage:
- Size: 383 KB
- Stars: 45
- Watchers: 7
- Forks: 15
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tree-sitter-cli
Incremental parsers for node
[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter-cli.svg?branch=master)](https://travis-ci.org/tree-sitter/tree-sitter-cli)
[![Build status](https://ci.appveyor.com/api/projects/status/t9775gnhcnsb5na1/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter-cli/branch/master)---
:warning: This repository is deprecated. :warning:
The source code for the Tree-sitter CLI is now part of [the main Tree-sitter repository](https://github.com/tree-sitter/tree-sitter).
---
### 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:```js
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](https://en.wikipedia.org/wiki/Canonical_LR_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.