sourcify 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.1.2 (Aug 30, 2010)
2
+
3
+ * introduced throwing of Sourcify::LexerInternalError when parser cannot handle
4
+ incorrectly lexed code fragments [#ngty]
5
+ * fixed bug in lexing keyword literals (represented as Symbol) [#ngty]
6
+
1
7
  === 0.1.1 (Aug 30, 2010)
2
8
 
3
9
  * fixed empty return for Proc#to_sexp after calling of Proc#to_source [#ngty]
data/README.rdoc CHANGED
@@ -139,6 +139,22 @@ following bug under 1.8.* may trip u over:
139
139
  This is another reason to install ParseTree when u are on 1.8.*. On JRuby, where u don't
140
140
  have the choice, just avoid multiple procs here line.
141
141
 
142
+ === 3. Imperfect lexer
143
+
144
+ Nothing is perfect, bugs are always lurking somewhere. When the parser encounters
145
+ unexpected lexing error (which may be due to Ripper, RubyLex, or Sourcify's wrapper lexer),
146
+ Sourcify::LexerInternalError is thrown.
147
+
148
+ == Additional Resources
149
+
150
+ Sourcify is heavily inspired by many ideas gathered from the ruby community:
151
+ * http://www.justskins.com/forums/breaking-ruby-code-into-117453.html
152
+ * http://rubyquiz.com/quiz38.html (Florian Groß's solution)
153
+ * http://svenfuchs.com/2009/07/05/using-ruby-1-9-ripper.html
154
+
155
+ The sad fact that Proc#to_source wouldn't be available in the near future:
156
+ * http://redmine.ruby-lang.org/issues/show/2080
157
+
142
158
  == Note on Patches/Pull Requests
143
159
 
144
160
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/sourcify/proc.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Sourcify
2
2
 
3
3
  class MultipleMatchingProcsPerLineError < Exception ; end
4
+ class LexerInternalError < Exception ; end
4
5
 
5
6
  module Proc
6
7
 
@@ -19,13 +20,12 @@ module Sourcify
19
20
  end
20
21
  end
21
22
 
22
- # When ParseTree is available, we just make use of all the convenience it offers :)
23
23
  if Object.const_defined?(:ParseTree)
24
-
24
+ # When ParseTree is available, we just make use of all the convenience it offers :)
25
25
  alias_method :to_source, :to_ruby
26
26
 
27
- # Otherwise, we are going to do abit of static text parsing :(
28
27
  else
28
+ # Otherwise, we are going to do abit of static text parsing :(
29
29
 
30
30
  unless meths.include?('to_source')
31
31
  def to_source
@@ -1,6 +1,6 @@
1
1
  module Sourcify
2
2
  module Proc
3
- class Counter
3
+ class Counter #:nodoc:all
4
4
 
5
5
  attr_accessor :marker
6
6
 
@@ -1,6 +1,6 @@
1
1
  module Sourcify
2
2
  module Proc
3
- module Lexer
3
+ module Lexer #:nodoc:all
4
4
 
5
5
  class << self
6
6
  def new(*args)
@@ -3,11 +3,12 @@ require 'irb/ruby-token'
3
3
 
4
4
  module Sourcify
5
5
  module Proc
6
+ class Lexer18 #:nodoc:all
6
7
 
7
- class Lexer18
8
-
9
- # Implementation of this class has been inspired by the discussion at
10
- # http://www.justskins.com/forums/breaking-ruby-code-into-117453.html
8
+ # NOTE: Implementation of this class has been heavily inspired by:
9
+ # * the discussion @ http://www.justskins.com/forums/breaking-ruby-code-into-117453.html
10
+ # * Florian Groß's solution for ruby quiz 'SerializableProc (#38)'
11
+ # @ http://rubyquiz.com/quiz38.html
11
12
 
12
13
  include Lexer::Commons
13
14
 
@@ -26,8 +27,9 @@ module Sourcify
26
27
  tkc = @tk.class.to_s.sub(/\ARubyToken::/, '').downcase.to_sym
27
28
  @tokens << [@tk.line_no, @tk.char_no, tkc]
28
29
  post_qstring if @qstring_data
29
- send(:"on_#{tkc}") rescue NoMethodError
30
30
  @lex.get_readed if tkc == :tknl
31
+ next if @do_end_counter.started? && @tokens.curr.symbolized_keyword?
32
+ send(:"on_#{tkc}") rescue NoMethodError
31
33
  end
32
34
  end
33
35
 
@@ -180,6 +182,17 @@ module Sourcify
180
182
  end
181
183
 
182
184
  def curr
185
+ (self[-1]).respond_to?(:symbolized_keyword?) ? self[-1] : (
186
+ preceding, current = self[-2 .. -1]
187
+ (class << current ; self ; end).class_eval do
188
+ define_method(:symbolized_keyword?) do
189
+ preceding[TYP] == :tksymbeg && %w{
190
+ class module def while until if unless for do begin case end
191
+ }.include?(current[TYP].to_s.sub('tk',''))
192
+ end
193
+ end
194
+ current
195
+ )
183
196
  self[-1]
184
197
  end
185
198
 
@@ -2,7 +2,7 @@ require 'ripper'
2
2
 
3
3
  module Sourcify
4
4
  module Proc
5
- class Lexer19 < Ripper::Lexer
5
+ class Lexer19 < Ripper::Lexer #:nodoc:all
6
6
 
7
7
  include Lexer::Commons
8
8
 
@@ -16,6 +16,7 @@ module Sourcify
16
16
 
17
17
  def on_kw(token)
18
18
  super.tap do |rs|
19
+ next if @do_end_counter.started? && rs.curr.symbolized_keyword?
19
20
  send(:"on_kw_#{token}", rs) rescue NoMethodError
20
21
  end
21
22
  end
@@ -125,7 +126,15 @@ module Sourcify
125
126
  end
126
127
 
127
128
  def curr
128
- self[-1]
129
+ (self[-1]).respond_to?(:symbolized_keyword?) ? self[-1] : (
130
+ preceding, current = self[-2 .. -1]
131
+ (class << current ; self ; end).class_eval do
132
+ define_method(:symbolized_keyword?) do
133
+ current[TYP] == :on_kw && preceding[TYP] == :on_symbeg
134
+ end
135
+ end
136
+ current
137
+ )
129
138
  end
130
139
 
131
140
  def same_line(line)
@@ -3,7 +3,7 @@ require 'sourcify/proc/counter'
3
3
 
4
4
  module Sourcify
5
5
  module Proc
6
- class Parser
6
+ class Parser #:nodoc:all
7
7
 
8
8
  RUBY_PARSER = RubyParser.new
9
9
  RUBY_2_RUBY = Ruby2Ruby.new
@@ -27,12 +27,22 @@ module Sourcify
27
27
  private
28
28
 
29
29
  def raw_source
30
- @raw_source ||= (
31
- frags = Sourcify::Proc::Lexer.new(raw_source_io, @file, @line).work.
32
- select{|frag| eval('proc ' + frag).arity == @arity }
33
- raise MultipleMatchingProcsPerLineError if frags.size > 1
34
- 'proc %s' % frags[0]
35
- )
30
+ @raw_source ||=
31
+ if (frags = raw_source_frags).size > 1
32
+ raise MultipleMatchingProcsPerLineError
33
+ else
34
+ 'proc %s' % frags[0]
35
+ end
36
+ end
37
+
38
+ def raw_source_frags
39
+ Sourcify::Proc::Lexer.new(raw_source_io, @file, @line).work.select do |frag|
40
+ begin
41
+ eval('proc ' + frag).arity == @arity
42
+ rescue SyntaxError, TypeError
43
+ raise LexerInternalError
44
+ end
45
+ end
36
46
  end
37
47
 
38
48
  def raw_source_io
data/sourcify.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sourcify}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["NgTzeYang"]
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
45
45
  "spec/proc/to_source_from_do_end_block_w_nested_do_end_block_spec.rb",
46
46
  "spec/proc/to_source_from_do_end_block_w_nested_for_spec.rb",
47
47
  "spec/proc/to_source_from_do_end_block_w_nested_if_spec.rb",
48
+ "spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb",
48
49
  "spec/proc/to_source_from_do_end_block_w_nested_method_spec.rb",
49
50
  "spec/proc/to_source_from_do_end_block_w_nested_module_spec.rb",
50
51
  "spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb",
@@ -79,6 +80,7 @@ Gem::Specification.new do |s|
79
80
  "spec/proc/to_source_from_do_end_block_w_nested_class_spec.rb",
80
81
  "spec/proc/to_source_from_do_end_block_w_nested_case_spec.rb",
81
82
  "spec/proc/to_source_magic_line_var_spec.rb",
83
+ "spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb",
82
84
  "spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb",
83
85
  "spec/proc/19x_extras.rb",
84
86
  "spec/proc/to_source_from_braced_block_w_nested_braced_block_spec.rb",
@@ -0,0 +1,101 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe "Proc#to_source from do ... end block (w nested literal keyword)" do
4
+
5
+ should 'handle :class' do
6
+ (
7
+ lambda do
8
+ x = :class
9
+ end
10
+ ).should.be having_source('proc { x = :class }')
11
+ end
12
+
13
+ should 'handle :module' do
14
+ (
15
+ lambda do
16
+ x = :module
17
+ end
18
+ ).should.be having_source('proc { x = :module }')
19
+ end
20
+
21
+ should 'handle :def' do
22
+ (
23
+ lambda do
24
+ x = :def
25
+ end
26
+ ).should.be having_source('proc { x = :def }')
27
+ end
28
+
29
+ should 'handle :if' do
30
+ (
31
+ lambda do
32
+ x = :if
33
+ end
34
+ ).should.be having_source('proc { x = :if }')
35
+ end
36
+
37
+ should 'handle :unless' do
38
+ (
39
+ lambda do
40
+ x = :unless
41
+ end
42
+ ).should.be having_source('proc { x = :unless }')
43
+ end
44
+
45
+ should 'handle :for' do
46
+ (
47
+ lambda do
48
+ x = :for
49
+ end
50
+ ).should.be having_source('proc { x = :for }')
51
+ end
52
+
53
+ should 'handle :while' do
54
+ (
55
+ lambda do
56
+ x = :while
57
+ end
58
+ ).should.be having_source('proc { x = :while }')
59
+ end
60
+
61
+ should 'handle :until' do
62
+ (
63
+ lambda do
64
+ x = :until
65
+ end
66
+ ).should.be having_source('proc { x = :until }')
67
+ end
68
+
69
+ should 'handle :begin' do
70
+ (
71
+ lambda do
72
+ x = :begin
73
+ end
74
+ ).should.be having_source('proc { x = :begin }')
75
+ end
76
+
77
+ should 'handle :case' do
78
+ (
79
+ lambda do
80
+ x = :case
81
+ end
82
+ ).should.be having_source('proc { x = :case }')
83
+ end
84
+
85
+ should 'handle :do' do
86
+ (
87
+ lambda do
88
+ x = :do
89
+ end
90
+ ).should.be having_source('proc { x = :do }')
91
+ end
92
+
93
+ should 'handle :end' do
94
+ (
95
+ lambda do
96
+ x = :end
97
+ end
98
+ ).should.be having_source('proc { x = :end }')
99
+ end
100
+
101
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sourcify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - NgTzeYang
@@ -86,6 +86,7 @@ files:
86
86
  - spec/proc/to_source_from_do_end_block_w_nested_do_end_block_spec.rb
87
87
  - spec/proc/to_source_from_do_end_block_w_nested_for_spec.rb
88
88
  - spec/proc/to_source_from_do_end_block_w_nested_if_spec.rb
89
+ - spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb
89
90
  - spec/proc/to_source_from_do_end_block_w_nested_method_spec.rb
90
91
  - spec/proc/to_source_from_do_end_block_w_nested_module_spec.rb
91
92
  - spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb
@@ -148,6 +149,7 @@ test_files:
148
149
  - spec/proc/to_source_from_do_end_block_w_nested_class_spec.rb
149
150
  - spec/proc/to_source_from_do_end_block_w_nested_case_spec.rb
150
151
  - spec/proc/to_source_magic_line_var_spec.rb
152
+ - spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb
151
153
  - spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb
152
154
  - spec/proc/19x_extras.rb
153
155
  - spec/proc/to_source_from_braced_block_w_nested_braced_block_spec.rb