aidl_language.cpp revision fd6fb483fbe3b48948ade670ee838c6622110020
1#include "aidl_language.h" 2 3#include <iostream> 4#include <stdio.h> 5#include <stdlib.h> 6#include <string> 7 8#include "aidl_language_y.hpp" 9#include "parse_helpers.h" 10 11#ifdef _WIN32 12int isatty(int fd) 13{ 14 return (fd == 0); 15} 16#endif 17 18using std::string; 19using std::cerr; 20using std::endl; 21using android::aidl::cpp_strdup; 22 23Parser *psGlobal = NULL; 24ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks; 25char const* g_currentPackage = NULL; 26 27 28void yylex_init(void **); 29void yylex_destroy(void *); 30void yyset_in(FILE *f, void *); 31int yyparse(Parser*); 32YY_BUFFER_STATE yy_scan_string(const char *, void *); 33void yy_delete_buffer(YY_BUFFER_STATE, void *); 34 35Parser::Parser(const string& filename) 36 : filename_(filename) { 37 yylex_init(&scanner_); 38} 39 40Parser::~Parser() { 41 if (buffer_is_valid_) 42 yy_delete_buffer(buffer_, scanner_); 43 yylex_destroy(scanner_); 44} 45 46AidlArgument::AidlArgument(AidlArgument::Direction direction, type_type type, 47 buffer_type name) 48 : type(type), 49 direction_(direction), 50 direction_specified_(true), 51 name_(name.data), 52 line_(name.lineno) {} 53 54AidlArgument::AidlArgument(type_type type, buffer_type name) 55 : type(type), 56 direction_(AidlArgument::IN_DIR), 57 direction_specified_(false), 58 name_(name.data), 59 line_(name.lineno) {} 60 61string AidlArgument::ToString() const { 62 string ret; 63 64 if (direction_specified_) { 65 switch(direction_) { 66 case AidlArgument::IN_DIR: 67 ret += "in "; 68 break; 69 case AidlArgument::OUT_DIR: 70 ret += "out "; 71 break; 72 case AidlArgument::INOUT_DIR: 73 ret += "inout "; 74 break; 75 } 76 } 77 78 ret += string(type.type.data); 79 if (type.array_token.data) 80 ret += string(type.array_token.data); 81 ret += " "; 82 ret += name_; 83 84 return ret; 85} 86 87string Parser::FileName() { 88 return filename_; 89} 90 91string Parser::Package() { 92 return g_currentPackage ? g_currentPackage : ""; 93} 94 95void Parser::ReportError(const string& err) { 96 /* FIXME: We're printing out the line number as -1. We used to use yylineno 97 * (which was NEVER correct even before reentrant parsing). Now we'll need 98 * another way. 99 */ 100 cerr << filename_ << ":" << -1 << ": " << err << endl; 101 error_ = 1; 102} 103 104bool Parser::FoundNoErrors() { 105 return error_ == 0; 106} 107 108void *Parser::Scanner() { 109 return scanner_; 110} 111 112bool Parser::OpenFileFromDisk() { 113 FILE *in = fopen(FileName().c_str(), "r"); 114 115 if (! in) 116 return false; 117 118 yyset_in(in, Scanner()); 119 return true; 120} 121 122void Parser::SetFileContents(const std::string& contents) { 123 if (buffer_is_valid_) 124 yy_delete_buffer(buffer_, scanner_); 125 126 buffer_ = yy_scan_string(contents.c_str(), scanner_); 127 buffer_is_valid_ = true; 128} 129 130bool Parser::RunParser() { 131 int ret = yy::parser(this).parse(); 132 133 delete[] g_currentPackage; 134 g_currentPackage = NULL; 135 136 return ret == 0 && error_ == 0; 137} 138 139void Parser::SetDocument(document_item_type *d) 140{ 141 document_ = d; 142} 143 144void Parser::AddImport(const buffer_type& statement) 145{ 146 import_info* import = new import_info(); 147 memset(import, 0, sizeof(import_info)); 148 import->from = cpp_strdup(this->FileName().c_str()); 149 import->statement.lineno = statement.lineno; 150 import->statement.data = cpp_strdup(statement.data); 151 import->statement.extra = NULL; 152 import->next = imports_; 153 import->neededClass = android::aidl::parse_import_statement(statement.data); 154 imports_ = import; 155} 156 157document_item_type *Parser::GetDocument() const 158{ 159 return document_; 160} 161 162import_info *Parser::GetImports() const 163{ 164 return imports_; 165} 166 167std::string type_type::Brackets() const { 168 std::string result; 169 170 for (int i = 0; i < dimension; i++) 171 result += "[]"; 172 173 return result; 174} 175