The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include "aidl_language.h" |
| 2 | #include <stdio.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | #include <stdlib.h> |
Casey Dahlin | 9941dcc | 2015-09-09 17:59:06 -0700 | [diff] [blame^] | 4 | #include <string> |
| 5 | #include <iostream> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6 | |
Casey Dahlin | 9941dcc | 2015-09-09 17:59:06 -0700 | [diff] [blame^] | 7 | using std::string; |
| 8 | using std::cerr; |
| 9 | using std::endl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | |
| 11 | ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks; |
| 12 | |
Casey Dahlin | 9941dcc | 2015-09-09 17:59:06 -0700 | [diff] [blame^] | 13 | void yylex_init(void **); |
| 14 | void yylex_destroy(void *); |
| 15 | void yyset_in(FILE *f, void *); |
| 16 | int yyparse(ParseState*); |
| 17 | |
| 18 | ParseState::ParseState() : ParseState("") {} |
| 19 | |
| 20 | ParseState::ParseState(const string& filename) |
| 21 | : filename_(filename) { |
| 22 | yylex_init(&scanner_); |
| 23 | } |
| 24 | |
| 25 | ParseState::~ParseState() { |
| 26 | yylex_destroy(scanner_); |
| 27 | } |
| 28 | |
| 29 | string ParseState::FileName() { |
| 30 | return filename_; |
| 31 | } |
| 32 | |
| 33 | string ParseState::Package() { |
| 34 | return g_currentPackage; |
| 35 | } |
| 36 | |
| 37 | void ParseState::ProcessDocument(const document_item_type& items) { |
| 38 | /* The cast is not my fault. I didn't write the code on the other side. */ |
| 39 | /* TODO(sadmac): b/23977313 */ |
| 40 | g_callbacks->document((document_item_type *)&items); |
| 41 | } |
| 42 | |
| 43 | void ParseState::ProcessImport(const buffer_type& statement) { |
| 44 | /* The cast is not my fault. I didn't write the code on the other side. */ |
| 45 | /* TODO(sadmac): b/23977313 */ |
| 46 | g_callbacks->import((buffer_type *)&statement); |
| 47 | } |
| 48 | |
| 49 | void ParseState::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 | |
| 58 | bool ParseState::FoundNoErrors() { |
| 59 | return error_ == 0; |
| 60 | } |
| 61 | |
| 62 | void *ParseState::Scanner() { |
| 63 | return scanner_; |
| 64 | } |
| 65 | |
| 66 | bool ParseState::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 | |
| 76 | int ParseState::RunParser() { |
| 77 | int ret = yyparse(this); |
| 78 | |
| 79 | free((void *)g_currentPackage); |
| 80 | g_currentPackage = NULL; |
| 81 | |
| 82 | if (error_) |
| 83 | return 1; |
| 84 | |
| 85 | return ret; |
| 86 | } |