1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "manifest_parser.h"
27 ManifestParser::ManifestParser(State* state, FileReader* file_reader)
28 : state_(state), file_reader_(file_reader) {
29 env_ = &state->bindings_;
32 bool ManifestParser::Load(const string& filename, string* err, Lexer* parent) {
33 METRIC_RECORD(".ninja parse");
36 if (!file_reader_->ReadFile(filename, &contents, &read_err)) {
37 *err = "loading '" + filename + "': " + read_err;
39 parent->Error(string(*err), err);
43 // The lexer needs a nul byte at the end of its input, to know when it's done.
44 // It takes a StringPiece, and StringPiece's string constructor uses
45 // string::data(). data()'s return value isn't guaranteed to be
46 // null-terminated (although in practice - libc++, libstdc++, msvc's stl --
47 // it is, and C++11 demands that too), so add an explicit nul byte.
48 contents.resize(contents.size() + 1);
50 return Parse(filename, contents, err);
53 bool ManifestParser::Parse(const string& filename, const string& input,
55 lexer_.Start(filename, input);
58 Lexer::Token token = lexer_.ReadToken();
73 if (!ParseDefault(err))
80 if (!ParseLet(&name, &let_value, err))
82 string value = let_value.Evaluate(env_);
83 // Check ninja_required_version immediately so we can exit
84 // before encountering any syntactic surprises.
85 if (name == "ninja_required_version")
86 CheckNinjaVersion(value);
87 env_->AddBinding(name, value);
91 if (!ParseFileInclude(false, err))
95 if (!ParseFileInclude(true, err))
99 return lexer_.Error(lexer_.DescribeLastError(), err);
106 return lexer_.Error(string("unexpected ") + Lexer::TokenName(token),
110 return false; // not reached
114 bool ManifestParser::ParsePool(string* err) {
116 if (!lexer_.ReadIdent(&name))
117 return lexer_.Error("expected pool name", err);
119 if (!ExpectToken(Lexer::NEWLINE, err))
122 if (state_->LookupPool(name) != NULL)
123 return lexer_.Error("duplicate pool '" + name + "'", err);
127 while (lexer_.PeekToken(Lexer::INDENT)) {
130 if (!ParseLet(&key, &value, err))
133 if (key == "depth") {
134 string depth_string = value.Evaluate(env_);
135 depth = atol(depth_string.c_str());
137 return lexer_.Error("invalid pool depth", err);
139 return lexer_.Error("unexpected variable '" + key + "'", err);
144 return lexer_.Error("expected 'depth =' line", err);
146 state_->AddPool(new Pool(name, depth));
151 bool ManifestParser::ParseRule(string* err) {
153 if (!lexer_.ReadIdent(&name))
154 return lexer_.Error("expected rule name", err);
156 if (!ExpectToken(Lexer::NEWLINE, err))
159 if (env_->LookupRuleCurrentScope(name) != NULL)
160 return lexer_.Error("duplicate rule '" + name + "'", err);
162 Rule* rule = new Rule(name); // XXX scoped_ptr
164 while (lexer_.PeekToken(Lexer::INDENT)) {
167 if (!ParseLet(&key, &value, err))
170 if (Rule::IsReservedBinding(key)) {
171 rule->AddBinding(key, value);
173 // Die on other keyvals for now; revisit if we want to add a
175 return lexer_.Error("unexpected variable '" + key + "'", err);
179 if (rule->bindings_["rspfile"].empty() !=
180 rule->bindings_["rspfile_content"].empty()) {
181 return lexer_.Error("rspfile and rspfile_content need to be "
182 "both specified", err);
185 if (rule->bindings_["command"].empty())
186 return lexer_.Error("expected 'command =' line", err);
192 bool ManifestParser::ParseLet(string* key, EvalString* value, string* err) {
193 if (!lexer_.ReadIdent(key))
194 return lexer_.Error("expected variable name", err);
195 if (!ExpectToken(Lexer::EQUALS, err))
197 if (!lexer_.ReadVarValue(value, err))
202 bool ManifestParser::ParseDefault(string* err) {
204 if (!lexer_.ReadPath(&eval, err))
207 return lexer_.Error("expected target name", err);
210 string path = eval.Evaluate(env_);
212 unsigned int slash_bits; // Unused because this only does lookup.
213 if (!CanonicalizePath(&path, &slash_bits, &path_err))
214 return lexer_.Error(path_err, err);
215 if (!state_->AddDefault(path, &path_err))
216 return lexer_.Error(path_err, err);
219 if (!lexer_.ReadPath(&eval, err))
221 } while (!eval.empty());
223 if (!ExpectToken(Lexer::NEWLINE, err))
229 bool ManifestParser::ParseEdge(string* err) {
230 vector<EvalString> ins, outs;
234 if (!lexer_.ReadPath(&out, err))
237 return lexer_.Error("expected path", err);
243 if (!lexer_.ReadPath(&out, err))
245 } while (!out.empty());
248 if (!ExpectToken(Lexer::COLON, err))
252 if (!lexer_.ReadIdent(&rule_name))
253 return lexer_.Error("expected build command name", err);
255 const Rule* rule = env_->LookupRule(rule_name);
257 return lexer_.Error("unknown build rule '" + rule_name + "'", err);
260 // XXX should we require one path here?
262 if (!lexer_.ReadPath(&in, err))
269 // Add all implicit deps, counting how many as we go.
271 if (lexer_.PeekToken(Lexer::PIPE)) {
274 if (!lexer_.ReadPath(&in, err))
283 // Add all order-only deps, counting how many as we go.
285 if (lexer_.PeekToken(Lexer::PIPE2)) {
288 if (!lexer_.ReadPath(&in, err))
297 if (!ExpectToken(Lexer::NEWLINE, err))
300 // Bindings on edges are rare, so allocate per-edge envs only when needed.
301 bool has_indent_token = lexer_.PeekToken(Lexer::INDENT);
302 BindingEnv* env = has_indent_token ? new BindingEnv(env_) : env_;
303 while (has_indent_token) {
306 if (!ParseLet(&key, &val, err))
309 env->AddBinding(key, val.Evaluate(env_));
310 has_indent_token = lexer_.PeekToken(Lexer::INDENT);
313 Edge* edge = state_->AddEdge(rule);
316 string pool_name = edge->GetBinding("pool");
317 if (!pool_name.empty()) {
318 Pool* pool = state_->LookupPool(pool_name);
320 return lexer_.Error("unknown pool name '" + pool_name + "'", err);
324 for (vector<EvalString>::iterator i = ins.begin(); i != ins.end(); ++i) {
325 string path = i->Evaluate(env);
327 unsigned int slash_bits;
328 if (!CanonicalizePath(&path, &slash_bits, &path_err))
329 return lexer_.Error(path_err, err);
330 state_->AddIn(edge, path, slash_bits);
332 for (vector<EvalString>::iterator i = outs.begin(); i != outs.end(); ++i) {
333 string path = i->Evaluate(env);
335 unsigned int slash_bits;
336 if (!CanonicalizePath(&path, &slash_bits, &path_err))
337 return lexer_.Error(path_err, err);
338 state_->AddOut(edge, path, slash_bits);
340 edge->implicit_deps_ = implicit;
341 edge->order_only_deps_ = order_only;
343 if (edge->outputs_.empty()) {
344 // All outputs of the edge are already created by other edges. Don't add
346 state_->edges_.pop_back();
351 // Multiple outputs aren't (yet?) supported with depslog.
352 string deps_type = edge->GetBinding("deps");
353 if (!deps_type.empty() && edge->outputs_.size() > 1) {
354 return lexer_.Error("multiple outputs aren't (yet?) supported by depslog; "
355 "bring this up on the mailing list if it affects you",
362 bool ManifestParser::ParseFileInclude(bool new_scope, string* err) {
364 if (!lexer_.ReadPath(&eval, err))
366 string path = eval.Evaluate(env_);
368 ManifestParser subparser(state_, file_reader_);
370 subparser.env_ = new BindingEnv(env_);
372 subparser.env_ = env_;
375 if (!subparser.Load(path, err, &lexer_))
378 if (!ExpectToken(Lexer::NEWLINE, err))
384 bool ManifestParser::ExpectToken(Lexer::Token expected, string* err) {
385 Lexer::Token token = lexer_.ReadToken();
386 if (token != expected) {
387 string message = string("expected ") + Lexer::TokenName(expected);
388 message += string(", got ") + Lexer::TokenName(token);
389 message += Lexer::TokenErrorHint(expected);
390 return lexer_.Error(message, err);