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};
@ -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<Token>,
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<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 {
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<Token>, entire_span: Span, is_expanded: bool) -> Self {
Lexer {
buf: Cow::Owned(buf),
buf,
cursor: 0,
entire_span,
is_expanded,

View File

@ -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 }
}

View File

@ -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!(

View File

@ -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,

View File

@ -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 }
}

View File

@ -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 }
}

View File

@ -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<usize>,
}
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,

View File

@ -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
}
}

View File

@ -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?

View File

@ -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,

View File

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