Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "aidl_language.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 2 | |
| 3 | #include <iostream> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 5 | #include <stdlib.h> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 6 | #include <string> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 7 | |
| 8 | #include "aidl_language_y.hpp" |
| 9 | #include "parse_helpers.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 10 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 11 | #ifdef _WIN32 |
| 12 | int isatty(int fd) |
| 13 | { |
| 14 | return (fd == 0); |
| 15 | } |
| 16 | #endif |
| 17 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 18 | using std::string; |
| 19 | using std::cerr; |
| 20 | using std::endl; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 21 | using android::aidl::cpp_strdup; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 23 | Parser *psGlobal = NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 24 | ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 25 | char const* g_currentPackage = NULL; |
| 26 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 27 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 28 | void yylex_init(void **); |
| 29 | void yylex_destroy(void *); |
| 30 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 31 | int yyparse(Parser*); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 32 | YY_BUFFER_STATE yy_scan_string(const char *, void *); |
| 33 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 34 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 35 | Parser::Parser(const string& filename) |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 36 | : filename_(filename) { |
| 37 | yylex_init(&scanner_); |
| 38 | } |
| 39 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 40 | Parser::~Parser() { |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 41 | if (buffer_is_valid_) |
| 42 | yy_delete_buffer(buffer_, scanner_); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 43 | yylex_destroy(scanner_); |
| 44 | } |
| 45 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 46 | AidlType::AidlType(const std::string& name, unsigned line, |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame^] | 47 | const std::string& comments, bool is_array) |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 48 | : name_(name), |
| 49 | line_(line), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame^] | 50 | is_array_(is_array), |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 51 | comments_(comments) {} |
| 52 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 53 | string AidlType::ToString() const { |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame^] | 54 | return name_ + (is_array_ ? "[]" : ""); |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 57 | AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type, |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 58 | buffer_type name) |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 59 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 60 | direction_(direction), |
| 61 | direction_specified_(true), |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 62 | name_(name.data), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 63 | line_(name.lineno) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 64 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 65 | AidlArgument::AidlArgument(AidlType* type, buffer_type name) |
| 66 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 67 | direction_(AidlArgument::IN_DIR), |
| 68 | direction_specified_(false), |
| 69 | name_(name.data), |
| 70 | line_(name.lineno) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 71 | |
| 72 | string AidlArgument::ToString() const { |
| 73 | string ret; |
| 74 | |
| 75 | if (direction_specified_) { |
| 76 | switch(direction_) { |
| 77 | case AidlArgument::IN_DIR: |
| 78 | ret += "in "; |
| 79 | break; |
| 80 | case AidlArgument::OUT_DIR: |
| 81 | ret += "out "; |
| 82 | break; |
| 83 | case AidlArgument::INOUT_DIR: |
| 84 | ret += "inout "; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 89 | ret += type_->ToString(); |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 90 | ret += " "; |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 91 | ret += name_; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 92 | |
| 93 | return ret; |
| 94 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 95 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 96 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 97 | std::vector<std::unique_ptr<AidlArgument>>* args, |
| 98 | unsigned line, std::string comments, int id) |
| 99 | : oneway_(oneway), |
| 100 | comments_(comments), |
| 101 | type_(type), |
| 102 | name_(name), |
| 103 | line_(line), |
| 104 | arguments_(std::move(*args)), |
| 105 | id_(id) { |
| 106 | has_id_ = true; |
| 107 | delete args; |
| 108 | } |
| 109 | |
| 110 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 111 | std::vector<std::unique_ptr<AidlArgument>>* args, |
| 112 | unsigned line, std::string comments) |
| 113 | : AidlMethod(oneway, type, name, args, line, comments, 0) { |
| 114 | has_id_ = false; |
| 115 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 116 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 117 | string Parser::FileName() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 118 | return filename_; |
| 119 | } |
| 120 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 121 | string Parser::Package() { |
Christopher Wiley | 5b86980 | 2015-09-15 19:40:38 -0700 | [diff] [blame] | 122 | return g_currentPackage ? g_currentPackage : ""; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 125 | void Parser::ReportError(const string& err) { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 126 | /* FIXME: We're printing out the line number as -1. We used to use yylineno |
| 127 | * (which was NEVER correct even before reentrant parsing). Now we'll need |
| 128 | * another way. |
| 129 | */ |
| 130 | cerr << filename_ << ":" << -1 << ": " << err << endl; |
| 131 | error_ = 1; |
| 132 | } |
| 133 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 134 | bool Parser::FoundNoErrors() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 135 | return error_ == 0; |
| 136 | } |
| 137 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 138 | void *Parser::Scanner() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 139 | return scanner_; |
| 140 | } |
| 141 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 142 | bool Parser::OpenFileFromDisk() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 143 | FILE *in = fopen(FileName().c_str(), "r"); |
| 144 | |
| 145 | if (! in) |
| 146 | return false; |
| 147 | |
| 148 | yyset_in(in, Scanner()); |
| 149 | return true; |
| 150 | } |
| 151 | |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 152 | void Parser::SetFileContents(const std::string& contents) { |
| 153 | if (buffer_is_valid_) |
| 154 | yy_delete_buffer(buffer_, scanner_); |
| 155 | |
| 156 | buffer_ = yy_scan_string(contents.c_str(), scanner_); |
| 157 | buffer_is_valid_ = true; |
| 158 | } |
| 159 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 160 | bool Parser::RunParser() { |
Casey Dahlin | fd2e08a | 2015-09-10 18:29:09 -0700 | [diff] [blame] | 161 | int ret = yy::parser(this).parse(); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 162 | |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 163 | delete[] g_currentPackage; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 164 | g_currentPackage = NULL; |
| 165 | |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 166 | return ret == 0 && error_ == 0; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 167 | } |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 168 | |
| 169 | void Parser::SetDocument(document_item_type *d) |
| 170 | { |
| 171 | document_ = d; |
| 172 | } |
| 173 | |
| 174 | void Parser::AddImport(const buffer_type& statement) |
| 175 | { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 176 | import_info* import = new import_info(); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 177 | memset(import, 0, sizeof(import_info)); |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 178 | import->from = cpp_strdup(this->FileName().c_str()); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 179 | import->statement.lineno = statement.lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 180 | import->statement.data = cpp_strdup(statement.data); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 181 | import->statement.extra = NULL; |
| 182 | import->next = imports_; |
| 183 | import->neededClass = android::aidl::parse_import_statement(statement.data); |
| 184 | imports_ = import; |
| 185 | } |
| 186 | |
| 187 | document_item_type *Parser::GetDocument() const |
| 188 | { |
| 189 | return document_; |
| 190 | } |
| 191 | |
| 192 | import_info *Parser::GetImports() const |
| 193 | { |
| 194 | return imports_; |
| 195 | } |