Refactor how StyleSheet is constructed

This commit is contained in:
ConnorSkees 2020-01-18 14:57:56 -05:00
parent fb33159996
commit 9be0826d01
3 changed files with 35 additions and 18 deletions

View File

@ -115,7 +115,7 @@ impl Css {
}
fn parse_stylesheet(mut self, s: StyleSheet) -> Css {
for stmt in s.rules {
for stmt in s.0 {
self.parse_stmt(stmt);
}
self

View File

@ -41,7 +41,7 @@ impl<W: Write> PrettyPrinter<W> {
/// The result should be an exact copy of the SCSS input
/// Empty rules are included
pub fn pretty_print(&mut self, s: &StyleSheet) -> io::Result<()> {
for rule in &s.rules {
for rule in &s.0 {
self.pretty_print_stmt(rule)?;
}
Ok(())
@ -85,7 +85,7 @@ impl<W: Write> PrettyPrinter<W> {
&mut self,
s: &StyleSheet,
) -> io::Result<()> {
for rule in &s.rules {
for rule in &s.0 {
self.pretty_print_stmt_preserve_super_selectors(rule)?;
}
writeln!(self.buf)?;

View File

@ -122,9 +122,7 @@ impl Display for TokenKind {
/// Represents a parsed SASS stylesheet with nesting
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StyleSheet {
rules: Vec<Stmt>,
}
pub struct StyleSheet(Vec<Stmt>);
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Stmt {
@ -178,25 +176,44 @@ enum Expr {
impl StyleSheet {
pub fn new(input: &str) -> SassResult<StyleSheet> {
StyleSheetParser {
global_scope: Scope::new(),
lexer: Lexer::new(input).peekable(),
rules: Vec::new(),
scope: 0,
file: String::from("stdin"),
}
.parse_toplevel()
Ok(StyleSheet(
StyleSheetParser {
global_scope: Scope::new(),
lexer: Lexer::new(input).peekable(),
rules: Vec::new(),
scope: 0,
file: String::from("stdin"),
}
.parse_toplevel()?
.0,
))
}
pub fn from_path<P: AsRef<Path> + Into<String>>(p: P) -> SassResult<StyleSheet> {
StyleSheetParser {
Ok(StyleSheet(
StyleSheetParser {
global_scope: Scope::new(),
lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(),
rules: Vec::new(),
scope: 0,
file: p.into(),
}
.parse_toplevel()?
.0,
))
}
pub(crate) fn export_from_path<P: AsRef<Path> + Into<String>>(
p: P,
) -> SassResult<(Vec<Stmt>, Scope)> {
Ok(StyleSheetParser {
global_scope: Scope::new(),
lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(),
rules: Vec::new(),
scope: 0,
file: p.into(),
}
.parse_toplevel()
.parse_toplevel()?)
}
/// Print the internal representation of a parsed stylesheet
@ -229,7 +246,7 @@ struct StyleSheetParser<'a> {
}
impl<'a> StyleSheetParser<'a> {
fn parse_toplevel(mut self) -> SassResult<StyleSheet> {
fn parse_toplevel(mut self) -> SassResult<(Vec<Stmt>, Scope)> {
let mut rules: Vec<Stmt> = Vec::new();
while let Some(Token { kind, .. }) = self.lexer.peek() {
match kind.clone() {
@ -296,7 +313,7 @@ impl<'a> StyleSheetParser<'a> {
}
};
}
Ok(StyleSheet { rules })
Ok((rules, self.global_scope))
}
fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {