blob: 4d462d0a590ba62f104256d67b93178989a4716f [file] [log] [blame]
Piotr Pawliczekcdd921f2020-05-06 17:35:58 -07001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FOOMATIC_SHELL_SCANNER_H_
6#define FOOMATIC_SHELL_SCANNER_H_
7
8#include "foomatic_shell/grammar.h"
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace foomatic_shell {
15
16// Converts a script into a sequences of Tokens. See the struct Token defined
17// in grammar.h.
18class Scanner {
19 public:
20 // Constructor. |data| is a reference to the input buffer. The input buffer
21 // must be constant and valid during the lifetime of this object.
22 explicit Scanner(const std::string& data);
23
24 ~Scanner();
25 Scanner(const Scanner&) = delete;
26 Scanner(Scanner&&) = delete;
27
28 // Parses the input given in the constructor and stores the resultant
29 // sequence in |tokens|. Returns true when succeed. Otherwise it stops
30 // on the first error and returns false. |tokens| must not be nullptr.
31 bool ParseWholeInput(std::vector<Token>* tokens);
32
33 // Returns the current position of the scanner as an iterator of the string
34 // given in the constructor. It is used to report a position of the error
35 // when the method ParseWholeInput(...) fails.
36 std::string::const_iterator GetPosition() const;
37
Piotr Pawliczek7bb6e512020-07-13 10:59:36 -070038 // Returns an error message if the call ParseWholeInput(...) failed.
39 // Returns an empty string if the call succeeded.
40 const std::string& GetMessage() const { return message_; }
41
Piotr Pawliczekcdd921f2020-05-06 17:35:58 -070042 private:
43 bool ParseLiteralString(std::vector<Token>* tokens);
44 bool ParseExecutedString(std::vector<Token>* tokens);
45 bool ParseInterpretedString(std::vector<Token>* tokens);
46 bool ParseNativeString(std::vector<Token>* tokens);
47 class Input;
48 std::unique_ptr<Input> data_;
49 std::string message_;
50};
51
52} // namespace foomatic_shell
53
54#endif // FOOMATIC_SHELL_SCANNER_H_