Skip to content

Commit 64a1d5a

Browse files
committed
Repl mode - fixed occasional bug in handling multiline input.
1 parent 7d3dc4f commit 64a1d5a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/Repl.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,14 @@ private function gatherLines(): string {
168168
}
169169

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

172-
if (self::isIncompleteInput($input)) {
173+
if ($incomplete) {
173174

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

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

196197
if (empty(trim($input))) {
197-
return false;
198+
return [false, 0];
198199
}
199200

200201
// Lines ending with opening curly brackets are considered incomplete.
201-
if ($input[-1] === "{" || $input[-1] === '\\') {
202-
return true;
202+
if ($input[-1] === "{") {
203+
return [true, 0];
204+
}
205+
206+
// Lines ending with backslashes are considered incomplete.
207+
// And such backslashes at the EOL are to be trimmed from real input.
208+
if ($input[-1] === '\\') {
209+
return [true, 1];
203210
}
204211

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

210217
}

0 commit comments

Comments
 (0)