blob: ec7410452ab951f5dd19ca217ba5b7aad4c2bcf0 [file] [log] [blame]
Christopher Wiley89e35862015-08-30 10:57:07 -07001#ifndef AIDL_AIDL_LANGUAGE_H_
2#define AIDL_AIDL_LANGUAGE_H_
Adam Lesinskiffa16862014-01-23 18:17:42 -08003
Casey Dahlinbc7a50a2015-09-28 19:20:50 -07004#include <memory>
Casey Dahlindd691812015-09-09 17:59:06 -07005#include <string>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -07006#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -07007
Christopher Wiley7c3a1eb2015-09-12 10:19:52 -07008#include <base/macros.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +00009
Casey Dahlin89d44842015-09-24 18:45:54 -070010struct yy_buffer_state;
11typedef yy_buffer_state* YY_BUFFER_STATE;
12
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070013enum which_extra_text {
Adam Lesinskiffa16862014-01-23 18:17:42 -080014 NO_EXTRA_TEXT = 0,
15 SHORT_COMMENT,
16 LONG_COMMENT,
17 COPY_TEXT,
18 WHITESPACE
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070019};
Adam Lesinskiffa16862014-01-23 18:17:42 -080020
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070021struct extra_text_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -080022 unsigned lineno;
23 which_extra_text which;
24 char* data;
25 unsigned len;
26 struct extra_text_type* next;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070027};
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070029struct buffer_type {
Casey Dahlin082f1d12015-09-21 14:06:25 -070030 unsigned lineno;
31 unsigned token;
32 char *data;
33 extra_text_type* extra;
34
35 std::string Literal() const {
Casey Dahlina834dd42015-09-23 11:52:15 -070036 return data ? std::string(data) : "";
Casey Dahlin082f1d12015-09-21 14:06:25 -070037 }
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070038};
Adam Lesinskiffa16862014-01-23 18:17:42 -080039
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070040struct type_type {
Casey Dahlin082f1d12015-09-21 14:06:25 -070041 buffer_type type;
42 buffer_type array_token;
43 int dimension;
44
45 std::string Brackets() const;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070046};
Adam Lesinskiffa16862014-01-23 18:17:42 -080047
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070048class AidlNode {
49 public:
50 AidlNode() = default;
51 virtual ~AidlNode() = default;
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(AidlNode);
55};
56
57class AidlArgument : public AidlNode {
58 public:
59 AidlArgument(buffer_type direction, type_type type, buffer_type name);
60 virtual ~AidlArgument() = default;
61
Casey Dahlina834dd42015-09-23 11:52:15 -070062 buffer_type direction;
Casey Dahlina834dd42015-09-23 11:52:15 -070063 buffer_type name;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070064 type_type type;
65
66 private:
67 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -070068};
Adam Lesinskiffa16862014-01-23 18:17:42 -080069
70enum {
71 METHOD_TYPE
72};
73
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070074struct interface_item_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -080075 unsigned item_type;
76 struct interface_item_type* next;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070077};
Adam Lesinskiffa16862014-01-23 18:17:42 -080078
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070079struct method_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -080080 interface_item_type interface_item;
81 type_type type;
82 bool oneway;
83 buffer_type oneway_token;
84 buffer_type name;
85 buffer_type open_paren_token;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070086 std::vector<std::unique_ptr<AidlArgument>>* args;
Adam Lesinskiffa16862014-01-23 18:17:42 -080087 buffer_type close_paren_token;
88 bool hasId;
89 buffer_type equals_token;
90 buffer_type id;
91 // XXX missing comments/copy text here
92 buffer_type semicolon_token;
93 buffer_type* comments_token; // points into this structure, DO NOT DELETE
94 int assigned_id;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070095};
Adam Lesinskiffa16862014-01-23 18:17:42 -080096
97enum {
98 USER_DATA_TYPE = 12,
Casey Dahlin88868fc2015-09-01 13:21:26 -070099 INTERFACE_TYPE_BINDER
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100};
101
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700102struct document_item_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103 unsigned item_type;
104 struct document_item_type* next;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700105};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106
107
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700108struct user_data_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 document_item_type document_item;
110 buffer_type keyword_token; // only the first one
111 char* package;
112 buffer_type name;
113 buffer_type semicolon_token;
Casey Dahlin88868fc2015-09-01 13:21:26 -0700114 bool parcelable;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700115};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800116
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700117struct interface_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118 document_item_type document_item;
119 buffer_type interface_token;
120 bool oneway;
121 buffer_type oneway_token;
122 char* package;
123 buffer_type name;
124 buffer_type open_brace_token;
125 interface_item_type* interface_items;
126 buffer_type close_brace_token;
127 buffer_type* comments_token; // points into this structure, DO NOT DELETE
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700128};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800129
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130
131#if __cplusplus
132extern "C" {
133#endif
134
Adam Lesinskiffa16862014-01-23 18:17:42 -0800135// in, out or inout
136enum {
137 IN_PARAMETER = 1,
138 OUT_PARAMETER = 2,
139 INOUT_PARAMETER = 3
140};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800141
142// callbacks from within the parser
143// these functions all take ownership of the strings
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700144struct ParserCallbacks {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800145 void (*document)(document_item_type* items);
146 void (*import)(buffer_type* statement);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700147};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148
149extern ParserCallbacks* g_callbacks;
150
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151// the package name for our current file
152extern char const* g_currentPackage;
153
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700154enum error_type {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155 STATEMENT_INSIDE_INTERFACE
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700156};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800157
158void init_buffer_type(buffer_type* buf, int lineno);
159
Casey Dahline2507492015-09-14 17:11:20 -0700160struct import_info {
161 const char* from;
162 const char* filename;
163 buffer_type statement;
164 const char* neededClass;
165 document_item_type* doc;
166 struct import_info* next;
167};
168
169class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700170 public:
Casey Dahline2507492015-09-14 17:11:20 -0700171 Parser(const std::string& filename);
172 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700173
174 bool OpenFileFromDisk();
Casey Dahlin89d44842015-09-24 18:45:54 -0700175
176 // Call this instead of OpenFileFromDisk to provide the text of the file
177 // directly.
178 void SetFileContents(const std::string& contents);
179
Casey Dahline2507492015-09-14 17:11:20 -0700180 bool RunParser();
Casey Dahlindd691812015-09-09 17:59:06 -0700181 void ReportError(const std::string& err);
182
183 bool FoundNoErrors();
184 std::string FileName();
185 std::string Package();
186 void *Scanner();
187
Casey Dahline2507492015-09-14 17:11:20 -0700188 void SetDocument(document_item_type *items);
189 void AddImport(const buffer_type& statement);
190
191 document_item_type *GetDocument() const;
192 import_info *GetImports() const;
Casey Dahlindd691812015-09-09 17:59:06 -0700193
194 private:
195 int error_ = 0;
196 std::string filename_;
197 std::string package_;
198 void *scanner_ = nullptr;
Casey Dahline2507492015-09-14 17:11:20 -0700199 document_item_type* document_ = nullptr;
200 import_info* imports_ = nullptr;
Casey Dahlin89d44842015-09-24 18:45:54 -0700201 bool buffer_is_valid_ = false;
202 YY_BUFFER_STATE buffer_;
Casey Dahlindd691812015-09-09 17:59:06 -0700203
Casey Dahline2507492015-09-14 17:11:20 -0700204 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700205};
206
Adam Lesinskiffa16862014-01-23 18:17:42 -0800207#if __cplusplus
208}
209#endif
210
Christopher Wiley89e35862015-08-30 10:57:07 -0700211#endif // AIDL_AIDL_LANGUAGE_H_