grass/src/parse/variable.rs

193 lines
6.7 KiB
Rust
Raw Normal View History

2020-06-16 22:34:01 -04:00
use crate::{
common::Identifier,
error::SassResult,
utils::{peek_ident_no_interpolation, read_until_closing_paren, read_until_closing_quote},
2020-06-16 22:34:01 -04:00
Token,
};
2020-07-05 10:13:49 -04:00
use super::Parser;
2020-06-16 22:34:01 -04:00
#[derive(Debug)]
pub(crate) struct VariableValue {
pub val_toks: Vec<Token>,
pub global: bool,
pub default: bool,
2020-06-16 22:34:01 -04:00
}
impl VariableValue {
pub const fn new(val_toks: Vec<Token>, global: bool, default: bool) -> Self {
2020-06-16 22:34:01 -04:00
Self {
val_toks,
2020-06-16 22:34:01 -04:00
global,
default,
}
}
}
impl<'a> Parser<'a> {
pub(super) fn parse_variable_declaration(&mut self) -> SassResult<()> {
assert!(matches!(self.toks.next(), Some(Token { kind: '$', .. })));
let ident: Identifier = self.parse_identifier_no_interpolation(false)?.node.into();
self.whitespace_or_comment();
self.expect_char(':')?;
let VariableValue {
val_toks,
global,
default,
} = self.parse_variable_value()?;
2020-06-16 22:34:01 -04:00
if default {
let config_val = self.module_config.get(ident);
2020-07-05 10:13:49 -04:00
if self.at_root && !self.flags.in_control_flow() {
2020-07-08 22:38:56 -04:00
if !self.global_scope.var_exists(ident) {
let value = if let Some(config_val) = config_val {
config_val
} else {
self.parse_value_from_vec(val_toks, true)?.node
};
self.global_scope.insert_var(ident, value);
2020-06-16 22:34:01 -04:00
}
} else {
let value = if let Some(config_val) = config_val {
config_val
} else {
self.parse_value_from_vec(val_toks, true)?.node
};
if global && !self.global_scope.var_exists(ident) {
self.global_scope.insert_var(ident, value.clone());
2020-06-16 22:34:01 -04:00
}
self.scopes.insert_default_var(ident, value);
2020-06-16 22:34:01 -04:00
}
return Ok(());
}
2020-07-27 18:55:38 -04:00
let value = self.parse_value_from_vec(val_toks, true)?.node;
if global {
self.global_scope.insert_var(ident, value.clone());
}
if self.at_root {
2020-07-05 10:13:49 -04:00
if self.flags.in_control_flow() {
2020-07-08 22:38:56 -04:00
if self.global_scope.var_exists(ident) {
self.global_scope.insert_var(ident, value);
2020-06-16 22:34:01 -04:00
} else {
self.scopes.insert_var(ident, value);
2020-06-16 22:34:01 -04:00
}
} else {
self.global_scope.insert_var(ident, value);
2020-06-16 22:34:01 -04:00
}
} else {
self.scopes.insert_var(ident, value);
2020-06-16 22:34:01 -04:00
}
Ok(())
}
pub(super) fn parse_variable_value(&mut self) -> SassResult<VariableValue> {
2020-06-16 22:34:01 -04:00
let mut default = false;
let mut global = false;
let mut val_toks = Vec::new();
let mut nesting = 0;
while let Some(tok) = self.toks.peek() {
match tok.kind {
';' => {
self.toks.next();
break;
}
'\\' => {
val_toks.push(self.toks.next().unwrap());
if self.toks.peek().is_some() {
val_toks.push(self.toks.next().unwrap());
}
}
'"' | '\'' => {
let quote = self.toks.next().unwrap();
val_toks.push(quote);
val_toks.extend(read_until_closing_quote(self.toks, quote.kind)?);
}
'#' => {
val_toks.push(self.toks.next().unwrap());
match self.toks.peek() {
Some(Token { kind: '{', .. }) => nesting += 1,
Some(Token { kind: ';', .. }) => break,
Some(Token { kind: '}', .. }) => {
if nesting == 0 {
break;
} else {
nesting -= 1;
}
}
Some(..) | None => {}
}
if let Some(tok) = self.toks.next() {
val_toks.push(tok);
}
2020-06-16 22:34:01 -04:00
}
'{' => break,
'}' => {
if nesting == 0 {
break;
} else {
nesting -= 1;
val_toks.push(self.toks.next().unwrap());
}
}
'/' => {
let next = self.toks.next().unwrap();
match self.toks.peek() {
Some(Token { kind: '/', .. }) => self.read_until_newline(),
2020-06-16 22:34:01 -04:00
Some(..) | None => val_toks.push(next),
};
continue;
}
'(' => {
val_toks.push(self.toks.next().unwrap());
val_toks.extend(read_until_closing_paren(self.toks)?);
}
'!' => {
let pos = tok.pos();
2020-07-02 15:54:33 -04:00
match self.toks.peek_forward(1) {
Some(Token { kind: '=', .. }) => {
self.toks.reset_cursor();
val_toks.push(self.toks.next().unwrap());
continue;
}
Some(..) => {}
None => return Err(("Expected identifier.", pos).into()),
2020-06-16 22:34:01 -04:00
}
// todo: it should not be possible to declare the same flag more than once
let mut ident = peek_ident_no_interpolation(self.toks, false, pos)?;
ident.node.make_ascii_lowercase();
match ident.node.as_str() {
"global" => {
2020-07-02 15:54:33 -04:00
self.toks.truncate_iterator_to_cursor();
2020-06-16 22:34:01 -04:00
global = true;
}
"default" => {
2020-07-02 15:54:33 -04:00
self.toks.truncate_iterator_to_cursor();
2020-06-16 22:34:01 -04:00
default = true;
}
"important" => {
2020-06-19 22:47:06 -04:00
self.toks.reset_cursor();
2020-06-16 22:34:01 -04:00
val_toks.push(self.toks.next().unwrap());
continue;
}
_ => {
return Err(("Invalid flag name.", ident.span).into());
}
}
}
_ => val_toks.push(self.toks.next().unwrap()),
}
}
Ok(VariableValue::new(val_toks, global, default))
2020-06-16 22:34:01 -04:00
}
}