sourcify 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +1 -0
  2. data/HISTORY.txt +11 -0
  3. data/README.rdoc +52 -28
  4. data/VERSION +1 -1
  5. data/lib/sourcify.rb +13 -1
  6. data/lib/sourcify/proc.rb +7 -4
  7. data/lib/sourcify/proc/parser.rb +109 -40
  8. data/lib/sourcify/proc/scanner.rb +2140 -0
  9. data/lib/sourcify/proc/scanner.rl +285 -0
  10. data/lib/sourcify/proc/scanner/comment.rb +21 -0
  11. data/lib/sourcify/proc/scanner/counter.rb +44 -0
  12. data/lib/sourcify/proc/scanner/dstring.rb +58 -0
  13. data/lib/sourcify/proc/scanner/extensions.rb +135 -0
  14. data/lib/sourcify/proc/scanner/heredoc.rb +24 -0
  15. data/sourcify.gemspec +38 -9
  16. data/spec/dump_object_space_procs.rb +84 -0
  17. data/spec/proc/created_on_the_fly_proc_spec.rb +172 -0
  18. data/spec/proc/others_spec.rb +36 -0
  19. data/spec/proc/to_sexp_variables_spec.rb +6 -6
  20. data/spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb +2 -0
  21. data/spec/proc/to_source_from_multi_blocks_w_many_matches_spec.rb +105 -29
  22. data/spec/proc/to_source_from_multi_blocks_w_single_match_spec.rb +85 -17
  23. data/spec/proc_scanner/block_comment_spec.rb +59 -0
  24. data/spec/proc_scanner/double_colons_spec.rb +14 -0
  25. data/spec/proc_scanner/double_quote_str_w_interpolation_spec.rb +62 -0
  26. data/spec/proc_scanner/double_quote_str_wo_interpolation_spec.rb +75 -0
  27. data/spec/proc_scanner/heredoc_spec.rb +142 -0
  28. data/spec/proc_scanner/kw_do_alias1_spec.rb +87 -0
  29. data/spec/proc_scanner/kw_do_alias2_spec.rb +86 -0
  30. data/spec/proc_scanner/per_line_comment_spec.rb +34 -0
  31. data/spec/proc_scanner/single_quote_str_spec.rb +68 -0
  32. data/spec/proc_scanner/spec_helper.rb +33 -0
  33. data/spec/proc_scanner/to_proc_spec.rb +15 -0
  34. data/spec/spec_helper.rb +23 -0
  35. metadata +39 -10
  36. data/lib/sourcify/proc/counter.rb +0 -41
  37. data/lib/sourcify/proc/lexer.rb +0 -40
  38. data/lib/sourcify/proc/lexer18.rb +0 -237
  39. data/lib/sourcify/proc/lexer19.rb +0 -204
  40. data/spec/proc/misc_spec.rb +0 -16
