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 { fn parse_stylesheet(mut self, s: StyleSheet) -> Css {
for stmt in s.rules { for stmt in s.0 {
self.parse_stmt(stmt); self.parse_stmt(stmt);
} }
self self

View File

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

View File

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