Ninja
eval_env.cc
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 #include "eval_env.h"
16 
17 string BindingEnv::LookupVariable(const string& var) {
18  map<string, string>::iterator i = bindings_.find(var);
19  if (i != bindings_.end())
20  return i->second;
21  if (parent_)
22  return parent_->LookupVariable(var);
23  return "";
24 }
25 
26 void BindingEnv::AddBinding(const string& key, const string& val) {
27  bindings_[key] = val;
28 }
29 
30 string BindingEnv::LookupWithFallback(const string& var,
31  const EvalString* eval,
32  Env* env) {
33  map<string, string>::iterator i = bindings_.find(var);
34  if (i != bindings_.end())
35  return i->second;
36 
37  if (eval)
38  return eval->Evaluate(env);
39 
40  if (parent_)
41  return parent_->LookupVariable(var);
42 
43  return "";
44 }
45 
46 string EvalString::Evaluate(Env* env) const {
47  string result;
48  for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
49  if (i->second == RAW)
50  result.append(i->first);
51  else
52  result.append(env->LookupVariable(i->first));
53  }
54  return result;
55 }
56 
58  // Add it to the end of an existing RAW token if possible.
59  if (!parsed_.empty() && parsed_.back().second == RAW) {
60  parsed_.back().first.append(text.str_, text.len_);
61  } else {
62  parsed_.push_back(make_pair(text.AsString(), RAW));
63  }
64 }
66  parsed_.push_back(make_pair(text.AsString(), SPECIAL));
67 }
68 
69 string EvalString::Serialize() const {
70  string result;
71  for (TokenList::const_iterator i = parsed_.begin();
72  i != parsed_.end(); ++i) {
73  result.append("[");
74  if (i->second == SPECIAL)
75  result.append("$");
76  result.append(i->first);
77  result.append("]");
78  }
79  return result;
80 }
Env * parent_
Definition: eval_env.h:54
const char * str_
Definition: string_piece.h:49
map< string, string > bindings_
Definition: eval_env.h:53
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
string AsString() const
Convert the slice into a full-fledged std::string, copying the data into a new string.
Definition: string_piece.h:45
void AddSpecial(StringPiece text)
Definition: eval_env.cc:65
string Serialize() const
Construct a human-readable representation of the parsed state, for use in tests.
Definition: eval_env.cc:69
virtual string LookupVariable(const string &var)=0
virtual string LookupVariable(const string &var)
Definition: eval_env.cc:17
string LookupWithFallback(const string &var, const EvalString *eval, Env *env)
This is tricky.
Definition: eval_env.cc:30
void AddBinding(const string &key, const string &val)
Definition: eval_env.cc:26
void AddText(StringPiece text)
Definition: eval_env.cc:57
size_t len_
Definition: string_piece.h:50
string Evaluate(Env *env) const
Definition: eval_env.cc:46
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