Blame src/bind.cpp

Packit Service 7770af
#include "sass.hpp"
Packit Service 7770af
#include "bind.hpp"
Packit Service 7770af
#include "ast.hpp"
Packit Service 7770af
#include "context.hpp"
Packit Service 7770af
#include "eval.hpp"
Packit Service 7770af
#include <map>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <sstream>
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  void bind(std::string type, std::string name, Parameters_Obj ps, Arguments_Obj as, Context* ctx, Env* env, Eval* eval)
Packit Service 7770af
  {
Packit Service 7770af
    std::string callee(type + " " + name);
Packit Service 7770af
Packit Service 7770af
    std::map<std::string, Parameter_Obj> param_map;
Packit Service 7770af
    List_Obj varargs = SASS_MEMORY_NEW(List, as->pstate());
Packit Service 7770af
    varargs->is_arglist(true); // enable keyword size handling
Packit Service 7770af
Packit Service 7770af
    for (size_t i = 0, L = as->length(); i < L; ++i) {
Packit Service 7770af
      if (auto str = Cast<String_Quoted>((*as)[i]->value())) {
Packit Service 7770af
        // force optional quotes (only if needed)
Packit Service 7770af
        if (str->quote_mark()) {
Packit Service 7770af
          str->quote_mark('*');
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // Set up a map to ensure named arguments refer to actual parameters. Also
Packit Service 7770af
    // eval each default value left-to-right, wrt env, populating env as we go.
Packit Service 7770af
    for (size_t i = 0, L = ps->length(); i < L; ++i) {
Packit Service 7770af
      Parameter_Obj  p = ps->at(i);
Packit Service 7770af
      param_map[p->name()] = p;
Packit Service 7770af
      // if (p->default_value()) {
Packit Service 7770af
      //   env->local_frame()[p->name()] = p->default_value()->perform(eval->with(env));
Packit Service 7770af
      // }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // plug in all args; if we have leftover params, deal with it later
Packit Service 7770af
    size_t ip = 0, LP = ps->length();
Packit Service 7770af
    size_t ia = 0, LA = as->length();
Packit Service 7770af
    while (ia < LA) {
Packit Service 7770af
      Argument_Obj a = as->at(ia);
Packit Service 7770af
      if (ip >= LP) {
Packit Service 7770af
        // skip empty rest arguments
Packit Service 7770af
        if (a->is_rest_argument()) {
Packit Service 7770af
          if (List_Obj l = Cast<List>(a->value())) {
Packit Service 7770af
            if (l->length() == 0) {
Packit Service 7770af
              ++ ia; continue;
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        std::stringstream msg;
Packit Service 7770af
        msg << "wrong number of arguments (" << LA << " for " << LP << ")";
Packit Service 7770af
        msg << " for `" << name << "'";
Packit Service 7770af
        return error(msg.str(), as->pstate());
Packit Service 7770af
      }
Packit Service 7770af
      Parameter_Obj p = ps->at(ip);
Packit Service 7770af
Packit Service 7770af
      // If the current parameter is the rest parameter, process and break the loop
Packit Service 7770af
      if (p->is_rest_parameter()) {
Packit Service 7770af
        // The next argument by coincidence provides a rest argument
Packit Service 7770af
        if (a->is_rest_argument()) {
Packit Service 7770af
Packit Service 7770af
          // We should always get a list for rest arguments
Packit Service 7770af
          if (List_Obj rest = Cast<List>(a->value())) {
Packit Service 7770af
              // create a new list object for wrapped items
Packit Service 7770af
              List_Ptr arglist = SASS_MEMORY_NEW(List,
Packit Service 7770af
                                              p->pstate(),
Packit Service 7770af
                                              0,
Packit Service 7770af
                                              rest->separator(),
Packit Service 7770af
                                              true);
Packit Service 7770af
              // wrap each item from list as an argument
Packit Service 7770af
              for (Expression_Obj item : rest->elements()) {
Packit Service 7770af
                if (Argument_Obj arg = Cast<Argument>(item)) {
Packit Service 7770af
                  arglist->append(SASS_MEMORY_COPY(arg)); // copy
Packit Service 7770af
                } else {
Packit Service 7770af
                  arglist->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                                  item->pstate(),
Packit Service 7770af
                                                  item,
Packit Service 7770af
                                                  "",
Packit Service 7770af
                                                  false,
Packit Service 7770af
                                                  false));
Packit Service 7770af
                }
Packit Service 7770af
              }
Packit Service 7770af
              // assign new arglist to environment
Packit Service 7770af
              env->local_frame()[p->name()] = arglist;
Packit Service 7770af
            }
Packit Service 7770af
          // invalid state
Packit Service 7770af
          else {
Packit Service 7770af
            throw std::runtime_error("invalid state");
Packit Service 7770af
          }
Packit Service 7770af
        } else if (a->is_keyword_argument()) {
Packit Service 7770af
Packit Service 7770af
          // expand keyword arguments into their parameters
Packit Service 7770af
          List_Ptr arglist = SASS_MEMORY_NEW(List, p->pstate(), 0, SASS_COMMA, true);
Packit Service 7770af
          env->local_frame()[p->name()] = arglist;
Packit Service 7770af
          Map_Obj argmap = Cast<Map>(a->value());
Packit Service 7770af
          for (auto key : argmap->keys()) {
Packit Service 7770af
            if (String_Constant_Obj str = Cast<String_Constant>(key)) {
Packit Service 7770af
              std::string param = unquote(str->value());
Packit Service 7770af
              arglist->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                              key->pstate(),
Packit Service 7770af
                                              argmap->at(key),
Packit Service 7770af
                                              "$" + param,
Packit Service 7770af
                                              false,
Packit Service 7770af
                                              false));
Packit Service 7770af
            } else {
Packit Service 7770af
              throw Exception::InvalidVarKwdType(key->pstate(), key->inspect(), a);
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
Packit Service 7770af
        } else {
Packit Service 7770af
Packit Service 7770af
          // create a new list object for wrapped items
Packit Service 7770af
          List_Obj arglist = SASS_MEMORY_NEW(List,
Packit Service 7770af
                                          p->pstate(),
Packit Service 7770af
                                          0,
Packit Service 7770af
                                          SASS_COMMA,
Packit Service 7770af
                                          true);
Packit Service 7770af
          // consume the next args
Packit Service 7770af
          while (ia < LA) {
Packit Service 7770af
            // get and post inc
Packit Service 7770af
            a = (*as)[ia++];
Packit Service 7770af
            // maybe we have another list as argument
Packit Service 7770af
            List_Obj ls = Cast<List>(a->value());
Packit Service 7770af
            // skip any list completely if empty
Packit Service 7770af
            if (ls && ls->empty() && a->is_rest_argument()) continue;
Packit Service 7770af
Packit Service 7770af
            Expression_Obj value = a->value();
Packit Service 7770af
            if (Argument_Obj arg = Cast<Argument>(value)) {
Packit Service 7770af
              arglist->append(arg);
Packit Service 7770af
            }
Packit Service 7770af
            // check if we have rest argument
Packit Service 7770af
            else if (a->is_rest_argument()) {
Packit Service 7770af
              // preserve the list separator from rest args
Packit Service 7770af
              if (List_Obj rest = Cast<List>(a->value())) {
Packit Service 7770af
                arglist->separator(rest->separator());
Packit Service 7770af
Packit Service 7770af
                for (size_t i = 0, L = rest->size(); i < L; ++i) {
Packit Service 7770af
                  Expression_Obj obj = rest->at(i);
Packit Service 7770af
                  arglist->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                                obj->pstate(),
Packit Service 7770af
                                                obj,
Packit Service 7770af
                                                "",
Packit Service 7770af
                                                false,
Packit Service 7770af
                                                false));
Packit Service 7770af
                }
Packit Service 7770af
              }
Packit Service 7770af
              // no more arguments
Packit Service 7770af
              break;
Packit Service 7770af
            }
Packit Service 7770af
            // wrap all other value types into Argument
Packit Service 7770af
            else {
Packit Service 7770af
              arglist->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                            a->pstate(),
Packit Service 7770af
                                            a->value(),
Packit Service 7770af
                                            a->name(),
Packit Service 7770af
                                            false,
Packit Service 7770af
                                            false));
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
          // assign new arglist to environment
Packit Service 7770af
          env->local_frame()[p->name()] = arglist;
Packit Service 7770af
        }
Packit Service 7770af
        // consumed parameter
Packit Service 7770af
        ++ip;
Packit Service 7770af
        // no more paramaters
Packit Service 7770af
        break;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // If the current argument is the rest argument, extract a value for processing
Packit Service 7770af
      else if (a->is_rest_argument()) {
Packit Service 7770af
        // normal param and rest arg
Packit Service 7770af
        List_Obj arglist = Cast<List>(a->value());
Packit Service 7770af
        if (!arglist) {
Packit Service 7770af
          if (Expression_Obj arg = Cast<Expression>(a->value())) {
Packit Service 7770af
            arglist = SASS_MEMORY_NEW(List, a->pstate(), 1);
Packit Service 7770af
            arglist->append(arg);
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        // empty rest arg - treat all args as default values
Packit Service 7770af
        if (!arglist || !arglist->length()) {
Packit Service 7770af
          break;
Packit Service 7770af
        } else {
Packit Service 7770af
          if (arglist->length() > LP - ip && !ps->has_rest_parameter()) {
Packit Service 7770af
            size_t arg_count = (arglist->length() + LA - 1);
Packit Service 7770af
            std::stringstream msg;
Packit Service 7770af
            msg << callee << " takes " << LP;
Packit Service 7770af
            msg << (LP == 1 ? " argument" : " arguments");
Packit Service 7770af
            msg << " but " << arg_count;
Packit Service 7770af
            msg << (arg_count == 1 ? " was passed" : " were passed.");
Packit Service 7770af
            deprecated_bind(msg.str(), as->pstate());
Packit Service 7770af
Packit Service 7770af
            while (arglist->length() > LP - ip) {
Packit Service 7770af
              arglist->elements().erase(arglist->elements().end() - 1);
Packit Service 7770af
            }
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        // otherwise move one of the rest args into the param, converting to argument if necessary
Packit Service 7770af
        Expression_Obj obj = arglist->at(0);
Packit Service 7770af
        if (!(a = Cast<Argument>(obj))) {
Packit Service 7770af
          Expression_Ptr a_to_convert = obj;
Packit Service 7770af
          a = SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                              a_to_convert->pstate(),
Packit Service 7770af
                              a_to_convert,
Packit Service 7770af
                              "",
Packit Service 7770af
                              false,
Packit Service 7770af
                              false);
Packit Service 7770af
        }
Packit Service 7770af
        arglist->elements().erase(arglist->elements().begin());
Packit Service 7770af
        if (!arglist->length() || (!arglist->is_arglist() && ip + 1 == LP)) {
Packit Service 7770af
          ++ia;
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
      } else if (a->is_keyword_argument()) {
Packit Service 7770af
        Map_Obj argmap = Cast<Map>(a->value());
Packit Service 7770af
Packit Service 7770af
        for (auto key : argmap->keys()) {
Packit Service 7770af
          std::string param = "$" + unquote(Cast<String_Constant>(key)->value());
Packit Service 7770af
Packit Service 7770af
          if (!param_map.count(param)) {
Packit Service 7770af
            std::stringstream msg;
Packit Service 7770af
            msg << callee << " has no parameter named " << param;
Packit Service 7770af
            error(msg.str(), a->pstate());
Packit Service 7770af
          }
Packit Service 7770af
          env->local_frame()[param] = argmap->at(key);
Packit Service 7770af
        }
Packit Service 7770af
        ++ia;
Packit Service 7770af
        continue;
Packit Service 7770af
      } else {
Packit Service 7770af
        ++ia;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      if (a->name().empty()) {
Packit Service 7770af
        if (env->has_local(p->name())) {
Packit Service 7770af
          std::stringstream msg;
Packit Service 7770af
          msg << "parameter " << p->name()
Packit Service 7770af
          << " provided more than once in call to " << callee;
Packit Service 7770af
          error(msg.str(), a->pstate());
Packit Service 7770af
        }
Packit Service 7770af
        // ordinal arg -- bind it to the next param
Packit Service 7770af
        env->local_frame()[p->name()] = a->value();
Packit Service 7770af
        ++ip;
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        // named arg -- bind it to the appropriately named param
Packit Service 7770af
        if (!param_map.count(a->name())) {
Packit Service 7770af
          if (ps->has_rest_parameter()) {
Packit Service 7770af
            varargs->append(a);
Packit Service 7770af
          } else {
Packit Service 7770af
            std::stringstream msg;
Packit Service 7770af
            msg << callee << " has no parameter named " << a->name();
Packit Service 7770af
            error(msg.str(), a->pstate());
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        if (param_map[a->name()]) {
Packit Service 7770af
          if (param_map[a->name()]->is_rest_parameter()) {
Packit Service 7770af
            std::stringstream msg;
Packit Service 7770af
            msg << "argument " << a->name() << " of " << callee
Packit Service 7770af
                << "cannot be used as named argument";
Packit Service 7770af
            error(msg.str(), a->pstate());
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        if (env->has_local(a->name())) {
Packit Service 7770af
          std::stringstream msg;
Packit Service 7770af
          msg << "parameter " << p->name()
Packit Service 7770af
              << "provided more than once in call to " << callee;
Packit Service 7770af
          error(msg.str(), a->pstate());
Packit Service 7770af
        }
Packit Service 7770af
        env->local_frame()[a->name()] = a->value();
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
    // EO while ia
Packit Service 7770af
Packit Service 7770af
    // If we make it here, we're out of args but may have leftover params.
Packit Service 7770af
    // That's only okay if they have default values, or were already bound by
Packit Service 7770af
    // named arguments, or if it's a single rest-param.
Packit Service 7770af
    for (size_t i = ip; i < LP; ++i) {
Packit Service 7770af
      Parameter_Obj leftover = ps->at(i);
Packit Service 7770af
      // cerr << "env for default params:" << endl;
Packit Service 7770af
      // env->print();
Packit Service 7770af
      // cerr << "********" << endl;
Packit Service 7770af
      if (!env->has_local(leftover->name())) {
Packit Service 7770af
        if (leftover->is_rest_parameter()) {
Packit Service 7770af
          env->local_frame()[leftover->name()] = varargs;
Packit Service 7770af
        }
Packit Service 7770af
        else if (leftover->default_value()) {
Packit Service 7770af
          Expression_Ptr dv = leftover->default_value()->perform(eval);
Packit Service 7770af
          env->local_frame()[leftover->name()] = dv;
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          // param is unbound and has no default value -- error
Packit Service 7770af
          throw Exception::MissingArgument(as->pstate(), name, leftover->name(), type);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    return;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
}