@@ -1,204 +0,0 @@
1
- require 'ripper'
2
-
3
- module Sourcify
4
- module Proc
5
- class Lexer19 < Ripper::Lexer #:nodoc:all
6
-
7
- include Lexer::Commons
8
-
9
- def on_nl(token)
10
- super.tap do |rs|
11
- raise EndOfLine unless @results.empty?
12
- end
13
- end
14
-
15
- alias_method :on_ignored_nl, :on_nl
16
-
17
- def on_kw(token)
18
- super.tap do |rs|
19
- next if @do_end_counter.started? && rs.curr.symbolized_keyword?
20
- send(:"on_kw_#{token}", rs) rescue NoMethodError
21
- end
22
- end
23
-
24
- def on_kw_class(rs)
25
- # Pretty straightforward for these, each of them will consume an 'end' close it
26
- @do_end_counter.increment_start if @do_end_counter.started?
27
- end
28
-
29
- # These work the same as 'class', the exception is 'for', which can have an optional
30
- # 'do' attached:
31
- # * for a in [1,2] do ... end
32
- # * for a in [1,2] \n ... end
33
- %w{def module begin case for}.each{|kw| alias_method :"on_kw_#{kw}", :on_kw_class }
34
-
35
- def on_kw_while(rs)
36
- # This has optional trailing 'do', and can work as a modifier as well, eg:
37
- # * while true do ... end # => 'do' must be on the same line as 'while'
38
- # * while true \n ... end
39
- # * ... while true # => 'while' is pre-pended with non-spaces
40
- if @do_end_counter.started? && (rs.start_of_line? or rs.within_block?)
41
- @do_end_counter.increment_start
42
- end
43
- end
44
-
45
- # These work exactly the same as 'while'.
46
- %w{until if unless}.each{|kw| alias_method :"on_kw_#{kw}", :on_kw_while }
47
-
48
- def on_kw_do(rs)
49
- if !@do_end_counter.started?
50
- rs.extend(Extensions) unless rs.respond_to?(:curr)
51
- @do_end_counter.marker = rs.curr
52
- @do_end_counter.increment_start
53
- elsif rs.same_as_curr_line.keywords(%w{for while until}).empty?
54
- # It is possible for a 'for', 'while' or 'until' to have an attached 'do',
55
- # for such a case, we want to skip it
56
- @do_end_counter.increment_start
57
- end
58
- end
59
-
60
- def on_kw_end(rs)
61
- if @do_end_counter.started? && @do_end_counter.increment_end.telly?
62
- @result = rs.to_code(@do_end_counter.marker)
63
- @is_multiline_block = rs.multiline?
64
- raise EndOfBlock
65
- end
66
- end
67
-
68
- def on_lbrace(token)
69
- super.tap do |rs|
70
- unless @do_end_counter.started?
71
- rs.extend(Extensions) unless rs.respond_to?(:curr)
72
- @braced_counter.marker = rs.curr unless @braced_counter.started?
73
- @braced_counter.increment_start
74
- end
75
- end
76
- end
77
-
78
- def on_rbrace(token)
79
- super.tap do |rs|
80
- if @braced_counter.started? && @braced_counter.increment_end.telly?
81
- @result = rs.to_code(@braced_counter.marker)
82
- @is_multiline_block = rs.multiline?
83
- raise EndOfBlock
84
- end
85
- end
86
- end
87
-
88
- def on_embexpr_beg(token)
89
- super.tap do |rs|
90
- @braced_counter.increment_start if @braced_counter.started?
91
- end
92
- end
93
-
94
- def on_op(token)
95
- super.tap do |rs|
96
- if @braced_counter.started? && token == '=>' && @braced_counter[:start] == 1
97
- @braced_counter.decrement_start
98
- end
99
- end
100
- end
101
-
102
- def on_label(token)
103
- super.tap do |rs|
104
- if @braced_counter.started? && @braced_counter[:start] == 1
105
- @braced_counter.decrement_start
106
- end
107
- end
108
- end
109
-
110
- # Ease working with the result set generated by Ripper
111
- module Extensions
112
-
113
- POS, TYP, VAL = 0, 1, 2
114
- ROW, COL= 0, 1
115
-
116
- def same_as_curr_line
117
- same_line(curr_line)
118
- end
119
-
120
- def multiline?
121
- self[0][POS][ROW] != self[-1][POS][ROW]
122
- end
123
-
124
- def curr_line
125
- curr[POS][ROW]
126
- end
127
-
128
- def curr
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
- )
138
- end
139
-
140
- def same_line(line)
141
- (
142
- # ignore the current node
143
- self[0..-2].reverse.take_while do |e|
144
- if e[TYP] == :on_semicolon && e[VAL] == ';'
145
- false
146
- elsif e[POS][ROW] == line
147
- true
148
- elsif e[TYP] == :on_sp && e[VAL] == "\\\n"
149
- line -= 1
150
- true
151
- end
152
- end.reverse
153
- ).extend(Extensions)
154
- end
155
-
156
- def keywords(*types)
157
- (
158
- types = [types].flatten.map(&:to_s)
159
- select{|e| e[TYP] == :on_kw && (types.empty? or types.include?(e[VAL])) }
160
- ).extend(Extensions)
161
- end
162
-
163
- def non_spaces(*types)
164
- (
165
- types = [types].flatten
166
- reject{|e| e[TYP] == :on_sp && (types.empty? or types.include?(e[VAL])) }
167
- ).extend(Extensions)
168
- end
169
-
170
- def start_of_line?
171
- same_as_curr_line.non_spaces.empty?
172
- end
173
-
174
- def within_block?
175
- same_as_curr_line.non_spaces[-1][TYP] == :on_lparen
176
- end
177
-
178
- def to_code(marker)
179
- heredoc_beg = false # fixing mysteriously missing newline after :on_heredoc_begin
180
- self[index(marker) .. -1].map do |e|
181
- if e[TYP] == :on_heredoc_beg
182
- heredoc_beg = true
183
- e[VAL]
184
- elsif heredoc_beg && e[TYP] != :on_nl
185
- heredoc_beg = false
186
- "\n" + e[VAL]
187
- else
188
- heredoc_beg = false
189
- if e[TYP] == :on_label
190
- ':%s => ' % e[VAL][0..-2]
191
- elsif e[TYP] == :on_kw && e[VAL] == '__LINE__'
192
- e[POS][ROW]
193
- else
194
- e[VAL]
195
- end
196
- end
197
- end.join
198
- end
199
-
200
- end
201
-
202
- end
203
- end
204
- end
@@ -1,16 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe "Misc" do
4
-
5
- should 'handle accessing #to_sexp after #to_source' do
6
- (s_proc = lambda { :test }).to_source
7
- s_proc.to_sexp.should.equal(s(:iter, s(:call, nil, :proc, s(:arglist)), nil, s(:lit, :test)))
8
- end
9
-
10
- should 'handle accessing #to_source after #to_sexp' do
11
- (s_proc = lambda { :test }).to_sexp
12
- s_proc.to_source.should.equal('proc { :test }')
13
- end
14
-
15
- end
16
-