Ninja
eval_env.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_EVAL_ENV_H_
16 #define NINJA_EVAL_ENV_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 using namespace std;
22 
23 #include "string_piece.h"
24 
25 struct EvalString;
26 
27 /// An interface for a scope for variable (e.g. "$foo") lookups.
28 struct Env {
29  virtual ~Env() {}
30  virtual string LookupVariable(const string& var) = 0;
31 };
32 
33 /// An Env which contains a mapping of variables to values
34 /// as well as a pointer to a parent scope.
35 struct BindingEnv : public Env {
36  BindingEnv() : parent_(NULL) {}
37  explicit BindingEnv(Env* parent) : parent_(parent) {}
38 
39  virtual ~BindingEnv() {}
40  virtual string LookupVariable(const string& var);
41 
42  void AddBinding(const string& key, const string& val);
43 
44  /// This is tricky. Edges want lookup scope to go in this order:
45  /// 1) value set on edge itself (edge_->env_)
46  /// 2) value set on rule, with expansion in the edge's scope
47  /// 3) value set on enclosing scope of edge (edge_->env_->parent_)
48  /// This function takes as parameters the necessary info to do (2).
49  string LookupWithFallback(const string& var, const EvalString* eval,
50  Env* env);
51 
52 private:
53  map<string, string> bindings_;
55 };
56 
57 /// A tokenized string that contains variable references.
58 /// Can be evaluated relative to an Env.
59 struct EvalString {
60  string Evaluate(Env* env) const;
61 
62  void Clear() { parsed_.clear(); }
63  bool empty() const { return parsed_.empty(); }
64 
65  void AddText(StringPiece text);
66  void AddSpecial(StringPiece text);
67 
68  /// Construct a human-readable representation of the parsed state,
69  /// for use in tests.
70  string Serialize() const;
71 
72 private:
73  enum TokenType { RAW, SPECIAL };
74  typedef vector<pair<string, TokenType> > TokenList;
76 };
77 
78 #endif // NINJA_EVAL_ENV_H_
Env * parent_
Definition: eval_env.h:54
vector< pair< string, TokenType > > TokenList
Definition: eval_env.h:74
map< string, string > bindings_
Definition: eval_env.h:53
BindingEnv()
Definition: eval_env.h:36
void Clear()
Definition: eval_env.h:62
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
virtual ~BindingEnv()
Definition: eval_env.h:39
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:35
BindingEnv(Env *parent)
Definition: eval_env.h:37
virtual ~Env()
Definition: eval_env.h:29
bool empty() const
Definition: eval_env.h:63
A tokenized string that contains variable references.
Definition: eval_env.h:59
An interface for a scope for variable (e.g. "$foo") lookups.
Definition: eval_env.h:28
TokenList parsed_
Definition: eval_env.h:75