Path: blob/main/cranelift/reader/src/error.rs
2450 views
//! Define the `Location`, `ParseError`, and `ParseResult` types.12#![macro_use]34use std::fmt;56/// The location of a `Token` or `Error`.7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]8pub struct Location {9/// Line number. Command-line arguments are line 0 and source file10/// lines start from 1.11pub line_number: usize,12}1314/// A parse error is returned when the parse failed.15#[derive(Debug)]16pub struct ParseError {17/// Location of the error.18pub location: Location,19/// Error message.20pub message: String,21/// Whether it's a warning or a plain error.22pub is_warning: bool,23}2425impl fmt::Display for ParseError {26fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {27if self.location.line_number == 0 {28write!(f, "command-line arguments: {}", self.message)29} else {30write!(f, "{}: {}", self.location.line_number, self.message)31}32}33}3435impl std::error::Error for ParseError {}3637/// Result of a parser operation. The `ParseError` variant includes a location.38pub type ParseResult<T> = Result<T, ParseError>;3940// Create an `Err` variant of `ParseResult<X>` from a location and `format!` args.41macro_rules! err {42( $loc:expr, $msg:expr ) => {43Err($crate::ParseError {44location: $loc.clone(),45message: $msg.to_string(),46is_warning: false,47})48};4950( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {51Err($crate::ParseError {52location: $loc.clone(),53message: format!( $fmt, $( $arg ),+ ),54is_warning: false,55})56};57}5859macro_rules! warn {60( $loc:expr, $fmt:expr, $( $arg:expr ),+ ) => {61Err($crate::ParseError {62location: $loc.clone(),63message: format!($fmt, $( $arg ),+ ),64is_warning: true,65})66};67}686970