From 5e81fb4754d19ebe4af284eec4f59abaf22f3bf3 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 12 Apr 2020 12:07:41 -0600 Subject: [PATCH] throw exception when attempting to post query as goal (#329) --- src/prolog/machine/machine_errors.rs | 26 +++++++++++++++++++++----- src/prolog/machine/toplevel.rs | 2 +- src/prolog/write.rs | 11 +++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/prolog/machine/machine_errors.rs b/src/prolog/machine/machine_errors.rs index 1f3dc180..1e09b2bb 100644 --- a/src/prolog/machine/machine_errors.rs +++ b/src/prolog/machine/machine_errors.rs @@ -301,9 +301,6 @@ impl MachineError { pub(super) fn session_error(h: usize, err: SessionError) -> Self { match err { - SessionError::ParserError(err) => { - Self::syntax_error(h, err) - } SessionError::CannotOverwriteBuiltIn(pred_str) | SessionError::CannotOverwriteImport(pred_str) => { Self::permission_error( @@ -329,7 +326,15 @@ impl MachineError { h, Permission::Access, "private_procedure", - functor!("modules_does_not_exist"), + functor!("module_does_not_exist"), + ) + } + SessionError::NamelessEntry => { + Self::permission_error( + h, + Permission::Create, + "static_procedure", + functor!("nameless_procedure") ) } SessionError::OpIsInfixAndPostFix(op) => { @@ -340,7 +345,17 @@ impl MachineError { functor!(clause_name(op)), ) } - _ => unreachable!(), + SessionError::ParserError(err) => { + Self::syntax_error(h, err) + } + SessionError::QueryCannotBePostedAsGoal => { + Self::permission_error( + h, + Permission::Create, + "static_procedure", + functor!("query_cannot_be_posted_as_goal") + ) + } } } @@ -681,6 +696,7 @@ pub enum SessionError { ModuleNotFound, NamelessEntry, OpIsInfixAndPostFix(ClauseName), + QueryCannotBePostedAsGoal, ParserError(ParserError), } diff --git a/src/prolog/machine/toplevel.rs b/src/prolog/machine/toplevel.rs index 40ccac3f..717f173e 100644 --- a/src/prolog/machine/toplevel.rs +++ b/src/prolog/machine/toplevel.rs @@ -1290,7 +1290,7 @@ impl<'a> TopLevelBatchWorker<'a> { TopLevel::Declaration(decl) => return Ok(Some(decl)), TopLevel::Query(_) => - return Err(SessionError::NamelessEntry), + return Err(SessionError::QueryCannotBePostedAsGoal), } } diff --git a/src/prolog/write.rs b/src/prolog/write.rs index fdb5b909..d83b848d 100644 --- a/src/prolog/write.rs +++ b/src/prolog/write.rs @@ -289,7 +289,9 @@ impl fmt::Display for IndexingInstruction { impl fmt::Display for SessionError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - &SessionError::CannotOverwriteBuiltIn(ref msg) => write!(f, "cannot overwrite {}", msg), + &SessionError::CannotOverwriteBuiltIn(ref msg) => { + write!(f, "cannot overwrite {}", msg) + } &SessionError::CannotOverwriteImport(ref msg) => { write!(f, "cannot overwrite import {}", msg) } @@ -312,7 +314,12 @@ impl fmt::Display for SessionError { &SessionError::NamelessEntry => { write!(f, "the predicate head is not an atom or clause.") } - &SessionError::ParserError(ref e) => write!(f, "syntax_error({})", e.as_str()), + &SessionError::ParserError(ref e) => { + write!(f, "syntax_error({})", e.as_str()) + } + &SessionError::QueryCannotBePostedAsGoal => { + write!(f, "query forms cannot be posted as goals.") + } } } } -- 2.54.0