rufo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39d02ca2465a942ceb6c85baccb46c58ef602ccd
4
+ data.tar.gz: 6d8c42eac82fbb8ed50569d5c1ff36d0d7bc726b
5
+ SHA512:
6
+ metadata.gz: 577fe64ea118fc7402e3380cc40f235a0e7639a92b7e27ede1a192648497713c27fbd4b249744b291ca47cd3ecde9b66eb0bfb359d0c2f12c7ef3ad7a924a30f
7
+ data.tar.gz: 95282a406c703e54f1e91c3913887b5aed847f853663bc7c080f058de965ecf5b71d6dfb7ca16a07623947434c2bcbbb409988640cb3b862af5576e9512a758d
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rufo.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ary Borenszweig
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.
@@ -0,0 +1,39 @@
1
+ # Rufo
2
+
3
+ *Ru*by *fo*rmatter (in development)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rufo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rufo
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ $ rufo file.rb
25
+ ```
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/asterite/rufo.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rufo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require "rufo"
3
+
4
+ filename = ARGV[0]
5
+ code = File.read(filename)
6
+
7
+ result = Rufo.format(code)
8
+ File.write(filename, result)
9
+
@@ -0,0 +1,9 @@
1
+ require "rufo/version"
2
+
3
+ module Rufo
4
+ def self.format(code)
5
+ Formatter.format(code)
6
+ end
7
+ end
8
+
9
+ require_relative "rufo/formatter"
@@ -0,0 +1,203 @@
1
+ require "ripper"
2
+
3
+ class Rufo::Formatter
4
+ def self.format(code)
5
+ formatter = new(code)
6
+ formatter.format
7
+ formatter.result
8
+ end
9
+
10
+ def initialize(code)
11
+ @code = code
12
+ @tokens = Ripper.lex(code).reverse!
13
+ @sexp = Ripper.sexp(code)
14
+ @output = ""
15
+ end
16
+
17
+ def format
18
+ visit @sexp
19
+ write_line
20
+ end
21
+
22
+ def visit(node)
23
+ case node.first
24
+ when :program
25
+ # [:program, exps]
26
+ visit_exps node[1]
27
+ when :void_stmt
28
+ # [:void_stmt]
29
+ check :on_eof
30
+ when :@int
31
+ # [:@int, "123", [1, 0]]
32
+ consume_token :on_int
33
+ when :string_literal
34
+ # [:string_literal, [:string_content, exps]]
35
+ consume_token :on_tstring_beg
36
+ visit_exps(node[1][1..-1])
37
+ consume_token :on_tstring_end
38
+ when :@tstring_content
39
+ # [:@tstring_content, "hello ", [1, 1]]
40
+ consume_token :on_tstring_content
41
+ when :string_embexpr
42
+ # [:string_embexpr, exps]
43
+ consume_token :on_embexpr_beg
44
+ skip_space
45
+ visit_exps node[1]
46
+ consume_token :on_embexpr_end
47
+ when :symbol_literal
48
+ # [:symbol_literal, [:symbol, [:@ident, "foo", [1, 1]]]]
49
+ consume_token :on_symbeg
50
+ visit_exps node[1][1..-1]
51
+ when :dyna_symbol
52
+ # [:dyna_symbol, exps]
53
+ consume_token :on_symbeg
54
+ visit_exps node[1]
55
+ consume_token :on_tstring_end
56
+ when :@ident
57
+ consume_token :on_ident
58
+ when :var_ref
59
+ # [:var_ref, exp]
60
+ visit node[1]
61
+ when :var_field
62
+ # [:var_field, exp]
63
+ visit node[1]
64
+ when :@kw
65
+ # [:@kw, "nil", [1, 0]]
66
+ consume_token :on_kw
67
+ when :assign
68
+ # [:assign, target, value]
69
+ visit node[1]
70
+ consume_space
71
+ consume_op "="
72
+ consume_space
73
+ visit node[2]
74
+ else
75
+ raise "Unhandled node: #{node.first}"
76
+ end
77
+ end
78
+
79
+ def visit_exps(exps)
80
+ exps.each_with_index do |exp, i|
81
+ visit exp
82
+ skip_space
83
+
84
+ case current_token_kind
85
+ when :on_semicolon
86
+ next_token
87
+ skip_space
88
+ case current_token_kind
89
+ when :on_ignored_nl
90
+ consume_lines
91
+ when :on_eof
92
+ # Nothing
93
+ else
94
+ write ";"
95
+ write " " unless last?(i, exps)
96
+ end
97
+ when :on_nl
98
+ consume_lines
99
+ end
100
+
101
+ skip_space_or_newline
102
+ end
103
+ end
104
+
105
+ def visit_string(node)
106
+ end
107
+
108
+ def consume_lines
109
+ written_lines = 0
110
+
111
+ while true
112
+ case current_token_kind
113
+ when :on_nl, :on_ignored_nl
114
+ write_line if written_lines < 2
115
+ written_lines += 1
116
+ next_token
117
+ when :on_sp, :on_semicolon
118
+ next_token
119
+ else
120
+ break
121
+ end
122
+ end
123
+ end
124
+
125
+ def consume_space
126
+ skip_space
127
+ write " "
128
+ end
129
+
130
+ def skip_space
131
+ while current_token_kind == :on_sp
132
+ next_token
133
+ end
134
+ end
135
+
136
+ def skip_space_or_newline
137
+ while true
138
+ case current_token_kind
139
+ when :on_sp, :on_nl, :on_semicolon
140
+ next_token
141
+ next
142
+ else
143
+ break
144
+ end
145
+ end
146
+ end
147
+
148
+ def write(value)
149
+ @output << value
150
+ end
151
+
152
+ def consume_token(kind)
153
+ check kind
154
+ write current_token_value
155
+ next_token
156
+ end
157
+
158
+ def consume_op(value)
159
+ check :on_op
160
+ if current_token_value != value
161
+ raise "Expected op #{value}, not #{current_token_value}"
162
+ end
163
+ write current_token_value
164
+ next_token
165
+ end
166
+
167
+ def write_line
168
+ @output << "\n"
169
+ end
170
+
171
+ def check(kind)
172
+ if current_token_kind != kind
173
+ raise "Expected token #{kind}, not #{current_token_kind}"
174
+ end
175
+ end
176
+
177
+ # [[1, 0], :on_int, "1"]
178
+ def current_token
179
+ @tokens.last
180
+ end
181
+
182
+ def current_token_kind
183
+ tok = current_token
184
+ tok ? tok[1] : :on_eof
185
+ end
186
+
187
+ def current_token_value
188
+ tok = current_token
189
+ tok ? tok[2] : ""
190
+ end
191
+
192
+ def next_token
193
+ @tokens.pop
194
+ end
195
+
196
+ def last?(i, array)
197
+ i == array.size - 1
198
+ end
199
+
200
+ def result
201
+ @output
202
+ end
203
+ end
@@ -0,0 +1,3 @@
1
+ module Rufo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rufo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rufo"
8
+ spec.version = Rufo::VERSION
9
+ spec.authors = ["Ary Borenszweig"]
10
+ spec.email = ["asterite@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby code formatter}
13
+ spec.description = %q{Ruby code formatter}
14
+ spec.homepage = "https://github.com/asterite/rufo"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rufo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ary Borenszweig
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Ruby code formatter
56
+ email:
57
+ - asterite@gmail.com
58
+ executables:
59
+ - rufo
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/rufo
73
+ - lib/rufo.rb
74
+ - lib/rufo/formatter.rb
75
+ - lib/rufo/version.rb
76
+ - rufo.gemspec
77
+ homepage: https://github.com/asterite/rufo
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.11
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby code formatter
101
+ test_files: []