add slightly more descriptive syntax errors

This commit is contained in:
Mark Thom
2018-08-30 00:35:42 -06:00
parent 872b153b08
commit ebd62a2c91
3 changed files with 69 additions and 24 deletions

View File

@@ -40,9 +40,9 @@ impl MachineError {
{
let mod_name = HeapCellValue::Addr(Addr::Con(Constant::Atom(mod_name)));
let name = HeapCellValue::Addr(Addr::Con(Constant::Atom(name)));
let mut stub = functor!("evaluation_error", 1, [HeapCellValue::Addr(Addr::HeapCell(h + 2))]);
stub.append(&mut functor!("/", 2, [HeapCellValue::Addr(Addr::HeapCell(h + 2 + 3)),
heap_integer!(arity)],
Fixity::In));
@@ -50,7 +50,7 @@ impl MachineError {
MachineError { stub, from: ErrorProvenance::Constructed }
}
pub(super) fn existence_error(h: usize, name: ClauseName, arity: usize) -> Self {
let mut stub = functor!("existence_error", 2, [heap_atom!("procedure"), heap_str!(3 + h)]);
stub.append(&mut Self::functor_stub(name, arity));
@@ -58,11 +58,60 @@ impl MachineError {
MachineError { stub, from: ErrorProvenance::Constructed }
}
pub(super) fn syntax_error(error: SyntaxError) -> Self {
let stub = functor!("syntax_error", 1, [heap_atom!(error.as_str())]);
MachineError { stub, from: ErrorProvenance::Received }
pub(super) fn syntax_error(h: usize, err: ParserError) -> Self {
let err = match err {
ParserError::Arithmetic(_) =>
vec![heap_atom!("arithmetic_error")],
ParserError::BackQuotedString =>
vec![heap_atom!("back_quoted_string")],
ParserError::UnexpectedChar(c) =>
functor!("unexpected_char", 1, [heap_char!(c)]),
ParserError::UnexpectedEOF =>
vec![heap_atom!("unexpected_end_of_file")],
ParserError::ExpectedRel =>
vec![heap_atom!("expected_relation")],
ParserError::InadmissibleFact =>
vec![heap_atom!("inadmissible_fact")],
ParserError::InadmissibleQueryTerm =>
vec![heap_atom!("inadmissible_query_term")],
ParserError::IncompleteReduction =>
vec![heap_atom!("incomplete_reduction")],
ParserError::InconsistentEntry =>
vec![heap_atom!("inconsistent_entry")],
ParserError::InvalidModuleDecl =>
vec![heap_atom!("invalid_module_declaration")],
ParserError::InvalidModuleExport =>
vec![heap_atom!("invalid_module_export")],
ParserError::InvalidModuleResolution =>
vec![heap_atom!("invalid_module_resolution")],
ParserError::InvalidRuleHead =>
vec![heap_atom!("invalid_head_of_rule")],
ParserError::InvalidUseModuleDecl =>
vec![heap_atom!("invalid_use_module_declaration")],
ParserError::IO(_) =>
vec![heap_atom!("input_output_error")],
ParserError::MissingQuote =>
vec![heap_atom!("missing_quote")],
ParserError::NonPrologChar =>
vec![heap_atom!("non_prolog_character")],
ParserError::ParseBigInt =>
vec![heap_atom!("cannot_parse_big_int")],
ParserError::ParseFloat =>
vec![heap_atom!("cannot_parse_float")],
ParserError::Utf8Conversion(_) =>
vec![heap_atom!("utf8_conversion_error")]
};
let mut stub = if err.len() == 1 {
functor!("syntax_error", 1)
} else {
functor!("syntax_error", 1, [heap_str!(h + 2)])
};
stub.extend(err.into_iter());
MachineError { stub, from: ErrorProvenance::Constructed }
}
pub(super) fn domain_error(error: DomainError, culprit: Addr) -> Self {
let stub = functor!("domain_error", 2, [heap_atom!(error.as_str()),
HeapCellValue::Addr(culprit)]);
@@ -140,19 +189,6 @@ impl ValidType {
}
}
#[derive(Clone, Copy)]
pub enum SyntaxError {
ImpDepAtom
}
impl SyntaxError {
pub fn as_str(&self) -> &'static str {
match self {
&SyntaxError::ImpDepAtom => "imp_dep_atom"
}
}
}
#[derive(Clone, Copy)]
pub enum DomainError {
NotLessThanZero

View File

@@ -601,10 +601,10 @@ pub(crate) trait CallPolicy: Any {
let addr = reader.machine_st[temp_v!(1)].clone();
reader.machine_st.unify(addr, Addr::HeapCell(offset));
},
Err(_) => {
// 8.14.1.3 k)-l)
Err(e) => {
let h = reader.machine_st.heap.h;
let stub = MachineError::functor_stub(clause_name!("read"), 1);
let err = MachineError::syntax_error(SyntaxError::ImpDepAtom);
let err = MachineError::syntax_error(h, e);
let err = reader.machine_st.error_form(err, stub);
return Err(err);

View File

@@ -46,6 +46,12 @@ macro_rules! heap_integer {
)
}
macro_rules! heap_char {
($c:expr) => (
HeapCellValue::Addr(Addr::Con(Constant::Char($c)))
)
}
macro_rules! heap_atom {
($name:expr) => (
HeapCellValue::Addr(Addr::Con(atom!($name)))
@@ -59,12 +65,15 @@ macro_rules! functor {
($name:expr) => (
vec![ heap_atom!($name) ]
);
($name:expr, $len:expr) => (
vec![ HeapCellValue::NamedStr($len, clause_name!($name), None) ]
);
($name:expr, $len:expr, [$($args:expr),*]) => (
vec![ HeapCellValue::NamedStr($len, clause_name!($name), None), $($args),* ]
);
($name:expr, $len:expr, [$($args:expr),*], $fix: expr) => (
vec![ HeapCellValue::NamedStr($len, clause_name!($name), Some($fix)), $($args),* ]
)
);
}
macro_rules! temp_v {