#![ allow( clippy ::std_instead_of_alloc ) ]
#![ allow( clippy ::std_instead_of_core ) ]
use core ::fmt;
use alloc ::string ::String;
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub struct StrSpan
{
pub start: usize,
pub end: usize,
}
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub enum SourceLocation
{
StrSpan
{
start: usize,
end: usize,
},
None,
}
impl SourceLocation
{
#[ must_use ]
pub fn start( &self ) -> usize
{
match self
{
SourceLocation ::StrSpan { start, .. } => *start,
SourceLocation ::None => 0,
}
}
#[ must_use ]
pub fn end( &self ) -> usize
{
match self
{
SourceLocation ::StrSpan { end, .. } => *end,
SourceLocation ::None => 0,
}
}
}
impl fmt ::Display for SourceLocation
{
fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
{
match self
{
SourceLocation ::StrSpan { start, end } => write!( f, "StrSpan {{ start: {start}, end: {end} }}" ),
SourceLocation ::None => write!( f, "None" ),
}
}
}
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub enum ErrorKind
{
Syntax( String ),
InvalidEscapeSequence( String ),
EmptyInstructionSegment,
TrailingDelimiter,
Unknown,
}
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub struct ParseError
{
pub kind: ErrorKind,
pub location: Option< SourceLocation >,
}
impl ParseError
{
#[ must_use ]
pub fn new( kind: ErrorKind, location: SourceLocation ) -> Self
{
Self
{
kind,
location: Some( location ),
}
}
}
impl fmt ::Display for ParseError
{
fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
{
match &self.kind
{
ErrorKind ::InvalidEscapeSequence( s ) => write!( f, "Invalid escape sequence: {s}" )?,
ErrorKind ::EmptyInstructionSegment => write!( f, "Empty instruction segment" )?,
ErrorKind ::TrailingDelimiter => write!( f, "Trailing delimiter" )?,
_ => write!( f, "{:?}", self.kind )?,
}
if let Some( location ) = &self.location
{
write!( f, " at {location}" )?;
}
Ok( () )
}
}
impl core ::error ::Error for ParseError {}