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 20using namespace std; 21 22bool 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) != 30 FileReader::Okay) { 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 40bool 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} 50