From 9a4df56017ba6d8d1520aa7bc4ea2d925931e80f Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 15 Sep 2019 11:31:29 +0100 Subject: [PATCH] Fix comments in REPL - fixes #78 Before this change, entering a comment in the REPL caused the REPL to read the comment indefinitely effectively breaking it. After this change the behaviour is the same as pypy. The cpython behaviour is slightly different printing a '...' after a comment line. This is rather illogical and difficult to emulate properly. --- repl/repl.go | 10 +++++++--- repl/repl_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/repl/repl.go b/repl/repl.go index a9e50862..d7fd9600 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -82,9 +82,13 @@ func (r *REPL) Run(line string) { // FIXME detect EOF properly! errText := err.Error() if strings.Contains(errText, "unexpected EOF while parsing") || strings.Contains(errText, "EOF while scanning triple-quoted string literal") { - r.continuation = true - r.previous += string(line) + "\n" - r.term.SetPrompt(ContinuationPrompt) + stripped := strings.TrimSpace(toCompile) + isComment := len(stripped) > 0 && stripped[0] == '#' + if !isComment { + r.continuation = true + r.previous += string(line) + "\n" + r.term.SetPrompt(ContinuationPrompt) + } return } } diff --git a/repl/repl_test.go b/repl/repl_test.go index e5324519..a98ce9a9 100644 --- a/repl/repl_test.go +++ b/repl/repl_test.go @@ -67,6 +67,14 @@ func TestREPL(t *testing.T) { r.Run("if") rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'") + + // test comments in the REPL work properly + r.Run("# this is a comment") + rt.assert(t, "comment", NormalPrompt, "") + r.Run("a = 42") + rt.assert(t, "comment continuation", NormalPrompt, "") + r.Run("a") + rt.assert(t, "comment check", NormalPrompt, "42") } func TestCompleter(t *testing.T) {