ruby_scribe 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -92,6 +92,7 @@ This feature is not developed yet, but is intended on presenting a standard recu
92
92
 
93
93
  == Known Issues
94
94
 
95
+ * Since there are still some holes in the implementation, any s-expression type that is unknown will cause the following to be emitted: "## RubyScribe-UNKNOWN: :type ##". Once stable any unknown type will instead throw an exception.
95
96
  * Anything involving order of operations currently much surround the expression in ( ). Will probably expand later to omit this when order of operations is implied, but this requires a context stack.
96
97
  * Elsif currently does not work as you'd expect and instead embeds another if block inside of the outer one's "else". This is how if statements are presented via ruby_parser.
97
98
  * Some of the more obscure types are not implemented.
data/bin/rubyscribe ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "ruby_scribe/runner"
5
+
6
+ RubyScribe::Runner.start
@@ -22,6 +22,8 @@ module RubyScribe
22
22
  emit_scope(e)
23
23
  when :rescue
24
24
  emit_rescue(e)
25
+ when :resbody
26
+ emit_rescue_body(e)
25
27
  when :module
26
28
  emit_module_definition(e)
27
29
  when :class
@@ -116,15 +118,25 @@ module RubyScribe
116
118
  end
117
119
 
118
120
  def emit_rescue(e)
119
- "begin" + indent { nl + emit(e.body[0]) } +
120
- nl("rescue ") + indent { nl + emit(e.body[1].body[1]) } +
121
+ block = e.body.size == 1 ? nil : e.body[0]
122
+ resbody = e.body.size == 1 ? e.body[0] : e.body[1]
123
+
124
+ "begin" + indent { nl + emit(block) } +
125
+ emit(resbody) +
121
126
  nl("end")
122
127
  end
123
128
 
129
+ def emit_rescue_body(e)
130
+ nl("rescue ".gsub(/ $/, '')) +
131
+ indent { nl + emit(e.body[1]) }
132
+ end
133
+
124
134
  def emit_method_rescue(e)
125
- emit(e.body[0]) +
126
- indent(-2) { nl("rescue ") } +
127
- nl + emit(e.body[1].body[1])
135
+ block = e.body.size == 1 ? nil : e.body[0]
136
+ resbody = e.body.size == 1 ? e.body[0] : e.body[1]
137
+
138
+ emit(block) +
139
+ indent(-2) { emit(resbody) }
128
140
  end
129
141
 
130
142
  def emit_class_definition(e)
@@ -251,7 +263,7 @@ module RubyScribe
251
263
 
252
264
  def determine_if_type(e)
253
265
  if e.body[1] && e.body[2] && e.body[0].line == e.body[1].try(:line) && e.line == e.body[2].try(:line)
254
- :terinary
266
+ :ternary
255
267
  elsif e.body[1] && !e.body[2] && e.line == e.body[1].line && e.body[1].kind != :block
256
268
  :dangling_if
257
269
  elsif !e.body[1] && e.body[2] && e.line == e.body[2].line && e.body[2].kind != :block
@@ -265,7 +277,7 @@ module RubyScribe
265
277
 
266
278
  def emit_conditional_block(e)
267
279
  case determine_if_type(e)
268
- when :terinary
280
+ when :ternary
269
281
  "#{emit(e.body[0])} ? #{emit(e.body[1] || s(:nil))} : #{emit(e.body[2] || s(:nil))}"
270
282
  when :dangling_if
271
283
  "#{emit(e.body[1])} if #{emit(e.body[0])}"
@@ -282,7 +294,7 @@ module RubyScribe
282
294
  end
283
295
 
284
296
  def emit_case_statement(e)
285
- "case #{emit(e.body.first)}" + e.body[1..-2].map {|c| emit(c) }.join + emit_case_else_statement(e.body[-1]) + nl("end")
297
+ "case #{emit(e.body[0])}".gsub(/ $/, '') + e.body[1..-2].map {|c| emit(c) }.join + emit_case_else_statement(e.body[-1]) + nl("end")
286
298
  end
287
299
 
288
300
  def emit_case_when_statement(e)
