blob: 52529205575530a7db5ffb16a4da75cd5c59a505 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include "aidl_language.h"
2#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003#include <stdlib.h>
Casey Dahlin9941dcc2015-09-09 17:59:06 -07004#include <string>
5#include <iostream>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006
Casey Dahlin9941dcc2015-09-09 17:59:06 -07007using std::string;
8using std::cerr;
9using std::endl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010
11ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks;
12
Casey Dahlin9941dcc2015-09-09 17:59:06 -070013void yylex_init(void **);
14void yylex_destroy(void *);
15void yyset_in(FILE *f, void *);
16int yyparse(ParseState*);
17
18ParseState::ParseState() : ParseState("") {}
19
20ParseState::ParseState(const string& filename)
21 : filename_(filename) {
22 yylex_init(&scanner_);
23}
24
25ParseState::~ParseState() {
26 yylex_destroy(scanner_);
27}
28
29string ParseState::FileName() {
30 return filename_;
31}
32
33string ParseState::Package() {
34 return g_currentPackage;
35}
36
37void 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
43void 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
49void 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
58bool ParseState::FoundNoErrors() {
59 return error_ == 0;
60}
61
62void *ParseState::Scanner() {
63 return scanner_;
64}
65
66bool 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
76int 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}