Branch: Tag:

2001-06-01

2001-06-01 09:14:22 by David Norlin <norlin@roxen.com>

API changed for telling the parser which words are field names.

Rev: lib/modules/Search.pmod/Grammar.pmod/AbstractParser.pike:1.2
Rev: lib/modules/Search.pmod/Grammar.pmod/DefaultParser.pike:1.2
Rev: lib/modules/Search.pmod/Grammar.pmod/module.pmod:1.3

5:   //static constant OrNode = Search.Grammar.OrNode;   //static constant AndNode = Search.Grammar.AndNode;   //static constant TextNode = Search.Grammar.TextNode; - // +    #include "debug.h"      // =========================================================================
64:      static array(array(Token|string)) tokens;   static array(string) fieldstack; - mapping(string:string) options; + mapping(string:mixed) options;      static array(Token|string) peek(void|int lookahead) {    if (lookahead >= sizeof(tokens))
77:    tokens = tokens[1 .. ];   }    - static void create(mapping(string:string)|void opt) { -  options = opt || ([]); + static int lookingAtFieldStart(void|int offset) { +  multiset(string) fields = options["fields"]; +  // SHOW(tokens); +  return peek(offset)[0] == TOKEN_WORD +  && fields[ lower_case(peek(offset)[1]) ] +  && peek(offset + 1)[0] == TOKEN_COLON;   }    -  + static void create(mapping(string:mixed)|void opt) { +  options = opt || ([ "implicit" : "or" ]); +  if (!options["fields"]) +  options["fields"] = getDefaultFields(); + } +    ParseNode parse(string q) {    fieldstack = ({ "any" });    tokens = tokenize(q);
88:   }      static ParseNode parseQuery() { +  // TRACE;    ParseNode or = OrNode();    for (;;) {    ParseNode n = parseExpr0();
103:   }      static ParseNode parseExpr0() { +  // TRACE;    ParseNode and = AndNode();    for (;;) {    ParseNode n = parseExpr1();
121:   }      static ParseNode parseExpr1() { +  // TRACE;    return parseExpr2();   }      static ParseNode parseExpr2() { -  +  // TRACE;       // field ':' expr3 -  if (peek()[0] == TOKEN_WORD -  && isFieldSpecWord(peek()[1]) -  && peek(1)[0] == TOKEN_COLON) +  if (lookingAtFieldStart())    { -  +  // TRACE;    fieldstack = ({ peek()[1] }) + fieldstack;    advance();    advance();
153:   }      static ParseNode parseExpr3() { -  if (peek()[0] == TOKEN_WORD -  && peek(1)[0] == TOKEN_COLON) +  // TRACE; +  if (lookingAtFieldStart())    return 0;    ParseNode or = OrNode();    for (;;) {    ParseNode n = parseExpr4();    or->addChild(n);    if (peek()[0] == TOKEN_OR) -  if (peek(1)[0] == TOKEN_WORD -  && peek(2)[0] == TOKEN_COLON) +  if (lookingAtFieldStart(1))    break; // it was a higher level OR    else    advance();
175:   }      static ParseNode parseExpr4() { +  // TRACE;    ParseNode and = AndNode();    for (;;) {    ParseNode n = parseExpr5();    and->addChild(n);    // NOTE: No implicit and here!    if (peek()[0] == TOKEN_AND -  && !(peek(1)[0] == TOKEN_WORD -  && peek(2)[0] == TOKEN_COLON // it was a higher level AND +  && !(lookingAtFieldStart(1) // it was a higher level AND    || peek(1)[0] == TOKEN_LPAREN))    advance();    else
194:   }      static ParseNode parseExpr5() { +  // TRACE;    ParseNode text = TextNode();    text->field = fieldstack[0];    for (;;) {
202:    TOKEN_RPAREN,    TOKEN_AND,    TOKEN_OR >) [ peek()[0] ] -  || (peek()[0] == TOKEN_WORD -  && peek(1)[0] == TOKEN_COLON) +  || lookingAtFieldStart()    || (peek()[0] == TOKEN_LPAREN))    break; // it was a higher level IMPLICIT AND    if (peek()[0] == TOKEN_OR)
225:   }      static void parseExpr6(TextNode node) { +  // TRACE;    int prefix = 0;       if (peek()[0] == TOKEN_MINUS) {
267:   }      static ParseNode parseDate() { +  // TRACE;    DateNode n = DateNode();    n->date = "";   loop:    for (;;) {    switch (peek()[0]) {    case TOKEN_WORD: -  if (isFieldSpecWord(peek()[1]) -  && peek(1)[0] == TOKEN_COLON) +  if (lookingAtFieldStart())    break loop; // it's a field specifier    break;    case TOKEN_UNKNOWN: