Skip to content

Commit

Permalink
remove lifetime from lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
connorskees committed Aug 4, 2024
1 parent f16fc40 commit e0f0be1
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 57 deletions.
15 changes: 7 additions & 8 deletions crates/compiler/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -11,17 +11,16 @@ 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.
/// This is possible due to interpolation.
is_expanded: bool,
}

impl<'a> Lexer<'a> {
impl Lexer {
pub fn raw_text(&self, start: usize) -> String {
self.buf[start..self.cursor]
.iter()
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions crates/compiler/src/parse/at_root_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
7 changes: 3 additions & 4 deletions crates/compiler/src/parse/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/src/parse/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions crates/compiler/src/parse/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
14 changes: 7 additions & 7 deletions crates/compiler/src/parse/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/src/parse/sass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/src/parse/scss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/compiler/src/parse/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
14 changes: 7 additions & 7 deletions crates/compiler/src/selector/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/lib/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e0f0be1

Please sign in to comment.