Permalink
Browse files

Use ra66i's technique of just rescuing from SyntaxError to detect inc…

…omplete expressions
  • Loading branch information...
1 parent 2e7c308 commit 6bd06b143b25f03546deba5f8d782c55ee5718f0 Yehuda Katz committed Dec 5, 2009
Showing with 53 additions and 1,705 deletions.
  1. +24 −13 irb.rb
  2. +0 −17 irb/context.rb
  3. +3 −4 irb/ext/multi-irb.rb
  4. +26 −0 irb/init.rb
  5. +0 −1,122 irb/ruby-lex.rb
  6. +0 −259 irb/ruby-token.rb
  7. +0 −290 irb/slex.rb
View
37 irb.rb
@@ -101,9 +101,6 @@ def initialize(workspace = nil, input_method = nil, output_method = nil)
@context = Context.new(self, workspace, input_method, output_method)
@context.main.extend ExtendCommandBundle
@signal_status = :IN_IRB
-
- @scanner = RubyLex.new
- @scanner.exception_on_syntax_error = false
@stdout = STDOUT
end
@@ -125,36 +122,50 @@ def handle_exception(exc)
IRB.puts "Maybe IRB bug!" if irb_bug
end
+ def each_top_level_statement(&block)
+ @lines << context.io.gets
+ @line_number += 1
+ yield @lines, @line_number
+ each_top_level_statement(&block)
+ end
+
def eval_input
- context.set_prompt(scanner)
- set_input
+ @lines = ""
+ @line_number = 0
+
+ # TODO: Use the new prompt object
+ context.io.prompt = ">> "
+
+ each_top_level_statement do |line, line_no|
+ context.io.prompt = ">> "
+
+ # TODO: Lex to see if this is *ever* valid
- @scanner.each_top_level_statement do |line, line_no|
with_signal_status(:IN_EVAL) do
begin
@context.evaluate(line, line_no)
+ rescue SyntaxError
rescue Interrupt => e
+ @lines = ""
handle_exception(e)
rescue SystemExit, SignalException
+ @lines = ""
raise
rescue Exception => e
+ @lines = ""
handle_exception(e)
+ else
+ @lines = ""
end
end
end
end
- def set_input
- scanner.set_input do
- with_signal_status(:IN_INPUT) { context.io.gets || "\n" }
- end
- end
-
def signal_handle
case @signal_status
when :IN_INPUT
IRB.puts "^C"
- raise RubyLex::TerminateLineInput
+ raise StandardError
when :IN_EVAL
IRB.irb_abort(self)
when :IN_LOAD
View
@@ -171,8 +171,6 @@ def use_readline=(opt)
def debug_level=(value)
@debug_level = value
- RubyLex.debug_level = value
- SLex.debug_level = value
end
def debug?
@@ -192,21 +190,6 @@ def evaluate(line, line_no)
PROMPTS = {:ltype => :prompt_s, :continue => :prompt_c, :indent => :prompt_n, :normal => :prompt_i}
- def set_prompt(scanner)
- scanner.set_prompt do |state, ltype, indent, line_no|
- format = send(PROMPTS[state]) || ""
-
- prompt = prompting? ? format_prompt(format, ltype, indent, line_no) : ""
-
- if auto_indent_mode && !ltype
- indent += 1 if state == :continue
- prompt += " " * indent * 2
- end
-
- io.prompt = prompt
- end
- end
-
def print_verbose(str)
IRB.puts str if verbose?
end
View
@@ -138,11 +138,10 @@ def inspect
"red"
end
- locals = irb.context.workspace.evaluate(nil, "local_variables") - ["_"]
+ locals = irb.context.workspace.evaluate(nil, "local_variables") - ["_", :_]
locals = locals.empty? ? "none" : locals.join(", ")
- str = format("[blue]#%d[/] [#{status}]%s[/] | locals: %s",
- i, irb.context.main, locals)
+ str = "[blue]##{i}[/] [#{status}]#{irb.context.main}[/] | locals: #{locals}"
ary.push str
end
@@ -212,7 +211,7 @@ def signal_handle
case @signal_status
when :IN_INPUT
print "^C\n"
- IRB.job_manager.thread(self).raise RubyLex::TerminateLineInput
+ IRB.job_manager.thread(self).raise StandardError
when :IN_EVAL
IRB.irb_abort(self)
when :IN_LOAD
View
@@ -71,6 +71,32 @@ def show(result)
end
end
+ class AbstractPrompt
+ def prompt(state, line, relative_line)
+ raise NotImplementedError
+ end
+ end
+
+ class NullPrompt
+ def prompt(state, line, relative_line)
+ ""
+ end
+ end
+
+ class DefaultPrompt
+ def prompt(state, line, relative_line)
+ prompt = "#{irb_name}(#{main}):#{line_number.rjust(3, "0")}:#{relative_line}"
+ prompt << state == :finished ? "> " : "* "
+ prompt
+ end
+ end
+
+ class SimplePrompt
+ def prompt(state, line, relative_line)
+ state == :finished ? ">> " : "?> "
+ end
+ end
+
# conf[ default setting
def IRB.init_config(ap_path)
# class instance variables
Oops, something went wrong.

0 comments on commit 6bd06b1

Please sign in to comment.