rubyast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rubyast.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # RubyAST
2
+
3
+ RubyAST is a small library on top of jrubyparser. The library works with ALL VERSIONS OF RUBY. YOU DON'T NEED TO USE JRUBY (but why not, it's awesome!).
4
+
5
+ ## #parse
6
+
7
+ Parsing a file:
8
+
9
+ source = File.read(file_name)
10
+ ast = RubyAST.parse(file_name, source)
11
+
12
+ Parsing a string:
13
+
14
+ ast = RubyAST.parse("(string)", "x = 1")
15
+
16
+ You can set a starting line number (by default it's 0):
17
+
18
+ ast = RubyAST.parse("(string)", "x = 1", :line_number => 100)
19
+
20
+ You can set ruby version (by default it's RUBY1_9):
21
+
22
+ ast = RubyAST.parse("(string)", "x = 1", :ruby_version => "RUBY1_9")
23
+
24
+ ## #to_source
25
+
26
+ Just pass AST:
27
+
28
+ source = RubyAST.to_source(ast)
29
+
30
+ If you want to keep quotes and heredocs, pass the original source:
31
+
32
+ original_source = File.read(file_name)
33
+ ast = RubyAST.parse(file_name, original_source)
34
+ new_source = RubyAST.to_source(ast, original_source)
35
+
36
+
37
+ ## AST Nodes
38
+
39
+ ast = RubyAST.parse("(string)", "x = 1")
40
+ new_line_node = ast.body_node
41
+
42
+ assignment = new_line_node.next_node
43
+ fixnum_node = assignment.value_node
44
+
45
+ assignment.name.should == "x"
46
+ fixnum_node.value.should == 1
47
+
48
+ Check out [jruby-parser](https://github.com/jruby/jruby-parser) for additoinal information about different types of nodes.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => [:spec]
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = Dir.glob('spec/**/*_spec.rb')
8
+ t.rspec_opts = ['--backtrace']
9
+ end
Binary file
@@ -0,0 +1,29 @@
1
+ module RubyAST
2
+ module J
3
+ jar_path = File.expand_path(File.dirname(__FILE__) + '/../../jar/jrubyparser-0.2.jar')
4
+
5
+ if defined?(JRUBY_VERSION)
6
+ require 'java'
7
+ require jar_path
8
+
9
+ java_import java.io.StringReader
10
+ java_import java.io.StringWriter
11
+ java_import org.jrubyparser.parser.ParserConfiguration
12
+ java_import org.jrubyparser.Parser
13
+ java_import org.jrubyparser.rewriter.ReWriteVisitor
14
+ java_import org.jrubyparser.CompatVersion
15
+
16
+ ::SyntaxException = org.jrubyparser.lexer.SyntaxException
17
+ else
18
+ require 'rjb'
19
+ Rjb::load(jar_path, [])
20
+
21
+ StringReader = Rjb::import('java.io.StringReader')
22
+ StringWriter = Rjb::import('java.io.StringWriter')
23
+ ParserConfiguration = Rjb::import('org.jrubyparser.parser.ParserConfiguration')
24
+ Parser = Rjb::import('org.jrubyparser.Parser')
25
+ ReWriteVisitor = Rjb::import('org.jrubyparser.rewriter.ReWriteVisitor')
26
+ CompatVersion = Rjb::import('org.jrubyparser.CompatVersion')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ module RubyAST
2
+ class SyntaxException < Exception; end
3
+
4
+ module JRubyParserWrapper
5
+ extend self
6
+
7
+ def parse file_name, source, line_number, ruby_version
8
+ version = J::CompatVersion.getVersionFromString(ruby_version)
9
+ config = J::ParserConfiguration.new(line_number, version)
10
+ reader = J::StringReader.new(source)
11
+
12
+ J::Parser.new.parse(file_name, reader, config)
13
+ rescue ::SyntaxException => e
14
+ raise SyntaxException, e.message
15
+ end
16
+
17
+ def to_source ast, original_source
18
+ writer = J::StringWriter.new
19
+ visitor = J::ReWriteVisitor.new(writer, original_source)
20
+ ast.accept(visitor)
21
+ writer.toString
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RubyAST
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rubyast.rb ADDED
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/rubyast/version")
2
+ require File.expand_path(File.dirname(__FILE__) + '/rubyast/include_jrubyparser')
3
+ require File.expand_path(File.dirname(__FILE__) + '/rubyast/jruby_parser_wrapper')
4
+
5
+ module RubyAST
6
+ def self.parse file_name, source, config = {}
7
+ line_number = config.fetch(:line_number, 0)
8
+ ruby_version = config.fetch(:ruby_version, "RUBY1_9")
9
+ JRubyParserWrapper.parse file_name, source, line_number, ruby_version
10
+ end
11
+
12
+ def self.to_source ast, original_source = ""
13
+ JRubyParserWrapper.to_source ast, original_source
14
+ end
15
+ end
data/rubyast.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rubyast/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rubyast"
7
+ s.version = RubyAST::VERSION
8
+ s.authors = ["Victor Savkin"]
9
+ s.email = ["vic.savkin@gmail.com"]
10
+ s.homepage = "http://github.com/vsavkin/rubyast"
11
+ s.summary = %q{Allows AST Transformations for Ruby.}
12
+ s.description = %q{Allows AST Transformations for Ruby: parsers source code,
13
+ transforms AST, generates source code based on AST}
14
+
15
+ s.rubyforge_project = "rubyast"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "pry"
25
+
26
+ unless defined?(JRUBY_VERSION)
27
+ s.add_runtime_dependency "rjb"
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/rubyast")
2
+
3
+ module RubyAST
4
+ describe RubyAST do
5
+ context "#parse" do
6
+ let(:filename) { "filename" }
7
+
8
+ let(:ast) do
9
+ RubyAST.parse(filename, "x = 1", :line_number => 100, :ruby_version => "RUBY1_8")
10
+ end
11
+
12
+ it "should return parsed AST" do
13
+ new_line_node = ast.body_node
14
+ assignment = new_line_node.next_node
15
+ fixnum_node = assignment.value_node
16
+
17
+ assignment.name.should == "x"
18
+ fixnum_node.value.should == 1
19
+ end
20
+
21
+ it "should set correct line number" do
22
+ ast.position.file.should == filename
23
+ ast.position.start_line.should == 100
24
+ ast.position.end_line.should == 100
25
+ end
26
+
27
+ it "should default line number to 0" do
28
+ ast = RubyAST.parse(filename, "x = 1")
29
+ ast.position.start_line.should == 0
30
+ end
31
+
32
+ it "should default ruby_version to 1.9" do
33
+ RubyAST.parse(filename, "{x: 1}")
34
+ lambda { RubyAST.parse(filename, "{x: 1}", :ruby_version => "RUBY1_8") }.should raise_error(SyntaxException)
35
+ end
36
+ end
37
+
38
+ context "#to_source" do
39
+ let(:original_source) do
40
+ %q{x = '1' + "2"}
41
+ end
42
+
43
+ let(:ast) do
44
+ RubyAST.parse("filename", original_source)
45
+ end
46
+
47
+ it "should generate source from AST" do
48
+ source = RubyAST.to_source(ast, original_source)
49
+ source.should == original_source
50
+ end
51
+
52
+ it "will ignore quote types when original source is not passed" do
53
+ source = RubyAST.to_source(ast)
54
+ source.should == 'x = "1" + "2"'
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Victor Savkin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2165284080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2165284080
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2165283080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2165283080
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &2165281940 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2165281940
47
+ - !ruby/object:Gem::Dependency
48
+ name: rjb
49
+ requirement: &2165281300 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *2165281300
58
+ description: ! 'Allows AST Transformations for Ruby: parsers source code,
59
+
60
+ transforms AST, generates source code based on AST'
61
+ email:
62
+ - vic.savkin@gmail.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - .gitignore
68
+ - .travis.yml
69
+ - Gemfile
70
+ - README.md
71
+ - Rakefile
72
+ - jar/jrubyparser-0.2.jar
73
+ - lib/rubyast.rb
74
+ - lib/rubyast/include_jrubyparser.rb
75
+ - lib/rubyast/jruby_parser_wrapper.rb
76
+ - lib/rubyast/version.rb
77
+ - rubyast.gemspec
78
+ - spec/rubyast/rubyast_spec.rb
79
+ homepage: http://github.com/vsavkin/rubyast
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project: rubyast
99
+ rubygems_version: 1.8.10
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Allows AST Transformations for Ruby.
103
+ test_files:
104
+ - spec/rubyast/rubyast_spec.rb