Skip to content

Commit

Permalink
update(deps): update logos to 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadiSave committed Dec 13, 2024
1 parent fd6493a commit fe9a35a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extended = []
compile = ["serde"]

[dependencies]
logos = "0.12"
logos = "0.15"
log = "0.4"
thiserror = "1"

Expand Down
67 changes: 29 additions & 38 deletions lib/src/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use logos::{Lexer, Logos};
use std::{collections::HashMap, fmt::Debug, num::ParseIntError, ops::Range};
use thiserror::Error;

fn parse_num(lex: &mut Lexer<Token>) -> Option<usize> {
fn parse_num(lex: &mut Lexer<Token>) -> Result<usize, ErrorKind> {
let src = if lex.slice().as_bytes()[0] == b'#' {
&lex.slice()[1..]
} else {
Expand All @@ -20,13 +20,9 @@ fn parse_num(lex: &mut Lexer<Token>) -> Option<usize> {
b'x' | b'X' | b'&' => usize::from_str_radix(&src[1..], 16),
b'o' | b'O' => usize::from_str_radix(&src[1..], 8),
_ => src.parse(),
};
}?;

res.map_err(|e| {
lex.extras
.push_error(lex.span(), ErrorKind::ParseIntError(e))
})
.ok()
Ok(res)
}

fn pop_parens(lex: &mut Lexer<Token>) -> String {
Expand All @@ -36,10 +32,10 @@ fn pop_parens(lex: &mut Lexer<Token>) -> String {
chars.collect()
}

#[derive(Debug, Clone, Error)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum ErrorKind {
#[error("Invalid integer format")]
ParseIntError(ParseIntError),
ParseIntError(#[from] ParseIntError),
#[error("Syntax error")]
SyntaxError,
#[error("Invalid opcode `{0}`")]
Expand All @@ -48,6 +44,12 @@ pub enum ErrorKind {
InvalidOperand,
}

impl Default for ErrorKind {
fn default() -> Self {
Self::SyntaxError
}
}

pub type ErrorMap = HashMap<Span, ErrorKind>;

pub type ParseError = WithSpan<ErrorKind>;
Expand All @@ -73,19 +75,9 @@ impl LinearMemory {
}
}

#[derive(Default, Debug, Clone)]
pub struct Extras {
pub errors: ErrorMap,
}

impl Extras {
pub fn push_error(&mut self, span: Range<usize>, err: ErrorKind) -> &mut ErrorKind {
self.errors.entry(span).or_insert(err)
}
}

#[derive(Logos, Debug, Clone, PartialEq, Eq)]
#[logos(extras = Extras)]
#[logos(skip r"[ \t]")]
#[logos(error = ErrorKind)]
pub enum Token {
#[regex(r"//[^\r\n]*", logos::skip)]
Comment,
Expand Down Expand Up @@ -113,17 +105,11 @@ pub enum Token {
#[regex(r"\(\w*\)", pop_parens)]
Indirect(String),

#[regex(r"[ \t]", logos::skip)]
Whitespace,

#[regex(r"(?:\r\n)|\n")]
Newline,

#[regex(r"\[[0-9]+;[0-9]+\]", LinearMemory::from_lexer)]
LinearMemory(LinearMemory),

#[error]
Error,
}

impl From<Token> for Op {
Expand All @@ -150,28 +136,33 @@ pub type Span = Range<usize>;
pub type WithSpan<T> = (Span, T);

#[derive(Debug, Clone)]
pub struct TokensWithSpan<'a>(pub Lexer<'a, Token>);
pub struct TokensWithError<'a>(pub Lexer<'a, Token>);

impl<'a> TokensWithSpan<'a> {
impl TokensWithError<'_> {
pub fn lines(mut self) -> (Vec<Vec<WithSpan<Token>>>, ErrorMap) {
let mut errors = ErrorMap::new();
let acc = self.by_ref().fold(vec![Vec::new()], |mut acc, (r, t)| {
if matches!(t, Token::Newline) {
acc.push(Vec::new());
} else {
acc.last_mut().unwrap().push((r, t));
match t {
Ok(Token::Newline) => {
acc.push(Vec::new());
}
Ok(t) => {
acc.last_mut().unwrap().push((r, t));
}
Err(e) => {
errors.entry(r).or_insert(e);
}
}

acc
});

let errs = self.0.extras.errors;

(acc, errs)
(acc, errors)
}
}

impl<'a> Iterator for TokensWithSpan<'a> {
type Item = WithSpan<Token>;
impl Iterator for TokensWithError<'_> {
type Item = WithSpan<Result<Token, ErrorKind>>;

fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|token| (self.0.span(), token))
Expand Down
4 changes: 2 additions & 2 deletions lib/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
exec::{self, DebugInfo},
inst::{self, InstSet, Op},
parse::lexer::{
ErrorKind, ErrorMap, LinearMemory, ParseError, Span, Token, TokensWithSpan, WithSpan,
ErrorKind, ErrorMap, LinearMemory, ParseError, Span, Token, TokensWithError, WithSpan,
},
};
use logos::Logos;
Expand Down Expand Up @@ -43,7 +43,7 @@ where
<I as FromStr>::Err: Display,
{
pub fn new(src: &'a str) -> Self {
let (lines, err) = TokensWithSpan(Token::lexer(src)).lines();
let (lines, err) = TokensWithError(Token::lexer(src)).lines();
Self {
src,
lines,
Expand Down

0 comments on commit fe9a35a

Please sign in to comment.