https://github.com/ruby-syntax-tree/syntax_tree-translator
Translate the Syntax Tree AST into other Ruby ASTs
https://github.com/ruby-syntax-tree/syntax_tree-translator
Last synced: 5 months ago
JSON representation
Translate the Syntax Tree AST into other Ruby ASTs
- Host: GitHub
- URL: https://github.com/ruby-syntax-tree/syntax_tree-translator
- Owner: ruby-syntax-tree
- License: mit
- Archived: true
- Created: 2022-04-04T13:26:36.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-16T15:11:15.000Z (about 1 year ago)
- Last Synced: 2024-08-07T08:11:22.313Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 242 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- stars - ruby-syntax-tree/syntax_tree-translator - Translate the Syntax Tree AST into other Ruby ASTs (Ruby)
README
> :warning: Deprecated - use prism's translation layer instead
# SyntaxTree::Translator
[](https://github.com/ruby-syntax-tree/syntax_tree-translator/actions/workflows/main.yml)
[](https://rubygems.org/gems/syntax_tree-translator)Translate [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree) syntax trees into other Ruby parser syntax trees.
## Installation
Add this line to your application's Gemfile:
```ruby
gem "syntax_tree-translator"
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install syntax_tree-translator
## Usage
First, you need to get the source code that you'd like to translate into a syntax tree. Then you need to parse it using Syntax Tree's parse method, as in:
```ruby
source = ARGF.read
program = SyntaxTree.parse(source)
```From there, you now have a `SyntaxTree::Program` node representing the top of your syntax tree. You can translate that into another format by using one of the provided visitors. Each is detailed below.
### parser
To translate into the [whitequark/parser](https://github.com/whitequark/parser) gem's syntax tree, instantiate a new source buffer, pass that along with the filename and line number into a new visitor, and call visit.
```ruby
buffer = Parser::Source::Buffer.new("(string)")
buffer.source = sourcevisitor = SyntaxTree::Translator::Parser.new(buffer)
node = visitor.visit(program)
```### rubocop-ast
To translate into the [rubocop/rubocop-ast](https://github.com/rubocop/rubocop-ast) gem's syntax tree (the one used internally by rubocop), you do pretty much the exact same thing as `parser`, except that it generates more specific node types with helper methods.
```ruby
buffer = Parser::Source::Buffer.new("(string)")
buffer.source = sourcevisitor = SyntaxTree::Translator::RuboCop.new(buffer)
node = visitor.visit(program)
```### ruby_parser
To translate into the [seattlerb/ruby_parser](https://github.com/seattlerb/ruby_parser) gem's syntax tree you instantiate a new visitor and pass it the program instance.
```ruby
visitor = SyntaxTree::Translator::RubyParser.new
node = visitor.visit(program)
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree-translator.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).