Merge IncompleteStatement into SyntaxError

Both are a form of syntax error, this reflects that better. No
functionality is lost, incomplete inputs are moved to a bool field of
SyntaxError.
This commit is contained in:
Jonas Schievink 2017-08-01 23:01:01 +02:00
parent ed0565c176
commit bf76e41487
4 changed files with 26 additions and 25 deletions

View File

@ -32,7 +32,7 @@ fn main() {
);
break;
}
Err(Error::IncompleteStatement(_)) => {
Err(Error::SyntaxError { incomplete_input: true, .. }) => {
// continue reading input and append it to `line`
write!(stdout, ">> ").unwrap();
stdout.flush().unwrap();

View File

@ -6,10 +6,16 @@ use std::result::Result as StdResult;
/// Error type returned by rlua methods.
#[derive(Debug, Clone)]
pub enum Error {
/// Lua syntax error, aka `LUA_ERRSYNTAX` that is NOT an incomplete statement.
SyntaxError(String),
/// Lua syntax error that IS an incomplete statement. Useful for implementing a REPL.
IncompleteStatement(String),
/// Syntax error while parsing Lua source code.
SyntaxError {
/// The error message as returned by Lua.
message: String,
/// `true` if the error can likely be fixed by appending more input to the source code.
///
/// This is useful for implementing REPLs as they can query the user for more input if this
/// is set.
incomplete_input: bool,
},
/// Lua runtime error, aka `LUA_ERRRUN`.
///
/// The Lua VM returns this error when a builtin operation is performed on incompatible types.
@ -105,10 +111,7 @@ pub type Result<T> = StdResult<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::SyntaxError(ref msg) => write!(fmt, "Lua syntax error: {}", msg),
Error::IncompleteStatement(ref msg) => {
write!(fmt, "Lua syntax error (incomplete statement): {}", msg)
}
Error::SyntaxError { ref message, .. } => write!(fmt, "syntax error: {}", message),
Error::RuntimeError(ref msg) => write!(fmt, "Lua runtime error: {}", msg),
Error::ErrorError(ref msg) => write!(fmt, "Lua error in error handler: {}", msg),
Error::ToLuaConversionError { from, to, ref message } => {
@ -141,8 +144,7 @@ impl fmt::Display for Error {
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::SyntaxError(_) => "lua syntax error",
Error::IncompleteStatement(_) => "lua incomplete statement",
Error::SyntaxError { .. } => "syntax error",
Error::RuntimeError(_) => "lua runtime error",
Error::ErrorError(_) => "lua error handling error",
Error::ToLuaConversionError { .. } => "conversion error to lua",

View File

@ -69,8 +69,8 @@ fn test_eval() {
assert_eq!(lua.eval::<bool>("false == false", None).unwrap(), true);
assert_eq!(lua.eval::<i32>("return 1 + 2", None).unwrap(), 3);
match lua.eval::<()>("if true then", None) {
Err(Error::IncompleteStatement(_)) => {}
r => panic!("expected IncompleteStatement, got {:?}", r),
Err(Error::SyntaxError { incomplete_input: true, .. }) => {}
r => panic!("expected SyntaxError with incomplete_input=true, got {:?}", r),
}
}
@ -514,12 +514,12 @@ fn test_error() {
assert!(return_string_error.call::<_, Error>(()).is_ok());
match lua.eval::<()>("if youre happy and you know it syntax error", None) {
Err(Error::SyntaxError(_)) => {}
Err(Error::SyntaxError { incomplete_input: false, .. }) => {}
Err(_) => panic!("error is not LuaSyntaxError::Syntax kind"),
_ => panic!("error not returned"),
}
match lua.eval::<()>("function i_will_finish_what_i()", None) {
Err(Error::IncompleteStatement(_)) => {}
Err(Error::SyntaxError { incomplete_input: true, .. }) => {}
Err(_) => panic!("error is not LuaSyntaxError::IncompleteStatement kind"),
_ => panic!("error not returned"),
}

View File

@ -245,10 +245,10 @@ pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result<c_int> {
}
// If the return code indicates an error, pops the error off of the stack and
// returns Err. If the error is actually a WrappedPaic, clears the current lua
// stack continues the panic. If the error on the top of the stack is actually
// a WrappedError, just returns it. Otherwise, interprets the error as the
// appropriate lua error.
// returns Err. If the error is actually a WrappedPanic, clears the current lua
// stack and continues the panic. If the error on the top of the stack is
// actually a WrappedError, just returns it. Otherwise, interprets the error as
// the appropriate lua error.
pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()> {
if err == ffi::LUA_OK || err == ffi::LUA_YIELD {
Ok(())
@ -280,12 +280,11 @@ pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()>
Err(match err {
ffi::LUA_ERRRUN => Error::RuntimeError(err_string),
ffi::LUA_ERRSYNTAX => {
// This seems terrible, but as far as I can tell, this is exactly what the stock
// lua repl does.
if err_string.ends_with("<eof>") {
Error::IncompleteStatement(err_string)
} else {
Error::SyntaxError(err_string)
Error::SyntaxError {
// This seems terrible, but as far as I can tell, this is exactly what the
// stock Lua REPL does.
incomplete_input: err_string.ends_with("<eof>"),
message: err_string,
}
}
ffi::LUA_ERRERR => Error::ErrorError(err_string),