Skip to content
Snippets Groups Projects
Commit f036a312 authored by Julian Cordes's avatar Julian Cordes
Browse files

refactord structure of lexer a bit

parent 97cbdfee
No related branches found
No related tags found
No related merge requests found
...@@ -151,12 +151,22 @@ static inline bool ignore_ifdef(enum ifdef_os os) { ...@@ -151,12 +151,22 @@ static inline bool ignore_ifdef(enum ifdef_os os) {
return true; return true;
} }
static void handle_ifdef(enum ifdef_os os, const char *s) { static inline char* remove_ifdef_start_and_endtag(char *code, int os_name_length) {
char *code = NULL;
char *code_without_ifdef = NULL;
unsigned int ifdef_length = strlen("#ifdef "); unsigned int ifdef_length = strlen("#ifdef ");
unsigned int endif_length = strlen("#endif"); unsigned int endif_length = strlen("#endif");
unsigned int newline_before_endif = 0; unsigned int newline_before_endif = 0;
char *code_without_ifdef = NULL;
code_without_ifdef = code + ifdef_length + os_name_length;
newline_before_endif = strlen(code_without_ifdef) - endif_length;
code_without_ifdef[newline_before_endif] = (char) 0;
return code_without_ifdef;
}
static void handle_ifdef(enum ifdef_os os, const char *s) {
char *code = NULL;
char *code_without_ifdef = NULL;
int os_name_length = get_os_name_length(os); int os_name_length = get_os_name_length(os);
if (os_name_length == -1) { if (os_name_length == -1) {
...@@ -174,13 +184,10 @@ static void handle_ifdef(enum ifdef_os os, const char *s) { ...@@ -174,13 +184,10 @@ static void handle_ifdef(enum ifdef_os os, const char *s) {
} }
code = strdup(s); code = strdup(s);
code_without_ifdef = code + ifdef_length + os_name_length;
newline_before_endif = strlen(code_without_ifdef) - endif_length;
code_without_ifdef[newline_before_endif] = (char) 0;
code_without_ifdef = remove_ifdef_start_and_endtag(code, os_name_length);
ifdef_stack[ifdef_stack_ptr++] = YY_CURRENT_BUFFER; ifdef_stack[ifdef_stack_ptr++] = YY_CURRENT_BUFFER;
YY_BUFFER_STATE state = yy_scan_string(code_without_ifdef); yy_switch_to_buffer(yy_scan_string(code_without_ifdef));
yy_switch_to_buffer(state);
free(code); free(code);
} }
...@@ -215,8 +222,11 @@ c_comment \/\*(([^*])|(\*[^\/]))*\*\/ ...@@ -215,8 +222,11 @@ c_comment \/\*(([^*])|(\*[^\/]))*\*\/
* (specific code only for linux) * (specific code only for linux)
* #endif * #endif
*/ */
/* these are the tags that identify the start and ending of an ifdef block */
ifdef_begin #ifdef[ ] ifdef_begin #ifdef[ ]
ifdef_end #endif ifdef_end #endif
/* end_matcher actually matches everything except the "#endif" tag. */
end_matcher (([^#])|(#[^e])|(#e[^n])|(#en[^d])|(#end[^i])|(#endi[^f]))* end_matcher (([^#])|(#[^e])|(#e[^n])|(#en[^d])|(#end[^i])|(#endi[^f]))*
ifdef_freebsd {ifdef_begin}FreeBSD{end_matcher}{ifdef_end} ifdef_freebsd {ifdef_begin}FreeBSD{end_matcher}{ifdef_end}
...@@ -589,12 +599,12 @@ NULL return NULL_; ...@@ -589,12 +599,12 @@ NULL return NULL_;
{code} yylval.string = code(yytext); return CODE; {code} yylval.string = code(yytext); return CODE;
{ipv4_addr} yylval.string = strdup(yytext); return IPV4_ADDR; {ipv4_addr} yylval.string = strdup(yytext); return IPV4_ADDR;
{ipv6_addr} yylval.string = strdup(yytext); return IPV6_ADDR; {ipv6_addr} yylval.string = strdup(yytext); return IPV6_ADDR;
<<EOF>> { <<EOF>> {
if ( --ifdef_stack_ptr < 0 ) { if ( --ifdef_stack_ptr < 0 ) {
yyterminate(); yyterminate();
} else { } else {
yy_delete_buffer(YY_CURRENT_BUFFER); yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(ifdef_stack[ifdef_stack_ptr]); yy_switch_to_buffer(ifdef_stack[ifdef_stack_ptr]);
}
} }
}
%% %%
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment