blob: e791f7a07b3b7bcea3318f3c1ee6bf28e98f7c89 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "aidl_language.h"
Christopher Wileyf690be52015-09-14 15:19:10 -07002
3#include <iostream>
Adam Lesinskiffa16862014-01-23 18:17:42 -08004#include <stdio.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -08005#include <stdlib.h>
Casey Dahlindd691812015-09-09 17:59:06 -07006#include <string>
Christopher Wileyf690be52015-09-14 15:19:10 -07007
8#include "aidl_language_y.hpp"
9#include "parse_helpers.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080010
Casey Dahlin07b9dde2015-09-10 19:13:49 -070011#ifdef _WIN32
12int isatty(int fd)
13{
14 return (fd == 0);
15}
16#endif
17
Casey Dahlindd691812015-09-09 17:59:06 -070018using std::string;
19using std::cerr;
20using std::endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -080021
Casey Dahline2507492015-09-14 17:11:20 -070022Parser *psGlobal = NULL;
Adam Lesinskiffa16862014-01-23 18:17:42 -080023ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070024char const* g_currentPackage = NULL;
25
Adam Lesinskiffa16862014-01-23 18:17:42 -080026
Casey Dahlindd691812015-09-09 17:59:06 -070027void yylex_init(void **);
28void yylex_destroy(void *);
29void yyset_in(FILE *f, void *);
Casey Dahline2507492015-09-14 17:11:20 -070030int yyparse(Parser*);
Casey Dahlin89d44842015-09-24 18:45:54 -070031YY_BUFFER_STATE yy_scan_string(const char *, void *);
32void yy_delete_buffer(YY_BUFFER_STATE, void *);
Casey Dahlindd691812015-09-09 17:59:06 -070033
Casey Dahline2507492015-09-14 17:11:20 -070034Parser::Parser(const string& filename)
Casey Dahlindd691812015-09-09 17:59:06 -070035 : filename_(filename) {
36 yylex_init(&scanner_);
37}
38
Casey Dahline2507492015-09-14 17:11:20 -070039Parser::~Parser() {
Casey Dahlin89d44842015-09-24 18:45:54 -070040 if (buffer_is_valid_)
41 yy_delete_buffer(buffer_, scanner_);
Casey Dahlindd691812015-09-09 17:59:06 -070042 yylex_destroy(scanner_);
43}
44
Casey Dahline2507492015-09-14 17:11:20 -070045string Parser::FileName() {
Casey Dahlindd691812015-09-09 17:59:06 -070046 return filename_;
47}
48
Casey Dahline2507492015-09-14 17:11:20 -070049string Parser::Package() {
Christopher Wiley5b869802015-09-15 19:40:38 -070050 return g_currentPackage ? g_currentPackage : "";
Casey Dahlindd691812015-09-09 17:59:06 -070051}
52
Casey Dahline2507492015-09-14 17:11:20 -070053void Parser::ReportError(const string& err) {
Casey Dahlindd691812015-09-09 17:59:06 -070054 /* 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 Dahline2507492015-09-14 17:11:20 -070062bool Parser::FoundNoErrors() {
Casey Dahlindd691812015-09-09 17:59:06 -070063 return error_ == 0;
64}
65
Casey Dahline2507492015-09-14 17:11:20 -070066void *Parser::Scanner() {
Casey Dahlindd691812015-09-09 17:59:06 -070067 return scanner_;
68}
69
Casey Dahline2507492015-09-14 17:11:20 -070070bool Parser::OpenFileFromDisk() {
Casey Dahlindd691812015-09-09 17:59:06 -070071 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 Dahlin89d44842015-09-24 18:45:54 -070080void 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 Dahline2507492015-09-14 17:11:20 -070088bool Parser::RunParser() {
Casey Dahlinfd2e08a2015-09-10 18:29:09 -070089 int ret = yy::parser(this).parse();
Casey Dahlindd691812015-09-09 17:59:06 -070090
91 free((void *)g_currentPackage);
92 g_currentPackage = NULL;
93
Casey Dahlin99801812015-09-15 18:19:25 -070094 return ret == 0 && error_ == 0;
Casey Dahlindd691812015-09-09 17:59:06 -070095}
Casey Dahline2507492015-09-14 17:11:20 -070096
97void Parser::SetDocument(document_item_type *d)
98{
99 document_ = d;
100}
101
102void 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
115document_item_type *Parser::GetDocument() const
116{
117 return document_;
118}
119
120import_info *Parser::GetImports() const
121{
122 return imports_;
123}
Casey Dahlin082f1d12015-09-21 14:06:25 -0700124
125std::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}