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

refactored lexer

parent ef869ed0
No related branches found
No related tags found
No related merge requests found
......@@ -111,54 +111,65 @@ enum ifdef_os {
YY_BUFFER_STATE ifdef_stack[MAX_IFDEF_DEPTH];
int ifdef_stack_ptr = 0;
static void handle_ifdef(enum ifdef_os os, const char *s) {
char *code = strdup(s);
unsigned int ifdef_length = strlen("#ifdef ");
unsigned int endif_length = strlen("#endif");
unsigned int os_name_length = 0;
static inline int get_os_name_length(enum ifdef_os os) {
switch (os) {
case Linux_IFDEF:
os_name_length = strlen("Linux");
break;
return strlen("Linux");
case FreeBSD_IFDEF:
os_name_length = strlen("FreeBSD");
break;
return strlen("FreeBSD");
case NetBSD_IFDEF:
os_name_length = strlen("NetBSD");
break;
return strlen("NetBSD");
case OpenBSD_IFDEF:
os_name_length = strlen("OpenBSD");
break;
return strlen("OpenBSD");
default:
fprintf(stderr, "handle_ifdef with unknown os called.\n");
exit(1);
return -1;
}
}
static inline bool ignore_ifdef(enum ifdef_os os) {
#ifdef linux
if (os != Linux_IFDEF) {
return;
if (os == Linux_IFDEF) {
return false;
}
#endif
#ifdef __FreeBSD__
if (os != FreeBSD_IFDEF) {
return;
if (os == FreeBSD_IFDEF) {
return false;
}
#endif
#ifdef __OpenBSD__
if (os != OpenBSD_IFDEF) {
return;
if (os == OpenBSD_IFDEF) {
return false;
}
#endif
#ifdef __NetBSD__
if (os != NetBSD_IFDEF) {
return;
if (os == NetBSD_IFDEF) {
return false;
}
#endif
char *code_without_ifdef = code+ifdef_length+os_name_length;
code_without_ifdef[strlen(code_without_ifdef) - (endif_length)] = (char) 0;
return true;
}
static void handle_ifdef(enum ifdef_os os, const char *s) {
char *code = strdup(s);
unsigned int ifdef_length = strlen("#ifdef ");
unsigned int endif_length = strlen("#endif");
int os_name_length = get_os_name_length(os);
if (os_name_length == -1) {
fprintf(stderr, "handle_ifdef with unknown os called.\n");
exit(1);
}
if (ignore_ifdef(os)) {
return;
}
char *code_without_ifdef = code + ifdef_length + os_name_length;
unsigned int newline_before_endif = strlen(code_without_ifdef) - endif_length;
code_without_ifdef[newline_before_endif] = (char) 0;
// fprintf( stdout, "\n%s\n", code_without_ifdef);
if (ifdef_stack_ptr >= MAX_IFDEF_DEPTH) {
......
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