]> Repositorios git - scryer-prolog.git/commitdiff
ajust/add devlaration errors to be more standard compliant
authorBennet Bleßmann <[email protected]>
Sat, 3 Aug 2024 20:59:29 +0000 (22:59 +0200)
committerBennet Bleßmann <[email protected]>
Mon, 5 Aug 2024 19:25:43 +0000 (21:25 +0200)
src/machine/machine_errors.rs
src/machine/machine_state.rs
src/machine/preprocessor.rs
tests-pl/invalid_decl12.pl [new file with mode: 0644]
tests-pl/invalid_decl13.pl [new file with mode: 0644]
tests-pl/invalid_decl14.pl [new file with mode: 0644]
tests-pl/invalid_decl15.pl [new file with mode: 0644]
tests-pl/invalid_decl16.pl [new file with mode: 0644]
tests/scryer/cli/src_tests/declaration_errors.md

index 899424b41964058a320ec2ef1fa94d058eca6508..87c4adf39571abec1355ffd4d9d4e4a94653b08d 100644 (file)
@@ -268,6 +268,26 @@ impl DomainError for HeapCellValue {
     }
 }
 
+impl DomainError for FunctorStub {
+    fn domain_error(
+        self,
+        machine_st: &mut MachineState,
+        valid_type: DomainErrorType,
+    ) -> MachineError {
+        let stub = functor!(
+            atom!("domain_error"),
+            [atom(valid_type.as_atom()), str(machine_st.heap.len(), 0)],
+            [self]
+        );
+
+        MachineError {
+            stub,
+            location: None,
+            from: ErrorProvenance::Constructed,
+        }
+    }
+}
+
 impl DomainError for Number {
     fn domain_error(self, machine_st: &mut MachineState, error: DomainErrorType) -> MachineError {
         let stub = functor!(
@@ -731,6 +751,8 @@ pub enum DeclarationError {
     InvalidOpDeclSpecValue(Atom),
     InvalidOpDeclPrecType(Term),
     InvalidOpDeclPrecDomain(Fixnum),
+    ShallNotCreate(Atom),
+    ShallNotModify(Atom),
 }
 
 impl From<ArithmeticError> for CompilationError {
@@ -844,6 +866,7 @@ pub(crate) enum DomainErrorType {
     StreamOrAlias,
     OperatorSpecifier,
     OperatorPriority,
+    Declaration,
 }
 
 impl DomainErrorType {
@@ -857,6 +880,7 @@ impl DomainErrorType {
             DomainErrorType::StreamOrAlias => atom!("stream_or_alias"),
             DomainErrorType::OperatorSpecifier => atom!("operator_specifier"),
             DomainErrorType::OperatorPriority => atom!("operator_priority"),
+            DomainErrorType::Declaration => atom!("declaration"),
         }
     }
 }
index 4f03c8e03915f03d2654789bf23713be6bb378a1..919017acf4cdc62b56ba1ce8b7dd79144cadd052 100644 (file)
@@ -986,7 +986,7 @@ impl MachineState {
                 atom_as_cell!(atom!("todo_insert_invalid_term_here")),
             ),
             DeclarationError::InvalidDecl(name, arity) => {
-                self.existence_error(ExistenceError::Declaration(name, arity))
+                self.domain_error(DomainErrorType::Declaration, functor_stub(name, arity))
             }
             DeclarationError::InvalidOpDeclNameType(_term) => self.type_error(
                 ValidType::List,
@@ -1006,6 +1006,12 @@ impl MachineState {
             DeclarationError::InvalidOpDeclPrecDomain(num) => {
                 self.domain_error(DomainErrorType::OperatorPriority, fixnum_as_cell!(num))
             }
+            DeclarationError::ShallNotCreate(atom) => {
+                self.permission_error(Permission::Create, atom!("operator"), atom)
+            }
+            DeclarationError::ShallNotModify(atom) => {
+                self.permission_error(Permission::Modify, atom!("operator"), atom)
+            }
         }
     }
 }
index 4fcdf9b7182514867c51cc74b4cd7b519397caa6..66d6e48e27605542beed284348dabeb5dbd66160 100644 (file)
@@ -60,6 +60,24 @@ fn setup_op_decl(mut terms: Vec<Term>, atom_tbl: &AtomTable) -> Result<OpDecl, C
         }
     };
 
+    if name == "[]" || name == "{}" {
+        return Err(CompilationError::InvalidDecl(
+            DeclarationError::ShallNotCreate(name),
+        ));
+    }
+
+    if name == "," {
+        return Err(CompilationError::InvalidDecl(
+            DeclarationError::ShallNotModify(name),
+        ));
+    }
+
+    if name == "|" && (prec < 1001 || !spec.is_infix()) {
+        return Err(CompilationError::InvalidDecl(
+            DeclarationError::ShallNotCreate(name),
+        ));
+    }
+
     Ok(to_op_decl(prec, spec, name))
 }
 
diff --git a/tests-pl/invalid_decl12.pl b/tests-pl/invalid_decl12.pl
new file mode 100644 (file)
index 0000000..6b57643
--- /dev/null
@@ -0,0 +1 @@
+:- op(500, xfy, {}).
\ No newline at end of file
diff --git a/tests-pl/invalid_decl13.pl b/tests-pl/invalid_decl13.pl
new file mode 100644 (file)
index 0000000..18ce453
--- /dev/null
@@ -0,0 +1 @@
+:- op(500, xfy, [{}]).
\ No newline at end of file
diff --git a/tests-pl/invalid_decl14.pl b/tests-pl/invalid_decl14.pl
new file mode 100644 (file)
index 0000000..6e7b74e
--- /dev/null
@@ -0,0 +1 @@
+:- op(1000, xfy, '|').
\ No newline at end of file
diff --git a/tests-pl/invalid_decl15.pl b/tests-pl/invalid_decl15.pl
new file mode 100644 (file)
index 0000000..59297f2
--- /dev/null
@@ -0,0 +1 @@
+:- op(1150, fx, '|').
\ No newline at end of file
diff --git a/tests-pl/invalid_decl16.pl b/tests-pl/invalid_decl16.pl
new file mode 100644 (file)
index 0000000..3499725
--- /dev/null
@@ -0,0 +1 @@
+:- op(500, yfx, ',').
\ No newline at end of file
index caa3041220515157d140bf64a4baea31796297db..db942c83db60265b7108c780cc63b19b757e367a 100644 (file)
@@ -18,13 +18,13 @@ $ scryer-prolog -f --no-add-history tests-pl/invalid_decl3.pl -g halt
 
 ```trycmd
 $ scryer-prolog -f --no-add-history tests-pl/invalid_decl4.pl -g halt
-   error(existence_error(declaration,op/4),load/1).
+   error(domain_error(declaration,op/4),load/1).
 
 ```
 
 ```trycmd
 $ scryer-prolog -f --no-add-history tests-pl/invalid_decl5.pl -g halt
-   error(existence_error(declaration,(;)/2),load/1).
+   error(domain_error(declaration,(;)/2),load/1).
 
 ```
 
@@ -66,3 +66,33 @@ The following test doesn't appear to terminate so its moved to a block quote for
 > % Warning: singleton variables Var at line 0 of invalid_decl11.pl
 >    error(instantiation_error,load/1).
 > ```
+
+```trycmd
+$ scryer-prolog -f --no-add-history tests-pl/invalid_decl12.pl -g halt
+   error(permission_error(create,operator,{}),load/1).
+
+```
+
+```trycmd
+$ scryer-prolog -f --no-add-history tests-pl/invalid_decl13.pl -g halt
+   error(permission_error(create,operator,{}),load/1).
+
+```
+
+```trycmd
+$ scryer-prolog -f --no-add-history tests-pl/invalid_decl14.pl -g halt
+   error(permission_error(create,operator,'|'),load/1).
+
+```
+
+```trycmd
+$ scryer-prolog -f --no-add-history tests-pl/invalid_decl15.pl -g halt
+   error(permission_error(create,operator,'|'),load/1).
+
+```
+
+```trycmd
+$ scryer-prolog -f --no-add-history tests-pl/invalid_decl16.pl -g halt
+   error(permission_error(modify,operator,','),load/1).
+
+```