aidl_language.cpp revision f2d23f7f05252fa1a06a8f95bd9475fa9d414c0c
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
46AidlType::AidlType(const std::string& name, unsigned line,
47                   const std::string& comments, unsigned dimension)
48    : name_(name),
49      line_(line),
50      dimension_(dimension),
51      comments_(comments) {}
52
53AidlType::AidlType(const std::string& name, unsigned line,
54                   const std::string& comments)
55    : AidlType(name, line, comments, 0) {}
56
57string AidlType::ToString() const {
58  return name_ + Brackets();
59}
60
61std::string AidlType::Brackets() const {
62  std::string result;
63
64  for (unsigned i = 0; i < dimension_; i++)
65    result += "[]";
66
67  return result;
68}
69
70AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type,
71                           buffer_type name)
72    : type_(type),
73      direction_(direction),
74      direction_specified_(true),
75      name_(name.data),
76      line_(name.lineno) {}
77
78AidlArgument::AidlArgument(AidlType* type, buffer_type name)
79    : type_(type),
80      direction_(AidlArgument::IN_DIR),
81      direction_specified_(false),
82      name_(name.data),
83      line_(name.lineno) {}
84
85string AidlArgument::ToString() const {
86  string ret;
87
88  if (direction_specified_) {
89    switch(direction_) {
90    case AidlArgument::IN_DIR:
91      ret += "in ";
92      break;
93    case AidlArgument::OUT_DIR:
94      ret += "out ";
95      break;
96    case AidlArgument::INOUT_DIR:
97      ret += "inout ";
98      break;
99    }
100  }
101
102  ret += type_->ToString();
103  ret += " ";
104  ret += name_;
105
106  return ret;
107}
108
109AidlMethod::AidlMethod(std::string comments)
110    : comments_(comments) {}
111
112string Parser::FileName() {
113  return filename_;
114}
115
116string Parser::Package() {
117  return g_currentPackage ? g_currentPackage : "";
118}
119
120void Parser::ReportError(const string& err) {
121  /* FIXME: We're printing out the line number as -1. We used to use yylineno
122   * (which was NEVER correct even before reentrant parsing). Now we'll need
123   * another way.
124   */
125  cerr << filename_ << ":" << -1 << ": " << err << endl;
126  error_ = 1;
127}
128
129bool Parser::FoundNoErrors() {
130  return error_ == 0;
131}
132
133void *Parser::Scanner() {
134  return scanner_;
135}
136
137bool Parser::OpenFileFromDisk() {
138  FILE *in = fopen(FileName().c_str(), "r");
139
140  if (! in)
141    return false;
142
143  yyset_in(in, Scanner());
144  return true;
145}
146
147void Parser::SetFileContents(const std::string& contents) {
148  if (buffer_is_valid_)
149    yy_delete_buffer(buffer_, scanner_);
150
151  buffer_ = yy_scan_string(contents.c_str(), scanner_);
152  buffer_is_valid_ = true;
153}
154
155bool Parser::RunParser() {
156  int ret = yy::parser(this).parse();
157
158  delete[] g_currentPackage;
159  g_currentPackage = NULL;
160
161  return ret == 0 && error_ == 0;
162}
163
164void Parser::SetDocument(document_item_type *d)
165{
166  document_ = d;
167}
168
169void Parser::AddImport(const buffer_type& statement)
170{
171  import_info* import = new import_info();
172  memset(import, 0, sizeof(import_info));
173  import->from = cpp_strdup(this->FileName().c_str());
174  import->statement.lineno = statement.lineno;
175  import->statement.data = cpp_strdup(statement.data);
176  import->statement.extra = NULL;
177  import->next = imports_;
178  import->neededClass = android::aidl::parse_import_statement(statement.data);
179  imports_ = import;
180}
181
182document_item_type *Parser::GetDocument() const
183{
184  return document_;
185}
186
187import_info *Parser::GetImports() const
188{
189  return imports_;
190}
191
192