tokenizer: simplify redundant paranthesis

This commit is contained in:
Daniel Marjamäki 2009-01-26 16:38:08 +00:00
parent 9fb11bbfcc
commit ca0f007ca4
3 changed files with 187 additions and 147 deletions

View File

@ -915,6 +915,7 @@ void Tokenizer::simplifyTokenList()
modified |= simplifyFunctionReturn();
modified |= simplifyKnownVariables();
modified |= removeReduntantConditions();
modified |= simplifyRedundantParanthesis();
}
}
//---------------------------------------------------------------------------
@ -1367,6 +1368,39 @@ bool Tokenizer::simplifyKnownVariables()
return ret;
}
bool Tokenizer::simplifyRedundantParanthesis()
{
bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::simpleMatch(tok, "( ("))
{
int parlevel = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
--parlevel;
if (parlevel == 1)
{
if (Token::simpleMatch(tok2, ") )"))
{
tok->deleteNext();
tok2->deleteNext();
}
break;
}
}
}
}
}
return ret;
}
//---------------------------------------------------------------------------
// Helper functions for handling the tokens list
//---------------------------------------------------------------------------

View File

@ -127,12 +127,18 @@ private:
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
*
* @param token The token list to check and modify.
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyKnownVariables();
/**
* Remove redundant paranthesis: "((x))" => "(x)"
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyRedundantParanthesis();
void InsertTokens(Token *dest, Token *src, unsigned int n);
Token *_tokensBack;

View File

@ -75,7 +75,7 @@ private:
// TODO TEST_CASE(simplify_function_parameters);
// TODO TEST_CASE(reduce_redundant_paranthesis); // Ticket #61
TEST_CASE(reduce_redundant_paranthesis); // Ticket #61
}