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