Skip to content

Commit

Permalink
Fixed error with unterminated char literal (#50)
Browse files Browse the repository at this point in the history
* fixed unterminated char literal not throwing an error. Also fixed spec failure possibly related to latest crystal.

* actually, backwards compatibility on that spec just in case

* using a more descriptive error catch for char literals

* a little reorganization of errors. Unterminated char literal catch shows full error from compiler
  • Loading branch information
jwoertink authored and veelenga committed Oct 9, 2017
1 parent 7d33305 commit cba9fe8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 9 additions & 1 deletion spec/integration/icr_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe "icr command" do
it "prints runtime error without crashing" do
input = "\"5a\".to_i"
output = icr(input)
output.should match /Invalid Int32: 5a \(ArgumentError\)/
output.should match /invalid Int32: 5a \(ArgumentError\)/i
end
end

Expand Down Expand Up @@ -238,6 +238,13 @@ describe "icr command" do
icr(input).should match /45/
end

it "fails for unterminated char literal" do
input = <<-CRYSTAL
puts 'aa'
CRYSTAL
icr(input).should match /\schar\s/
end

describe "using constants" do
it "allows for constant assignment" do
input = <<-CRYSTAL
Expand Down Expand Up @@ -292,6 +299,7 @@ describe "icr command" do
icr(input).should match /dynamic\sconstant\sassignment/
end
end

it "allows for macros" do
input = <<-CRYSTAL
macro a_macro
Expand Down
15 changes: 12 additions & 3 deletions src/icr/console.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ module Icr
when :unexpected_eof, :unterminated_literal
# If syntax is invalid because of unexpected EOF, or
# we are still waiting for a closing bracket, keep asking for input
next_command_part = ask_for_input(1)
new_command = "#{command}\n#{next_command_part}"
process_command(new_command)
continue_processing(command)
when :error
# Give it the second try, validate the command in scope of entire file
@command_stack.push(command)
Expand All @@ -98,6 +96,13 @@ module Icr
end
end

# If the command has been processed, but not complete
private def continue_processing(command)
next_command_part = ask_for_input(1)
new_command = "#{command}\n#{next_command_part}"
process_command(new_command)
end

private def execute
result = @executer.execute
if result.success?
Expand Down Expand Up @@ -148,7 +153,11 @@ module Icr
case err.message.to_s
when .includes?("EOF")
SyntaxCheckResult.new(:unexpected_eof)
when .includes?("unterminated char literal")
# catches error for 'aa' and returns compiler error
SyntaxCheckResult.new(:ok)
when .includes?("unterminated")
# catches unterminated hashes and arrays
SyntaxCheckResult.new(:unterminated_literal)
else
SyntaxCheckResult.new(:error, err.message.to_s)
Expand Down

0 comments on commit cba9fe8

Please sign in to comment.