Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / environment.hpp
1 #ifndef SASS_ENVIRONMENT_H
2 #define SASS_ENVIRONMENT_H
3
4 #include <string>
5 #include <map>
6
7 #include "ast_fwd_decl.hpp"
8 #include "ast_def_macros.hpp"
9
10 namespace Sass {
11
12   template <typename T>
13   class Environment {
14     // TODO: test with map
15     std::map<std::string, T> local_frame_;
16     ADD_PROPERTY(Environment*, parent)
17     ADD_PROPERTY(bool, is_shadow)
18
19   public:
20     Environment(bool is_shadow = false);
21     Environment(Environment* env, bool is_shadow = false);
22     Environment(Environment& env, bool is_shadow = false);
23
24     // link parent to create a stack
25     void link(Environment& env);
26     void link(Environment* env);
27
28     // this is used to find the global frame
29     // which is the second last on the stack
30     bool is_lexical() const;
31
32     // only match the real root scope
33     // there is still a parent around
34     // not sure what it is actually use for
35     // I guess we store functions etc. there
36     bool is_global() const;
37
38     // scope operates on the current frame
39
40     std::map<std::string, T>& local_frame();
41
42     bool has_local(const std::string& key) const;
43
44     T& get_local(const std::string& key);
45
46     // set variable on the current frame
47     void set_local(const std::string& key, T val);
48
49     void del_local(const std::string& key);
50
51     // global operates on the global frame
52     // which is the second last on the stack
53     Environment* global_env();
54     // get the env where the variable already exists
55     // if it does not yet exist, we return current env
56     Environment* lexical_env(const std::string& key);
57
58     bool has_global(const std::string& key);
59
60     T& get_global(const std::string& key);
61
62     // set a variable on the global frame
63     void set_global(const std::string& key, T val);
64
65     void del_global(const std::string& key);
66
67     // see if we have a lexical variable
68     // move down the stack but stop before we
69     // reach the global frame (is not included)
70     bool has_lexical(const std::string& key) const;
71
72     // see if we have a lexical we could update
73     // either update already existing lexical value
74     // or we create a new one on the current frame
75     void set_lexical(const std::string& key, T val);
76
77     // look on the full stack for key
78     // include all scopes available
79     bool has(const std::string& key) const;
80
81     // use array access for getter and setter functions
82     T& operator[](const std::string& key);
83
84     #ifdef DEBUG
85     size_t print(std::string prefix = "");
86     #endif
87
88   };
89
90   // define typedef for our use case
91   typedef Environment<AST_Node_Obj> Env;
92
93 }
94
95 #endif