@@ -16,7 +16,8 @@ def initialize
16
16
end
17
17
end
18
18
19
- def initialize
19
+ def initialize ( context )
20
+ @context = context
20
21
@exp_line_no = @line_no = 1
21
22
@indent = 0
22
23
@continue = false
@@ -42,13 +43,13 @@ def self.compile_with_errors_suppressed(code, line_no: 1)
42
43
end
43
44
44
45
# io functions
45
- def set_input ( io , context : , &block )
46
+ def set_input ( io , &block )
46
47
@io = io
47
48
if @io . respond_to? ( :check_termination )
48
49
@io . check_termination do |code |
49
50
if Reline ::IOGate . in_pasting?
50
- lex = RubyLex . new
51
- rest = lex . check_termination_in_prev_line ( code , context : context )
51
+ lex = RubyLex . new ( @context )
52
+ rest = lex . check_termination_in_prev_line ( code )
52
53
if rest
53
54
Reline . delete_text
54
55
rest . bytes . reverse_each do |c |
@@ -61,13 +62,13 @@ def set_input(io, context:, &block)
61
62
else
62
63
# Accept any single-line input for symbol aliases or commands that transform args
63
64
command = code . split ( /\s / , 2 ) . first
64
- if context . symbol_alias? ( command ) || context . transform_args? ( command )
65
+ if @ context. symbol_alias? ( command ) || @ context. transform_args? ( command )
65
66
next true
66
67
end
67
68
68
69
code . gsub! ( /\s *\z / , '' ) . concat ( "\n " )
69
- tokens = self . class . ripper_lex_without_warning ( code , context : context )
70
- ltype , indent , continue , code_block_open = check_state ( code , tokens , context : context )
70
+ tokens = self . class . ripper_lex_without_warning ( code , context : @ context)
71
+ ltype , indent , continue , code_block_open = check_state ( code , tokens )
71
72
if ltype or indent > 0 or continue or code_block_open
72
73
false
73
74
else
@@ -80,7 +81,7 @@ def set_input(io, context:, &block)
80
81
@io . dynamic_prompt do |lines |
81
82
lines << '' if lines . empty?
82
83
result = [ ]
83
- tokens = self . class . ripper_lex_without_warning ( lines . map { |l | l + "\n " } . join , context : context )
84
+ tokens = self . class . ripper_lex_without_warning ( lines . map { |l | l + "\n " } . join , context : @ context)
84
85
code = String . new
85
86
partial_tokens = [ ]
86
87
unprocessed_tokens = [ ]
@@ -93,7 +94,7 @@ def set_input(io, context:, &block)
93
94
t_str . each_line ( "\n " ) do |s |
94
95
code << s
95
96
next unless s . include? ( "\n " )
96
- ltype , indent , continue , code_block_open = check_state ( code , partial_tokens , context : context )
97
+ ltype , indent , continue , code_block_open = check_state ( code , partial_tokens )
97
98
result << @prompt . call ( ltype , indent , continue || code_block_open , @line_no + line_num_offset )
98
99
line_num_offset += 1
99
100
end
@@ -104,7 +105,7 @@ def set_input(io, context:, &block)
104
105
end
105
106
106
107
unless unprocessed_tokens . empty?
107
- ltype , indent , continue , code_block_open = check_state ( code , unprocessed_tokens , context : context )
108
+ ltype , indent , continue , code_block_open = check_state ( code , unprocessed_tokens )
108
109
result << @prompt . call ( ltype , indent , continue || code_block_open , @line_no + line_num_offset )
109
110
end
110
111
result
@@ -187,11 +188,11 @@ def find_prev_spaces(line_index)
187
188
prev_spaces
188
189
end
189
190
190
- def set_auto_indent ( context )
191
- if @io . respond_to? ( :auto_indent ) and context . auto_indent_mode
191
+ def set_auto_indent
192
+ if @io . respond_to? ( :auto_indent ) and @ context. auto_indent_mode
192
193
@io . auto_indent do |lines , line_index , byte_pointer , is_newline |
193
194
if is_newline
194
- @tokens = self . class . ripper_lex_without_warning ( lines [ 0 ..line_index ] . join ( "\n " ) , context : context )
195
+ @tokens = self . class . ripper_lex_without_warning ( lines [ 0 ..line_index ] . join ( "\n " ) , context : @ context)
195
196
prev_spaces = find_prev_spaces ( line_index )
196
197
depth_difference = check_newline_depth_difference
197
198
depth_difference = 0 if depth_difference < 0
@@ -200,18 +201,18 @@ def set_auto_indent(context)
200
201
code = line_index . zero? ? '' : lines [ 0 ..( line_index - 1 ) ] . map { |l | l + "\n " } . join
201
202
last_line = lines [ line_index ] &.byteslice ( 0 , byte_pointer )
202
203
code += last_line if last_line
203
- @tokens = self . class . ripper_lex_without_warning ( code , context : context )
204
+ @tokens = self . class . ripper_lex_without_warning ( code , context : @ context)
204
205
check_corresponding_token_depth ( lines , line_index )
205
206
end
206
207
end
207
208
end
208
209
end
209
210
210
- def check_state ( code , tokens , context : )
211
+ def check_state ( code , tokens )
211
212
ltype = process_literal_type ( tokens )
212
213
indent = process_nesting_level ( tokens )
213
214
continue = process_continue ( tokens )
214
- lvars_code = self . class . generate_local_variables_assign_code ( context . local_variables )
215
+ lvars_code = self . class . generate_local_variables_assign_code ( @ context. local_variables )
215
216
code = "#{ lvars_code } \n #{ code } " if lvars_code
216
217
code_block_open = check_code_block ( code , tokens )
217
218
[ ltype , indent , continue , code_block_open ]
@@ -232,13 +233,13 @@ def initialize_input
232
233
@code_block_open = false
233
234
end
234
235
235
- def each_top_level_statement ( context )
236
+ def each_top_level_statement
236
237
initialize_input
237
238
catch ( :TERM_INPUT ) do
238
239
loop do
239
240
begin
240
241
prompt
241
- unless l = lex ( context )
242
+ unless l = lex
242
243
throw :TERM_INPUT if @line == ''
243
244
else
244
245
@line_no += l . count ( "\n " )
@@ -268,15 +269,15 @@ def each_top_level_statement(context)
268
269
end
269
270
end
270
271
271
- def lex ( context )
272
+ def lex
272
273
line = @input . call
273
274
if @io . respond_to? ( :check_termination )
274
275
return line # multiline
275
276
end
276
277
code = @line + ( line . nil? ? '' : line )
277
278
code . gsub! ( /\s *\z / , '' ) . concat ( "\n " )
278
- @tokens = self . class . ripper_lex_without_warning ( code , context : context )
279
- @ltype , @indent , @continue , @code_block_open = check_state ( code , @tokens , context : context )
279
+ @tokens = self . class . ripper_lex_without_warning ( code , context : @ context)
280
+ @ltype , @indent , @continue , @code_block_open = check_state ( code , @tokens )
280
281
line
281
282
end
282
283
@@ -777,8 +778,8 @@ def process_literal_type(tokens)
777
778
end
778
779
end
779
780
780
- def check_termination_in_prev_line ( code , context : )
781
- tokens = self . class . ripper_lex_without_warning ( code , context : context )
781
+ def check_termination_in_prev_line ( code )
782
+ tokens = self . class . ripper_lex_without_warning ( code , context : @ context)
782
783
past_first_newline = false
783
784
index = tokens . rindex do |t |
784
785
# traverse first token before last line
0 commit comments