From fe9a35a51cbfdfaeb997a9cd33ae6fea24dc3efb Mon Sep 17 00:00:00 2001 From: SaadiSave Date: Fri, 13 Dec 2024 20:31:32 +0100 Subject: [PATCH] update(deps): update logos to 0.15 --- lib/Cargo.toml | 2 +- lib/src/parse/lexer.rs | 67 ++++++++++++++++++----------------------- lib/src/parse/parser.rs | 4 +-- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index d4ae76e..b304af9 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -18,7 +18,7 @@ extended = [] compile = ["serde"] [dependencies] -logos = "0.12" +logos = "0.15" log = "0.4" thiserror = "1" diff --git a/lib/src/parse/lexer.rs b/lib/src/parse/lexer.rs index 4cf7476..f4f9e96 100755 --- a/lib/src/parse/lexer.rs +++ b/lib/src/parse/lexer.rs @@ -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) -> Option { +fn parse_num(lex: &mut Lexer) -> Result { let src = if lex.slice().as_bytes()[0] == b'#' { &lex.slice()[1..] } else { @@ -20,13 +20,9 @@ fn parse_num(lex: &mut Lexer) -> Option { 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) -> String { @@ -36,10 +32,10 @@ fn pop_parens(lex: &mut Lexer) -> 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}`")] @@ -48,6 +44,12 @@ pub enum ErrorKind { InvalidOperand, } +impl Default for ErrorKind { + fn default() -> Self { + Self::SyntaxError + } +} + pub type ErrorMap = HashMap; pub type ParseError = WithSpan; @@ -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, 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, @@ -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 for Op { @@ -150,28 +136,33 @@ pub type Span = Range; pub type WithSpan = (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>>, 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; +impl Iterator for TokensWithError<'_> { + type Item = WithSpan>; fn next(&mut self) -> Option { self.0.next().map(|token| (self.0.span(), token)) diff --git a/lib/src/parse/parser.rs b/lib/src/parse/parser.rs index 3113d84..7cd067b 100755 --- a/lib/src/parse/parser.rs +++ b/lib/src/parse/parser.rs @@ -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; @@ -43,7 +43,7 @@ where ::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,