TOKEN: Patterns like "if ; !!else" are now possible

This commit is contained in:
Reijo Tomperi 2008-12-12 21:00:25 +00:00
parent 0cfec3c89c
commit 4e8e35f666
3 changed files with 106 additions and 12 deletions

View File

@ -52,6 +52,8 @@ private:
TEST_CASE( match1 ); TEST_CASE( match1 );
TEST_CASE( match2 );
TEST_CASE( varid1 ); TEST_CASE( varid1 );
} }
@ -263,6 +265,68 @@ private:
} }
} }
void match2()
{
{
const std::string code("");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "!!else") );
}
{
const std::string code("if ;");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "if ; !!else") );
}
{
const std::string code("if ; something");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "if ; !!else") );
}
{
const std::string code("else");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS( false, TOKEN::Match(tokenizer.tokens(), "!!else") );
}
{
const std::string code("if ; else");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS( false, TOKEN::Match(tokenizer.tokens(), "if ; !!else") );
}
}
void varid1() void varid1()
{ {

View File

@ -142,19 +142,24 @@ int TOKEN::multiCompare( const char *needle, const char *haystack )
return -1; return -1;
} }
bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[], unsigned int varid) bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[], unsigned int varid)
{ {
if (!tok)
return false;
const char *p = pattern; const char *p = pattern;
while (*p) while ( *p )
{ {
// Skip spaces in pattern.. // Skip spaces in pattern..
while ( *p == ' ' ) while ( *p == ' ' )
p++; p++;
if (!tok)
{
// If we have no tokens, pattern "!!else" should return true
if( isNotPattern( p ) )
return true;
else
return false;
}
// Extract token from pattern.. // Extract token from pattern..
// TODO: Refactor this so there can't be buffer overflows // TODO: Refactor this so there can't be buffer overflows
char str[500]; char str[500];
@ -254,18 +259,31 @@ bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[]
} }
} }
// Parse "not" options. Token can be anything except the given one
else if( isNotPattern( str ) )
{
if( strcmp( tok->aaaa(), &(str[2]) ) == 0 )
return false;
}
else if (str != tok->_str) else if (str != tok->_str)
return false; return false;
tok = tok->next(); tok = tok->next();
if (!tok && *p)
return false;
} }
// The end of the pattern has been reached and nothing wrong has been found // The end of the pattern has been reached and nothing wrong has been found
return true; return true;
} }
bool TOKEN::isNotPattern( const char *pattern )
{
if( pattern && strlen(pattern) > 2 && pattern[0] == '!' && pattern[1] == '!' )
return true;
else
return false;
}
bool TOKEN::isName() const bool TOKEN::isName() const
{ {
return _isName; return _isName;

12
token.h
View File

@ -75,6 +75,7 @@ public:
* "[abc]" Any of the characters 'a' or 'b' or 'c' * "[abc]" Any of the characters 'a' or 'b' or 'c'
* "int|void|char" Any of the strings, int, void or char * "int|void|char" Any of the strings, int, void or char
* "int|void|char|" Any of the strings, int, void or char or empty string * "int|void|char|" Any of the strings, int, void or char or empty string
* "!!else" No tokens or any token that is not "else".
* "someRandomText" If token contains "someRandomText". * "someRandomText" If token contains "someRandomText".
* *
* The patterns can be also combined to compare to multiple tokens at once * The patterns can be also combined to compare to multiple tokens at once
@ -159,6 +160,17 @@ public:
private: private:
void next( TOKEN *next ); void next( TOKEN *next );
void previous( TOKEN *previous ); void previous( TOKEN *previous );
/**
* Return true if pattern is e.g. "!!else".
* See Match() for more info.
*
* @param pattern Pattern to match, e.g. "if ; !!else"
* @return true if pattern starts with "!!" and contains 3
* or more characters.
*/
static bool isNotPattern( const char *pattern );
std::string _str; std::string _str;
char * _cstr; char * _cstr;
bool _isName; bool _isName;