Ignore:
Timestamp:
2003-05-01T10:58:25+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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/mgpp/text/GSDLQueryParser.cpp

    r3165 r4209  
    2626static QueryNode *ParseExpression (UCArray::const_iterator &here,
    2727                   UCArray::const_iterator end,
    28                    int defaultBoolCOmbine,
     28                   int defaultBoolCombine,
    2929                   int defaultStemMethod);
    3030
     
    9595  UCArray NEARBY; SetCStr(NEARBY, "NEAR");
    9696  if (nearby == NEARBY) { // no modifier
    97     termNode.startRange = -21;
    98     termNode.endRange = 20;
     97    termNode.startRange = (NEAR_DEFAULT+1)*-1;
     98    termNode.endRange = NEAR_DEFAULT;
     99
    99100  }
    100101  else { // extract number
     
    215216
    216217// expects starting brackets to have been parsed
     218// sets error to true if something has gone wrong
    217219static void ParseSquareBrackets(UCArray::const_iterator &here,
    218220                UCArray::const_iterator end,
    219221                ProxMatchQueryNode *proxNode,
    220                 int defaultStemMethod) {
     222                int defaultStemMethod,
     223                bool & error) {
    221224
    222225  LexEl el;
     
    240243      break;
    241244    }
    242     else if (el.lexType == AndOpE) {
    243       // ignore, the words are AND'ed anyway
    244       cerr << "and inside []\n";
    245     }
    246     else if (el.lexType == OrOpE) {
    247       cerr << "or inside []\n";
    248     }
    249245    else if (el.lexType == QuoteE) {
    250246      // phrase inside square brackets
     
    253249    }
    254250    else {
    255       //error
    256       // just ignore for now
    257       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;
    258255    }
    259256  } // while
     
    267264             UCArray::const_iterator end,
    268265             ProxMatchQueryNode &proxNode,
    269              int defaultStemMethod) {
     266             int defaultStemMethod,
     267             bool &error) {
    270268  LexEl el;
    271269  bool first = true;
     
    289287     
    290288    } else {
    291       // error
    292       break;
     289      // error
     290      error = true;
     291      return;
    293292    }
    294293  }
     
    316315    oldHere = here;  // dont backtrack past here
    317316    if (ParseLexEl(here, end, el) && el.lexType == NearOpE) {
    318       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   
    319329      proxNode = (ProxMatchQueryNode *)ParseTerm(here, end, defaultBoolCombine,
    320330                         defaultStemMethod);
     
    322332      proxNode->terms.push_back (termNode);
    323333      return proxNode;
    324     }
    325     else {
     334     
     335    } else {
    326336      here = oldHere; // backtrack
    327337      proxNode->terms.push_back (termNode);
     
    330340    }
    331341  } else if (el.lexType == QuoteE) {
    332     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    }
    333348    return proxNode;
    334349  }
    335350  else if (el.lexType == OpenSquareBracketE) {
    336     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    }
    337357    ParseProxModifiers (here, end, proxNode);
    338358    return proxNode;
     
    352372  LexEl el;
    353373  QueryNode *curTree = NULL;
    354 
    355374  UCArray::const_iterator oldHere = here;
    356375  while (ParseLexEl (here, end, el)) {
    357     if (el.lexType == OpenSquareBracketE ||
    358     el.lexType == OpenBracketE ||
    359     el.lexType == TermE ||
    360     el.lexType == QuoteE ||
    361     el.lexType == IntegerE ) {
    362     //  el.lexType == TagE) { //tag at end of term now
    363       // some type of term, back track and parse it
    364       here = oldHere;
    365       // if default==1, AND, else if==0, OR
    366       if (defaultBoolCombine) {
    367     curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    368                           defaultStemMethod));
    369       }
    370       else {
    371     curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    372                          defaultStemMethod));
    373       }
    374 
    375     } else if (el.lexType == AndOpE) {
    376       curTree = AndAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    377                         defaultStemMethod));
    378      
    379     } else if (el.lexType == OrOpE) {
    380       curTree = OrAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    381                        defaultStemMethod));
    382      
    383     } else if (el.lexType == NotOpE) {
    384       curTree = NotAdd (curTree, ParseTerm (here, end, defaultBoolCombine,
    385                         defaultStemMethod));
    386      
    387     } else if (el.lexType == CloseBracketE) {
     376    if (el.lexType == CloseBracketE) {
    388377      // parsebracketexpression is waiting for the last bracket, so put it back
    389378      here = oldHere;
    390379      break;
    391    
    392     } else break;
    393    
     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
    394440    oldHere = here;
    395441  }
Note: See TracChangeset for help on using the changeset viewer.