Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tree-sitter/ruby-tree-sitter.old
Ruby bindings to tree-sitter
https://github.com/tree-sitter/ruby-tree-sitter.old
binding ruby tree-sitter
Last synced: about 2 months ago
JSON representation
Ruby bindings to tree-sitter
- Host: GitHub
- URL: https://github.com/tree-sitter/ruby-tree-sitter.old
- Owner: tree-sitter
- License: mit
- Archived: true
- Created: 2017-02-28T04:29:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-06-21T23:05:12.000Z (over 2 years ago)
- Last Synced: 2024-04-14T06:44:51.326Z (7 months ago)
- Topics: binding, ruby, tree-sitter
- Language: C
- Size: 288 KB
- Stars: 60
- Watchers: 7
- Forks: 12
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-tree-sitter - Ruby
README
# TreeSitter
This gem wraps around [tree-sitter](https://github.com/tree-sitter/tree-sitter).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'tree-sitter'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install tree-sitter
## Usage
Tree-sitter performs [two different operations](https://github.com/tree-sitter/tree-sitter#overview): compiling grammars and parsing text with those grammars.
This gem is only concerned with parsing a blob of text, given the appropriate grammars. Compiling grammars from JSON is trivial and can just be kept as an executable generated from C.
### Running the parser
The libruntime static library needs to make use of the C code generated by the libcompile executable. Once you've got those, you'll need to provide an absolute path to the directory containing the C files that the library can incorporate. For example:
``` bash
# if you've installed this gem
$ TREE_SITTER_PARSER_DIR=/somewhere/code bundle install
# if you're using this gem locally
$ TREE_SITTER_PARSER_DIR=/somewhere/code bundle exec rake compile
```This directory is appended with the glob suffix `**/*.{c,cc}`, so it can contain as many nested parser files as necessary.
Then, you can start making use of the parser:
``` ruby
# first argument is the string to parse
# second argument is a set of options; currently, only one is available, `language:`,
# which identifies the text's language
document = TreeSitter::Document.new('a + b * 5', language: 'tree_sitter_arithmetic')# you can change any of these values whenever you want, just remember to call `parse`
# provide the function name
document.input_string = '12 * 12'
document.language = "tree_sitter_python"
document.parse
```## Development
After checking out the repo, run `script/bootstrap` to install dependencies. Then, run `script/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.