Changeset 4210


Ignore:
Timestamp:
2003-05-01T10:59:14+12:00 (21 years ago)
Author:
kjdon
Message:

parseQuery (in GSDLQueryParser) now returns NULL if there has been a syntax error. So all the auxiliary functions either return NULl
if there is an error, or set an error flag to true. Queryer now tells the user that there has been invalid syntax rather than seg faulting

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/indexers/mgpp/text/GSDLQueryParser.cpp

    r3782 r4210  
    2626static QueryNode *ParseExpression (UCArray::const_iterator &here,
    2727                   UCArray::const_iterator end,
    28                    int defaultBoolCOmbine,
     28                   int defaultBoolCombine,
    2929                   int defaultStemMethod);
    3030
     
    216216
    217217// expects starting brackets to have been parsed
     218// sets error to true if something has gone wrong
    218219static void ParseSquareBrackets(UCArray::const_iterator &here,
    219220                UCArray::const_iterator end,
    220221                ProxMatchQueryNode *proxNode,
    221                 int defaultStemMethod) {
     222                int defaultStemMethod,
     223                bool & error) {
    222224
    223225  LexEl el;
     
    241243      break;
    242244    }
    243     else if (el.lexType == AndOpE) {
    244       // ignore, the words are AND'ed anyway
    245       cerr << "and inside []\n";
    246     }
    247     else if (el.lexType == OrOpE) {
    248       cerr << "or inside []\n";
    249     }
    250245    else if (el.lexType == QuoteE) {
    251246      // phrase inside square brackets
     
    254249    }
    255250    else {
    256       //error
    257       // just ignore for now
    258       cerr <<"bad syntax inside []\n";
     251      //error - we set the proxNode to NULL,
     252      cerr <<"GSDLQueryParser: bad syntax inside []\n";
     253      error = true;
     254      return;
    259255    }
    260256  } // while
     
    268264             UCArray::const_iterator end,
    269265             ProxMatchQueryNode &proxNode,
    270              int defaultStemMethod) {
     266             int defaultStemMethod,
     267             bool &error) {
    271268  LexEl el;
    272269  bool first = true;
     
    290287     
    291288    } else {
    292       // error
    293       break;
     289      // error
     290      error = true;
     291      return;
    294292    }
    295293  }
     
    317315    oldHere = here;  // dont backtrack past here
    318316    if (ParseLexEl(here, end, el) && el.lexType == NearOpE) {
    319       delete proxNode;
     317    delete proxNode;
     318    oldHere = here;
     319      // this is calling ParseTerm again, but only a subset of the things accepted by ParseTerm are appropriate here. add in some hacks to avoid segmentation faults - kjdon, 04/2003
     320     
     321      // if the next element is a '(' have a syntax error, return NULL
     322      LexEl temp_el;
     323      if (ParseLexEl(here, end, temp_el) && temp_el.lexType == OpenBracketE) {
     324    cerr << "GSDLQueryParser: NEAR cannot be followed by a '('\n";
     325    return NULL;
     326      }
     327      here = oldHere; // else backtrack
     328   
    320329      proxNode = (ProxMatchQueryNode *)ParseTerm(here, end, defaultBoolCombine,
    321330                         defaultStemMethod);
     
    323332      proxNode->terms.push_back (termNode);
    324333      return proxNode;
    325     }
    326     else {
     334     
     335    } else {
    327336      here = oldHere; // backtrack
    328337      proxNode->terms.push_back (termNode);
     
    331340    }
    332341  } else if (el.lexType == QuoteE) {
    333     ParsePhrase (here, end, *proxNode, defaultStemMethod);
     342    bool error = false;
     343    ParsePhrase (here, end, *proxNode, defaultStemMethod, error);
     344    if (error) {
     345      delete proxNode;
     346      return NULL;
     347    }
    334348    return proxNode;
    335349  }
    336350  else if (el.lexType == OpenSquareBracketE) {
    337     ParseSquareBrackets (here, end, proxNode, defaultStemMethod);
     351    bool error = false;
     352    ParseSquareBrackets (here, end, proxNode, defaultStemMethod, error);
     353    if (error) {
     354      delete proxNode;
     355      return NULL;
     356    }
    338357    ParseProxModifiers (here, end, proxNode);
    339358    return proxNode;
     
    353372  LexEl el;
    354373  QueryNode *curTree = NULL;
    355 
    356374  UCArray::const_iterator oldHere = here;
    357375  while (ParseLexEl (here, end, el)) {
    358     if (el.lexType == OpenSquareBracketE ||
    359     el.lexType == OpenBracketE ||
    360     el.lexType == TermE ||
    361     el.lexType == QuoteE ||
    362     el.lexType == IntegerE ) {
    363     //  el.lexType == TagE) { //tag at end of term now
    364       // some type of term, back track and parse it
    365       here = oldHere;
    366       // if default==1, AND, else if==0, OR
    367       if (defaultBoolCombine) {
    368     curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    369                           defaultStemMethod));
    370       }
    371       else {
    372     curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    373                          defaultStemMethod));
    374       }
    375 
    376     } else if (el.lexType == AndOpE) {
    377       curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    378                         defaultStemMethod));
    379      
    380     } else if (el.lexType == OrOpE) {
    381       curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    382                        defaultStemMethod));
    383      
    384     } else if (el.lexType == NotOpE) {
    385       curTree = NotAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    386                         defaultStemMethod));
    387      
    388     } else if (el.lexType == CloseBracketE) {
     376    if (el.lexType == CloseBracketE) {
    389377      // parsebracketexpression is waiting for the last bracket, so put it back
    390378      here = oldHere;
    391379      break;
    392    
    393     } else break;
    394    
     380     
     381    } else if (el.lexType == OpenSquareBracketE ||
     382           el.lexType == OpenBracketE ||
     383           el.lexType == TermE ||
     384           el.lexType == QuoteE ||
     385           el.lexType == IntegerE ) {
     386     
     387      // some type of term, back track and parse it
     388      here = oldHere;
     389
     390      //  parse the term
     391      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     392                       defaultStemMethod);
     393      if (newTerm == NULL) {
     394    delete curTree;
     395    return NULL;
     396      }
     397
     398      // if default==1, AND, else if==0, OR
     399      if (defaultBoolCombine) {
     400    curTree = AndAdd (curTree, newTerm);
     401      }
     402      else {
     403    curTree = OrAdd (curTree, newTerm);
     404      }
     405     
     406    } else if (el.lexType == AndOpE) {
     407      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     408                       defaultStemMethod);
     409      if (newTerm == NULL) {
     410    delete curTree;
     411    return NULL;
     412      }
     413      curTree = AndAdd (curTree, newTerm);
     414     
     415    } else if (el.lexType == OrOpE) {
     416      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     417                       defaultStemMethod);
     418      if (newTerm == NULL) {
     419    delete curTree;
     420    return NULL;
     421      }
     422      curTree = OrAdd (curTree, newTerm);
     423     
     424    } else if (el.lexType == NotOpE) {
     425      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     426                       defaultStemMethod);
     427      if (newTerm == NULL) {
     428    delete curTree;
     429    return NULL;
     430      }
     431      curTree = NotAdd (curTree, newTerm);
     432     
     433    } else {
     434
     435      // syntax error, return NUll
     436      delete curTree;
     437      return NULL;
     438    }
     439
    395440    oldHere = here;
    396441  }
  • trunk/indexers/mgpp/text/Queryer.cpp

    r3365 r4210  
    242242      // regular query
    243243      queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod);
    244 
    245       // print the query
    246       PrintNode (cout, queryTree);
    247 
    248       MGQuery (indexData, queryInfo, queryTree, queryResult, level);
    249       if (shortOutput) {
    250     queryResult.printShort(cout);
    251     cout << "\n";
    252       }else {
    253     cout << queryResult;
    254     cout << "\n";
    255       }
    256       // delete the query
    257       if (queryTree != NULL) delete queryTree;
    258       queryTree = NULL;
     244      if (queryTree == NULL) {
     245    cout << "invalid syntax\n";
     246      } else {
     247    // print the query
     248    PrintNode (cout, queryTree);
     249   
     250    MGQuery (indexData, queryInfo, queryTree, queryResult, level);
     251    if (shortOutput) {
     252      queryResult.printShort(cout);
     253      cout << "\n";
     254    } else {
     255      cout << queryResult;
     256      cout << "\n";
     257    }
     258    // delete the query
     259    delete queryTree;
     260    queryTree = NULL;
     261      }
    259262    }
    260263  }
  • trunk/mgpp/text/GSDLQueryParser.cpp

    r3782 r4210  
    2626static QueryNode *ParseExpression (UCArray::const_iterator &here,
    2727                   UCArray::const_iterator end,
    28                    int defaultBoolCOmbine,
     28                   int defaultBoolCombine,
    2929                   int defaultStemMethod);
    3030
     
    216216
    217217// expects starting brackets to have been parsed
     218// sets error to true if something has gone wrong
    218219static void ParseSquareBrackets(UCArray::const_iterator &here,
    219220                UCArray::const_iterator end,
    220221                ProxMatchQueryNode *proxNode,
    221                 int defaultStemMethod) {
     222                int defaultStemMethod,
     223                bool & error) {
    222224
    223225  LexEl el;
     
    241243      break;
    242244    }
    243     else if (el.lexType == AndOpE) {
    244       // ignore, the words are AND'ed anyway
    245       cerr << "and inside []\n";
    246     }
    247     else if (el.lexType == OrOpE) {
    248       cerr << "or inside []\n";
    249     }
    250245    else if (el.lexType == QuoteE) {
    251246      // phrase inside square brackets
     
    254249    }
    255250    else {
    256       //error
    257       // just ignore for now
    258       cerr <<"bad syntax inside []\n";
     251      //error - we set the proxNode to NULL,
     252      cerr <<"GSDLQueryParser: bad syntax inside []\n";
     253      error = true;
     254      return;
    259255    }
    260256  } // while
     
    268264             UCArray::const_iterator end,
    269265             ProxMatchQueryNode &proxNode,
    270              int defaultStemMethod) {
     266             int defaultStemMethod,
     267             bool &error) {
    271268  LexEl el;
    272269  bool first = true;
     
    290287     
    291288    } else {
    292       // error
    293       break;
     289      // error
     290      error = true;
     291      return;
    294292    }
    295293  }
     
    317315    oldHere = here;  // dont backtrack past here
    318316    if (ParseLexEl(here, end, el) && el.lexType == NearOpE) {
    319       delete proxNode;
     317    delete proxNode;
     318    oldHere = here;
     319      // this is calling ParseTerm again, but only a subset of the things accepted by ParseTerm are appropriate here. add in some hacks to avoid segmentation faults - kjdon, 04/2003
     320     
     321      // if the next element is a '(' have a syntax error, return NULL
     322      LexEl temp_el;
     323      if (ParseLexEl(here, end, temp_el) && temp_el.lexType == OpenBracketE) {
     324    cerr << "GSDLQueryParser: NEAR cannot be followed by a '('\n";
     325    return NULL;
     326      }
     327      here = oldHere; // else backtrack
     328   
    320329      proxNode = (ProxMatchQueryNode *)ParseTerm(here, end, defaultBoolCombine,
    321330                         defaultStemMethod);
     
    323332      proxNode->terms.push_back (termNode);
    324333      return proxNode;
    325     }
    326     else {
     334     
     335    } else {
    327336      here = oldHere; // backtrack
    328337      proxNode->terms.push_back (termNode);
     
    331340    }
    332341  } else if (el.lexType == QuoteE) {
    333     ParsePhrase (here, end, *proxNode, defaultStemMethod);
     342    bool error = false;
     343    ParsePhrase (here, end, *proxNode, defaultStemMethod, error);
     344    if (error) {
     345      delete proxNode;
     346      return NULL;
     347    }
    334348    return proxNode;
    335349  }
    336350  else if (el.lexType == OpenSquareBracketE) {
    337     ParseSquareBrackets (here, end, proxNode, defaultStemMethod);
     351    bool error = false;
     352    ParseSquareBrackets (here, end, proxNode, defaultStemMethod, error);
     353    if (error) {
     354      delete proxNode;
     355      return NULL;
     356    }
    338357    ParseProxModifiers (here, end, proxNode);
    339358    return proxNode;
     
    353372  LexEl el;
    354373  QueryNode *curTree = NULL;
    355 
    356374  UCArray::const_iterator oldHere = here;
    357375  while (ParseLexEl (here, end, el)) {
    358     if (el.lexType == OpenSquareBracketE ||
    359     el.lexType == OpenBracketE ||
    360     el.lexType == TermE ||
    361     el.lexType == QuoteE ||
    362     el.lexType == IntegerE ) {
    363     //  el.lexType == TagE) { //tag at end of term now
    364       // some type of term, back track and parse it
    365       here = oldHere;
    366       // if default==1, AND, else if==0, OR
    367       if (defaultBoolCombine) {
    368     curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    369                           defaultStemMethod));
    370       }
    371       else {
    372     curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    373                          defaultStemMethod));
    374       }
    375 
    376     } else if (el.lexType == AndOpE) {
    377       curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    378                         defaultStemMethod));
    379      
    380     } else if (el.lexType == OrOpE) {
    381       curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    382                        defaultStemMethod));
    383      
    384     } else if (el.lexType == NotOpE) {
    385       curTree = NotAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    386                         defaultStemMethod));
    387      
    388     } else if (el.lexType == CloseBracketE) {
     376    if (el.lexType == CloseBracketE) {
    389377      // parsebracketexpression is waiting for the last bracket, so put it back
    390378      here = oldHere;
    391379      break;
    392    
    393     } else break;
    394    
     380     
     381    } else if (el.lexType == OpenSquareBracketE ||
     382           el.lexType == OpenBracketE ||
     383           el.lexType == TermE ||
     384           el.lexType == QuoteE ||
     385           el.lexType == IntegerE ) {
     386     
     387      // some type of term, back track and parse it
     388      here = oldHere;
     389
     390      //  parse the term
     391      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     392                       defaultStemMethod);
     393      if (newTerm == NULL) {
     394    delete curTree;
     395    return NULL;
     396      }
     397
     398      // if default==1, AND, else if==0, OR
     399      if (defaultBoolCombine) {
     400    curTree = AndAdd (curTree, newTerm);
     401      }
     402      else {
     403    curTree = OrAdd (curTree, newTerm);
     404      }
     405     
     406    } else if (el.lexType == AndOpE) {
     407      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     408                       defaultStemMethod);
     409      if (newTerm == NULL) {
     410    delete curTree;
     411    return NULL;
     412      }
     413      curTree = AndAdd (curTree, newTerm);
     414     
     415    } else if (el.lexType == OrOpE) {
     416      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     417                       defaultStemMethod);
     418      if (newTerm == NULL) {
     419    delete curTree;
     420    return NULL;
     421      }
     422      curTree = OrAdd (curTree, newTerm);
     423     
     424    } else if (el.lexType == NotOpE) {
     425      QueryNode * newTerm = ParseTerm (here, end, defaultBoolCombine,
     426                       defaultStemMethod);
     427      if (newTerm == NULL) {
     428    delete curTree;
     429    return NULL;
     430      }
     431      curTree = NotAdd (curTree, newTerm);
     432     
     433    } else {
     434
     435      // syntax error, return NUll
     436      delete curTree;
     437      return NULL;
     438    }
     439
    395440    oldHere = here;
    396441  }
  • trunk/mgpp/text/Queryer.cpp

    r3365 r4210  
    242242      // regular query
    243243      queryTree = ParseQuery (queryArray, defaultBoolCombine, defaultStemMethod);
    244 
    245       // print the query
    246       PrintNode (cout, queryTree);
    247 
    248       MGQuery (indexData, queryInfo, queryTree, queryResult, level);
    249       if (shortOutput) {
    250     queryResult.printShort(cout);
    251     cout << "\n";
    252       }else {
    253     cout << queryResult;
    254     cout << "\n";
    255       }
    256       // delete the query
    257       if (queryTree != NULL) delete queryTree;
    258       queryTree = NULL;
     244      if (queryTree == NULL) {
     245    cout << "invalid syntax\n";
     246      } else {
     247    // print the query
     248    PrintNode (cout, queryTree);
     249   
     250    MGQuery (indexData, queryInfo, queryTree, queryResult, level);
     251    if (shortOutput) {
     252      queryResult.printShort(cout);
     253      cout << "\n";
     254    } else {
     255      cout << queryResult;
     256      cout << "\n";
     257    }
     258    // delete the query
     259    delete queryTree;
     260    queryTree = NULL;
     261      }
    259262    }
    260263  }
Note: See TracChangeset for help on using the changeset viewer.