Rename AtRule enum to AtRuleKind

This commit is contained in:
ConnorSkees 2020-01-20 08:36:06 -05:00
parent 3e3f08599b
commit 33244c34d7
3 changed files with 18 additions and 17 deletions

View File

@ -139,7 +139,7 @@ impl TryFrom<char> for Symbol {
pub struct MediaQuery {} pub struct MediaQuery {}
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum AtRule { pub enum AtRuleKind {
// SASS specific @rules // SASS specific @rules
/// Loads mixins, functions, and variables from other Sass stylesheets, and combines CSS from multiple stylesheets together /// Loads mixins, functions, and variables from other Sass stylesheets, and combines CSS from multiple stylesheets together
Use, Use,
@ -206,7 +206,7 @@ pub enum AtRule {
CounterStyle, CounterStyle,
} }
impl TryFrom<&str> for AtRule { impl TryFrom<&str> for AtRuleKind {
type Error = &'static str; type Error = &'static str;
fn try_from(c: &str) -> Result<Self, &'static str> { fn try_from(c: &str) -> Result<Self, &'static str> {
@ -248,7 +248,7 @@ impl TryFrom<&str> for AtRule {
} }
} }
impl Display for AtRule { impl Display for AtRuleKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::Use => write!(f, "@use"), Self::Use => write!(f, "@use"),

View File

@ -2,7 +2,7 @@ use std::convert::TryFrom;
use std::iter::Peekable; use std::iter::Peekable;
use std::str::Chars; use std::str::Chars;
use crate::common::{AtRule, Keyword, Op, Pos, Symbol}; use crate::common::{AtRuleKind, Keyword, Op, Pos, Symbol};
use crate::selector::{Attribute, AttributeKind}; use crate::selector::{Attribute, AttributeKind};
use crate::units::Unit; use crate::units::Unit;
use crate::{Token, TokenKind, Whitespace}; use crate::{Token, TokenKind, Whitespace};
@ -150,7 +150,7 @@ impl<'a> Lexer<'a> {
string.push(tok); string.push(tok);
} }
if let Ok(rule) = AtRule::try_from(string.as_ref()) { if let Ok(rule) = AtRuleKind::try_from(string.as_ref()) {
TokenKind::AtRule(rule) TokenKind::AtRule(rule)
} else { } else {
panic!("expected ident after `@`") panic!("expected ident after `@`")

View File

@ -47,7 +47,7 @@ use std::io::{self, stdout, BufWriter, Write};
use std::iter::{Iterator, Peekable}; use std::iter::{Iterator, Peekable};
use std::path::Path; use std::path::Path;
use crate::common::{AtRule, Keyword, Op, Pos, Printer, Scope, Symbol, Whitespace}; use crate::common::{AtRuleKind, Keyword, Op, Pos, Printer, Scope, Symbol, Whitespace};
use crate::css::Css; use crate::css::Css;
use crate::error::SassError; use crate::error::SassError;
use crate::format::PrettyPrinter; use crate::format::PrettyPrinter;
@ -104,7 +104,7 @@ pub enum TokenKind {
Ident(String), Ident(String),
Symbol(Symbol), Symbol(Symbol),
String(String), String(String),
AtRule(AtRule), AtRule(AtRuleKind),
Keyword(Keyword), Keyword(Keyword),
Number(String), Number(String),
Unit(Unit), Unit(Unit),
@ -317,7 +317,7 @@ impl<'a> StyleSheetParser<'a> {
}; };
rules.push(Stmt::MultilineComment(comment)); rules.push(Stmt::MultilineComment(comment));
} }
TokenKind::AtRule(AtRule::Import) => { TokenKind::AtRule(AtRuleKind::Import) => {
let Token { pos, .. } = self let Token { pos, .. } = self
.lexer .lexer
.next() .next()
@ -360,7 +360,7 @@ impl<'a> StyleSheetParser<'a> {
rules.extend(new_rules); rules.extend(new_rules);
self.global_scope.merge(new_scope); self.global_scope.merge(new_scope);
} }
TokenKind::AtRule(AtRule::Mixin) => { TokenKind::AtRule(AtRuleKind::Mixin) => {
let (name, mixin) = let (name, mixin) =
Mixin::from_tokens(&mut self.lexer, &self.global_scope).unwrap(); Mixin::from_tokens(&mut self.lexer, &self.global_scope).unwrap();
self.global_scope.mixins.insert(name, mixin); self.global_scope.mixins.insert(name, mixin);
@ -430,40 +430,41 @@ impl<'a> StyleSheetParser<'a> {
} }
fn eat_at_rule<I: Iterator<Item = Token>>( fn eat_at_rule<I: Iterator<Item = Token>>(
rule: &AtRule, rule: &AtRuleKind,
pos: Pos, pos: Pos,
toks: &mut Peekable<I>, toks: &mut Peekable<I>,
scope: &Scope, scope: &Scope,
) -> Result<Expr, Printer> { ) -> Result<Expr, Printer> {
devour_whitespace(toks); devour_whitespace(toks);
match rule { match rule {
AtRule::Error => { AtRuleKind::Error => {
let message = toks let message = toks
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon)) .take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
.map(|x| x.kind.to_string()) .map(|x| x.kind.to_string())
.collect::<String>(); .collect::<String>();
Err(Printer::Error(pos, message)) Err(Printer::Error(pos, message))
} }
AtRule::Warn => { AtRuleKind::Warn => {
let message = toks let message = toks
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon)) .take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
.map(|x| x.kind.to_string()) .map(|x| x.kind.to_string())
.collect::<String>(); .collect::<String>();
devour_whitespace(toks);
Err(Printer::Warn(pos, message)) Err(Printer::Warn(pos, message))
} }
AtRule::Debug => { AtRuleKind::Debug => {
let message = toks let message = toks
.by_ref() .by_ref()
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon)) .take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
.map(|x| x.kind.to_string()) .map(|x| x.kind.to_string())
.collect::<String>(); .collect::<String>();
devour_whitespace(toks);
Err(Printer::Debug(pos, message)) Err(Printer::Debug(pos, message))
} }
AtRule::Mixin => { AtRuleKind::Mixin => {
let (name, mixin) = Mixin::from_tokens(toks, scope)?; let (name, mixin) = Mixin::from_tokens(toks, scope)?;
Ok(Expr::MixinDecl(name, mixin)) Ok(Expr::MixinDecl(name, mixin))
} }
// AtRule::Include => return Some(self.eat_include()),
_ => todo!("encountered unimplemented at rule"), _ => todo!("encountered unimplemented at rule"),
} }
} }
@ -531,14 +532,14 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
values.push(tok); values.push(tok);
} }
} }
TokenKind::AtRule(AtRule::Include) => { TokenKind::AtRule(AtRuleKind::Include) => {
return Ok(Some(Expr::Include(eat_include( return Ok(Some(Expr::Include(eat_include(
toks, toks,
scope, scope,
super_selector, super_selector,
)?))); )?)));
} }
TokenKind::AtRule(AtRule::Mixin) => { TokenKind::AtRule(AtRuleKind::Mixin) => {
toks.next(); toks.next();
let (name, mixin) = Mixin::from_tokens(toks, scope).unwrap(); let (name, mixin) = Mixin::from_tokens(toks, scope).unwrap();
return Ok(Some(Expr::MixinDecl(name, mixin))); return Ok(Some(Expr::MixinDecl(name, mixin)));