Blame src/environment.hpp

Packit Service 7770af
#ifndef SASS_ENVIRONMENT_H
Packit Service 7770af
#define SASS_ENVIRONMENT_H
Packit Service 7770af
Packit Service 7770af
#include <string>
Packit Service 7770af
#include "ast_fwd_decl.hpp"
Packit Service 7770af
#include "ast_def_macros.hpp"
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  typedef environment_map<std::string, AST_Node_Obj>::iterator EnvIter;
Packit Service 7770af
Packit Service 7770af
  class EnvResult {
Packit Service 7770af
    public:
Packit Service 7770af
      EnvIter it;
Packit Service 7770af
      bool found;
Packit Service 7770af
    public:
Packit Service 7770af
      EnvResult(EnvIter it, bool found)
Packit Service 7770af
      : it(it), found(found) {}
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  template <typename T>
Packit Service 7770af
  class Environment {
Packit Service 7770af
    // TODO: test with map
Packit Service 7770af
    environment_map<std::string, T> local_frame_;
Packit Service 7770af
    ADD_PROPERTY(Environment*, parent)
Packit Service 7770af
    ADD_PROPERTY(bool, is_shadow)
Packit Service 7770af
Packit Service 7770af
  public:
Packit Service 7770af
    Environment(bool is_shadow = false);
Packit Service 7770af
    Environment(Environment* env, bool is_shadow = false);
Packit Service 7770af
    Environment(Environment& env, bool is_shadow = false);
Packit Service 7770af
Packit Service 7770af
    // link parent to create a stack
Packit Service 7770af
    void link(Environment& env);
Packit Service 7770af
    void link(Environment* env);
Packit Service 7770af
Packit Service 7770af
    // this is used to find the global frame
Packit Service 7770af
    // which is the second last on the stack
Packit Service 7770af
    bool is_lexical() const;
Packit Service 7770af
Packit Service 7770af
    // only match the real root scope
Packit Service 7770af
    // there is still a parent around
Packit Service 7770af
    // not sure what it is actually use for
Packit Service 7770af
    // I guess we store functions etc. there
Packit Service 7770af
    bool is_global() const;
Packit Service 7770af
Packit Service 7770af
    // scope operates on the current frame
Packit Service 7770af
Packit Service 7770af
    environment_map<std::string, T>& local_frame();
Packit Service 7770af
Packit Service 7770af
    bool has_local(const std::string& key) const;
Packit Service 7770af
Packit Service 7770af
    EnvResult find_local(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    T& get_local(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    // set variable on the current frame
Packit Service 7770af
    void set_local(const std::string& key, const T& val);
Packit Service 7770af
    void set_local(const std::string& key, T&& val);
Packit Service 7770af
Packit Service 7770af
    void del_local(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    // global operates on the global frame
Packit Service 7770af
    // which is the second last on the stack
Packit Service 7770af
    Environment* global_env();
Packit Service 7770af
    // get the env where the variable already exists
Packit Service 7770af
    // if it does not yet exist, we return current env
Packit Service 7770af
    Environment* lexical_env(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    bool has_global(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    T& get_global(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    // set a variable on the global frame
Packit Service 7770af
    void set_global(const std::string& key, const T& val);
Packit Service 7770af
    void set_global(const std::string& key, T&& val);
Packit Service 7770af
Packit Service 7770af
    void del_global(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    // see if we have a lexical variable
Packit Service 7770af
    // move down the stack but stop before we
Packit Service 7770af
    // reach the global frame (is not included)
Packit Service 7770af
    bool has_lexical(const std::string& key) const;
Packit Service 7770af
Packit Service 7770af
    // see if we have a lexical we could update
Packit Service 7770af
    // either update already existing lexical value
Packit Service 7770af
    // or we create a new one on the current frame
Packit Service 7770af
    void set_lexical(const std::string& key, T&& val);
Packit Service 7770af
    void set_lexical(const std::string& key, const T& val);
Packit Service 7770af
Packit Service 7770af
    // look on the full stack for key
Packit Service 7770af
    // include all scopes available
Packit Service 7770af
    bool has(const std::string& key) const;
Packit Service 7770af
Packit Service 7770af
    // look on the full stack for key
Packit Service 7770af
    // include all scopes available
Packit Service 7770af
    EnvResult find(const std::string& key);
Packit Service 7770af
Packit Service 7770af
    // use array access for getter and setter functions
Packit Service 7770af
    T& operator[](const std::string& key);
Packit Service 7770af
Packit Service 7770af
    #ifdef DEBUG
Packit Service 7770af
    size_t print(std::string prefix = "");
Packit Service 7770af
    #endif
Packit Service 7770af
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  // define typedef for our use case
Packit Service 7770af
  typedef Environment<AST_Node_Obj> Env;
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
#endif