Skip to content

Commit

Permalink
Fix formating
Browse files Browse the repository at this point in the history
  • Loading branch information
veera-sivarajan committed May 28, 2023
1 parent b3d00de commit 19bc547
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
13 changes: 5 additions & 8 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ impl fmt::Display for Index {
#[derive(Debug)]
pub enum BessyError {
UnterminatedString(Index),
Unexpected {
msg: Box<str>,
span: Index,
},
Unexpected { msg: Box<str>, 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 {}


28 changes: 20 additions & 8 deletions core/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 }
}
}
Expand All @@ -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,
}
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
))
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
@@ -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<T>
Expand Down

0 comments on commit 19bc547

Please sign in to comment.