From 977c8815482495af8ae3a5a90b17c960787e0ded Mon Sep 17 00:00:00 2001 From: Alexey Dobriyan Date: Wed, 14 Apr 2010 21:53:22 +0300 Subject: [PATCH] acc: "parse" #ifdef, #ifndef --- main.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/main.c b/main.c index a4215c6..31beb9b 100644 --- a/main.c +++ b/main.c @@ -214,6 +214,8 @@ struct pp_token { PP_TOKEN_INCLUDE_GLOBAL, /* #include < */ PP_TOKEN_INCLUDE_LOCAL, /* #include " */ PP_TOKEN_DEFINE, /* #define */ + PP_TOKEN_IFDEF, /* #ifdef */ + PP_TOKEN_IFNDEF, /* #ifndef */ #define _2(c1, c2) (((c1) << 8) | (c2)) #define _3(c1, c2, c3) (((c1) << 16)| ((c2) << 8) | (c3)) @@ -313,6 +315,12 @@ static void pp_token_print(struct pp_token *ppt) case PP_TOKEN_DEFINE: printf("#define %s", ppt->str); break; + case PP_TOKEN_IFDEF: + printf("#ifdef %s", ppt->str); + break; + case PP_TOKEN_IFNDEF: + printf("#ifndef %s", ppt->str); + break; case PP_TOKEN_DOTDOTDOT: case PP_TOKEN_LSHIFT_EQ: case PP_TOKEN_RSHIFT_EQ: @@ -606,6 +614,50 @@ static struct pp_token *pp_include(const char *c, unsigned int nr_c, unsigned in return ppt; } +static struct pp_token *pp_ifdef(const char *c, unsigned int nr_c, unsigned int *start, struct pos *pos) +{ + struct pp_token *ppt; + unsigned int name_start, name_end; + unsigned int i; + + i = *start; + if (i >= nr_c || !pp_nondigit(c[i])) + error_exit(pos, "invalid #ifdef directive"); + name_start = i; + i = _pp_identifier_end(c, nr_c, i + 1); + name_end = i; + + ppt = pp_token_create(PP_TOKEN_IFDEF, pos); + pp_token_add(ppt, c, name_start, name_end); + + while (i < nr_c && c[i] != '\n') + i++; + *start = i + 1; + return ppt; +} + +static struct pp_token *pp_ifndef(const char *c, unsigned int nr_c, unsigned int *start, struct pos *pos) +{ + struct pp_token *ppt; + unsigned int name_start, name_end; + unsigned int i; + + i = *start; + if (i >= nr_c || !pp_nondigit(c[i])) + error_exit(pos, "invalid #ifndef directive"); + name_start = i; + i = _pp_identifier_end(c, nr_c, i + 1); + name_end = i; + + ppt = pp_token_create(PP_TOKEN_IFNDEF, pos); + pp_token_add(ppt, c, name_start, name_end); + + while (i < nr_c && c[i] != '\n') + i++; + *start = i + 1; + return ppt; +} + static struct pp_token *pp_tokenize(const char *c, unsigned int nr_c, struct pos *_pos) { struct pp_token *ppt_head, *ppt_tail; @@ -628,6 +680,8 @@ static struct pp_token *pp_tokenize(const char *c, unsigned int nr_c, struct pos { .len = 6, .str = "define", .f = pp_define, }, { .len = 5, .str = "error", .f = pp_error, }, { .len = 7, .str = "include", .f = pp_include, }, + { .len = 5, .str = "ifdef", .f = pp_ifdef, }, + { .len = 6, .str = "ifndef", .f = pp_ifndef, }, }; unsigned int sharp_start, ppd_start, ppd_end; unsigned int j, k; -- 2.11.4.GIT