syntax_tree-translator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fff2de442626d6c24ef93737c1f5c67378c0ae6cc6e6178fc37f0bf8b7e481c2
4
+ data.tar.gz: cf26490e749feff3973697e6430315745531d2422bbfad968b4a16e82048f3dc
5
+ SHA512:
6
+ metadata.gz: 0cb26c6e535bb0c9ac35f8cc70e8731aab49cf00877ed11ceb9b5603b4b49c65f6a0c5ba0d08a560daf06c860380cbf3dba37b13aa04db7caeed3e4742aba572
7
+ data.tar.gz: f164933fc32106a8dbd0787b847066c47277b3f81b54dbf2703bd1d1f340c00d0fb7e4982ce7313a22e81a11d8804d1e5c2005a06b32579281c7b05401f97aa2
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,34 @@
1
+ name: Main
2
+ on:
3
+ - push
4
+ - pull_request_target
5
+ jobs:
6
+ ci:
7
+ name: CI
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ CI: true
11
+ steps:
12
+ - uses: actions/checkout@master
13
+ with:
14
+ submodules: recursive
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ bundler-cache: true
18
+ ruby-version: '3.1'
19
+ - name: Test
20
+ run: bundle exec rake test
21
+ automerge:
22
+ name: AutoMerge
23
+ needs: ci
24
+ runs-on: ubuntu-latest
25
+ if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
26
+ steps:
27
+ - uses: actions/github-script@v3
28
+ with:
29
+ script: |
30
+ github.pulls.merge({
31
+ owner: context.payload.repository.owner.login,
32
+ repo: context.payload.repository.name,
33
+ pull_number: context.payload.pull_request.number
34
+ })
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /pkg/
6
+ /rdocs/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+
11
+ test.rb
data/.gitmodules ADDED
@@ -0,0 +1,6 @@
1
+ [submodule "suites/parser"]
2
+ path = suites/parser
3
+ url = https://github.com/whitequark/parser
4
+ [submodule "suites/ruby_parser"]
5
+ path = suites/ruby_parser
6
+ url = https://github.com/seattlerb/ruby_parser
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2022-04-20
10
+
11
+ ### Added
12
+
13
+ - 🎉 Initial release! 🎉
14
+
15
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-translator/compare/v0.1.0...HEAD
16
+ [0.1.0]: https://github.com/ruby-syntax-tree/syntax_tree-translator/compare/d80468...v0.1.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ syntax_tree-translator (0.1.0)
5
+ parser
6
+ rubocop-ast
7
+ ruby_parser
8
+ syntax_tree
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ docile (1.4.0)
15
+ minitest (5.15.0)
16
+ parser (3.1.2.0)
17
+ ast (~> 2.4.1)
18
+ rake (13.0.6)
19
+ rubocop-ast (1.17.0)
20
+ parser (>= 3.1.1.0)
21
+ ruby_parser (3.19.1)
22
+ sexp_processor (~> 4.16)
23
+ sexp_processor (4.16.1)
24
+ simplecov (0.21.2)
25
+ docile (~> 1.1)
26
+ simplecov-html (~> 0.11)
27
+ simplecov_json_formatter (~> 0.1)
28
+ simplecov-html (0.12.3)
29
+ simplecov_json_formatter (0.1.4)
30
+ syntax_tree (2.2.0)
31
+
32
+ PLATFORMS
33
+ x86_64-darwin-21
34
+ x86_64-linux
35
+
36
+ DEPENDENCIES
37
+ bundler
38
+ minitest
39
+ rake
40
+ simplecov
41
+ syntax_tree-translator!
42
+
43
+ BUNDLED WITH
44
+ 2.3.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present Kevin Newton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # SyntaxTree::Translator
2
+
3
+ [![Build Status](https://github.com/ruby-syntax-tree/syntax_tree-translator/actions/workflows/main.yml/badge.svg)](https://github.com/ruby-syntax-tree/syntax_tree-translator/actions/workflows/main.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/syntax_tree-translator.svg)](https://rubygems.org/gems/syntax_tree-translator)
5
+
6
+ Translate [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree) syntax trees into other Ruby parser syntax trees.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "syntax_tree-translator"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install syntax_tree-translator
23
+
24
+ ## Usage
25
+
26
+ 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:
27
+
28
+ ```ruby
29
+ source = ARGF.read
30
+ program = SyntaxTree.parse(source)
31
+ ```
32
+
33
+ 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.
34
+
35
+ ### parser
36
+
37
+ 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.
38
+
39
+ ```ruby
40
+ buffer = Parser::Source::Buffer.new("(string)")
41
+ buffer.source = source
42
+
43
+ visitor = SyntaxTree::Translator::Parser.new(buffer)
44
+ node = visitor.visit(program)
45
+ ```
46
+
47
+ ### rubocop-ast
48
+
49
+ 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.
50
+
51
+ ```ruby
52
+ buffer = Parser::Source::Buffer.new("(string)")
53
+ buffer.source = source
54
+
55
+ visitor = SyntaxTree::Translator::RuboCop.new(buffer)
56
+ node = visitor.visit(program)
57
+ ```
58
+
59
+ ### ruby_parser
60
+
61
+ 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.
62
+
63
+ ```ruby
64
+ visitor = SyntaxTree::Translator::RubyParser.new
65
+ node = visitor.visit(program)
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree-translator.
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+
9
+ # This is the list of tests from our translator gem itself.
10
+ file_list = FileList["test/**/*_test.rb"]
11
+
12
+ # This is a big test file from the parser gem that tests its functionality.
13
+ file_list << "suites/parser/test/test_parser.rb"
14
+
15
+ # This is a big test file from the ruby_parser gem that tests its
16
+ # functionality.
17
+ file_list << "suites/ruby_parser/test/test_ruby_parser.rb"
18
+
19
+ t.test_files = file_list
20
+ end
21
+
22
+ task default: :test
data/bin/compare-p ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "parser/current"
6
+
7
+ $:.unshift(File.expand_path("../lib", __dir__))
8
+ require "syntax_tree/translator"
9
+
10
+ # First, opt in to every AST feature.
11
+ Parser::Builders::Default.modernize
12
+
13
+ # Modify the source map == check so that it doesn't check against the node
14
+ # itself so we don't get into a recursive loop.
15
+ # Parser::Source::Map.prepend(
16
+ # Module.new {
17
+ # def ==(other)
18
+ # self.class == other.class &&
19
+ # (instance_variables - %i[@node]).map do |ivar|
20
+ # instance_variable_get(ivar) == other.instance_variable_get(ivar)
21
+ # end.reduce(:&)
22
+ # end
23
+ # }
24
+ # )
25
+
26
+ # Next, ensure that we're comparing the nodes and also comparing the source
27
+ # ranges so that we're getting all of the necessary information.
28
+ # Parser::AST::Node.prepend(
29
+ # Module.new {
30
+ # def ==(other)
31
+ # super && (location == other.location)
32
+ # end
33
+ # }
34
+ # )
35
+
36
+ source = ARGF.read
37
+
38
+ parser = Parser::CurrentRuby.new
39
+ parser.diagnostics.all_errors_are_fatal = true
40
+
41
+ buffer = Parser::Source::Buffer.new("(string)", 1)
42
+ buffer.source = source.dup.force_encoding(parser.default_encoding)
43
+
44
+ visitor = SyntaxTree::Translator::Parser.new(buffer)
45
+ stree = visitor.visit(SyntaxTree.parse(source))
46
+ ptree = parser.parse(buffer)
47
+
48
+ if stree == ptree
49
+ puts "Syntax trees are equivalent."
50
+ else
51
+ warn "Syntax trees are different."
52
+
53
+ warn "syntax_tree:"
54
+ pp stree
55
+
56
+ warn "parser:"
57
+ pp ptree
58
+ end
data/bin/compare-rp ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+
6
+ $:.unshift(File.expand_path("../lib", __dir__))
7
+ require "syntax_tree/translator"
8
+ require "ruby_parser"
9
+
10
+ source = ARGF.read
11
+
12
+ visitor = SyntaxTree::Translator::RubyParser.new
13
+ stree = visitor.visit(SyntaxTree.parse(source))
14
+ rptree = RubyParser.for_current_ruby.parse(source)
15
+
16
+ if stree == rptree
17
+ puts "Syntax trees are equivalent."
18
+ else
19
+ warn "Syntax trees are different."
20
+
21
+ warn "syntax_tree:"
22
+ pp stree
23
+
24
+ warn "ruby_parser:"
25
+ pp rptree
26
+ end
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+
6
+ $:.unshift(File.expand_path("../lib", __dir__))
7
+ require "syntax_tree/translator"
8
+
9
+ require "irb"
10
+ IRB.start(__FILE__)
data/bin/rubocop ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/inline"
5
+ gemfile do
6
+ source "https://rubygems.org"
7
+ gem "rubocop"
8
+ gem "syntax_tree"
9
+ end
10
+
11
+ $:.unshift File.expand_path("../lib", __dir__)
12
+ require "syntax_tree/translator"
13
+
14
+ class RuboCop::AST::ProcessedSource
15
+ private
16
+
17
+ def tokenize(parser)
18
+ visitor = SyntaxTree::Translator::RuboCop.new(@buffer, "(string)", 1)
19
+
20
+ ast = visitor.visit(SyntaxTree.parse(@buffer.source))
21
+ ast.complete!
22
+
23
+ _, comments, tokens = parser.tokenize(@buffer)
24
+ tokens.map! { |t| RuboCop::AST::Token.from_parser_token(t) }
25
+
26
+ [ast, comments, tokens]
27
+ rescue Parser::SyntaxError
28
+ [nil, [], []]
29
+ end
30
+ end
31
+
32
+ exit(RuboCop::CLI.new.run)