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 | METRIC_RECORD(".ninja parse" ); |
24 | string contents; |
25 | string read_err; |
26 | if (file_reader_->ReadFile(filename, &contents, &read_err) != |
27 | FileReader::Okay) { |
28 | *err = "loading '" + filename + "': " + read_err; |
29 | if (parent) |
30 | parent->Error(string(*err), err); |
31 | return false; |
32 | } |
33 | |
34 | return Parse(filename, contents, err); |
35 | } |
36 | |
37 | bool Parser::ExpectToken(Lexer::Token expected, string* err) { |
38 | Lexer::Token token = lexer_.ReadToken(); |
39 | if (token != expected) { |
40 | string message = string("expected " ) + Lexer::TokenName(expected); |
41 | message += string(", got " ) + Lexer::TokenName(token); |
42 | message += Lexer::TokenErrorHint(expected); |
43 | return lexer_.Error(message, err); |
44 | } |
45 | return true; |
46 | } |
47 | |