Add an error class
This commit is contained in:
		
							parent
							
								
									b5f76deb98
								
							
						
					
					
						commit
						1dfdc001b3
					
				
					 2 changed files with 24 additions and 4 deletions
				
			
		
							
								
								
									
										16
									
								
								lexer/src/error.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								lexer/src/error.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					/* lexer/src/error.rs
 | 
				
			||||||
 | 
					 * Eryn Wells <eryn@erynwells.me>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Eq, PartialEq)]
 | 
				
			||||||
 | 
					pub struct Error {
 | 
				
			||||||
 | 
					    message: String
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Error {
 | 
				
			||||||
 | 
					    pub fn new(msg: String) -> Error {
 | 
				
			||||||
 | 
					        Error {
 | 
				
			||||||
 | 
					            message: msg
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -4,6 +4,10 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use std::iter::Peekable;
 | 
					use std::iter::Peekable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					mod error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub use error::Error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Eq, PartialEq)]
 | 
					#[derive(Debug, Eq, PartialEq)]
 | 
				
			||||||
pub enum Token { LeftParen, RightParen, Id(String), }
 | 
					pub enum Token { LeftParen, RightParen, Id(String), }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,7 +19,7 @@ enum IterationResult {
 | 
				
			||||||
    Finish,
 | 
					    Finish,
 | 
				
			||||||
    Continue,
 | 
					    Continue,
 | 
				
			||||||
    Emit(Token, Resume),
 | 
					    Emit(Token, Resume),
 | 
				
			||||||
    Error(String),
 | 
					    Error(Error),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Lexer<T> where T: Iterator<Item=char> {
 | 
					pub struct Lexer<T> where T: Iterator<Item=char> {
 | 
				
			||||||
| 
						 | 
					@ -32,12 +36,12 @@ impl<T> Lexer<T> where T: Iterator<Item=char> {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn fail(&self, msg: String) -> IterationResult {
 | 
					    fn fail(&self, msg: String) -> IterationResult {
 | 
				
			||||||
        IterationResult::Error(msg)
 | 
					        IterationResult::Error(Error::new(msg))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
 | 
					impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
 | 
				
			||||||
    type Item = Result<Token, String>;
 | 
					    type Item = Result<Token, Error>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn next(&mut self) -> Option<Self::Item> {
 | 
					    fn next(&mut self) -> Option<Self::Item> {
 | 
				
			||||||
        let mut buffer = String::new();
 | 
					        let mut buffer = String::new();
 | 
				
			||||||
| 
						 | 
					@ -81,7 +85,7 @@ impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    return Some(Ok(token))
 | 
					                    return Some(Ok(token))
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                IterationResult::Error(msg) => return Some(Err(msg)),
 | 
					                IterationResult::Error(err) => return Some(Err(err)),
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        None
 | 
					        None
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue