Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "aidl_language.h" |
| 20 | #include "aidl_typenames.h" |
| 21 | #include "io_delegate.h" |
| 22 | #include "options.h" |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 24 | |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | struct yy_buffer_state; |
| 30 | typedef yy_buffer_state* YY_BUFFER_STATE; |
| 31 | |
Jiyong Park | 187bcc9 | 2020-07-22 18:40:30 +0900 | [diff] [blame] | 32 | class AidlToken { |
| 33 | public: |
| 34 | AidlToken(const std::string& text, const std::string& comments) |
| 35 | : text_(text), comments_(comments) {} |
| 36 | ~AidlToken() = default; |
| 37 | |
| 38 | AidlToken(const AidlToken&) = delete; |
| 39 | AidlToken(AidlToken&&) = delete; |
| 40 | AidlToken& operator=(const AidlToken&) = delete; |
| 41 | AidlToken& operator=(AidlToken&&) = delete; |
| 42 | |
| 43 | const std::string& GetText() const { return text_; } |
| 44 | const std::string& GetComments() const { return comments_; } |
| 45 | |
| 46 | private: |
| 47 | std::string text_; |
| 48 | std::string comments_; |
| 49 | }; |
| 50 | |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 51 | class Parser { |
| 52 | public: |
| 53 | // non-copyable, non-assignable |
| 54 | Parser(const Parser&) = delete; |
| 55 | Parser& operator=(const Parser&) = delete; |
| 56 | |
| 57 | ~Parser(); |
| 58 | |
| 59 | // Parse contents of file |filename|. Should only be called once. |
| 60 | static std::unique_ptr<Parser> Parse(const std::string& filename, |
| 61 | const android::aidl::IoDelegate& io_delegate, |
| 62 | AidlTypenames& typenames); |
| 63 | |
| 64 | void AddError() { error_++; } |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 65 | bool HasError() const { return error_ != 0; } |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 66 | |
| 67 | const std::string& FileName() const { return filename_; } |
| 68 | void* Scanner() const { return scanner_; } |
| 69 | |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 70 | void SetPackage(const std::string& package) { package_ = package; } |
| 71 | const std::string& Package() const { return package_; } |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 72 | |
| 73 | void DeferResolution(AidlTypeSpecifier* typespec) { |
| 74 | unresolved_typespecs_.emplace_back(typespec); |
| 75 | } |
| 76 | |
| 77 | const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; } |
| 78 | |
| 79 | bool Resolve(); |
Jiyong Park | 6251551 | 2020-06-08 15:57:11 +0900 | [diff] [blame] | 80 | void SetDocument(std::unique_ptr<AidlDocument>&& document) { |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 81 | // The parsed document is owned by typenames_. This parser object only has |
| 82 | // a reference to it. |
| 83 | document_ = document.get(); |
| 84 | if (!typenames_.AddDocument(std::move(document))) { |
| 85 | document_ = nullptr; |
| 86 | AddError(); |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 90 | const AidlDocument& ParsedDocument() const { |
| 91 | CHECK(!HasError()); |
| 92 | return *document_; |
| 93 | } |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | explicit Parser(const std::string& filename, std::string& raw_buffer, |
| 97 | android::aidl::AidlTypenames& typenames); |
| 98 | |
| 99 | std::string filename_; |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 100 | std::string package_; |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 101 | AidlTypenames& typenames_; |
| 102 | |
| 103 | void* scanner_ = nullptr; |
| 104 | YY_BUFFER_STATE buffer_; |
| 105 | int error_ = 0; |
| 106 | |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 107 | vector<AidlTypeSpecifier*> unresolved_typespecs_; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 108 | const AidlDocument* document_; |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 109 | }; |