Ninja
parser.cc
Go to the documentation of this file.
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 
20 using namespace std;
21 
22 bool Parser::Load(const string& filename, string* err, Lexer* parent) {
23  // If |parent| is not NULL, metrics collection has been started by a parent
24  // Parser::Load() in our call stack. Do not start a new one here to avoid
25  // over-counting parsing times.
26  METRIC_RECORD_IF(".ninja parse", parent == NULL);
27  string contents;
28  string read_err;
29  if (file_reader_->ReadFile(filename, &contents, &read_err) !=
31  *err = "loading '" + filename + "': " + read_err;
32  if (parent)
33  parent->Error(string(*err), err);
34  return false;
35  }
36 
37  return Parse(filename, contents, err);
38 }
39 
40 bool Parser::ExpectToken(Lexer::Token expected, string* err) {
41  Lexer::Token token = lexer_.ReadToken();
42  if (token != expected) {
43  string message = string("expected ") + Lexer::TokenName(expected);
44  message += string(", got ") + Lexer::TokenName(token);
45  message += Lexer::TokenErrorHint(expected);
46  return lexer_.Error(message, err);
47  }
48  return true;
49 }
#define METRIC_RECORD_IF(name, condition)
A variant of METRIC_RECORD that doesn't record anything if |condition| is false.
Definition: metrics.h:90
Definition: hash_map.h:26
Definition: lexer.h:27
static const char * TokenName(Token t)
Return a human-readable form of a token, used in error messages.
Definition: lexer.cc:75
Token
Definition: lexer.h:32
static const char * TokenErrorHint(Token expected)
Return a human-readable token hint, used in error messages.
Definition: lexer.cc:97
bool Error(const std::string &message, std::string *err)
Construct an error message with context.
Definition: lexer.cc:25
bool Load(const std::string &filename, std::string *err, Lexer *parent=NULL)
Load and parse a file.
Definition: parser.cc:22
bool ExpectToken(Lexer::Token expected, std::string *err)
If the next token is not expected, produce an error string saying "expected foo, got bar".
Definition: parser.cc:40