Skip to content

Commit dac12bf

Browse files
committed
Parsing
1 parent 8aa1f5e commit dac12bf

15 files changed

+3221
-1131
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
/pkg/
77
/spec/reports/
88
/tmp/
9+
10+
test.rb

CHANGELOG.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9-
## [0.1.0] - 2020-10-09
9+
## [0.1.0] - 2021-11-16
1010

1111
### Added
1212

1313
- 🎉 Initial release! 🎉
1414

15-
[unreleased]: https://github.com/kddeisz/template/compare/v0.1.0...HEAD
16-
[0.1.0]: https://github.com/kddeisz/template/compare/39c5a7...v0.1.0
15+
[unreleased]: https://github.com/kddnewton/syntax_tree/compare/v0.1.0...HEAD
16+
[0.1.0]: https://github.com/kddnewton/syntax_tree/compare/8aa1f5...v0.1.0

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
source 'https://rubygems.org'
44

55
gemspec
6+
7+
gem 'benchmark-ips'
8+
gem 'parser'
9+
gem 'ruby_parser'

Gemfile.lock

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
PATH
22
remote: .
33
specs:
4-
template (0.1.0)
4+
syntax_tree (0.1.0)
55

66
GEM
77
remote: https://rubygems.org/
88
specs:
9-
docile (1.3.5)
9+
ast (2.4.2)
10+
benchmark-ips (2.9.2)
11+
docile (1.4.0)
1012
minitest (5.14.4)
13+
parser (3.0.3.1)
14+
ast (~> 2.4.1)
1115
rake (13.0.6)
16+
ruby_parser (3.18.1)
17+
sexp_processor (~> 4.16)
18+
sexp_processor (4.16.0)
1219
simplecov (0.21.2)
1320
docile (~> 1.1)
1421
simplecov-html (~> 0.11)
1522
simplecov_json_formatter (~> 0.1)
1623
simplecov-html (0.12.3)
17-
simplecov_json_formatter (0.1.2)
24+
simplecov_json_formatter (0.1.3)
1825

1926
PLATFORMS
20-
ruby
27+
x86_64-darwin-19
28+
x86_64-linux
2129

2230
DEPENDENCIES
31+
benchmark-ips
2332
bundler
2433
minitest
34+
parser
2535
rake
36+
ruby_parser
2637
simplecov
27-
template!
38+
syntax_tree!
2839

2940
BUNDLED WITH
30-
2.2.3
41+
2.2.15

README.md

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# Template
1+
# SyntaxTree
22

3-
[![Build Status](https://github.com/kddnewton/ripper-parse_tree/workflows/Main/badge.svg)](https://github.com/kddnewton/ripper-parse_tree/actions)
4-
[![Gem Version](https://img.shields.io/gem/v/ripper-parse_tree.svg)](https://rubygems.org/gems/ripper-parse_tree)
3+
[![Build Status](https://github.com/kddnewton/syntax_tree/workflows/Main/badge.svg)](https://github.com/kddnewton/syntax_tree/actions)
4+
[![Gem Version](https://img.shields.io/gem/v/syntax_tree.svg)](https://rubygems.org/gems/syntax_tree)
5+
6+
A fast ripper subclass used for generating syntax trees from Ruby code.
57

68
## Installation
79

810
Add this line to your application's Gemfile:
911

1012
```ruby
11-
gem 'ripper-parse_tree'
13+
gem 'syntax_tree'
1214
```
1315

1416
And then execute:
@@ -17,10 +19,32 @@ And then execute:
1719

1820
Or install it yourself as:
1921

20-
$ gem install ripper-parse_tree
22+
$ gem install syntax_tree
2123

2224
## Usage
2325

26+
From code:
27+
28+
```ruby
29+
require 'syntax_tree'
30+
31+
parser = SyntaxTree.new(source)
32+
33+
node = parser.parse
34+
raise if parser.error?
35+
36+
pp node
37+
```
38+
39+
From the CLI:
40+
41+
```sh
42+
$ stree program.rb
43+
(program
44+
(statements
45+
...
46+
```
47+
2448
## Development
2549
2650
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -29,12 +53,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
2953
3054
## Contributing
3155
32-
Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/ripper-parse_tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kddnewton/ripper-parse_tree/blob/main/CODE_OF_CONDUCT.md).
56+
Bug reports and pull requests are welcome on GitHub at https://github.com/kddnewton/syntax_tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kddnewton/syntax_tree/blob/main/CODE_OF_CONDUCT.md).
3357
3458
## License
3559
3660
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
3761
3862
## Code of Conduct
3963
40-
Everyone interacting in the Template project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kddeisz/ripper-parse_tree/blob/main/CODE_OF_CONDUCT.md).
64+
Everyone interacting in the syntax_tree project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kddnewton/syntax_tree/blob/main/CODE_OF_CONDUCT.md).

bin/bench

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'bundler/setup'
5+
require 'benchmark/ips'
6+
7+
require_relative '../lib/syntax_tree'
8+
require 'ruby_parser'
9+
require 'parser/current'
10+
11+
def compare(filepath)
12+
prefix = "#{File.expand_path('..', __dir__)}/"
13+
puts "=== #{filepath.delete_prefix(prefix)} ==="
14+
15+
source = File.read(filepath)
16+
17+
Benchmark.ips do |x|
18+
x.report('syntax_tree') { SyntaxTree.new(source).parse }
19+
x.report('parser') { Parser::CurrentRuby.parse(source) }
20+
x.report('ruby_parser') { RubyParser.new.parse(source) }
21+
x.compare!
22+
end
23+
end
24+
25+
filepaths = ARGV
26+
27+
# If the user didn't supply any files to parse to benchmark, then we're going to
28+
# default to parsing this file and the main syntax_tree file (a small and large
29+
# file).
30+
if filepaths.empty?
31+
filepaths = [
32+
File.expand_path('bench', __dir__),
33+
File.expand_path('../lib/syntax_tree.rb', __dir__)
34+
]
35+
end
36+
37+
filepaths.each { |filepath| compare(filepath) }

bin/console

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# frozen_string_literal: true
33

44
require 'bundler/setup'
5-
require 'ripper/parse_tree'
5+
require 'syntax_tree'
66

77
require 'irb'
88
IRB.start(__FILE__)

exe/stree

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require_relative File.expand_path('../lib/syntax_tree', __dir__)
5+
pp SyntaxTree.new(ARGF.read).parse

lib/ripper/parse_tree/version.rb

-7
This file was deleted.

0 commit comments

Comments
 (0)