From 012ba2f492282f1387c63780ea76e4ef8174ef25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 May 2007 17:11:53 +0000 Subject: [PATCH] Tokenize: Replace 'typedef' --- tokenize.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++- tokenize.h | 4 ++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/tokenize.cpp b/tokenize.cpp index b7f473b1a..86b7c0fc0 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -525,6 +525,68 @@ void SimplifyTokenList() + // typedefs.. + TOKEN *prev = NULL; + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + if (!prev) + tok = tokens; + + if ( strcmp(tok->str, "typedef") == 0 ) + { + TOKEN *type0 = tok->next; + int len = 1; + while ( strcmp(getstr(type0, len+1), ";") != 0) + len++; + + const char *typestr = getstr(type0, len); + if (typestr[0] != ')') // Function pointer + { + // Replace tokens.. + for ( TOKEN *tok2 = gettok(type0, len+1); tok2; tok2 = tok2->next ) + { + if (strcmp(tok2->str,"typedef")==0) + { + while (tok2->next->str[0] != ';') + tok2 = tok2->next; + continue; + } + + TOKEN *next = tok2->next; + if (next && strcmp(next->str, typestr) == 0) + { + DeleteNextToken(tok2); + InsertTokens( tok2, type0, len ); + } + } + } + + // Delete typedef.. + if (!prev) + { + while ( tokens && tokens->str[0] != ';' ) + { + TOKEN *next = tokens->next; + free(tokens->str); + delete tokens; + tokens = next; + } + tok = tokens; + prev = NULL; + continue; + } + else + { + while ( prev->next->str[0] != ';' ) + DeleteNextToken(prev); + tok = prev; + } + } + prev = tok; + } + + + // Fill the map TypeSize.. TypeSize.clear(); TypeSize["char"] = sizeof(char); @@ -732,7 +794,7 @@ void SimplifyTokenList() else if ( match(type0, "type * var ,") ) { tok2 = gettok(type0, 3); // The ',' token - typelen = 2; + typelen = 1; } else if ( match(type0, "type var [ num ] ,") ) @@ -744,6 +806,18 @@ void SimplifyTokenList() else if ( match(type0, "type * var [ num ] ,") ) { tok2 = gettok(type0, 6); // The ',' token + typelen = 1; + } + + else if ( match(type0, "struct type var ,") ) + { + tok2 = gettok(type0, 3); + typelen = 2; + } + + else if ( match(type0, "struct type * var ,") ) + { + tok2 = gettok(type0, 4); typelen = 2; } @@ -757,6 +831,12 @@ void SimplifyTokenList() else if ( match(type0, "type * var =") ) { tok2 = gettok(type0, 3); + typelen = 1; + } + + else if ( match(type0, "struct type * var =") ) + { + tok2 = gettok(type0, 4); typelen = 2; } @@ -791,7 +871,10 @@ void SimplifyTokenList() else if ( parlevel==0 && strchr(";,",tok2->str[0]) ) { // "type var =" => "type var; var =" - InsertTokens(eq, gettok(type0,typelen), 2); + TOKEN *VarTok = gettok(type0,typelen); + if (VarTok->str[0]=='*') + VarTok = VarTok->next; + InsertTokens(eq, VarTok, 2); free(eq->str); eq->str = strdup(";"); @@ -912,5 +995,26 @@ const char *getstr(TOKEN *tok, int index) +// Deallocate lists.. +void DeallocateTokens() +{ + while (tokens) + { + TOKEN *next = tokens->next; + free(tokens->str); + delete tokens; + tokens = next; + } + tokens_back = tokens; + + while (dsymlist) + { + struct DefineSymbol *next = dsymlist->next; + free(dsymlist->name); + free(dsymlist->value); + delete dsymlist; + dsymlist = next; + } +} diff --git a/tokenize.h b/tokenize.h index c5f4e09b0..0abb50b20 100644 --- a/tokenize.h +++ b/tokenize.h @@ -25,6 +25,10 @@ void Tokenize(const char FileName[]); void SimplifyTokenList(); +// Deallocate lists.. +void DeallocateTokens(); + + // Helper functions for handling the tokens list.. TOKEN *findtoken(TOKEN *tok1, const char *tokenstr[]); bool match(TOKEN *tok, const std::string pattern);