blob: 23da2696a7393230eba6743189e950d83c30bfe3 [file] [log] [blame]
Jiyong Parke5c45292020-05-26 19:06:24 +09001/*
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 Park8e79b7f2020-07-20 20:52:38 +090023#include <android-base/logging.h>
Jiyong Parke5c45292020-05-26 19:06:24 +090024
25#include <memory>
26#include <string>
27#include <vector>
28
29struct yy_buffer_state;
30typedef yy_buffer_state* YY_BUFFER_STATE;
31
Jiyong Park187bcc92020-07-22 18:40:30 +090032class 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 Parke5c45292020-05-26 19:06:24 +090051class 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 Park8e79b7f2020-07-20 20:52:38 +090065 bool HasError() const { return error_ != 0; }
Jiyong Parke5c45292020-05-26 19:06:24 +090066
67 const std::string& FileName() const { return filename_; }
68 void* Scanner() const { return scanner_; }
69
Jiyong Park18132182020-06-08 20:24:40 +090070 void SetPackage(const std::string& package) { package_ = package; }
71 const std::string& Package() const { return package_; }
Jiyong Parke5c45292020-05-26 19:06:24 +090072
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 Park62515512020-06-08 15:57:11 +090080 void SetDocument(std::unique_ptr<AidlDocument>&& document) {
Jiyong Park8e79b7f2020-07-20 20:52:38 +090081 // 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 Parke5c45292020-05-26 19:06:24 +090087 }
88 }
89
Jiyong Park8e79b7f2020-07-20 20:52:38 +090090 const AidlDocument& ParsedDocument() const {
91 CHECK(!HasError());
92 return *document_;
93 }
Jiyong Parke5c45292020-05-26 19:06:24 +090094
95 private:
96 explicit Parser(const std::string& filename, std::string& raw_buffer,
97 android::aidl::AidlTypenames& typenames);
98
99 std::string filename_;
Jiyong Park18132182020-06-08 20:24:40 +0900100 std::string package_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900101 AidlTypenames& typenames_;
102
103 void* scanner_ = nullptr;
104 YY_BUFFER_STATE buffer_;
105 int error_ = 0;
106
Jiyong Parke5c45292020-05-26 19:06:24 +0900107 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900108 const AidlDocument* document_;
Jiyong Parke5c45292020-05-26 19:06:24 +0900109};