remove lifetime from lexer

This commit is contained in:
Connor Skees 2024-08-04 18:02:48 +00:00
parent f16fc40bde
commit e0f0be112a
11 changed files with 55 additions and 57 deletions

View File

@ -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}; use codemap::{File, Span};
@ -11,9 +11,8 @@ pub(crate) struct Token {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
// todo: remove lifetime as Cow is now superfluous pub(crate) struct Lexer {
pub(crate) struct Lexer<'a> { buf: Vec<Token>,
buf: Cow<'a, [Token]>,
entire_span: Span, entire_span: Span,
cursor: usize, cursor: usize,
/// If the input this lexer is spanned over is larger than the original span. /// 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, is_expanded: bool,
} }
impl<'a> Lexer<'a> { impl Lexer {
pub fn raw_text(&self, start: usize) -> String { pub fn raw_text(&self, start: usize) -> String {
self.buf[start..self.cursor] self.buf[start..self.cursor]
.iter() .iter()
@ -97,7 +96,7 @@ impl<'a> Lexer<'a> {
} }
} }
impl<'a> Iterator for Lexer<'a> { impl Iterator for Lexer {
type Item = Token; type Item = Token;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -151,7 +150,7 @@ impl<'a> Iterator for TokenLexer<'a> {
} }
} }
impl<'a> Lexer<'a> { impl Lexer {
pub fn new_from_file(file: &Arc<File>) -> Self { pub fn new_from_file(file: &Arc<File>) -> Self {
let buf = TokenLexer::new(file.source().chars().peekable()).collect(); let buf = TokenLexer::new(file.source().chars().peekable()).collect();
Self::new(buf, file.span, false) Self::new(buf, file.span, false)
@ -166,7 +165,7 @@ impl<'a> Lexer<'a> {
fn new(buf: Vec<Token>, entire_span: Span, is_expanded: bool) -> Self { fn new(buf: Vec<Token>, entire_span: Span, is_expanded: bool) -> Self {
Lexer { Lexer {
buf: Cow::Owned(buf), buf,
cursor: 0, cursor: 0,
entire_span, entire_span,
is_expanded, is_expanded,

View File

@ -4,22 +4,22 @@ use crate::{ast::AtRootQuery, error::SassResult, lexer::Lexer};
use super::BaseParser; use super::BaseParser;
pub(crate) struct AtRootQueryParser<'a> { pub(crate) struct AtRootQueryParser {
toks: Lexer<'a>, toks: Lexer,
} }
impl<'a> BaseParser<'a> for AtRootQueryParser<'a> { impl BaseParser for AtRootQueryParser {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
} }
impl<'a> AtRootQueryParser<'a> { impl AtRootQueryParser {
pub fn new(toks: Lexer<'a>) -> AtRootQueryParser<'a> { pub fn new(toks: Lexer) -> AtRootQueryParser {
AtRootQueryParser { toks } AtRootQueryParser { toks }
} }

View File

@ -5,10 +5,9 @@ use crate::{
Token, Token,
}; };
// todo: can we simplify lifetimes (by maybe not storing reference to lexer) pub(crate) trait BaseParser {
pub(crate) trait BaseParser<'a> { fn toks(&self) -> &Lexer;
fn toks(&self) -> &Lexer<'a>; fn toks_mut(&mut self) -> &mut Lexer;
fn toks_mut(&mut self) -> &mut Lexer<'a>;
fn whitespace_without_comments(&mut self) { fn whitespace_without_comments(&mut self) {
while matches!( while matches!(

View File

@ -10,19 +10,19 @@ use crate::{
use super::{value::ValueParser, BaseParser, StylesheetParser}; use super::{value::ValueParser, BaseParser, StylesheetParser};
pub(crate) struct CssParser<'a> { pub(crate) struct CssParser<'a> {
pub toks: Lexer<'a>, pub toks: Lexer,
pub path: &'a Path, pub path: &'a Path,
pub empty_span: Span, pub empty_span: Span,
pub flags: ContextFlags, pub flags: ContextFlags,
pub options: &'a Options<'a>, pub options: &'a Options<'a>,
} }
impl<'a> BaseParser<'a> for CssParser<'a> { impl<'a> BaseParser for CssParser<'a> {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
@ -103,7 +103,7 @@ impl<'a> StylesheetParser<'a> for CssParser<'a> {
impl<'a> CssParser<'a> { impl<'a> CssParser<'a> {
pub fn new( pub fn new(
toks: Lexer<'a>, toks: Lexer,
options: &'a Options<'a>, options: &'a Options<'a>,
empty_span: Span, empty_span: Span,
file_name: &'a Path, file_name: &'a Path,

View File

@ -14,22 +14,22 @@ impl fmt::Display for KeyframesSelector {
} }
} }
pub(crate) struct KeyframesSelectorParser<'a> { pub(crate) struct KeyframesSelectorParser {
toks: Lexer<'a>, toks: Lexer,
} }
impl<'a> BaseParser<'a> for KeyframesSelectorParser<'a> { impl<'a> BaseParser for KeyframesSelectorParser {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
} }
impl<'a> KeyframesSelectorParser<'a> { impl KeyframesSelectorParser {
pub fn new(toks: Lexer<'a>) -> KeyframesSelectorParser<'a> { pub fn new(toks: Lexer) -> KeyframesSelectorParser {
KeyframesSelectorParser { toks } KeyframesSelectorParser { toks }
} }

View File

@ -2,22 +2,22 @@ use crate::{ast::MediaQuery, error::SassResult, lexer::Lexer};
use super::BaseParser; use super::BaseParser;
pub(crate) struct MediaQueryParser<'a> { pub(crate) struct MediaQueryParser {
pub toks: Lexer<'a>, pub toks: Lexer,
} }
impl<'a> BaseParser<'a> for MediaQueryParser<'a> { impl BaseParser for MediaQueryParser {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
} }
impl<'a> MediaQueryParser<'a> { impl MediaQueryParser {
pub fn new(toks: Lexer<'a>) -> MediaQueryParser<'a> { pub fn new(toks: Lexer) -> MediaQueryParser {
MediaQueryParser { toks } MediaQueryParser { toks }
} }

View File

@ -7,7 +7,7 @@ use crate::{ast::*, error::SassResult, lexer::Lexer, ContextFlags, Options, Toke
use super::{BaseParser, StylesheetParser}; use super::{BaseParser, StylesheetParser};
pub(crate) struct SassParser<'a> { pub(crate) struct SassParser<'a> {
pub toks: Lexer<'a>, pub toks: Lexer,
pub path: &'a Path, pub path: &'a Path,
pub empty_span: Span, pub empty_span: Span,
pub flags: ContextFlags, pub flags: ContextFlags,
@ -18,12 +18,12 @@ pub(crate) struct SassParser<'a> {
pub next_indentation_end: Option<usize>, pub next_indentation_end: Option<usize>,
} }
impl<'a> BaseParser<'a> for SassParser<'a> { impl<'a> BaseParser for SassParser<'a> {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
@ -344,7 +344,7 @@ impl<'a> StylesheetParser<'a> for SassParser<'a> {
impl<'a> SassParser<'a> { impl<'a> SassParser<'a> {
pub fn new( pub fn new(
toks: Lexer<'a>, toks: Lexer,
options: &'a Options<'a>, options: &'a Options<'a>,
empty_span: Span, empty_span: Span,
file_name: &'a Path, file_name: &'a Path,

View File

@ -7,7 +7,7 @@ use crate::{lexer::Lexer, ContextFlags, Options};
use super::{BaseParser, StylesheetParser}; use super::{BaseParser, StylesheetParser};
pub(crate) struct ScssParser<'a> { pub(crate) struct ScssParser<'a> {
pub toks: Lexer<'a>, pub toks: Lexer,
pub path: &'a Path, pub path: &'a Path,
pub empty_span: Span, pub empty_span: Span,
pub flags: ContextFlags, pub flags: ContextFlags,
@ -16,7 +16,7 @@ pub(crate) struct ScssParser<'a> {
impl<'a> ScssParser<'a> { impl<'a> ScssParser<'a> {
pub fn new( pub fn new(
toks: Lexer<'a>, toks: Lexer,
options: &'a Options<'a>, options: &'a Options<'a>,
empty_span: Span, empty_span: Span,
file_name: &'a Path, file_name: &'a Path,
@ -35,12 +35,12 @@ impl<'a> ScssParser<'a> {
} }
} }
impl<'a> BaseParser<'a> for ScssParser<'a> { impl<'a> BaseParser for ScssParser<'a> {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
} }

View File

@ -23,10 +23,9 @@ use super::{
BaseParser, DeclarationOrBuffer, ScssParser, VariableDeclOrInterpolation, RESERVED_IDENTIFIERS, 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 /// Default implementations are oriented towards the SCSS syntax, as both CSS and
/// SCSS share the behavior /// SCSS share the behavior
pub(crate) trait StylesheetParser<'a>: BaseParser<'a> + Sized { pub(crate) trait StylesheetParser<'a>: BaseParser + Sized {
// todo: make constant? // todo: make constant?
fn is_plain_css(&self) -> bool; fn is_plain_css(&self) -> bool;
// todo: make constant? // todo: make constant?

View File

@ -33,30 +33,30 @@ const SELECTOR_PSEUDO_CLASSES: [&str; 9] = [
/// Pseudo-element selectors that take unadorned selectors as arguments. /// Pseudo-element selectors that take unadorned selectors as arguments.
const SELECTOR_PSEUDO_ELEMENTS: [&str; 1] = ["slotted"]; const SELECTOR_PSEUDO_ELEMENTS: [&str; 1] = ["slotted"];
pub(crate) struct SelectorParser<'a> { pub(crate) struct SelectorParser {
/// Whether this parser allows the parent selector `&`. /// Whether this parser allows the parent selector `&`.
allows_parent: bool, allows_parent: bool,
/// Whether this parser allows placeholder selectors beginning with `%`. /// Whether this parser allows placeholder selectors beginning with `%`.
allows_placeholder: bool, allows_placeholder: bool,
pub toks: Lexer<'a>, pub toks: Lexer,
span: Span, span: Span,
} }
impl<'a> BaseParser<'a> for SelectorParser<'a> { impl BaseParser for SelectorParser {
fn toks(&self) -> &Lexer<'a> { fn toks(&self) -> &Lexer {
&self.toks &self.toks
} }
fn toks_mut(&mut self) -> &mut Lexer<'a> { fn toks_mut(&mut self) -> &mut Lexer {
&mut self.toks &mut self.toks
} }
} }
impl<'a> SelectorParser<'a> { impl SelectorParser {
pub fn new(toks: Lexer<'a>, allows_parent: bool, allows_placeholder: bool, span: Span) -> Self { pub fn new(toks: Lexer, allows_parent: bool, allows_placeholder: bool, span: Span) -> Self {
Self { Self {
toks, toks,
allows_parent, allows_parent,

View File

@ -174,6 +174,7 @@ struct TestLoggerState {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct TestLogger(RefCell<TestLoggerState>); pub struct TestLogger(RefCell<TestLoggerState>);
#[allow(unused)]
impl TestLogger { impl TestLogger {
pub fn debug_messages(&self) -> Vec<String> { pub fn debug_messages(&self) -> Vec<String> {
self.0.borrow().debug_messages.clone() self.0.borrow().debug_messages.clone()