2 #include "aidl_language.h"
3 #include "aidl_language_y.h"
4 #include "search_path.h"
10 // comment and whitespace handling
11 // these functions save a copy of the buffer
12 static void begin_extra_text(unsigned lineno, which_extra_text which);
13 static void append_extra_text(char* text);
14 static extra_text_type* get_extra_text(void); // you now own the object
16 static void drop_extra_text(void);
19 static void do_package_statement(const char* importText);
21 #define SET_BUFFER(t) \
23 yylval.buffer.lineno = yylineno; \
24 yylval.buffer.token = (t); \
25 yylval.buffer.data = strdup(yytext); \
26 yylval.buffer.extra = get_extra_text(); \
34 %x COPYING LONG_COMMENT
36 identifier [_a-zA-Z][_a-zA-Z0-9\.]*
37 whitespace ([ \t\n\r]+)
38 brackets \[{whitespace}?\]
39 idvalue (0|[1-9][0-9]*)
44 \%\%\{ { begin_extra_text(yylineno, COPY_TEXT); BEGIN(COPYING); }
45 <COPYING>\}\%\% { BEGIN(INITIAL); }
46 <COPYING>.*\n { append_extra_text(yytext); }
47 <COPYING>.* { append_extra_text(yytext); }
48 <COPYING>\n+ { append_extra_text(yytext); }
51 \/\* { begin_extra_text(yylineno, (which_extra_text)LONG_COMMENT);
52 BEGIN(LONG_COMMENT); }
53 <LONG_COMMENT>[^*]* { append_extra_text(yytext); }
54 <LONG_COMMENT>\*+[^/] { append_extra_text(yytext); }
55 <LONG_COMMENT>\n { append_extra_text(yytext); }
56 <LONG_COMMENT>\**\/ { BEGIN(INITIAL); }
58 ^{whitespace}?import{whitespace}[^ \t\r\n]+{whitespace}?; {
62 ^{whitespace}?package{whitespace}[^ \t\r\n]+{whitespace}?; {
63 do_package_statement(yytext);
67 <<EOF>> { yyterminate(); }
69 \/\/.*\n { begin_extra_text(yylineno, SHORT_COMMENT);
70 append_extra_text(yytext); }
72 {whitespace} { /* begin_extra_text(yylineno, WHITESPACE);
73 append_extra_text(yytext); */ }
75 ; { SET_BUFFER(';'); return ';'; }
76 \{ { SET_BUFFER('{'); return '{'; }
77 \} { SET_BUFFER('}'); return '}'; }
78 \( { SET_BUFFER('('); return '('; }
79 \) { SET_BUFFER(')'); return ')'; }
80 , { SET_BUFFER(','); return ','; }
81 = { SET_BUFFER('='); return '='; }
84 parcelable { SET_BUFFER(PARCELABLE); return PARCELABLE; }
85 interface { SET_BUFFER(INTERFACE); return INTERFACE; }
86 flattenable { SET_BUFFER(FLATTENABLE); return FLATTENABLE; }
87 rpc { SET_BUFFER(INTERFACE); return RPC; }
88 in { SET_BUFFER(IN); return IN; }
89 out { SET_BUFFER(OUT); return OUT; }
90 inout { SET_BUFFER(INOUT); return INOUT; }
91 oneway { SET_BUFFER(ONEWAY); return ONEWAY; }
93 {brackets}+ { SET_BUFFER(ARRAY); return ARRAY; }
94 {idvalue} { SET_BUFFER(IDVALUE); return IDVALUE; }
95 {identifier} { SET_BUFFER(IDENTIFIER); return IDENTIFIER; }
96 {identifier}\<{whitespace}*{identifier}({whitespace}*,{whitespace}*{identifier})*{whitespace}*\> {
97 SET_BUFFER(GENERIC); return GENERIC; }
100 . { printf("UNKNOWN(%s)", yytext);
101 yylval.buffer.lineno = yylineno;
102 yylval.buffer.token = IDENTIFIER;
103 yylval.buffer.data = strdup(yytext);
109 // comment and whitespace handling
110 // ================================================
111 extra_text_type* g_extraText = NULL;
112 extra_text_type* g_nextExtraText = NULL;
114 void begin_extra_text(unsigned lineno, which_extra_text which)
116 extra_text_type* text = (extra_text_type*)malloc(sizeof(extra_text_type));
117 text->lineno = lineno;
122 if (g_nextExtraText == NULL) {
125 g_nextExtraText->next = text;
127 g_nextExtraText = text;
130 void append_extra_text(char* text)
132 if (g_nextExtraText->data == NULL) {
133 g_nextExtraText->data = strdup(text);
134 g_nextExtraText->len = strlen(text);
136 char* orig = g_nextExtraText->data;
137 unsigned oldLen = g_nextExtraText->len;
138 unsigned len = strlen(text);
139 g_nextExtraText->len += len;
140 g_nextExtraText->data = (char*)malloc(g_nextExtraText->len+1);
141 memcpy(g_nextExtraText->data, orig, oldLen);
142 memcpy(g_nextExtraText->data+oldLen, text, len);
143 g_nextExtraText->data[g_nextExtraText->len] = '\0';
151 extra_text_type* result = g_extraText;
153 g_nextExtraText = NULL;
157 void drop_extra_text(void)
159 extra_text_type* p = g_extraText;
161 extra_text_type* next = p->next;
167 g_nextExtraText = NULL;
172 // ================================================
173 void do_package_statement(const char* importText)
175 if (g_currentPackage) free((void*)g_currentPackage);
176 g_currentPackage = parse_import_statement(importText);
180 // main parse function
181 // ================================================
182 char const* g_currentFilename = NULL;
183 char const* g_currentPackage = NULL;
187 int parse_aidl(char const *filename)
189 yyin = fopen(filename, "r");
191 char const* oldFilename = g_currentFilename;
192 char const* oldPackage = g_currentPackage;
193 g_currentFilename = strdup(filename);
202 free((void*)g_currentFilename);
203 g_currentFilename = oldFilename;
205 if (g_currentPackage) free((void*)g_currentPackage);
206 g_currentPackage = oldPackage;
210 fprintf(stderr, "aidl: unable to open file for read: %s\n", filename);