Add IsComment
trait and devour_comment
utils
This commit is contained in:
parent
87bf73aa47
commit
30cac02f9c
21
src/lib.rs
21
src/lib.rs
@ -56,8 +56,7 @@ use crate::lexer::Lexer;
|
|||||||
use crate::mixin::{eat_include, Mixin};
|
use crate::mixin::{eat_include, Mixin};
|
||||||
use crate::selector::{Attribute, Selector};
|
use crate::selector::{Attribute, Selector};
|
||||||
use crate::style::Style;
|
use crate::style::Style;
|
||||||
use crate::units::Unit;
|
use crate::utils::{devour_whitespace, eat_variable_value, IsComment, IsWhitespace};
|
||||||
use crate::utils::{devour_whitespace, eat_variable_value, IsWhitespace};
|
|
||||||
|
|
||||||
mod color;
|
mod color;
|
||||||
mod common;
|
mod common;
|
||||||
@ -99,6 +98,24 @@ impl IsWhitespace for &Token {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IsComment for Token {
|
||||||
|
fn is_comment(&self) -> bool {
|
||||||
|
if let TokenKind::MultilineComment(_) = self.kind {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IsComment for &Token {
|
||||||
|
fn is_comment(&self) -> bool {
|
||||||
|
if let TokenKind::MultilineComment(_) = self.kind {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub(crate) enum TokenKind {
|
pub(crate) enum TokenKind {
|
||||||
Ident(String),
|
Ident(String),
|
||||||
|
14
src/style.rs
14
src/style.rs
@ -1,5 +1,6 @@
|
|||||||
use crate::common::{Scope, Symbol};
|
use crate::common::{Scope, Symbol};
|
||||||
use crate::utils::{deref_variable, eat_interpolation};
|
use crate::utils::{devour_whitespace_or_comment, eat_interpolation};
|
||||||
|
use crate::value::Value;
|
||||||
use crate::{Token, TokenKind};
|
use crate::{Token, TokenKind};
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
@ -38,15 +39,6 @@ impl<'a> StyleParser<'a> {
|
|||||||
Ok(StyleParser { tokens, scope })
|
Ok(StyleParser { tokens, scope })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn devour_whitespace_or_comment(&mut self) {
|
|
||||||
while let Some(Token { kind, .. }) = self.tokens.peek() {
|
|
||||||
match kind {
|
|
||||||
TokenKind::Whitespace(_) | TokenKind::MultilineComment(_) => self.tokens.next(),
|
|
||||||
_ => break,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse(&mut self) -> Style {
|
fn parse(&mut self) -> Style {
|
||||||
let mut property = String::new();
|
let mut property = String::new();
|
||||||
// read property until `:`
|
// read property until `:`
|
||||||
@ -65,7 +57,7 @@ impl<'a> StyleParser<'a> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
self.devour_whitespace_or_comment();
|
devour_whitespace_or_comment(&mut self.tokens);
|
||||||
|
|
||||||
let mut value = String::new();
|
let mut value = String::new();
|
||||||
|
|
||||||
|
18
src/utils.rs
18
src/utils.rs
@ -20,6 +20,24 @@ pub(crate) fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(
|
|||||||
found_whitespace
|
found_whitespace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) trait IsComment {
|
||||||
|
fn is_comment(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn devour_whitespace_or_comment<I: Iterator<Item = W>, W: IsWhitespace + IsComment>(
|
||||||
|
s: &mut Peekable<I>,
|
||||||
|
) -> bool {
|
||||||
|
let mut found_whitespace = false;
|
||||||
|
while let Some(w) = s.peek() {
|
||||||
|
if !w.is_whitespace() && !w.is_comment() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
found_whitespace = true;
|
||||||
|
s.next();
|
||||||
|
}
|
||||||
|
found_whitespace
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "nightly", track_caller)]
|
#[cfg_attr(feature = "nightly", track_caller)]
|
||||||
pub(crate) fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
|
pub(crate) fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
|
||||||
let mut toks = scope
|
let mut toks = scope
|
||||||
|
Loading…
x
Reference in New Issue
Block a user