aidl_language.cpp revision 9980181fae4239b0161878de2947ff2ccc8d5f82
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*); 31 32Parser::Parser(const string& filename) 33 : filename_(filename) { 34 yylex_init(&scanner_); 35} 36 37Parser::~Parser() { 38 yylex_destroy(scanner_); 39} 40 41string Parser::FileName() { 42 return filename_; 43} 44 45string Parser::Package() { 46 return g_currentPackage ? g_currentPackage : ""; 47} 48 49void Parser::ReportError(const string& err) { 50 /* FIXME: We're printing out the line number as -1. We used to use yylineno 51 * (which was NEVER correct even before reentrant parsing). Now we'll need 52 * another way. 53 */ 54 cerr << filename_ << ":" << -1 << ": " << err << endl; 55 error_ = 1; 56} 57 58bool Parser::FoundNoErrors() { 59 return error_ == 0; 60} 61 62void *Parser::Scanner() { 63 return scanner_; 64} 65 66bool Parser::OpenFileFromDisk() { 67 FILE *in = fopen(FileName().c_str(), "r"); 68 69 if (! in) 70 return false; 71 72 yyset_in(in, Scanner()); 73 return true; 74} 75 76bool Parser::RunParser() { 77 int ret = yy::parser(this).parse(); 78 79 free((void *)g_currentPackage); 80 g_currentPackage = NULL; 81 82 return ret == 0 && error_ == 0; 83} 84 85void Parser::SetDocument(document_item_type *d) 86{ 87 document_ = d; 88} 89 90void Parser::AddImport(const buffer_type& statement) 91{ 92 import_info* import = (import_info*)malloc(sizeof(import_info)); 93 memset(import, 0, sizeof(import_info)); 94 import->from = strdup(this->FileName().c_str()); 95 import->statement.lineno = statement.lineno; 96 import->statement.data = strdup(statement.data); 97 import->statement.extra = NULL; 98 import->next = imports_; 99 import->neededClass = android::aidl::parse_import_statement(statement.data); 100 imports_ = import; 101} 102 103document_item_type *Parser::GetDocument() const 104{ 105 return document_; 106} 107 108import_info *Parser::GetImports() const 109{ 110 return imports_; 111} 112