qvnt_qasm/
error.rs

1use std::fmt;
2
3use crate::token::TokenType;
4
5/// Represents Errors that can occur during parsing.
6///
7/// The name of each corresponds to the type of error.
8/// This enum implements the display trait, thus there is
9/// nice outputs when printing:
10///
11/// ```rust
12/// extern crate qasm;
13///
14/// println!("Got an error: {}", qasm::Error::UnsupportedVersion);
15/// // "Got an error: Unsupported Version. Please Use OpenQASM Version 2.0"
16/// ```
17#[derive(Debug, PartialEq, Clone)]
18pub enum Error<'t> {
19    MissingSemicolon,
20    UnsupportedVersion,
21    SourceError,
22    MissingReal,
23    MissingInt,
24    MissingIdentifier,
25    MissingVersion,
26    UnexpectedToken(TokenType<'t>, TokenType<'t>),
27}
28
29impl<'t> fmt::Display for Error<'t> {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        match self {
32            Error::MissingSemicolon => write!(f, "Missing Semicolon"),
33            Error::UnsupportedVersion => {
34                write!(f, "Unsupported Version. Please Use OpenQASM Version 2.0")
35            }
36            Error::SourceError => write!(f, "There Was An Error In Your Source Code"),
37            Error::MissingReal => write!(f, "Missing A Real Number"),
38            Error::MissingInt => write!(f, "Missing An Integer"),
39            Error::MissingIdentifier => write!(f, "Missing An Identifier"),
40            Error::MissingVersion => {
41                write!(f, "Missing A Version Statement At The Start Of The File")
42            }
43            Error::UnexpectedToken(expected, found) => {
44                write!(f, "Expected Token {expected:?} But Found {found:?}",)
45            }
46        }
47    }
48}