aidl_language.cpp revision f690be5044a56fdf75bb0bcae640074ec97023cd
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;
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  if (error_)
83    return true;
84
85  return ret;
86}
87
88void Parser::SetDocument(document_item_type *d)
89{
90  document_ = d;
91}
92
93void Parser::AddImport(const buffer_type& statement)
94{
95  import_info* import = (import_info*)malloc(sizeof(import_info));
96  memset(import, 0, sizeof(import_info));
97  import->from = strdup(this->FileName().c_str());
98  import->statement.lineno = statement.lineno;
99  import->statement.data = strdup(statement.data);
100  import->statement.extra = NULL;
101  import->next = imports_;
102  import->neededClass = android::aidl::parse_import_statement(statement.data);
103  imports_ = import;
104}
105
106document_item_type *Parser::GetDocument() const
107{
108  return document_;
109}
110
111import_info *Parser::GetImports() const
112{
113  return imports_;
114}
115