Add IsComment trait and devour_comment utils

This commit is contained in:
ConnorSkees 2020-01-25 09:54:38 -05:00
parent 87bf73aa47
commit 30cac02f9c
3 changed files with 40 additions and 13 deletions

View File

@ -56,8 +56,7 @@ use crate::lexer::Lexer;
use crate::mixin::{eat_include, Mixin};
use crate::selector::{Attribute, Selector};
use crate::style::Style;
use crate::units::Unit;
use crate::utils::{devour_whitespace, eat_variable_value, IsWhitespace};
use crate::utils::{devour_whitespace, eat_variable_value, IsComment, IsWhitespace};
mod color;
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)]
pub(crate) enum TokenKind {
Ident(String),

View File

@ -1,5 +1,6 @@
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 std::fmt::{self, Display};
use std::iter::Peekable;
@ -38,15 +39,6 @@ impl<'a> StyleParser<'a> {
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 {
let mut property = String::new();
// 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();

View File

@ -20,6 +20,24 @@ pub(crate) fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(
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)]
pub(crate) fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
let mut toks = scope