From e0f0be112a63df3c23b99256a4a3676021e0208a Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 4 Aug 2024 18:02:48 +0000 Subject: [PATCH] remove lifetime from lexer --- crates/compiler/src/lexer.rs | 15 +++++++-------- crates/compiler/src/parse/at_root_query.rs | 14 +++++++------- crates/compiler/src/parse/base.rs | 7 +++---- crates/compiler/src/parse/css.rs | 10 +++++----- crates/compiler/src/parse/keyframes.rs | 14 +++++++------- crates/compiler/src/parse/media_query.rs | 14 +++++++------- crates/compiler/src/parse/sass.rs | 10 +++++----- crates/compiler/src/parse/scss.rs | 10 +++++----- crates/compiler/src/parse/stylesheet.rs | 3 +-- crates/compiler/src/selector/parse.rs | 14 +++++++------- crates/lib/tests/macros.rs | 1 + 11 files changed, 55 insertions(+), 57 deletions(-) diff --git a/crates/compiler/src/lexer.rs b/crates/compiler/src/lexer.rs index 0f8a52c..3d5741f 100644 --- a/crates/compiler/src/lexer.rs +++ b/crates/compiler/src/lexer.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, iter::Peekable, str::Chars, sync::Arc}; +use std::{iter::Peekable, str::Chars, sync::Arc}; use codemap::{File, Span}; @@ -11,9 +11,8 @@ pub(crate) struct Token { } #[derive(Debug, Clone)] -// todo: remove lifetime as Cow is now superfluous -pub(crate) struct Lexer<'a> { - buf: Cow<'a, [Token]>, +pub(crate) struct Lexer { + buf: Vec, entire_span: Span, cursor: usize, /// If the input this lexer is spanned over is larger than the original span. @@ -21,7 +20,7 @@ pub(crate) struct Lexer<'a> { is_expanded: bool, } -impl<'a> Lexer<'a> { +impl Lexer { pub fn raw_text(&self, start: usize) -> String { self.buf[start..self.cursor] .iter() @@ -97,7 +96,7 @@ impl<'a> Lexer<'a> { } } -impl<'a> Iterator for Lexer<'a> { +impl Iterator for Lexer { type Item = Token; fn next(&mut self) -> Option { @@ -151,7 +150,7 @@ impl<'a> Iterator for TokenLexer<'a> { } } -impl<'a> Lexer<'a> { +impl Lexer { pub fn new_from_file(file: &Arc) -> Self { let buf = TokenLexer::new(file.source().chars().peekable()).collect(); Self::new(buf, file.span, false) @@ -166,7 +165,7 @@ impl<'a> Lexer<'a> { fn new(buf: Vec, entire_span: Span, is_expanded: bool) -> Self { Lexer { - buf: Cow::Owned(buf), + buf, cursor: 0, entire_span, is_expanded, diff --git a/crates/compiler/src/parse/at_root_query.rs b/crates/compiler/src/parse/at_root_query.rs index 190e80b..c75dce7 100644 --- a/crates/compiler/src/parse/at_root_query.rs +++ b/crates/compiler/src/parse/at_root_query.rs @@ -4,22 +4,22 @@ use crate::{ast::AtRootQuery, error::SassResult, lexer::Lexer}; use super::BaseParser; -pub(crate) struct AtRootQueryParser<'a> { - toks: Lexer<'a>, +pub(crate) struct AtRootQueryParser { + toks: Lexer, } -impl<'a> BaseParser<'a> for AtRootQueryParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl BaseParser for AtRootQueryParser { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } } -impl<'a> AtRootQueryParser<'a> { - pub fn new(toks: Lexer<'a>) -> AtRootQueryParser<'a> { +impl AtRootQueryParser { + pub fn new(toks: Lexer) -> AtRootQueryParser { AtRootQueryParser { toks } } diff --git a/crates/compiler/src/parse/base.rs b/crates/compiler/src/parse/base.rs index 8bd7b9d..d4ff00f 100644 --- a/crates/compiler/src/parse/base.rs +++ b/crates/compiler/src/parse/base.rs @@ -5,10 +5,9 @@ use crate::{ Token, }; -// todo: can we simplify lifetimes (by maybe not storing reference to lexer) -pub(crate) trait BaseParser<'a> { - fn toks(&self) -> &Lexer<'a>; - fn toks_mut(&mut self) -> &mut Lexer<'a>; +pub(crate) trait BaseParser { + fn toks(&self) -> &Lexer; + fn toks_mut(&mut self) -> &mut Lexer; fn whitespace_without_comments(&mut self) { while matches!( diff --git a/crates/compiler/src/parse/css.rs b/crates/compiler/src/parse/css.rs index aff8e68..17c04a5 100644 --- a/crates/compiler/src/parse/css.rs +++ b/crates/compiler/src/parse/css.rs @@ -10,19 +10,19 @@ use crate::{ use super::{value::ValueParser, BaseParser, StylesheetParser}; pub(crate) struct CssParser<'a> { - pub toks: Lexer<'a>, + pub toks: Lexer, pub path: &'a Path, pub empty_span: Span, pub flags: ContextFlags, pub options: &'a Options<'a>, } -impl<'a> BaseParser<'a> for CssParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl<'a> BaseParser for CssParser<'a> { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } @@ -103,7 +103,7 @@ impl<'a> StylesheetParser<'a> for CssParser<'a> { impl<'a> CssParser<'a> { pub fn new( - toks: Lexer<'a>, + toks: Lexer, options: &'a Options<'a>, empty_span: Span, file_name: &'a Path, diff --git a/crates/compiler/src/parse/keyframes.rs b/crates/compiler/src/parse/keyframes.rs index 7124a00..de11160 100644 --- a/crates/compiler/src/parse/keyframes.rs +++ b/crates/compiler/src/parse/keyframes.rs @@ -14,22 +14,22 @@ impl fmt::Display for KeyframesSelector { } } -pub(crate) struct KeyframesSelectorParser<'a> { - toks: Lexer<'a>, +pub(crate) struct KeyframesSelectorParser { + toks: Lexer, } -impl<'a> BaseParser<'a> for KeyframesSelectorParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl<'a> BaseParser for KeyframesSelectorParser { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } } -impl<'a> KeyframesSelectorParser<'a> { - pub fn new(toks: Lexer<'a>) -> KeyframesSelectorParser<'a> { +impl KeyframesSelectorParser { + pub fn new(toks: Lexer) -> KeyframesSelectorParser { KeyframesSelectorParser { toks } } diff --git a/crates/compiler/src/parse/media_query.rs b/crates/compiler/src/parse/media_query.rs index 53d3c17..55eb97f 100644 --- a/crates/compiler/src/parse/media_query.rs +++ b/crates/compiler/src/parse/media_query.rs @@ -2,22 +2,22 @@ use crate::{ast::MediaQuery, error::SassResult, lexer::Lexer}; use super::BaseParser; -pub(crate) struct MediaQueryParser<'a> { - pub toks: Lexer<'a>, +pub(crate) struct MediaQueryParser { + pub toks: Lexer, } -impl<'a> BaseParser<'a> for MediaQueryParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl BaseParser for MediaQueryParser { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } } -impl<'a> MediaQueryParser<'a> { - pub fn new(toks: Lexer<'a>) -> MediaQueryParser<'a> { +impl MediaQueryParser { + pub fn new(toks: Lexer) -> MediaQueryParser { MediaQueryParser { toks } } diff --git a/crates/compiler/src/parse/sass.rs b/crates/compiler/src/parse/sass.rs index 07569fd..f466b4d 100644 --- a/crates/compiler/src/parse/sass.rs +++ b/crates/compiler/src/parse/sass.rs @@ -7,7 +7,7 @@ use crate::{ast::*, error::SassResult, lexer::Lexer, ContextFlags, Options, Toke use super::{BaseParser, StylesheetParser}; pub(crate) struct SassParser<'a> { - pub toks: Lexer<'a>, + pub toks: Lexer, pub path: &'a Path, pub empty_span: Span, pub flags: ContextFlags, @@ -18,12 +18,12 @@ pub(crate) struct SassParser<'a> { pub next_indentation_end: Option, } -impl<'a> BaseParser<'a> for SassParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl<'a> BaseParser for SassParser<'a> { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } @@ -344,7 +344,7 @@ impl<'a> StylesheetParser<'a> for SassParser<'a> { impl<'a> SassParser<'a> { pub fn new( - toks: Lexer<'a>, + toks: Lexer, options: &'a Options<'a>, empty_span: Span, file_name: &'a Path, diff --git a/crates/compiler/src/parse/scss.rs b/crates/compiler/src/parse/scss.rs index fe59f0f..17ed507 100644 --- a/crates/compiler/src/parse/scss.rs +++ b/crates/compiler/src/parse/scss.rs @@ -7,7 +7,7 @@ use crate::{lexer::Lexer, ContextFlags, Options}; use super::{BaseParser, StylesheetParser}; pub(crate) struct ScssParser<'a> { - pub toks: Lexer<'a>, + pub toks: Lexer, pub path: &'a Path, pub empty_span: Span, pub flags: ContextFlags, @@ -16,7 +16,7 @@ pub(crate) struct ScssParser<'a> { impl<'a> ScssParser<'a> { pub fn new( - toks: Lexer<'a>, + toks: Lexer, options: &'a Options<'a>, empty_span: Span, file_name: &'a Path, @@ -35,12 +35,12 @@ impl<'a> ScssParser<'a> { } } -impl<'a> BaseParser<'a> for ScssParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl<'a> BaseParser for ScssParser<'a> { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } } diff --git a/crates/compiler/src/parse/stylesheet.rs b/crates/compiler/src/parse/stylesheet.rs index 85110c6..95d7484 100644 --- a/crates/compiler/src/parse/stylesheet.rs +++ b/crates/compiler/src/parse/stylesheet.rs @@ -23,10 +23,9 @@ use super::{ BaseParser, DeclarationOrBuffer, ScssParser, VariableDeclOrInterpolation, RESERVED_IDENTIFIERS, }; -// todo: can we simplify lifetimes (by maybe not storing reference to lexer) /// Default implementations are oriented towards the SCSS syntax, as both CSS and /// SCSS share the behavior -pub(crate) trait StylesheetParser<'a>: BaseParser<'a> + Sized { +pub(crate) trait StylesheetParser<'a>: BaseParser + Sized { // todo: make constant? fn is_plain_css(&self) -> bool; // todo: make constant? diff --git a/crates/compiler/src/selector/parse.rs b/crates/compiler/src/selector/parse.rs index 10d1e04..a0af3ec 100644 --- a/crates/compiler/src/selector/parse.rs +++ b/crates/compiler/src/selector/parse.rs @@ -33,30 +33,30 @@ const SELECTOR_PSEUDO_CLASSES: [&str; 9] = [ /// Pseudo-element selectors that take unadorned selectors as arguments. const SELECTOR_PSEUDO_ELEMENTS: [&str; 1] = ["slotted"]; -pub(crate) struct SelectorParser<'a> { +pub(crate) struct SelectorParser { /// Whether this parser allows the parent selector `&`. allows_parent: bool, /// Whether this parser allows placeholder selectors beginning with `%`. allows_placeholder: bool, - pub toks: Lexer<'a>, + pub toks: Lexer, span: Span, } -impl<'a> BaseParser<'a> for SelectorParser<'a> { - fn toks(&self) -> &Lexer<'a> { +impl BaseParser for SelectorParser { + fn toks(&self) -> &Lexer { &self.toks } - fn toks_mut(&mut self) -> &mut Lexer<'a> { + fn toks_mut(&mut self) -> &mut Lexer { &mut self.toks } } -impl<'a> SelectorParser<'a> { - pub fn new(toks: Lexer<'a>, allows_parent: bool, allows_placeholder: bool, span: Span) -> Self { +impl SelectorParser { + pub fn new(toks: Lexer, allows_parent: bool, allows_placeholder: bool, span: Span) -> Self { Self { toks, allows_parent, diff --git a/crates/lib/tests/macros.rs b/crates/lib/tests/macros.rs index e7d1bca..bac51e2 100644 --- a/crates/lib/tests/macros.rs +++ b/crates/lib/tests/macros.rs @@ -174,6 +174,7 @@ struct TestLoggerState { #[derive(Debug, Default)] pub struct TestLogger(RefCell); +#[allow(unused)] impl TestLogger { pub fn debug_messages(&self) -> Vec { self.0.borrow().debug_messages.clone()