refactor how default variables are evaluated

This commit is contained in:
Connor Skees 2020-08-17 06:16:18 -04:00
parent 44ecd82454
commit 8d1e8a99c5
4 changed files with 66 additions and 33 deletions

View File

@ -39,30 +39,43 @@ impl<'a> Parser<'a> {
} = self.parse_variable_value()?;
if default {
let config_val = self.module_config.get(ident);
if self.at_root && !self.flags.in_control_flow() {
if !self.global_scope.default_var_exists(ident) {
let value = if let Some(config_val) = config_val {
config_val
} else {
self.parse_value_from_vec(val_toks, true)?.node
};
let config_val = self.module_config.get(ident).filter(|v| !v.is_null());
self.global_scope.insert_var(ident, value);
}
} else {
let value = if let Some(config_val) = config_val {
config_val
let value = if (self.at_root && !self.flags.in_control_flow()) || global {
if self.global_scope.default_var_exists(ident) {
return Ok(());
} else if let Some(value) = config_val {
value
} else {
self.parse_value_from_vec(val_toks, true)?.node
};
if global && !self.global_scope.default_var_exists(ident) {
self.global_scope.insert_var(ident, value.clone());
}
self.scopes.insert_default_var(ident, value);
} else if self.at_root && self.flags.in_control_flow() {
if self.global_scope.default_var_exists(ident) {
return Ok(());
}
self.parse_value_from_vec(val_toks, true)?.node
} else if !self.at_root {
if self.scopes.default_var_exists(ident) {
return Ok(());
}
self.parse_value_from_vec(val_toks, true)?.node
} else {
self.parse_value_from_vec(val_toks, true)?.node
};
if self.at_root && !self.flags.in_control_flow() {
self.global_scope.insert_var(ident, value);
return Ok(());
}
if global {
self.global_scope.insert_var(ident, value.clone());
}
self.scopes.insert_var(ident, value);
return Ok(());
}

View File

@ -88,21 +88,13 @@ impl Scope {
self.merge(other.scope);
}
pub fn default_var_exists(&mut self, s: Identifier) -> bool {
pub fn default_var_exists(&self, s: Identifier) -> bool {
if let Some(default_var) = self.get_var_no_err(s) {
!default_var.is_null()
} else {
false
}
}
pub fn insert_default_var(&mut self, s: Identifier, v: Value) -> Option<Value> {
if self.default_var_exists(s) {
None
} else {
self.insert_var(s, v)
}
}
}
#[derive(Debug, Default)]
@ -171,12 +163,14 @@ impl Scopes {
}
}
pub fn insert_default_var(&mut self, s: Identifier, v: Value) -> Option<Value> {
if let Some(scope) = self.0.last_mut() {
scope.insert_default_var(s, v)
} else {
panic!()
pub fn default_var_exists(&self, name: Identifier) -> bool {
for scope in self.0.iter().rev() {
if scope.default_var_exists(name) {
return true;
}
}
false
}
pub fn get_var<'a>(

View File

@ -682,7 +682,7 @@ test!(
"null {\n color: null;\n}\n"
);
test!(
#[ignore = "we do not yet have a good way of consuming a string without converting \a to a newline"]
#[ignore = "we do not yet have a good way of consuming a string without converting \\a to a newline"]
silent_comment_in_quoted_attribute_value,
".foo bar[val=\"//\"] {\n color: &;\n}\n",
".foo bar[val=\"//\"] {\n color: .foo bar[val=\"//\"];\n}\n"

View File

@ -182,6 +182,32 @@ test!(
}",
"a {\n color: red;\n}\n"
);
test!(
default_at_root_inside_control_flow,
"$a: initial;
@if true {
$a: outer !default;
}
a {
color: $a;
}",
"a {\n color: initial;\n}\n"
);
test!(
default_at_root_inside_control_flow_outer_is_null,
"$a: null;
@if true {
$a: outer !default;
}
a {
color: $a;
}",
"a {\n color: outer;\n}\n"
);
// https://github.com/Kixiron/lasso/issues/7
test!(
regression_test_for_lasso_0_3_0,