Skip to content
Snippets Groups Projects
Commit faeaa445 authored by zldunn's avatar zldunn
Browse files

QL works with nested parentheses (sp)

parent a39561b4
No related branches found
No related tags found
1 merge request!10Basic query language
......@@ -130,6 +130,25 @@ vector<Tuple * > QueryParser::breakOnOR( string input )
if( query[ i ] == "(")
{
++depth;
++i;
string text;
while ( depth != 0)
{
if( query[ i ] == "(")
++depth;
else if ( query[ i ] == ")")
--depth;
if( depth != 0)
{
if( text!= "")
text+=" ";
text+=query[ i ];
}
}
Tuple * subConstraint = Constraint( text );
constraintList.push_back( subConstraint );
start = i + 1;
}
else if( query[ i ] == ")")
{
......@@ -173,7 +192,7 @@ vector<Tuple * > QueryParser::breakOnOR( string input )
*/
bool QueryParser::isOrType( string input )
{
vector<string> query = splitStr (input, ' ', true);
vector<string> query = splitStr (input, ' ', false);
int depth = 0;
for( auto word = query.begin(); word != query.end(); ++word )
{
......@@ -198,7 +217,7 @@ bool QueryParser::isOrType( string input )
*/
bool QueryParser::isAndType( string input )
{
vector<string> query = splitStr (input, ' ', true);
vector<string> query = splitStr (input, ' ', false);
if( query.size( ) == 1)
return false;
......@@ -238,7 +257,7 @@ vector<Tuple * > QueryParser::breakOnAND( string input )
closedBracket.insert(')');
closedBracket.insert('}');
closedBracket.insert(']');
vector<string> query = splitStr (input, ' ', true);
vector<string> query = splitStr (input, ' ', false);
vector<Tuple *> constraintList;
int start = 0;
......@@ -248,6 +267,26 @@ vector<Tuple * > QueryParser::breakOnAND( string input )
if( query[ i ] == "(")
{
++depth;
++i;
string text;
while ( depth != 0)
{
if( query[ i ] == "(")
++depth;
else if ( query[ i ] == ")")
--depth;
if( depth != 0)
{
if( text!= "")
text+=" ";
text+=query[ i ];
}
++i;
}
Tuple * subConstraint = Constraint( text );
constraintList.push_back( subConstraint );
start = i + 1;
}
else if( query[ i ] == ")")
{
......
......@@ -13,7 +13,7 @@ int main()
parser.parse( query );
parser.printCompiledQuery();
string query1 = " apollo moon (landing OR fake)";
string query1 = " apollo moon ( landing OR fake )";
QueryParser parser1;
parser1.parse( query1 );
parser1.printCompiledQuery ();
......
......@@ -210,7 +210,14 @@ vector< string > splitStr ( string originalText, char delim, bool removeSyms )
string word = "";
while ( begin != delim && i < originalText.size( ) )
{
if ( removeSyms && ( isAlpha( begin ) || isNum( begin ) ) )
if ( removeSyms)
{
if( isAlpha( begin ) || isNum( begin ) )
{
word.push_back( begin );
}
}
else
{
word.push_back( begin );
}
......
......@@ -145,7 +145,7 @@ void testSplitStr ( string original )
assert( vec[ 0 ] == "hello" && vec[ 1 ] == "goodbye" );
word = "apollo moon OR landing";
vec = splitStr( word, ' ', true );
vec = splitStr( word, ' ', false );
assert( vec.size( ) == 4 );
assert( vec[ 0 ] == "apollo" && vec[ 1 ] == "moon" && vec[ 2 ] == "OR" && vec[ 3 ] == "landing" );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment