Allow for unknown at-rules

This commit is contained in:
ConnorSkees 2020-02-09 19:44:45 -05:00
parent 7fcfeee97e
commit 589ebce452
2 changed files with 65 additions and 55 deletions

View File

@ -1,4 +1,3 @@
use std::convert::TryFrom;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::iter::Peekable; use std::iter::Peekable;
@ -100,6 +99,7 @@ impl AtRule {
AtRuleKind::While => todo!("@while not yet implemented"), AtRuleKind::While => todo!("@while not yet implemented"),
AtRuleKind::Media => todo!("@media not yet implemented"), AtRuleKind::Media => todo!("@media not yet implemented"),
AtRuleKind::Keyframes => todo!("@keyframes not yet implemented"), AtRuleKind::Keyframes => todo!("@keyframes not yet implemented"),
AtRuleKind::Unknown(_) => todo!("unknown @ rules are not yet implemented"),
_ => todo!("encountered unimplemented at rule"), _ => todo!("encountered unimplemented at rule"),
} }
} }
@ -111,15 +111,20 @@ fn eat_media_query<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) {}
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum AtRuleKind { 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,
/// Loads a Sass stylesheet and makes its mixins, functions, and variables available when your stylesheet is loaded with the `@use` rule /// Loads a Sass stylesheet and makes its mixins, functions,
/// and variables available when your stylesheet is loaded
/// with the `@use` rule
Forward, Forward,
/// Extends the CSS at-rule to load styles, mixins, functions, and variables from other stylesheets /// Extends the CSS at-rule to load styles, mixins, functions,
/// and variables from other stylesheets
Import, Import,
Mixin, Mixin,
Include, Include,
/// Defines custom functions that can be used in SassScript expressions /// Defines custom functions that can be used in SassScript
/// expressions
Function, Function,
Return, Return,
/// Allows selectors to inherit styles from one another /// Allows selectors to inherit styles from one another
@ -141,13 +146,18 @@ pub enum AtRuleKind {
// CSS @rules // CSS @rules
/// Defines the character set used by the style sheet /// Defines the character set used by the style sheet
Charset, Charset,
/// Tells the CSS engine that all its content must be considered prefixed with an XML namespace /// Tells the CSS engine that all its content must be considered
/// prefixed with an XML namespace
Namespace, Namespace,
/// A conditional group rule that will apply its content if the device meets the criteria of the condition defined using a media query /// A conditional group rule that will apply its content if
/// the device meets the criteria of the
/// condition defined using a media query
Media, Media,
/// A conditional group rule that will apply its content if the browser meets the criteria of the given condition /// A conditional group rule that will apply its content if the
/// browser meets the criteria of the given condition
Supports, Supports,
/// Describes the aspect of layout changes that will be applied when printing the document /// Describes the aspect of layout changes that will be
/// applied when printing the document
Page, Page,
/// Describes the aspect of an external font to be downloaded /// Describes the aspect of an external font to be downloaded
FontFace, FontFace,
@ -168,7 +178,8 @@ pub enum AtRuleKind {
/// ///
/// Currently at the Working Draft stage /// Currently at the Working Draft stage
Viewport, Viewport,
/// A conditional group rule that will apply its content if the document in which the style sheet is applied meets the criteria of the given condition /// A conditional group rule that will apply its content if the document in
/// which the style sheet is applied meets the criteria of the given condition
/// ///
/// Deferred to Level 4 of CSS Spec /// Deferred to Level 4 of CSS Spec
Document, Document,
@ -176,48 +187,50 @@ pub enum AtRuleKind {
/// ///
/// At the Candidate Recommendation stage /// At the Candidate Recommendation stage
CounterStyle, CounterStyle,
/// An unknown at rule.
/// For compatibility, they are parsed the same as @media
Unknown(String),
} }
impl TryFrom<&str> for AtRuleKind { impl From<&str> for AtRuleKind {
type Error = &'static str; fn from(c: &str) -> Self {
match c.to_ascii_lowercase().as_str() {
fn try_from(c: &str) -> Result<Self, &'static str> { "use" => Self::Use,
match c { "forward" => Self::Forward,
"use" => Ok(Self::Use), "import" => Self::Import,
"forward" => Ok(Self::Forward), "mixin" => Self::Mixin,
"import" => Ok(Self::Import), "include" => Self::Include,
"mixin" => Ok(Self::Mixin), "function" => Self::Function,
"include" => Ok(Self::Include), "return" => Self::Return,
"function" => Ok(Self::Function), "extend" => Self::Extend,
"return" => Ok(Self::Return), "at-root" => Self::AtRoot,
"extend" => Ok(Self::Extend), "error" => Self::Error,
"at-root" => Ok(Self::AtRoot), "warn" => Self::Warn,
"error" => Ok(Self::Error), "debug" => Self::Debug,
"warn" => Ok(Self::Warn), "if" => Self::If,
"debug" => Ok(Self::Debug), "else" => Self::Else,
"if" => Ok(Self::If), "each" => Self::Each,
"else" => Ok(Self::Else), "for" => Self::For,
"each" => Ok(Self::Each), "while" => Self::While,
"for" => Ok(Self::For), "charset" => Self::Charset,
"while" => Ok(Self::While), "namespace" => Self::Namespace,
"charset" => Ok(Self::Charset), "media" => Self::Media,
"namespace" => Ok(Self::Namespace), "supports" => Self::Supports,
"media" => Ok(Self::Media), "page" => Self::Page,
"supports" => Ok(Self::Supports), "fontface" => Self::FontFace,
"page" => Ok(Self::Page), "keyframes" => Self::Keyframes,
"fontface" => Ok(Self::FontFace), "fontfeaturevalues" => Self::FontFeatureValues,
"keyframes" => Ok(Self::Keyframes), "swash" => Self::Swash,
"fontfeaturevalues" => Ok(Self::FontFeatureValues), "ornaments" => Self::Ornaments,
"swash" => Ok(Self::Swash), "annotation" => Self::Annotation,
"ornaments" => Ok(Self::Ornaments), "stylistic" => Self::Stylistic,
"annotation" => Ok(Self::Annotation), "styleset" => Self::Styleset,
"stylistic" => Ok(Self::Stylistic), "charactervariant" => Self::CharacterVariant,
"styleset" => Ok(Self::Styleset), "viewport" => Self::Viewport,
"charactervariant" => Ok(Self::CharacterVariant), "document" => Self::Document,
"viewport" => Ok(Self::Viewport), "counterstyle" => Self::CounterStyle,
"document" => Ok(Self::Document), s => Self::Unknown(s.to_owned()),
"counterstyle" => Ok(Self::CounterStyle),
_ => Err("invalid at rule"),
} }
} }
} }
@ -259,6 +272,7 @@ impl Display for AtRuleKind {
Self::Viewport => write!(f, "@viewport"), Self::Viewport => write!(f, "@viewport"),
Self::Document => write!(f, "@document"), Self::Document => write!(f, "@document"),
Self::CounterStyle => write!(f, "@counterstyle"), Self::CounterStyle => write!(f, "@counterstyle"),
Self::Unknown(s) => write!(f, "@{}", s),
} }
} }
} }

View File

@ -214,11 +214,7 @@ impl<'a> Lexer<'a> {
string.push(tok); string.push(tok);
} }
if let Ok(rule) = AtRuleKind::try_from(string.as_ref()) { TokenKind::AtRule(AtRuleKind::from(string.as_ref()))
TokenKind::AtRule(rule)
} else {
panic!("expected ident after `@`")
}
} }
fn lex_forward_slash(&mut self) -> TokenKind { fn lex_forward_slash(&mut self) -> TokenKind {