2007-05-24 07:40:45 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "tokenize.h"
|
2007-05-25 08:50:16 +02:00
|
|
|
#include "CommonCheck.h" // <- IsName
|
2007-05-24 07:40:45 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <locale>
|
|
|
|
#include <fstream>
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
#include <stdlib.h> // <- strtoul
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Helper functions..
|
|
|
|
|
|
|
|
static void Define(const char Name[], const char Value[]);
|
|
|
|
|
|
|
|
static void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
|
|
|
|
|
|
|
|
static void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]);
|
|
|
|
|
|
|
|
static void DeleteNextToken(TOKEN *tok);
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
std::vector<std::string> Files;
|
|
|
|
struct TOKEN *tokens, *tokens_back;
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Defined symbols.
|
|
|
|
// "#define abc 123" will create a defined symbol "abc" with the value 123
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
struct DefineSymbol
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
struct DefineSymbol *next;
|
|
|
|
};
|
|
|
|
static struct DefineSymbol * dsymlist;
|
|
|
|
|
|
|
|
static void Define(const char Name[], const char Value[])
|
|
|
|
{
|
|
|
|
if (!(Name && Name[0]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(Value && Value[0]))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Is 'Value' a decimal value..
|
|
|
|
bool dec = true, hex = true;
|
|
|
|
for (int i = 0; Value[i]; i++)
|
|
|
|
{
|
|
|
|
if ( ! std::isdigit(Value[i]) )
|
|
|
|
dec = false;
|
|
|
|
|
|
|
|
if ( ! std::isxdigit(Value[i]) && (!(i==1 && Value[i]=='x')))
|
|
|
|
hex = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dec && !hex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *strValue = strdup(Value);
|
|
|
|
|
|
|
|
if (!dec && hex)
|
|
|
|
{
|
|
|
|
char str[50];
|
|
|
|
unsigned long value = strtoul(Value+2, NULL, 16);
|
|
|
|
free(strValue);
|
|
|
|
strValue = strdup(itoa(value, str, 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
DefineSymbol *NewSym = new DefineSymbol;
|
|
|
|
memset(NewSym, 0, sizeof(DefineSymbol));
|
|
|
|
NewSym->name = strdup(Name);
|
|
|
|
NewSym->value = strValue;
|
|
|
|
NewSym->next = dsymlist;
|
|
|
|
dsymlist = NewSym;
|
|
|
|
}
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// addtoken
|
|
|
|
// add a token. Used by 'Tokenizer'
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
static void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno)
|
|
|
|
{
|
|
|
|
if (str[0] == 0)
|
|
|
|
return;
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// The keyword 'unsigned' can be skipped for now.
|
|
|
|
if (strcmp(str,"unsigned")==0)
|
|
|
|
return;
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
// Replace hexadecimal value with decimal
|
|
|
|
char str2[50];
|
|
|
|
memset(str2, 0, sizeof(str2));
|
|
|
|
if (strncmp(str,"0x",2)==0)
|
|
|
|
{
|
|
|
|
unsigned int value = strtoul(str+2, NULL, 16);
|
|
|
|
itoa(value, str2, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN *newtoken = new TOKEN;
|
|
|
|
memset(newtoken, 0, sizeof(TOKEN));
|
|
|
|
newtoken->str = strdup(str2[0] ? str2 : str);
|
|
|
|
newtoken->linenr = lineno;
|
|
|
|
newtoken->FileIndex = fileno;
|
|
|
|
if (tokens_back)
|
|
|
|
{
|
|
|
|
tokens_back->next = newtoken;
|
|
|
|
tokens_back = newtoken;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tokens = tokens_back = newtoken;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if str is defined..
|
|
|
|
for (DefineSymbol *sym = dsymlist; sym; sym = sym->next)
|
|
|
|
{
|
|
|
|
if (strcmp(str,sym->name)==0)
|
|
|
|
{
|
|
|
|
free(newtoken->str);
|
|
|
|
newtoken->str = strdup(sym->value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// combine_2tokens
|
|
|
|
// Combine two tokens that belong to each other. Ex: "<" and "=" may become "<="
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
static void combine_2tokens(TOKEN *tok, const char str1[], const char str2[])
|
|
|
|
{
|
|
|
|
if (!(tok && tok->next))
|
|
|
|
return;
|
|
|
|
if (strcmp(tok->str,str1) || strcmp(tok->next->str,str2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(tok->str);
|
|
|
|
tok->str = (char *)malloc(strlen(str1)+strlen(str2)+1);
|
|
|
|
strcpy(tok->str, str1);
|
|
|
|
strcat(tok->str, str2);
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
DeleteNextToken(tok);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// SizeOfType - gives the size of a type
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
std::map<std::string, unsigned int> TypeSize;
|
|
|
|
|
2007-05-30 22:08:05 +02:00
|
|
|
int SizeOfType(const char type[])
|
2007-05-28 08:17:18 +02:00
|
|
|
{
|
|
|
|
if (!type)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return TypeSize[type];
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// DeleteNextToken. Unlink and delete next token.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void DeleteNextToken(TOKEN *tok)
|
|
|
|
{
|
|
|
|
TOKEN *next = tok->next;
|
|
|
|
tok->next = next->next;
|
|
|
|
free(next->str);
|
|
|
|
delete next;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// InsertTokens - Copy and insert tokens
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void InsertTokens(TOKEN *dest, TOKEN *src, unsigned int n)
|
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
|
|
|
TOKEN *NewToken = new TOKEN;
|
|
|
|
NewToken->FileIndex = src->FileIndex;
|
|
|
|
NewToken->linenr = src->linenr;
|
|
|
|
NewToken->str = strdup(src->str);
|
|
|
|
|
|
|
|
NewToken->next = dest->next;
|
|
|
|
dest->next = NewToken;
|
|
|
|
|
|
|
|
dest = dest->next;
|
|
|
|
src = src->next;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Tokenize - tokenizes a given file.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
void Tokenize(const char FileName[])
|
|
|
|
{
|
|
|
|
// Has this file been tokenized already?
|
|
|
|
for (unsigned int i = 0; i < Files.size(); i++)
|
|
|
|
{
|
|
|
|
if ( stricmp(Files[i].c_str(), FileName) == 0 )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream fin(FileName);
|
|
|
|
if (!fin.is_open())
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int CurrentFile = Files.size();
|
|
|
|
Files.push_back(FileName);
|
|
|
|
|
|
|
|
unsigned int lineno = 1;
|
|
|
|
char CurrentToken[1000];
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
char *pToken = CurrentToken;
|
|
|
|
for (char ch = (char)fin.get(); !fin.eof(); ch = (char)fin.get())
|
|
|
|
{
|
|
|
|
if (ch == '#' && !CurrentToken[0])
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
getline(fin,line);
|
|
|
|
line = "#" + line;
|
|
|
|
if (strncmp(line.c_str(),"#include",8)==0 &&
|
|
|
|
line.find("\"") != std::string::npos)
|
|
|
|
{
|
|
|
|
// Extract the filename
|
|
|
|
line.erase(0, line.find("\"")+1);
|
|
|
|
line.erase(line.find("\""));
|
|
|
|
|
|
|
|
// Relative path..
|
|
|
|
if (strchr(FileName,'\\'))
|
|
|
|
{
|
|
|
|
char path[1000];
|
|
|
|
memset(path,0,sizeof(path));
|
|
|
|
const char *p = strrchr(FileName, '\\');
|
|
|
|
memcpy(path, FileName, p-FileName+1);
|
|
|
|
line = path + line;
|
|
|
|
}
|
|
|
|
|
|
|
|
addtoken("#include", lineno, CurrentFile);
|
|
|
|
addtoken(line.c_str(), lineno, CurrentFile);
|
|
|
|
|
|
|
|
Tokenize(line.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strncmp(line.c_str(), "#define", 7) == 0)
|
|
|
|
{
|
|
|
|
char *strId = NULL;
|
|
|
|
enum {Space1, Id, Space2, Value} State;
|
|
|
|
State = Space1;
|
|
|
|
for (unsigned int i = 8; i < line.length(); i++)
|
|
|
|
{
|
|
|
|
if (State==Space1 || State==Space2)
|
|
|
|
{
|
|
|
|
if (std::isspace(line[i]))
|
|
|
|
continue;
|
|
|
|
State = (State==Space1) ? Id : Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (State==Id && std::isspace(line[i]))
|
|
|
|
{
|
|
|
|
strId = strdup(CurrentToken);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
State = Space2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pToken = line[i];
|
|
|
|
pToken++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (State==Value)
|
|
|
|
{
|
|
|
|
Define(strId, CurrentToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
pToken = CurrentToken;
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
free(strId);
|
|
|
|
}
|
|
|
|
|
2007-06-02 18:32:07 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
addtoken("#", lineno, CurrentFile);
|
|
|
|
addtoken(";", lineno, CurrentFile);
|
|
|
|
}
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
lineno++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
// Add current token..
|
|
|
|
addtoken(CurrentToken, lineno++, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comments..
|
|
|
|
if (ch == '/' && !fin.eof())
|
|
|
|
{
|
|
|
|
// Add current token..
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
|
|
|
|
// Read next character..
|
|
|
|
ch = (char)fin.get();
|
|
|
|
|
|
|
|
// If '//'..
|
|
|
|
if (ch == '/')
|
|
|
|
{
|
|
|
|
while (!fin.eof() && (char)fin.get()!='\n');
|
|
|
|
lineno++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If '/*'..
|
|
|
|
if (ch == '*')
|
|
|
|
{
|
|
|
|
char chPrev;
|
|
|
|
ch = chPrev = 'A';
|
|
|
|
while (!fin.eof() && (chPrev!='*' || ch!='/'))
|
|
|
|
{
|
|
|
|
chPrev = ch;
|
|
|
|
ch = (char)fin.get();
|
|
|
|
if (ch == '\n')
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a comment.. add token..
|
|
|
|
addtoken("/", lineno, CurrentFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// char..
|
|
|
|
if (ch == '\'')
|
|
|
|
{
|
|
|
|
// Add previous token
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
|
|
|
|
// Read this ..
|
|
|
|
CurrentToken[0] = ch;
|
|
|
|
CurrentToken[1] = (char)fin.get();
|
|
|
|
CurrentToken[2] = (char)fin.get();
|
|
|
|
if (CurrentToken[1] == '\\')
|
|
|
|
CurrentToken[3] = (char)fin.get();
|
|
|
|
|
|
|
|
// Add token and start on next..
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String..
|
|
|
|
if (ch == '\"')
|
|
|
|
{
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
bool special = false;
|
|
|
|
char c = ch;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Append token..
|
2007-05-30 22:08:05 +02:00
|
|
|
if ( pToken < &CurrentToken[sizeof(CurrentToken)-10] )
|
|
|
|
{
|
|
|
|
*pToken = c;
|
|
|
|
pToken++;
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
|
|
|
|
// Special sequence '\.'
|
|
|
|
if (special)
|
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
|
|
|
c = (char)fin.get();
|
|
|
|
}
|
|
|
|
while (special || c != '\"');
|
|
|
|
*pToken = '\"';
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.",ch))
|
|
|
|
{
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
CurrentToken[0] = ch;
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
pToken = CurrentToken;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (std::isspace(ch) || std::iscntrl(ch))
|
|
|
|
{
|
|
|
|
addtoken(CurrentToken, lineno, CurrentFile);
|
|
|
|
pToken = CurrentToken;
|
|
|
|
memset(CurrentToken, 0, sizeof(CurrentToken));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pToken = ch;
|
|
|
|
pToken++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine tokens..
|
|
|
|
for (TOKEN *tok = tokens; tok && tok->next; tok = tok->next)
|
|
|
|
{
|
|
|
|
combine_2tokens(tok, "<", "<");
|
|
|
|
combine_2tokens(tok, ">", ">");
|
|
|
|
|
|
|
|
combine_2tokens(tok, "&", "&");
|
|
|
|
combine_2tokens(tok, "|", "|");
|
|
|
|
|
|
|
|
combine_2tokens(tok, "+", "=");
|
|
|
|
combine_2tokens(tok, "-", "=");
|
|
|
|
combine_2tokens(tok, "*", "=");
|
|
|
|
combine_2tokens(tok, "/", "=");
|
|
|
|
combine_2tokens(tok, "&", "=");
|
|
|
|
combine_2tokens(tok, "|", "=");
|
|
|
|
|
|
|
|
combine_2tokens(tok, "=", "=");
|
|
|
|
combine_2tokens(tok, "!", "=");
|
|
|
|
combine_2tokens(tok, "<", "=");
|
|
|
|
combine_2tokens(tok, ">", "=");
|
|
|
|
|
|
|
|
combine_2tokens(tok, ":", ":");
|
|
|
|
combine_2tokens(tok, "-", ">");
|
|
|
|
|
|
|
|
combine_2tokens(tok, "private", ":");
|
|
|
|
combine_2tokens(tok, "protected", ":");
|
|
|
|
combine_2tokens(tok, "public", ":");
|
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Simplify token list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SimplifyTokenList()
|
|
|
|
{
|
2007-05-24 07:40:45 +02:00
|
|
|
// Replace constants..
|
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
if (match(tok,"const type var = num ;"))
|
2007-05-24 07:40:45 +02:00
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
const char *sym = getstr(tok,2);
|
|
|
|
const char *num = getstr(tok,4);
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
for (TOKEN *tok2 = gettok(tok,6); tok2; tok2 = tok2->next)
|
|
|
|
{
|
|
|
|
if (strcmp(tok2->str,sym) == 0)
|
|
|
|
{
|
|
|
|
free(tok2->str);
|
|
|
|
tok2->str = strdup(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
2007-05-29 19:11:53 +02:00
|
|
|
// 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;
|
2007-06-02 18:32:07 +02:00
|
|
|
int len = 0, parlevel = 0;
|
|
|
|
for ( TOKEN *tok2 = gettok(type0,1); tok2; tok2 = tok2->next)
|
|
|
|
{
|
|
|
|
if (strchr("{(", tok2->str[0]))
|
|
|
|
parlevel++;
|
|
|
|
else if (strchr("})", tok2->str[0]))
|
|
|
|
parlevel--;
|
|
|
|
else if (parlevel==0 && tok2->str[0]==';')
|
|
|
|
break;
|
2007-05-29 19:11:53 +02:00
|
|
|
len++;
|
2007-06-02 18:32:07 +02:00
|
|
|
}
|
2007-05-29 19:11:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-06-03 19:08:30 +02:00
|
|
|
int parlevel = 0;
|
|
|
|
while (parlevel > 0 || tok2->next->str[0] != ';')
|
|
|
|
{
|
|
|
|
if (tok2->str[0] == '{')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok2->str[0] == '}')
|
|
|
|
parlevel--;
|
|
|
|
|
2007-05-29 19:11:53 +02:00
|
|
|
tok2 = tok2->next;
|
2007-06-03 19:08:30 +02:00
|
|
|
if (!tok2->next)
|
|
|
|
break;
|
|
|
|
}
|
2007-05-29 19:11:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN *next = tok2->next;
|
|
|
|
if (next && strcmp(next->str, typestr) == 0)
|
|
|
|
{
|
|
|
|
DeleteNextToken(tok2);
|
|
|
|
InsertTokens( tok2, type0, len );
|
2007-05-30 22:08:05 +02:00
|
|
|
tok2 = gettok(tok2, len);
|
2007-05-29 19:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete typedef..
|
|
|
|
if (!prev)
|
|
|
|
{
|
2007-06-03 19:08:30 +02:00
|
|
|
int parlevel = 0;
|
|
|
|
while ( parlevel > 0 || tokens->str[0] != ';' )
|
2007-05-29 19:11:53 +02:00
|
|
|
{
|
2007-06-03 19:08:30 +02:00
|
|
|
if ( strchr( "({", tokens->str[0] ) )
|
|
|
|
parlevel++;
|
|
|
|
else if ( strchr( ")}", tokens->str[0] ) )
|
|
|
|
parlevel--;
|
|
|
|
|
|
|
|
// Delete the first element in the tokens list.
|
2007-05-29 19:11:53 +02:00
|
|
|
TOKEN *next = tokens->next;
|
|
|
|
free(tokens->str);
|
|
|
|
delete tokens;
|
|
|
|
tokens = next;
|
2007-06-03 19:08:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (!tokens)
|
|
|
|
break;
|
2007-05-29 19:11:53 +02:00
|
|
|
}
|
|
|
|
tok = tokens;
|
|
|
|
prev = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-06-03 19:08:30 +02:00
|
|
|
int parlevel = 0;
|
|
|
|
while ( parlevel > 0 || prev->next->str[0] != ';' )
|
|
|
|
{
|
|
|
|
if ( strchr( "({", prev->next->str[0] ) )
|
|
|
|
parlevel++;
|
|
|
|
else if ( strchr( ")}", prev->next->str[0] ) )
|
|
|
|
parlevel--;
|
2007-05-29 19:11:53 +02:00
|
|
|
DeleteNextToken(prev);
|
2007-06-03 19:08:30 +02:00
|
|
|
if (!prev->next)
|
|
|
|
break;
|
|
|
|
}
|
2007-05-29 19:11:53 +02:00
|
|
|
tok = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// Fill the map TypeSize..
|
|
|
|
TypeSize.clear();
|
|
|
|
TypeSize["char"] = sizeof(char);
|
|
|
|
TypeSize["short"] = sizeof(short);
|
|
|
|
TypeSize["int"] = sizeof(int);
|
|
|
|
TypeSize["long"] = sizeof(long);
|
|
|
|
TypeSize["float"] = sizeof(float);
|
|
|
|
TypeSize["double"] = sizeof(double);
|
2007-05-25 08:50:16 +02:00
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
if (match(tok,"class type"))
|
|
|
|
{
|
|
|
|
TypeSize[getstr(tok,1)] = 11;
|
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
else if (match(tok, "struct type"))
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
TypeSize[getstr(tok,1)] = 13;
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
2007-05-28 08:17:18 +02:00
|
|
|
}
|
|
|
|
|
2007-05-25 08:50:16 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// Replace 'sizeof(type)'..
|
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if (strcmp(tok->str,"sizeof") != 0)
|
|
|
|
continue;
|
2007-05-25 08:50:16 +02:00
|
|
|
|
|
|
|
if (match(tok, "sizeof ( type * )"))
|
|
|
|
{
|
|
|
|
free(tok->str);
|
|
|
|
char str[10];
|
2007-05-28 08:17:18 +02:00
|
|
|
// 'sizeof(type *)' has the same size as 'sizeof(char *)'
|
2007-05-25 08:50:16 +02:00
|
|
|
tok->str = strdup(itoa(sizeof(char *), str, 10));
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-25 08:50:16 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
DeleteNextToken(tok);
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (match(tok, "sizeof ( type )"))
|
|
|
|
{
|
|
|
|
const char *type = getstr(tok, 2);
|
2007-05-28 08:17:18 +02:00
|
|
|
int size = SizeOfType(type);
|
|
|
|
if (size > 0)
|
2007-05-25 08:50:16 +02:00
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
free(tok->str);
|
|
|
|
char str[10];
|
|
|
|
tok->str = strdup(itoa(size, str, 10));
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
DeleteNextToken(tok);
|
|
|
|
}
|
2007-05-25 08:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
2007-05-26 08:44:28 +02:00
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
// Replace 'sizeof(var)'
|
2007-05-26 08:44:28 +02:00
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
// type array [ num ] ;
|
2007-05-26 08:44:28 +02:00
|
|
|
if ( ! match(tok, "type var [ num ] ;") )
|
|
|
|
continue;
|
|
|
|
|
2007-05-28 08:17:18 +02:00
|
|
|
int size = SizeOfType(tok->str);
|
2007-05-26 08:44:28 +02:00
|
|
|
if (size <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varname = getstr(tok, 1);
|
|
|
|
int total_size = size * atoi( getstr(tok, 3) );
|
|
|
|
|
|
|
|
// Replace 'sizeof(var)' with number
|
|
|
|
int indentlevel = 0;
|
|
|
|
for ( TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if (tok2->str[0] == '{')
|
|
|
|
{
|
|
|
|
indentlevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tok2->str[0] == '}')
|
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if (indentlevel < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (match(tok2, "sizeof ( var )"))
|
|
|
|
{
|
|
|
|
if (strcmp(getstr(tok2,2), varname) == 0)
|
|
|
|
{
|
|
|
|
free(tok2->str);
|
|
|
|
char str[20];
|
|
|
|
tok2->str = strdup(itoa(total_size, str, 10));
|
|
|
|
|
|
|
|
// Delete the other tokens..
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
DeleteNextToken(tok2);
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
2007-05-26 08:44:28 +02:00
|
|
|
// Simple calculations..
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if (match(tok->next, "* 1") || match(tok->next, "1 *"))
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
DeleteNextToken(tok);
|
2007-05-26 08:44:28 +02:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (1-2)
|
|
|
|
if (strchr("[,(=<>",tok->str[0]) &&
|
|
|
|
IsNumber(getstr(tok,1)) &&
|
|
|
|
strchr("+-*/",*(getstr(tok,2))) &&
|
|
|
|
IsNumber(getstr(tok,3)) &&
|
|
|
|
strchr("],);=<>",*(getstr(tok,4))) )
|
|
|
|
{
|
|
|
|
done = false;
|
|
|
|
|
|
|
|
int i1 = atoi(getstr(tok,1));
|
|
|
|
int i2 = atoi(getstr(tok,3));
|
|
|
|
switch (*(getstr(tok,2)))
|
|
|
|
{
|
|
|
|
case '+': i1 += i2; break;
|
|
|
|
case '-': i1 -= i2; break;
|
|
|
|
case '*': i1 *= i2; break;
|
|
|
|
case '/': i1 /= i2; break;
|
|
|
|
}
|
|
|
|
tok = tok->next;
|
|
|
|
free(tok->str);
|
|
|
|
char str[10];
|
|
|
|
tok->str = strdup(itoa(i1,str,10));
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2007-05-28 08:17:18 +02:00
|
|
|
DeleteNextToken(tok);
|
2007-05-26 08:44:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-28 12:34:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Replace "*(str + num)" => "str[num]"
|
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if ( ! strchr(";{}(=<>", tok->str[0]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TOKEN *next = tok->next;
|
|
|
|
if ( ! next )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (match(next, "* ( var + num )"))
|
|
|
|
{
|
|
|
|
const char *str[4] = {"var","[","num","]"};
|
|
|
|
str[0] = getstr(tok,3);
|
|
|
|
str[2] = getstr(tok,5);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
tok = tok->next;
|
|
|
|
free(tok->str);
|
|
|
|
tok->str = strdup(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteNextToken(tok);
|
|
|
|
DeleteNextToken(tok);
|
|
|
|
}
|
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Split up variable declarations if possible..
|
|
|
|
for (TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if ( ! strchr("{};", tok->str[0]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TOKEN *type0 = tok->next;
|
2007-06-02 18:32:07 +02:00
|
|
|
if (!type0)
|
|
|
|
break;
|
|
|
|
if (strcmp(type0->str, "else") == 0)
|
|
|
|
continue;
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
TOKEN *tok2 = NULL;
|
|
|
|
unsigned int typelen = 0;
|
|
|
|
|
|
|
|
if ( match(type0, "type var ,") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 2); // The ',' token
|
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( match(type0, "type * var ,") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 3); // The ',' token
|
2007-05-29 19:11:53 +02:00
|
|
|
typelen = 1;
|
2007-05-29 08:24:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ( match(type0, "type var [ num ] ,") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 5); // The ',' token
|
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( match(type0, "type * var [ num ] ,") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 6); // The ',' token
|
2007-05-29 19:11:53 +02:00
|
|
|
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);
|
2007-05-29 08:24:36 +02:00
|
|
|
typelen = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if ( match(type0, "type var =") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 2);
|
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( match(type0, "type * var =") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 3);
|
2007-05-29 19:11:53 +02:00
|
|
|
typelen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( match(type0, "struct type * var =") )
|
|
|
|
{
|
|
|
|
tok2 = gettok(type0, 4);
|
2007-05-29 08:24:36 +02:00
|
|
|
typelen = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2)
|
|
|
|
{
|
|
|
|
if (tok2->str[0] == ',')
|
|
|
|
{
|
|
|
|
free(tok2->str);
|
|
|
|
tok2->str = strdup(";");
|
|
|
|
InsertTokens(tok2, type0, typelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TOKEN *eq = tok2;
|
|
|
|
|
|
|
|
int parlevel = 0;
|
|
|
|
while (tok2)
|
|
|
|
{
|
|
|
|
if ( strchr("{(", tok2->str[0]) )
|
|
|
|
{
|
|
|
|
parlevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( strchr("})", tok2->str[0]) )
|
|
|
|
{
|
|
|
|
if (parlevel<0)
|
|
|
|
break;
|
|
|
|
parlevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( parlevel==0 && strchr(";,",tok2->str[0]) )
|
|
|
|
{
|
|
|
|
// "type var =" => "type var; var ="
|
2007-05-29 19:11:53 +02:00
|
|
|
TOKEN *VarTok = gettok(type0,typelen);
|
|
|
|
if (VarTok->str[0]=='*')
|
|
|
|
VarTok = VarTok->next;
|
|
|
|
InsertTokens(eq, VarTok, 2);
|
2007-05-29 08:24:36 +02:00
|
|
|
free(eq->str);
|
|
|
|
eq->str = strdup(";");
|
|
|
|
|
|
|
|
// "= x, " => "= x; type "
|
|
|
|
if (tok2->str[0] == ',')
|
|
|
|
{
|
|
|
|
free(tok2->str);
|
|
|
|
tok2->str = strdup(";");
|
|
|
|
InsertTokens( tok2, type0, typelen );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok2 = tok2->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 07:40:45 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Helper functions for handling the tokens list
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
TOKEN *findtoken(TOKEN *tok1, const char *tokenstr[])
|
|
|
|
{
|
|
|
|
for (TOKEN *ret = tok1; ret; ret = ret->next)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
TOKEN *tok = ret;
|
|
|
|
while (tokenstr[i])
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return NULL;
|
|
|
|
if (*(tokenstr[i]) && strcmp(tokenstr[i],tok->str))
|
|
|
|
break;
|
|
|
|
tok = tok->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (!tokenstr[i])
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool match(TOKEN *tok, const std::string pattern)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *p = pattern.c_str();
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
char str[50];
|
|
|
|
char *s = str;
|
|
|
|
while (*p==' ')
|
|
|
|
p++;
|
|
|
|
while (*p && *p!=' ')
|
|
|
|
{
|
|
|
|
*s = *p;
|
|
|
|
s++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
if (str[0] == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (strcmp(str,"var")==0 || strcmp(str,"type")==0)
|
|
|
|
{
|
|
|
|
if (!IsName(tok->str))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (strcmp(str,"num")==0)
|
|
|
|
{
|
|
|
|
if (!std::isdigit(tok->str[0]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (strcmp(str, tok->str) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->next;
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
TOKEN *gettok(TOKEN *tok, int index)
|
|
|
|
{
|
|
|
|
while (tok && index>0)
|
|
|
|
{
|
|
|
|
tok = tok->next;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char *getstr(TOKEN *tok, int index)
|
|
|
|
{
|
|
|
|
tok = gettok(tok, index);
|
|
|
|
return tok ? tok->str : "";
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
2007-05-29 19:11:53 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
|