@@ -302,8 +314,8 @@ module RubyScribe
302
314
  end
303
315
 
304
316
  def emit_loop_block(e)
305
- "#{e.kind} #{e.body.first}" +
306
- indent { emit(e.body[1]) } +
317
+ "#{e.kind} #{emit(e.body[0])}" +
318
+ indent { nl + emit(e.body[1]) } +
307
319
  nl("end")
308
320
  end
309
321
 
@@ -440,7 +452,7 @@ module RubyScribe
440
452
  end
441
453
 
442
454
  def emit_unknown_expression(e)
443
- nl("## UNKNOWN: #{e.kind} ##")
455
+ nl("## RubyScribe-UNKNOWN: #{e.kind} ##")
444
456
  end
445
457
  end
446
458
  end
@@ -1,4 +1,5 @@
1
1
  require "thor"
2
+ require "ruby_scribe"
2
3
 
3
4
  module RubyScribe
4
5
  class Runner < Thor
@@ -1,3 +1,3 @@
1
1
  module RubyScribe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/ruby_scribe.rb CHANGED
@@ -4,5 +4,5 @@ require "ruby_parser"
4
4
 
5
5
  require "ruby_scribe/emitter_helpers"
6
6
  require "ruby_scribe/emitter"
7
- require "ruby_scribe/preprocessor"
8
- require "ruby_scribe/ext/sexp"
7
+ require "ruby_scribe/transformation"
8
+ require "ruby_scribe/ext/sexp"
@@ -0,0 +1,17 @@
1
+ require "some_file"
2
+ $:.unshift("directory")
3
+
4
+ module RubyScribe
5
+ module Example
6
+ def module_method
7
+ add + something
8
+ subtract - something
9
+ self.array << "append"
10
+ end
11
+ end
12
+
13
+ class MyClass < Subclass
14
+ attr_accessor :no_parathesis
15
+ call_method("Apartness")
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ RSpec::Matchers.define :emit_as do |expected|
2
+ match do |actual|
3
+ @emitted = RubyScribe::Emitter.new.emit(actual)
4
+ @emitted == expected
5
+ end
6
+
7
+ failure_message_for_should do |actual|
8
+ "expected that:\n\n#{actual}\n\n would emit as:\n\n#{with_boundaries(expected)}\n\nbut instead was:\n\n#{with_boundaries(@emitted)}"
9
+ end
10
+
11
+ failure_message_for_should_not do |actual|
12
+ "expected that:\n\n#{actual}\n\n would not emit as:\n\n#{with_boundaries(expected)}\n\nbut instead was:\n\n#{with_boundaries(@emitted)}"
13
+ end
14
+
15
+ description do
16
+ segment = expected.split("\n")[0] + "..."
17
+ "emit as #{segment}"
18
+ end
19
+
20
+ def with_boundaries(string)
21
+ string.split("\n").map {|s| s + "|"}.join("\n")
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ RSpec::Matchers.define :emit_itself do
2
+ match do |actual|
3
+ @parsed = RubyParser.new.parse(actual)
4
+ @emitted = RubyScribe::Emitter.new.emit(@parsed)
5
+ @emitted == actual
6
+ end
7
+
8
+ failure_message_for_should do |actual|
9
+ "expected that the s-expression:\n\n#{@parsed}\n\nrepresenting:\n\n#{with_boundaries(actual)}\n\nwould emit itself, but emitted:\n\n#{with_boundaries(@emitted)}\n\n"
10
+ end
11
+
12
+ failure_message_for_should_not do |actual|
13
+ "expected that the s-expression:\n\n#{@parsed}\n\nrepresenting:\n\n#{actual}\n\nwould emit itself, but emitted:\n\n#{@emitted}\n\n"
14
+ end
15
+
16
+ description do
17
+ "emit itself"
18
+ end
19
+
20
+ def with_boundaries(string)
21
+ string.split("\n").map {|s| s + "|"}.join("\n")
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe RubyScribe::Emitter, "Examples" do
4
+ before { @emitter = RubyScribe::Emitter.new }
5
+
6
+ Dir[File.join(File.dirname(__FILE__), "../examples/*.rb")].each do |example|
7
+ describe "#{File.basename(example)} example" do
8
+ specify("should emit itself") { File.read(example).should emit_itself }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,187 @@
1
+ require "spec_helper"
2
+
3
+ ## Relying on RubyParser here to emit what we expect, but much more readable than manually composite s-expressions
4
+
5
+ describe RubyScribe::Emitter do
6
+ before { @emitter = RubyScribe::Emitter.new }
7
+
8
+ context "class definition" do
9
+ specify "simple class should emit itself" do
10
+ %{class Animal\n \nend}.should emit_itself
11
+ end
12
+
13
+ specify "extended class should emit itself" do
14
+ %{class Animal < Creature\n \nend}.should emit_itself
15
+ end
16
+
17
+ specify "namespaced class should emit itself" do
18
+ pending do
19
+ %{class Scribe::Animal\n \nend}.should emit_itself
20
+ end
21
+ end
22
+
23
+ specify "eigenclass should emit itself" do
24
+ pending do
25
+ %{class << self\n \nend}.should emit_itself
26
+ end
27
+ end
28
+ end
29
+
30
+ context "module definition" do
31
+ specify "simple module should emit itself" do
32
+ %{module Animal\n \nend}.should emit_itself
33
+ end
34
+
35
+ specify "namespaced class should emit itself" do
36
+ pending do
37
+ %{module Scribe::Animal\n \nend}.should emit_itself
38
+ end
39
+ end
40
+ end
41
+
42
+ context "rescue definition" do
43
+ specify "rescue all should emit itself" do
44
+ %{begin\n \nrescue\n \nend}.should emit_itself
45
+ end
46
+
47
+ specify "method-wide rescue should emit itself" do
48
+ %{def method\n \nrescue\n \nend}.should emit_itself
49
+ end
50
+ end
51
+
52
+ context "method definition" do
53
+ specify "without arguments" do
54
+ %{def method\n \nend}.should emit_itself
55
+ end
56
+
57
+ specify "with one argument" do
58
+ %{def method(one)\n \nend}.should emit_itself
59
+ end
60
+
61
+ specify "with multiple arguments" do
62
+ %{def method(one, two)\n \nend}.should emit_itself
63
+ end
64
+
65
+ specify "with optional arguments" do
66
+ %{def method(one = 1, two = {})\n \nend}.should emit_itself
67
+ end
68
+
69
+ specify "with block argument" do
70
+ %{def method(one, &two)\n \nend}.should emit_itself
71
+ end
72
+ end
73
+
74
+ context "method call" do
75
+ specify "without arguments" do
76
+ %{method}.should emit_itself
77
+ end
78
+
79
+ specify "with one argument" do
80
+ %{method("One")}.should emit_itself
81
+ end
82
+
83
+ specify "with multiple arguments" do
84
+ %{method("One", 2)}.should emit_itself
85
+ end
86
+
87
+ specify "with last argument as a hash" do
88
+ %{method("One", :option => :one)}.should emit_itself
89
+ end
90
+
91
+ specify "with block" do
92
+ %{method do\n \nend}.should emit_itself
93
+ end
94
+ end
95
+
96
+ context "case statement" do
97
+ specify "with argument" do
98
+ %{case something\nwhen 1\n \nend}.should emit_itself
99
+ end
100
+
101
+ specify "without argument" do
102
+ %{case\nwhen 1 == 1\n \nend}
103
+ end
104
+
105
+ specify "with else block" do
106
+ %{case something\nwhen 1\n \nelse\n 2\nend}.should emit_itself
107
+ end
108
+ end
109
+
110
+ context "attribute assignment" do
111
+ specify "to simple local variable" do
112
+ %{variable = 1}.should emit_itself
113
+ end
114
+
115
+ specify "to instance variable" do
116
+ %{@variable = 1}.should emit_itself
117
+ end
118
+
119
+ specify "to multiple variables" do
120
+ %{variable_1, variable_2 = 1, 2}.should emit_itself
121
+ end
122
+
123
+ specify "with or" do
124
+ %{@variable ||= 1}.should emit_itself
125
+ end
126
+
127
+ specify "with and" do
128
+ %{@variable &&= 1}.should emit_itself
129
+ end
130
+ end
131
+
132
+ context "conditionals" do
133
+ specify "simple block if" do
134
+ %{if true\n something\nend}.should emit_itself
135
+ end
136
+
137
+ specify "simple block unless" do
138
+ %{unless true\n something\nend}.should emit_itself
139
+ end
140
+
141
+ specify "simple block if else" do
142
+ %{if true\n something\nelse\n something_else\nend}.should emit_itself
143
+ end
144
+
145
+ specify "dangling if" do
146
+ %{something if true}.should emit_itself
147
+ end
148
+
149
+ specify "dangling unless" do
150
+ %{something unless true}.should emit_itself
151
+ end
152
+
153
+ specify "ternary if" do
154
+ %{something ? true : false}.should emit_itself
155
+ end
156
+ end
157
+
158
+ context "looping expression definition" do
159
+ specify "while" do
160
+ %{while true\n \nend}.should emit_itself
161
+ end
162
+
163
+ specify "until" do
164
+ %{until true\n \nend}.should emit_itself
165
+ end
166
+
167
+ specify "for in array" do
168
+ %{for something in array\n \nend}.should emit_itself
169
+ end
170
+ end
171
+
172
+ context "binary expressions" do
173
+ specify "||" do
174
+ %{(one || two)}.should emit_itself
175
+ end
176
+
177
+ specify "&&" do
178
+ %{(one && two)}.should emit_itself
179
+ end
180
+ end
181
+
182
+ context "unary expressions" do
183
+ specify "!something" do
184
+ %{!something}.should emit_itself
185
+ end
186
+ end
187
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_scribe
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Hughes
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-25 00:00:00 +03:00
18
+ date: 2010-10-27 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -66,8 +66,8 @@ dependencies:
66
66
  version_requirements: *id003
67
67
  description: A ruby formatting tool that takes S-expression as input and intelligently outputs formatted Ruby code.
68
68
  email: ben@railsgarden.com
69
- executables: []
70
-
69
+ executables:
70
+ - rubyscribe
71
71
  extensions: []
72
72
 
73
73
  extra_rdoc_files: []
@@ -76,19 +76,23 @@ files:
76
76
  - lib/ruby_scribe/emitter.rb
77
77
  - lib/ruby_scribe/emitter_helpers.rb
78
78
  - lib/ruby_scribe/ext/sexp.rb
79
- - lib/ruby_scribe/preprocessor.rb
80
79
  - lib/ruby_scribe/runner.rb
80
+ - lib/ruby_scribe/transformation.rb
81
81
  - lib/ruby_scribe/version.rb
82
82
  - lib/ruby_scribe.rb
83
83
  - lib/tasks/scribe.rake
84
- - spec/examples/simple_class_with_methods.rb
85
- - spec/proprocessor_spec.rb
84
+ - spec/examples/identity.rb
85
+ - spec/matchers/should_emit_as.rb
86
+ - spec/matchers/should_emit_itself.rb
87
+ - spec/ruby_scribe/emitter_examples_spec.rb
88
+ - spec/ruby_scribe/emitter_spec.rb
89
+ - spec/ruby_scribe/transformation_spec.rb
86
90
  - spec/spec_helper.rb
87
- - spec/strategy_spec.rb
88
91
  - LICENSE
89
92
  - Rakefile
90
93
  - README.rdoc
91
94
  - TODO.rdoc
95
+ - bin/rubyscribe
92
96
  has_rdoc: true
93
97
  homepage: http://github.com/rubiety/ruby_scribe
94
98
  licenses: []
@@ -1,14 +0,0 @@
1
- module RubyScribe
2
-
3
-
4
- # My Comment
5
- class Sample < Base;
6
- def method; do_something_here; end
7
-
8
- if(new_record?) then
9
- puts "Yes"
10
- else
11
- puts 'No'
12
- end
13
- end
14
- end
@@ -1,9 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe RubyScribe::Strategy do
4
- before do
5
- @strategy = RubyScribe::Strategy.new
6
- end
7
-
8
-
9
- end