use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use mln_conversion::{Convertible, get_json_type};
use mln_vec_map::VecMap;
use crate::assertion::Assertion;
use crate::at::At;
use crate::boolean_operator::{parse_all, parse_any};
use crate::case::Case;
use crate::check_subtype::check_subtype;
use crate::coalesce::Coalesce;
use crate::coercion::Coercion;
use crate::collator_expression::CollatorExpression;
use crate::comparison::parse_comparison;
use crate::compound_expression::parse_compound_expression;
use crate::distance::Distance;
use crate::expression::{Dependency, EvaluationContext, Expr, Node};
use crate::find_zoom_curve::{ZoomCurve, find_zoom_curve};
use crate::format_expression::FormatExpression;
use crate::functions::Functions;
use crate::image_expression::ImageExpression;
use crate::in_::In;
use crate::index_of::IndexOf;
use crate::interpolate::parse_interpolate;
use crate::let_::{Let, Var};
use crate::literal::Literal;
use crate::match_::parse_match;
use crate::number_format::NumberFormat;
use crate::semiliteral::Semiliteral;
use crate::slice::Slice;
use crate::step::Step;
use crate::type_::Type;
use crate::value::Value;
use crate::within::Within;
#[derive(Clone, Debug, Default, PartialEq, Eq, thiserror::Error)]
pub struct ParsingError {
pub message: String,
pub key: String,
}
impl std::fmt::Display for ParsingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !self.key.is_empty() {
write!(f, "{}: ", self.key)?;
}
f.write_str(&self.message)
}
}
pub type ParseResult = Option<Expr>;
pub(crate) type Bindings = VecMap<String, Arc<Expr>>;
pub(crate) struct Scope {
bindings: Bindings,
parent: Option<Rc<Scope>>,
}
impl Scope {
#[must_use]
pub(crate) fn new(bindings: Bindings, parent: Option<Rc<Scope>>) -> Self {
Scope { bindings, parent }
}
#[must_use]
pub(crate) fn get(&self, name: &str) -> Option<Arc<Expr>> {
if let Some(binding) = self.bindings.get(name) {
Some(Arc::clone(binding))
} else if let Some(parent) = &self.parent {
parent.get(name)
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TypeAnnotationOption {
Coerce,
Assert,
Omit,
}
pub struct ParsingContext {
key: String,
expected: Option<Type>,
scope: Option<Rc<Scope>>,
errors: Rc<RefCell<Vec<ParsingError>>>,
functions: Arc<Functions>,
}
const EVALUATION_INPUTS: Dependency = Dependency(
Dependency::FEATURE.0
| Dependency::ZOOM.0
| Dependency::ELEVATION.0
| Dependency::GLOBAL_STATE.0
| Dependency::PROPERTY_INPUT.0,
);
fn is_constant(expression: &Expr) -> bool {
match expression.node() {
Node::Var(var) => return is_constant(var.bound_expression()),
Node::Compound(compound) if compound.overload().result == Type::Error => return false,
_ => {}
}
let is_type_annotation = matches!(expression.node(), Node::Coercion(_) | Node::Assertion(_));
let mut children_constant = true;
expression.each_child(&mut |child| {
if is_type_annotation {
children_constant = children_constant && is_constant(child);
} else {
children_constant = children_constant && child.literal_value().is_some();
}
});
children_constant && !expression.has(EVALUATION_INPUTS)
}
type ParseFunction = fn(&Convertible<'_>, &mut ParsingContext) -> ParseResult;
fn expression_registry(name: &str) -> Option<ParseFunction> {
Some(match name {
"==" | "!=" | ">" | "<" | ">=" | "<=" => parse_comparison,
"all" => parse_all,
"any" => parse_any,
"array" | "boolean" | "number" | "object" | "string" => Assertion::parse,
"at" => At::parse,
"in" => In::parse,
"case" => Case::parse,
"coalesce" => Coalesce::parse,
"collator" => CollatorExpression::parse,
"distance" => Distance::parse,
"format" => FormatExpression::parse,
"image" => ImageExpression::parse,
"interpolate" | "interpolate-hcl" | "interpolate-lab" | "interpolate-oklch" | "interpolate-oklab" => {
parse_interpolate
}
"length" => crate::length::parse,
"let" => Let::parse,
"literal" => Literal::parse,
"match" => parse_match,
"number-format" => NumberFormat::parse,
"step" => Step::parse,
"to-boolean" | "to-color" | "to-padding" | "to-number" | "to-string" => Coercion::parse,
"var" => Var::parse,
"within" => Within::parse,
"index-of" => IndexOf::parse,
"slice" => Slice::parse,
"semiliteral" => Semiliteral::parse,
_ => return None,
})
}
#[must_use]
pub(crate) fn is_special_form(name: &str) -> bool {
expression_registry(name).is_some()
}
impl ParsingContext {
#[must_use]
pub fn new(functions: &Arc<Functions>) -> Self {
ParsingContext {
key: String::new(),
expected: None,
scope: None,
errors: Rc::new(RefCell::new(Vec::new())),
functions: Arc::clone(functions),
}
}
#[must_use]
pub fn with_expected(functions: &Arc<Functions>, expected: Type) -> Self {
ParsingContext { expected: Some(expected), ..ParsingContext::new(functions) }
}
pub(crate) fn without_functions() -> Self {
ParsingContext::new(&Arc::new(Functions::new()))
}
pub(crate) fn detached(&self) -> Self {
ParsingContext { key: self.key.clone(), ..ParsingContext::new(&self.functions) }
}
fn child(&self, index: usize, expected: Option<Type>, scope: Option<Rc<Scope>>) -> Self {
ParsingContext {
key: format!("{}[{}]", self.key, index),
expected,
scope,
errors: Rc::clone(&self.errors),
functions: Arc::clone(&self.functions),
}
}
pub(crate) fn functions(&self) -> &Arc<Functions> {
&self.functions
}
#[must_use]
pub(crate) fn get_expected(&self) -> Option<Type> {
self.expected.clone()
}
#[must_use]
pub fn get_errors(&self) -> Vec<ParsingError> {
self.errors.borrow().clone()
}
#[must_use]
pub(crate) fn has_errors(&self) -> bool {
!self.errors.borrow().is_empty()
}
#[must_use]
pub fn get_combined_errors(&self) -> String {
self.errors.borrow().iter().map(ToString::to_string).collect::<Vec<_>>().join("\n")
}
pub fn parse_expression(
&mut self,
value: &Convertible<'_>,
type_annotation_option: Option<TypeAnnotationOption>,
) -> ParseResult {
self.parse_value(value, type_annotation_option)
}
pub fn parse_layer_property_expression(&mut self, value: &Convertible<'_>) -> ParseResult {
let type_annotation_option =
if self.expected.as_ref() == Some(&Type::String) { Some(TypeAnnotationOption::Coerce) } else { None };
let parsed = self.parse_value(value, type_annotation_option);
if let Some(p) = &parsed
&& p.has(Dependency::ZOOM)
{
match find_zoom_curve(p) {
Some(ZoomCurve::Error(err)) => {
self.error(err.message);
return None;
}
Some(_) => {}
None => {
self.error(
r#""zoom" expression may only be used as input to a top-level "step" or "interpolate" expression."#
.to_owned(),
);
return None;
}
}
}
parsed
}
pub(crate) fn parse(
&mut self,
value: &Convertible<'_>,
index: usize,
expected: Option<Type>,
type_annotation_option: Option<TypeAnnotationOption>,
) -> ParseResult {
let mut child = self.child(index, expected, self.scope.clone());
child.parse_value(value, type_annotation_option)
}
pub(crate) fn parse_with_bindings(
&mut self,
value: &Convertible<'_>,
index: usize,
expected: Option<Type>,
bindings: &Bindings,
) -> ParseResult {
let mut child = self.child(index, expected, Some(Rc::new(Scope::new(bindings.clone(), self.scope.clone()))));
child.parse_value(value, None)
}
pub(crate) fn check_type(&mut self, t: &Type) -> Option<String> {
let err = check_subtype(self.expected.as_ref()?, t);
if let Some(err) = &err {
self.error(err.clone());
}
err
}
#[must_use]
pub(crate) fn get_binding(&self, name: &str) -> Option<Arc<Expr>> {
self.scope.as_ref()?.get(name)
}
pub(crate) fn error(&mut self, message: String) {
self.errors.borrow_mut().push(ParsingError { message, key: self.key.clone() });
}
pub(crate) fn error_at(&mut self, message: String, child: usize) {
self.errors.borrow_mut().push(ParsingError { message, key: format!("{}[{}]", self.key, child) });
}
pub(crate) fn error_at2(&mut self, message: String, child: usize, grandchild: usize) {
self.errors
.borrow_mut()
.push(ParsingError { message, key: format!("{}[{}][{}]", self.key, child, grandchild) });
}
pub(crate) fn append_errors(&mut self, ctx: &ParsingContext) {
let mut theirs = ctx.errors.borrow_mut();
self.errors.borrow_mut().append(&mut theirs);
}
pub(crate) fn clear_errors(&mut self) {
self.errors.borrow_mut().clear();
}
fn parse_value(
&mut self,
value: &Convertible<'_>,
type_annotation_option: Option<TypeAnnotationOption>,
) -> ParseResult {
let parsed = if value.is_array() {
let length = value.array_length();
if length == 0 {
self.error(
r#"Expected an array with at least one element. If you wanted a literal array, use ["literal", []]."#
.to_owned(),
);
return None;
}
let Some(op) = value.array_member(0).to_string() else {
self.error_at(
format!(
r#"Expression name must be a string, but found {} instead. If you wanted a literal array, use ["literal", [...]]."#,
get_json_type(&value.array_member(0))
),
0,
);
return None;
};
if let Some(parse_function) = expression_registry(&op) {
parse_function(value, self)
} else {
parse_compound_expression(&op, value, self)
}
} else {
Literal::parse(value, self)
};
let Some(mut expr) = parsed else {
debug_assert!(!self.errors.borrow().is_empty());
return None;
};
let annotate = |expression: Expr, type_: Type, type_annotation: TypeAnnotationOption| -> Expr {
match type_annotation {
TypeAnnotationOption::Assert => Assertion::new_expr(type_, vec![expression]),
TypeAnnotationOption::Coerce => Coercion::new_expr(type_, vec![expression]),
TypeAnnotationOption::Omit => expression,
}
};
if let Some(expected) = self.expected.clone() {
let actual = expr.get_type();
if (expected == Type::String
|| expected == Type::Number
|| expected == Type::Boolean
|| expected == Type::Object
|| expected.is_array())
&& actual == Type::Value
{
expr = annotate(expr, expected, type_annotation_option.unwrap_or(TypeAnnotationOption::Assert));
} else if ((expected == Type::Color || expected == Type::Formatted || expected == Type::Image)
&& (actual == Type::Value || actual == Type::String))
|| (expected == Type::Padding && (actual == Type::Value || actual == Type::Number || actual.is_array()))
|| (expected == Type::ProjectionDefinition
&& (actual == Type::Value || actual == Type::String || actual.is_array()))
|| (expected == Type::VariableAnchorOffsetCollection && (actual == Type::Value || actual.is_array()))
{
expr = annotate(expr, expected, type_annotation_option.unwrap_or(TypeAnnotationOption::Coerce));
} else {
let t = expr.get_type();
self.check_type(&t);
if !self.errors.borrow().is_empty() {
return None;
}
}
}
if expr.literal_value().is_none() && expr.get_type() != Type::Image && is_constant(&expr) {
let params = EvaluationContext::from_feature(None);
let evaluated = expr.evaluate(¶ms);
let evaluated = match evaluated {
Ok(v) => v,
Err(e) => {
self.error(e.message().to_owned());
return None;
}
};
return Some(match (expr.get_type(), evaluated) {
(Type::Array(array), Value::Array(items)) => Literal::array(*array, items),
(_, evaluated) => Expr::literal(evaluated),
});
}
Some(expr)
}
}