• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 "parser.h"
16 
17 #include "disk_interface.h"
18 #include "metrics.h"
19 
Load(const string & filename,string * err,Lexer * parent)20 bool Parser::Load(const string& filename, string* err, Lexer* parent) {
21   METRIC_RECORD(".ninja parse");
22   string contents;
23   string read_err;
24   if (file_reader_->ReadFile(filename, &contents, &read_err) !=
25       FileReader::Okay) {
26     *err = "loading '" + filename + "': " + read_err;
27     if (parent)
28       parent->Error(string(*err), err);
29     return false;
30   }
31 
32   // The lexer needs a nul byte at the end of its input, to know when it's done.
33   // It takes a StringPiece, and StringPiece's string constructor uses
34   // string::data().  data()'s return value isn't guaranteed to be
35   // null-terminated (although in practice - libc++, libstdc++, msvc's stl --
36   // it is, and C++11 demands that too), so add an explicit nul byte.
37   contents.resize(contents.size() + 1);
38 
39   return Parse(filename, contents, err);
40 }
41 
ExpectToken(Lexer::Token expected,string * err)42 bool Parser::ExpectToken(Lexer::Token expected, string* err) {
43   Lexer::Token token = lexer_.ReadToken();
44   if (token != expected) {
45     string message = string("expected ") + Lexer::TokenName(expected);
46     message += string(", got ") + Lexer::TokenName(token);
47     message += Lexer::TokenErrorHint(expected);
48     return lexer_.Error(message, err);
49   }
50   return true;
51 }
52