Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

ruby-syntax-tree/syntax_tree-translator

Repository files navigation

⚠️ Deprecated - use prism's translation layer instead

SyntaxTree::Translator

Build Status Gem Version

Translate Syntax Tree syntax trees into other Ruby parser syntax trees.

Installation

Add this line to your application's Gemfile:

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:

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 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.

buffer = Parser::Source::Buffer.new("(string)")
buffer.source = source

visitor = SyntaxTree::Translator::Parser.new(buffer)
node = visitor.visit(program)

rubocop-ast

To translate into the 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.

buffer = Parser::Source::Buffer.new("(string)")
buffer.source = source

visitor = SyntaxTree::Translator::RuboCop.new(buffer)
node = visitor.visit(program)

ruby_parser

To translate into the seattlerb/ruby_parser gem's syntax tree you instantiate a new visitor and pass it the program instance.

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.

About

Translate the Syntax Tree AST into other Ruby ASTs

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages