Skip to content

Commit

Permalink
Repl mode - fixed occasional bug in handling multiline input.
Browse files Browse the repository at this point in the history
  • Loading branch information
smuuf committed Jul 10, 2019
1 parent 7d3dc4f commit 64a1d5a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Repl.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ private function gatherLines(): string {
}

$input = $this->driver->readline($prompt);
[$incomplete, $trim] = self::isIncompleteInput($input);

if (self::isIncompleteInput($input)) {
if ($incomplete) {

// Consider non-empty line ending with a "\" character as
// a part of multiline input. That is: Trim the backslash and
// go read another line from the user.
$lines .= mb_substr($input, 0, mb_strlen($input) - 1) . "\n";
$lines .= mb_substr($input, 0, mb_strlen($input) - $trim) . "\n";
$gathering = true;

} else {
Expand All @@ -194,17 +195,23 @@ private function gatherLines(): string {
private function isIncompleteInput(string $input) {

if (empty(trim($input))) {
return false;
return [false, 0];
}

// Lines ending with opening curly brackets are considered incomplete.
if ($input[-1] === "{" || $input[-1] === '\\') {
return true;
if ($input[-1] === "{") {
return [true, 0];
}

// Lines ending with backslashes are considered incomplete.
// And such backslashes at the EOL are to be trimmed from real input.
if ($input[-1] === '\\') {
return [true, 1];
}

// Lines starting with a SPACE or a TAB are considered incomplete.
if (strspn($input, "\t ") !== 0) {
return true;
return [true, 0];
}

}
Expand Down

0 comments on commit 64a1d5a

Please sign in to comment.