From cdd98135eeef4a040310abae1fce6ff5f995ceb6 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Fri, 15 May 2020 17:23:08 +0900 Subject: [PATCH] EOF after `\\` raise EOF Error Return EOF Error to get the next line after` \\ `in the shell. Closes #1928 --- parser/src/error.rs | 2 ++ parser/src/lexer.rs | 7 +++++++ src/shell.rs | 6 +++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/parser/src/error.rs b/parser/src/error.rs index ebe683c8c2..f5935eb3a7 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -28,6 +28,7 @@ pub enum LexicalErrorType { UnrecognizedToken { tok: char }, FStringError(FStringErrorType), LineContinuationError, + EOF, OtherError(String), } @@ -59,6 +60,7 @@ impl fmt::Display for LexicalErrorType { LexicalErrorType::LineContinuationError => { write!(f, "unexpected character after line continuation character") } + LexicalErrorType::EOF => write!(f, "unexpected EOF while parsing"), LexicalErrorType::OtherError(msg) => write!(f, "{}", msg), } } diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index eda96ea741..8a1254fac4 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -1191,6 +1191,13 @@ where location: self.get_pos(), }); } + + if self.chr0.is_none() { + return Err(LexicalError { + error: LexicalErrorType::EOF, + location: self.get_pos(), + }); + } } _ => { diff --git a/src/shell.rs b/src/shell.rs index 4a6c095e61..2785c8ae11 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,7 +1,7 @@ mod helper; use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType}; -use rustpython_parser::error::ParseErrorType; +use rustpython_parser::error::{LexicalErrorType, ParseErrorType}; use rustpython_vm::readline::{Readline, ReadlineResult}; use rustpython_vm::{ exceptions::{print_exception, PyBaseExceptionRef}, @@ -23,6 +23,10 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul Ok(_val) => ShellExecResult::Ok, Err(err) => ShellExecResult::PyErr(err), }, + Err(CompileError { + error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::EOF)), + .. + }) => ShellExecResult::Continue, Err(CompileError { error: CompileErrorType::Parse(ParseErrorType::EOF), ..