Blame src/values.cpp

Packit Service 7770af
#include "sass.hpp"
Packit Service 7770af
#include "sass.h"
Packit Service 7770af
#include "values.hpp"
Packit Service 7770af
Packit Service 7770af
#include <stdint.h>
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  // convert value from C++ side to C-API
Packit Service 7770af
  union Sass_Value* ast_node_to_sass_value (const Expression_Ptr val)
Packit Service 7770af
  {
Packit Service 7770af
    if (val->concrete_type() == Expression::NUMBER)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr_Const res = Cast<Number>(val);
Packit Service 7770af
      return sass_make_number(res->value(), res->unit().c_str());
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::COLOR)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr_Const col = Cast<Color>(val);
Packit Service 7770af
      return sass_make_color(col->r(), col->g(), col->b(), col->a());
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::LIST)
Packit Service 7770af
    {
Packit Service 7770af
      List_Ptr_Const l = Cast<List>(val);
Packit Service 7770af
      union Sass_Value* list = sass_make_list(l->size(), l->separator());
Packit Service 7770af
      for (size_t i = 0, L = l->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj obj = l->at(i);
Packit Service 7770af
        auto val = ast_node_to_sass_value(obj);
Packit Service 7770af
        sass_list_set_value(list, i, val);
Packit Service 7770af
      }
Packit Service 7770af
      return list;
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::MAP)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Ptr_Const m = Cast<Map>(val);
Packit Service 7770af
      union Sass_Value* map = sass_make_map(m->length());
Packit Service 7770af
      size_t i = 0; for (Expression_Obj key : m->keys()) {
Packit Service 7770af
        sass_map_set_key(map, i, ast_node_to_sass_value(key));
Packit Service 7770af
        sass_map_set_value(map, i, ast_node_to_sass_value(m->at(key)));
Packit Service 7770af
        ++ i;
Packit Service 7770af
      }
Packit Service 7770af
      return map;
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::NULL_VAL)
Packit Service 7770af
    {
Packit Service 7770af
      return sass_make_null();
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::BOOLEAN)
Packit Service 7770af
    {
Packit Service 7770af
      Boolean_Ptr_Const res = Cast<Boolean>(val);
Packit Service 7770af
      return sass_make_boolean(res->value());
Packit Service 7770af
    }
Packit Service 7770af
    else if (val->concrete_type() == Expression::STRING)
Packit Service 7770af
    {
Packit Service 7770af
      if (String_Quoted_Ptr_Const qstr = Cast<String_Quoted>(val))
Packit Service 7770af
      {
Packit Service 7770af
        return sass_make_qstring(qstr->value().c_str());
Packit Service 7770af
      }
Packit Service 7770af
      else if (String_Constant_Ptr_Const cstr = Cast<String_Constant>(val))
Packit Service 7770af
      {
Packit Service 7770af
        return sass_make_string(cstr->value().c_str());
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    return sass_make_error("unknown sass value type");
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // convert value from C-API to C++ side
Packit Service 7770af
  Value_Ptr sass_value_to_ast_node (const union Sass_Value* val)
Packit Service 7770af
  {
Packit Service 7770af
    switch (sass_value_get_tag(val)) {
Packit Service 7770af
      case SASS_NUMBER:
Packit Service 7770af
        return SASS_MEMORY_NEW(Number,
Packit Service 7770af
                               ParserState("[C-VALUE]"),
Packit Service 7770af
                               sass_number_get_value(val),
Packit Service 7770af
                               sass_number_get_unit(val));
Packit Service 7770af
      case SASS_BOOLEAN:
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean,
Packit Service 7770af
                               ParserState("[C-VALUE]"),
Packit Service 7770af
                               sass_boolean_get_value(val));
Packit Service 7770af
      case SASS_COLOR:
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               ParserState("[C-VALUE]"),
Packit Service 7770af
                               sass_color_get_r(val),
Packit Service 7770af
                               sass_color_get_g(val),
Packit Service 7770af
                               sass_color_get_b(val),
Packit Service 7770af
                               sass_color_get_a(val));
Packit Service 7770af
      case SASS_STRING:
Packit Service 7770af
        if (sass_string_is_quoted(val)) {
Packit Service 7770af
          return SASS_MEMORY_NEW(String_Quoted,
Packit Service 7770af
                                 ParserState("[C-VALUE]"),
Packit Service 7770af
                                 sass_string_get_value(val));
Packit Service 7770af
        }
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Constant,
Packit Service 7770af
                                 ParserState("[C-VALUE]"),
Packit Service 7770af
                                 sass_string_get_value(val));
Packit Service 7770af
      case SASS_LIST: {
Packit Service 7770af
        List_Ptr l = SASS_MEMORY_NEW(List,
Packit Service 7770af
                                  ParserState("[C-VALUE]"),
Packit Service 7770af
                                  sass_list_get_length(val),
Packit Service 7770af
                                  sass_list_get_separator(val));
Packit Service 7770af
        for (size_t i = 0, L = sass_list_get_length(val); i < L; ++i) {
Packit Service 7770af
          l->append(sass_value_to_ast_node(sass_list_get_value(val, i)));
Packit Service 7770af
        }
Packit Service 7770af
        return l;
Packit Service 7770af
      }
Packit Service 7770af
      case SASS_MAP: {
Packit Service 7770af
        Map_Ptr m = SASS_MEMORY_NEW(Map, ParserState("[C-VALUE]"));
Packit Service 7770af
        for (size_t i = 0, L = sass_map_get_length(val); i < L; ++i) {
Packit Service 7770af
          *m << std::make_pair(
Packit Service 7770af
            sass_value_to_ast_node(sass_map_get_key(val, i)),
Packit Service 7770af
            sass_value_to_ast_node(sass_map_get_value(val, i)));
Packit Service 7770af
        }
Packit Service 7770af
        return m;
Packit Service 7770af
      }
Packit Service 7770af
      case SASS_NULL:
Packit Service 7770af
        return SASS_MEMORY_NEW(Null, ParserState("[C-VALUE]"));
Packit Service 7770af
      case SASS_ERROR:
Packit Service 7770af
        return SASS_MEMORY_NEW(Custom_Error,
Packit Service 7770af
                               ParserState("[C-VALUE]"),
Packit Service 7770af
                               sass_error_get_message(val));
Packit Service 7770af
      case SASS_WARNING:
Packit Service 7770af
        return SASS_MEMORY_NEW(Custom_Warning,
Packit Service 7770af
                               ParserState("[C-VALUE]"),
Packit Service 7770af
                               sass_warning_get_message(val));
Packit Service 7770af
      default: break;
Packit Service 7770af
    }
Packit Service 7770af
    return 0;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
}