From 19bc547ebd5a2b6f56c6172f039c931d0e3b935c Mon Sep 17 00:00:00 2001 From: Veera Date: Sun, 28 May 2023 00:41:31 -0400 Subject: [PATCH] Fix formating --- core/src/error.rs | 13 +++++-------- core/src/lexer.rs | 28 ++++++++++++++++++++-------- core/src/lib.rs | 2 +- core/src/parser.rs | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/core/src/error.rs b/core/src/error.rs index 4c22d07..85e039c 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -24,22 +24,19 @@ impl fmt::Display for Index { #[derive(Debug)] pub enum BessyError { UnterminatedString(Index), - Unexpected { - msg: Box, - span: Index, - }, + Unexpected { msg: Box, span: Index }, } impl fmt::Display for BessyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use BessyError::*; match self { - UnterminatedString(span) => write!(f, "Lex error: Unterminated String Literal at {span}."), - Unexpected{msg, span} => write!(f, "Parse error: {msg} at {span}."), + UnterminatedString(span) => { + write!(f, "Lex error: Unterminated String Literal at {span}.") + } + Unexpected { msg, span } => write!(f, "Parse error: {msg} at {span}."), } } } impl std::error::Error for BessyError {} - - diff --git a/core/src/lexer.rs b/core/src/lexer.rs index dbe3519..2352e14 100644 --- a/core/src/lexer.rs +++ b/core/src/lexer.rs @@ -1,6 +1,6 @@ +use crate::error::BessyError; use std::iter::Peekable; use std::str::CharIndices; -use crate::error::BessyError; #[derive(Clone, Debug, PartialEq)] pub enum TokenType { @@ -52,8 +52,8 @@ pub struct Span { impl From<(usize, usize)> for Span { fn from(start_len: (usize, usize)) -> Span { - let start= start_len.0 as u16; - let end= start + start_len.1 as u16; + let start = start_len.0 as u16; + let end = start + start_len.1 as u16; Span { start, end } } } @@ -67,7 +67,11 @@ pub struct Token { impl Token { pub fn new(span: Span, token_type: TokenType, line: u16) -> Self { - Self { span, token_type, line } + Self { + span, + token_type, + line, + } } } @@ -137,8 +141,11 @@ impl<'src> Lexer<'src> { '/' => TokenType::Slash, _ => unreachable!(), }; - self.tokens - .push(Token::new((start_pos, c.len_utf8()).into(), kind, self.line)); + self.tokens.push(Token::new( + (start_pos, c.len_utf8()).into(), + kind, + self.line, + )); } fn scan_comment(&mut self) { @@ -157,7 +164,10 @@ impl<'src> Lexer<'src> { that: TokenType, ) -> Token { if let Some((end_pos, _)) = self.cursor.next_if(|x| x.1 == '=') { - let span = Span { start: start_pos as u16, end: end_pos as u16 + 1 }; + let span = Span { + start: start_pos as u16, + end: end_pos as u16 + 1, + }; Token::new(span, this, self.line) } else { Token::new((start_pos, len).into(), that, self.line) @@ -211,7 +221,9 @@ impl<'src> Lexer<'src> { self.line, )) } else { - Err(BessyError::UnterminatedString((start_pos as u16, self.line).into())) + Err(BessyError::UnterminatedString( + (start_pos as u16, self.line).into(), + )) } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 78b0328..889d64b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,8 +1,8 @@ +mod error; mod expr; mod lexer; mod parser; mod stmt; -mod error; pub fn evaluate(text: &str) { let mut lex = lexer::Lexer::new(text); diff --git a/core/src/parser.rs b/core/src/parser.rs index 53f185b..56a4065 100644 --- a/core/src/parser.rs +++ b/core/src/parser.rs @@ -1,10 +1,10 @@ // a parser takens in a stream of tokens and turns them into a // intermediate representation in the form of an abstract syntax tree +use crate::error::BessyError; use crate::expr::Expr; use crate::lexer::Token; use crate::stmt::Stmt; -use crate::error::BessyError; use std::iter::Peekable; pub struct Parser