1695b41eeSopenharmony_ci// Copyright 2018 Google Inc. All Rights Reserved.
2695b41eeSopenharmony_ci//
3695b41eeSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4695b41eeSopenharmony_ci// you may not use this file except in compliance with the License.
5695b41eeSopenharmony_ci// You may obtain a copy of the License at
6695b41eeSopenharmony_ci//
7695b41eeSopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8695b41eeSopenharmony_ci//
9695b41eeSopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10695b41eeSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11695b41eeSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12695b41eeSopenharmony_ci// See the License for the specific language governing permissions and
13695b41eeSopenharmony_ci// limitations under the License.
14695b41eeSopenharmony_ci
15695b41eeSopenharmony_ci#ifndef NINJA_PARSER_H_
16695b41eeSopenharmony_ci#define NINJA_PARSER_H_
17695b41eeSopenharmony_ci
18695b41eeSopenharmony_ci#include <string>
19695b41eeSopenharmony_ci
20695b41eeSopenharmony_ci#include "lexer.h"
21695b41eeSopenharmony_ci
22695b41eeSopenharmony_cistruct FileReader;
23695b41eeSopenharmony_cistruct State;
24695b41eeSopenharmony_ci
25695b41eeSopenharmony_ci/// Base class for parsers.
26695b41eeSopenharmony_cistruct Parser {
27695b41eeSopenharmony_ci  Parser(State* state, FileReader* file_reader)
28695b41eeSopenharmony_ci      : state_(state), file_reader_(file_reader) {}
29695b41eeSopenharmony_ci
30695b41eeSopenharmony_ci  /// Load and parse a file.
31695b41eeSopenharmony_ci  bool Load(const std::string& filename, std::string* err, Lexer* parent = NULL);
32695b41eeSopenharmony_ci
33695b41eeSopenharmony_ciprotected:
34695b41eeSopenharmony_ci  /// If the next token is not \a expected, produce an error string
35695b41eeSopenharmony_ci  /// saying "expected foo, got bar".
36695b41eeSopenharmony_ci  bool ExpectToken(Lexer::Token expected, std::string* err);
37695b41eeSopenharmony_ci
38695b41eeSopenharmony_ci  State* state_;
39695b41eeSopenharmony_ci  FileReader* file_reader_;
40695b41eeSopenharmony_ci  Lexer lexer_;
41695b41eeSopenharmony_ci
42695b41eeSopenharmony_ciprivate:
43695b41eeSopenharmony_ci  /// Parse a file, given its contents as a string.
44695b41eeSopenharmony_ci  virtual bool Parse(const std::string& filename, const std::string& input,
45695b41eeSopenharmony_ci                     std::string* err) = 0;
46695b41eeSopenharmony_ci};
47695b41eeSopenharmony_ci
48695b41eeSopenharmony_ci#endif  // NINJA_PARSER_H_
49