aidl_language.cpp revision e25074935064cf06d30568ca61f2aaa0f75f0b9a
1#include "aidl_language.h"
2#include "aidl_language_y.hpp"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string>
6#include <iostream>
7
8#ifdef _WIN32
9int isatty(int  fd)
10{
11    return (fd == 0);
12}
13#endif
14
15using std::string;
16using std::cerr;
17using std::endl;
18
19Parser *psGlobal = NULL;
20ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks;
21char const* g_currentPackage = NULL;
22
23
24void yylex_init(void **);
25void yylex_destroy(void *);
26void yyset_in(FILE *f, void *);
27int yyparse(Parser*);
28
29Parser::Parser(const string& filename)
30    : filename_(filename) {
31  yylex_init(&scanner_);
32}
33
34Parser::~Parser() {
35  yylex_destroy(scanner_);
36}
37
38string Parser::FileName() {
39  return filename_;
40}
41
42string Parser::Package() {
43  return g_currentPackage;
44}
45
46void Parser::ReportError(const string& err) {
47  /* FIXME: We're printing out the line number as -1. We used to use yylineno
48   * (which was NEVER correct even before reentrant parsing). Now we'll need
49   * another way.
50   */
51  cerr << filename_ << ":" << -1 << ": " << err << endl;
52  error_ = 1;
53}
54
55bool Parser::FoundNoErrors() {
56  return error_ == 0;
57}
58
59void *Parser::Scanner() {
60  return scanner_;
61}
62
63bool Parser::OpenFileFromDisk() {
64  FILE *in = fopen(FileName().c_str(), "r");
65
66  if (! in)
67    return false;
68
69  yyset_in(in, Scanner());
70  return true;
71}
72
73bool Parser::RunParser() {
74  int ret = yy::parser(this).parse();
75
76  free((void *)g_currentPackage);
77  g_currentPackage = NULL;
78
79  if (error_)
80    return true;
81
82  return ret;
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