2008-11-24 09:59:10 +01:00
|
|
|
/*
|
2008-11-24 08:35:03 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007-2008 Daniel Marjamäki and Reijo Tomperi
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "token.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#include <ctype.h> // isalpha, isdigit
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TOKEN::TOKEN()
|
|
|
|
{
|
|
|
|
FileIndex = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
_cstr = 0;
|
|
|
|
_str = "";
|
2008-11-24 08:35:03 +01:00
|
|
|
linenr = 0;
|
|
|
|
next = 0;
|
|
|
|
_isName = false;
|
|
|
|
_isNumber = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOKEN::~TOKEN()
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
std::free(_cstr);
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::setstr( const char s[] )
|
2008-11-24 20:38:08 +01:00
|
|
|
{
|
|
|
|
_str = s;
|
|
|
|
std::free(_cstr);
|
2008-11-24 08:35:03 +01:00
|
|
|
#ifndef _MSC_VER
|
2008-11-24 20:38:08 +01:00
|
|
|
_cstr = strdup(s);
|
2008-11-24 08:35:03 +01:00
|
|
|
#else
|
2008-11-24 20:38:08 +01:00
|
|
|
_cstr = _strdup(s);
|
2008-11-24 08:35:03 +01:00
|
|
|
#endif
|
2008-11-24 20:38:08 +01:00
|
|
|
_isName = bool(_str[0]=='_' || isalpha(_str[0]));
|
2008-11-26 08:22:34 +01:00
|
|
|
_isNumber = bool(isdigit(_str[0]) != 0);
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::combineWithNext(const char str1[], const char str2[])
|
|
|
|
{
|
|
|
|
if (!(next))
|
|
|
|
return;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (_str!=str1 || next->_str!=str2)
|
2008-11-24 08:35:03 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::string newstr(std::string(str1) + std::string(str2));
|
|
|
|
setstr( newstr.c_str() );
|
|
|
|
deleteNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOKEN::deleteNext()
|
|
|
|
{
|
|
|
|
TOKEN *n = next;
|
|
|
|
next = n->next;
|
|
|
|
delete n;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::tokAt(int index) const
|
|
|
|
{
|
|
|
|
const TOKEN *tok = this;
|
|
|
|
while (index>0 && tok)
|
|
|
|
{
|
|
|
|
tok = tok->next;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *TOKEN::strAt(int index) const
|
|
|
|
{
|
|
|
|
const TOKEN *tok = this->tokAt(index);
|
2008-11-24 20:38:08 +01:00
|
|
|
return tok ? tok->_cstr : "";
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *p = pattern;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
// Skip spaces in pattern..
|
|
|
|
while ( *p == ' ' )
|
|
|
|
p++;
|
|
|
|
|
|
|
|
// Extract token from pattern..
|
|
|
|
char str[50];
|
|
|
|
char *s = str;
|
|
|
|
while (*p && *p!=' ')
|
|
|
|
{
|
|
|
|
*s = *p;
|
|
|
|
s++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
// No token => Success!
|
|
|
|
if (str[0] == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Any symbolname..
|
|
|
|
if (strcmp(str,"%var%")==0 || strcmp(str,"%type%")==0)
|
|
|
|
{
|
|
|
|
if (!tok->isName())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable name..
|
|
|
|
else if (strcmp(str,"%var1%")==0 || strcmp(str,"%var2%")==0)
|
|
|
|
{
|
|
|
|
const char **varname = (strcmp(str,"%var1%")==0) ? varname1 : varname2;
|
|
|
|
|
|
|
|
if ( ! varname )
|
|
|
|
return false;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->_str != varname[0])
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( int i = 1; varname[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( !(tok->tokAt(2)) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(tok->strAt( 1), ".") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(tok->strAt( 2), varname[i]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strcmp(str,"%num%")==0)
|
|
|
|
{
|
|
|
|
if ( ! tok->isNumber() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (strcmp(str,"%str%")==0)
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->_str[0] != '\"' )
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [.. => search for a one-character token..
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (str[0]=='[' && strchr(str, ']') && tok->_str[1] == 0)
|
2008-11-24 08:35:03 +01:00
|
|
|
{
|
|
|
|
*strrchr(str, ']') = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( strchr( str + 1, tok->_str[0] ) == 0 )
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (str != tok->_str)
|
2008-11-24 08:35:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->next;
|
|
|
|
if (!tok && *p)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the pattern has been reached and nothing wrong has been found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TOKEN::isName() const
|
|
|
|
{
|
|
|
|
return _isName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TOKEN::isNumber() const
|
|
|
|
{
|
|
|
|
return _isNumber;
|
|
|
|
}
|
|
|
|
|
2008-11-26 08:22:34 +01:00
|
|
|
bool TOKEN::isStandardType() const
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2008-11-24 08:35:03 +01:00
|
|
|
const char *type[] = {"bool","char","short","int","long","float","double",0};
|
|
|
|
for (int i = 0; type[i]; i++)
|
2008-11-26 08:22:34 +01:00
|
|
|
ret |= (_str == type[i]);
|
|
|
|
return ret;
|
2008-11-24 08:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
|
|
|
|
{
|
|
|
|
for ( ; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if ( TOKEN::Match(tok, pattern, varname1, varname2) )
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TOKEN *TOKEN::findtoken(const TOKEN *tok1, const char *tokenstr[])
|
|
|
|
{
|
|
|
|
for (const TOKEN *ret = tok1; ret; ret = ret->next)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
const TOKEN *tok = ret;
|
|
|
|
while (tokenstr[i])
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return NULL;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (*(tokenstr[i]) && (tokenstr[i] != tok->_str))
|
2008-11-24 08:35:03 +01:00
|
|
|
break;
|
|
|
|
tok = tok->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (!tokenstr[i])
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|