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; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 21 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 22 | Parser *psGlobal = NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 | ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 24 | char const* g_currentPackage = NULL; |
| 25 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 26 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 27 | void yylex_init(void **); |
| 28 | void yylex_destroy(void *); |
| 29 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 30 | int yyparse(Parser*); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame^] | 31 | YY_BUFFER_STATE yy_scan_string(const char *, void *); |
| 32 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 33 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 34 | Parser::Parser(const string& filename) |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 35 | : filename_(filename) { |
| 36 | yylex_init(&scanner_); |
| 37 | } |
| 38 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 39 | Parser::~Parser() { |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame^] | 40 | if (buffer_is_valid_) |
| 41 | yy_delete_buffer(buffer_, scanner_); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 42 | yylex_destroy(scanner_); |
| 43 | } |
| 44 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 45 | string Parser::FileName() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 46 | return filename_; |
| 47 | } |
| 48 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 49 | string Parser::Package() { |
Christopher Wiley | 5b86980 | 2015-09-15 19:40:38 -0700 | [diff] [blame] | 50 | return g_currentPackage ? g_currentPackage : ""; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 53 | void Parser::ReportError(const string& err) { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 54 | /* FIXME: We're printing out the line number as -1. We used to use yylineno |
| 55 | * (which was NEVER correct even before reentrant parsing). Now we'll need |
| 56 | * another way. |
| 57 | */ |
| 58 | cerr << filename_ << ":" << -1 << ": " << err << endl; |
| 59 | error_ = 1; |
| 60 | } |
| 61 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 62 | bool Parser::FoundNoErrors() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 63 | return error_ == 0; |
| 64 | } |
| 65 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 66 | void *Parser::Scanner() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 67 | return scanner_; |
| 68 | } |
| 69 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 70 | bool Parser::OpenFileFromDisk() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 71 | FILE *in = fopen(FileName().c_str(), "r"); |
| 72 | |
| 73 | if (! in) |
| 74 | return false; |
| 75 | |
| 76 | yyset_in(in, Scanner()); |
| 77 | return true; |
| 78 | } |
| 79 | |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame^] | 80 | void Parser::SetFileContents(const std::string& contents) { |
| 81 | if (buffer_is_valid_) |
| 82 | yy_delete_buffer(buffer_, scanner_); |
| 83 | |
| 84 | buffer_ = yy_scan_string(contents.c_str(), scanner_); |
| 85 | buffer_is_valid_ = true; |
| 86 | } |
| 87 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 88 | bool Parser::RunParser() { |
Casey Dahlin | fd2e08a | 2015-09-10 18:29:09 -0700 | [diff] [blame] | 89 | int ret = yy::parser(this).parse(); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 90 | |
| 91 | free((void *)g_currentPackage); |
| 92 | g_currentPackage = NULL; |
| 93 | |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 94 | return ret == 0 && error_ == 0; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 95 | } |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 96 | |
| 97 | void Parser::SetDocument(document_item_type *d) |
| 98 | { |
| 99 | document_ = d; |
| 100 | } |
| 101 | |
| 102 | void Parser::AddImport(const buffer_type& statement) |
| 103 | { |
| 104 | import_info* import = (import_info*)malloc(sizeof(import_info)); |
| 105 | memset(import, 0, sizeof(import_info)); |
| 106 | import->from = strdup(this->FileName().c_str()); |
| 107 | import->statement.lineno = statement.lineno; |
| 108 | import->statement.data = strdup(statement.data); |
| 109 | import->statement.extra = NULL; |
| 110 | import->next = imports_; |
| 111 | import->neededClass = android::aidl::parse_import_statement(statement.data); |
| 112 | imports_ = import; |
| 113 | } |
| 114 | |
| 115 | document_item_type *Parser::GetDocument() const |
| 116 | { |
| 117 | return document_; |
| 118 | } |
| 119 | |
| 120 | import_info *Parser::GetImports() const |
| 121 | { |
| 122 | return imports_; |
| 123 | } |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 124 | |
| 125 | std::string type_type::Brackets() const { |
| 126 | std::string result; |
| 127 | |
| 128 | for (int i = 0; i < dimension; i++) |
| 129 | result += "[]"; |
| 130 | |
| 131 | return result; |
| 132 | } |