crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.37k stars 1.62k forks source link

Crystal REPL instantly executes statement with intertwined heredoc #14597

Open BlobCodes opened 4 months ago

BlobCodes commented 4 months ago

Bug Report

The interpreter instantly executes statements with intertwined heredocs, even though it hasn't been terminated yet. The heredoc is simply never read.

Example:

icr:1> p <<-EOF.to_i
Unhandled exception: Invalid Int32: "" (ArgumentError)

$ bin/crystal --version
Using compiled compiler at .build/crystal
Crystal 1.13.0-dev [a23f0fbee] (2024-05-07)

LLVM: 18.1.1
Default target: x86_64-redhat-linux-gnu
I3oris commented 4 months ago

The workaround is to press alt-enter to force continue on a new line.

This error happen because the reader doesn't know if it should wait for a new line or not before sending to the interpreter.

The current method used is to parse it anyway and if it raises an unterminated error, it lets the user types a new line. See code here

However in this case the parser raise 'invalid int32' which tell no information about if the expression is complete or not.

The solution is either to raises an unterminated heredoc error in this case, either we could add a flag on the parser that said if the incorect parsed expression is just 'unterminated' and could be continued on a new line. (This mean to correctly set the flag when the parser raises an error)