From fcbd2392c7173d9b7a69ea8eca4d52d4ddda4390 Mon Sep 17 00:00:00 2001 From: Karol Belina Date: Sun, 13 Jun 2021 19:06:00 +0200 Subject: [PATCH] Document the rest of the errors and warnings --- src/anomalies.rs | 98 +++++++++++++++++++++++++++++ src/bin/ruxnasm/reporter/display.rs | 4 +- src/lib.rs | 8 +++ src/walker.rs | 2 +- 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/anomalies.rs b/src/anomalies.rs index d519520..208fd63 100644 --- a/src/anomalies.rs +++ b/src/anomalies.rs @@ -32,12 +32,30 @@ pub enum Warning { /// Span of the instruction mode character defined for the first time. other_span: Range, }, + /// This warning gets reported when a macro is never used. + /// + /// # Example + /// + /// ```uxntal + /// %macro { #0001 } + /// ``` MacroUnused { + /// Name of the unused macro. name: String, + /// Span of the macro definition. span: Range, }, + /// This warning gets reported when a label is never used. + /// + /// # Example + /// + /// ```uxntal + /// @label + /// ``` LabelUnused { + /// Name of the unused label. name: String, + /// Span of the label definition. span: Range, }, } @@ -402,42 +420,122 @@ pub enum Error { /// Span of the opening bracket with no matching closing bracket. span: Range, }, + /// This error wraps an error that has been reported from a macro definition. + /// + /// # Example + /// + /// ```uxntal + /// %macro { #001 } + /// macro + /// ``` MacroError { + /// The error that has been reported from a macro definition. original_error: Box, + /// Span of the macro invocation. span: Range, }, + /// This error gets reported during an attempt to reference a sublabel, when + /// no previous label has been defined. + /// + /// # Example + /// + /// ```uxntal + /// .&sublabel + /// ``` SublabelReferencedWithoutScope { /// Name of the sublabel. name: String, /// Span of the sublabel reference. span: Range, }, + /// This error gets reported during an attempt to reference a label that + /// has not been defined. + /// + /// # Example + /// + /// ```uxntal + /// .label + /// ``` LabelUndefined { /// Name of the label. name: String, /// Span of the label reference. span: Range, }, + /// This error gets reported during an attempt to reference a non-zero-page label + /// after a literal zero-page address rune. + /// + /// # Example + /// + /// ```uxntal + /// |0100 @label + /// .label + /// ``` AddressNotZeroPage { + /// The actuall address that is not zero-page. address: u16, + /// Name of the identifier that is referenced by the literal zero-page address. identifier: String, + /// Span of the literal zero-page address. span: Range, }, + /// This error gets reported during an attempt to reference a label that + /// is too far to be a relative address after a literal relative address rune. + /// + /// # Example + /// + /// ```uxntal + /// @label + /// |0100 ,label + /// ``` AddressTooFar { + /// The distance in bytes from the literal relative address and the label definition. distance: usize, + /// Name of the identifier that is referenced by the literal relative address. identifier: String, + /// Span of the literal relative address. span: Range, + /// Span of the label definition that is referenced by the literal relative address. other_span: Range, }, + /// This error gets reported when there are bytes in the zeroth page (first + /// 256 bytes) of the binary. + /// + /// # Example + /// + /// ```uxntal + /// #01 #02 ADD + /// ``` BytesInZerothPage { + /// Span of the tokens in the zeroth page. span: Range, }, + /// This error gets reported during an attempt to do an absolute pad + /// to an address before the current address pointer. + /// + /// # Example + /// + /// ```uxntal + /// #01 #02 ADD + /// |0000 #02 #03 ADD + /// ``` PaddedBackwards { + /// The address at which the absolute pad is attempted. previous_pointer: usize, + /// The address to which the absolute pad is attempted. desired_pointer: usize, + /// Span of the absolute pad. span: Range, }, + /// This error gets reported when the program size exceeds 65536 bytes. + /// + /// # Example + /// + /// ```uxntal + /// |ffff #01 #02 ADD + /// ``` ProgramTooLong { + /// Span of the tokens that exceed the maximum size. span: Range, }, } diff --git a/src/bin/ruxnasm/reporter/display.rs b/src/bin/ruxnasm/reporter/display.rs index 3f85dfe..aeb009f 100644 --- a/src/bin/ruxnasm/reporter/display.rs +++ b/src/bin/ruxnasm/reporter/display.rs @@ -339,7 +339,7 @@ impl From for FileDiagnostic { span, } => FileDiagnostic::error() .with_message(format!( - "address {:#06x} of label {} is not zero-page", + "address {:#06x} of label `{}` is not zero-page", address, identifier )) .with_label(Label { @@ -354,7 +354,7 @@ impl From for FileDiagnostic { other_span, } => FileDiagnostic::error() .with_message(format!( - "address of label {} is too far to be a relative address (distance {})", + "address of label `{}` is too far to be a relative address (distance {})", identifier, distance )) .with_label(Label { diff --git a/src/lib.rs b/src/lib.rs index 1ed5ab8..584ce44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,14 @@ pub(crate) use token::{Identifier, Token}; /// `Err((Vec, Vec))`, which contains all [`Error`]s in the program, along with /// any [`Warning`]s that may have also been generated. The `Vec` containing the errors is always /// non-empty. +/// +/// # Example +/// +/// ```rust +/// let (binary, _) = ruxnasm::assemble("|0100 #02 #03 ADD").unwrap(); +/// +/// assert_eq!(binary, [0x01, 0x02, 0x01, 0x03, 0x18]); +/// ``` pub fn assemble( source: impl AsRef, ) -> Result<(Vec, Vec), (Vec, Vec)> { diff --git a/src/walker.rs b/src/walker.rs index e94efa0..906bdb3 100644 --- a/src/walker.rs +++ b/src/walker.rs @@ -233,7 +233,7 @@ fn walk_rec( previous_pointer: previous_address as usize, desired_pointer: value as usize, span: span.into(), - }) + }), } } Spanned {