FileDocCategorySizeDatePackage
QueryParser.javaAPI DocApache Lucene 1.921219Mon Feb 20 09:17:44 GMT 2006org.apache.lucene.queryParser.surround.parser

QueryParser

public class QueryParser extends Object implements QueryParserConstants
This class is generated by JavaCC. The only method that clients should need to call is parse().

Fields Summary
final int
minimumPrefixLength
final int
minimumCharsInTrunc
final String
truncationErrorMessage
final String
boostErrorMessage
final char
truncator
final char
anyChar
final char
quote
final char
fieldOperator
final char
comma
final char
carat
public QueryParserTokenManager
token_source
public Token
token
public Token
jj_nt
private int
jj_ntk
private Token
jj_scanpos
private Token
jj_lastpos
private int
jj_la
public boolean
lookingAhead
private boolean
jj_semLA
private int
jj_gen
private final int[]
jj_la1
private static int[]
jj_la1_0
private final JJCalls[]
jj_2_rtns
private boolean
jj_rescan
private int
jj_gc
private final LookaheadSuccess
jj_ls
private Vector
jj_expentries
private int[]
jj_expentry
private int
jj_kind
private int[]
jj_lasttokens
private int
jj_endpos
Constructors Summary
public QueryParser()

    this(new FastCharStream(new StringReader("")));
  
public QueryParser(CharStream stream)


     
    token_source = new QueryParserTokenManager(stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 10; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  
public QueryParser(QueryParserTokenManager tm)

    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 10; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  
Methods Summary
public final org.apache.lucene.queryParser.surround.query.SrndQueryAndQuery()

  SrndQuery q;
  ArrayList queries = null;
  Token oprt = null;
    q = NotQuery();
    label_3:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case AND:
        ;
        break;
      default:
        jj_la1[1] = jj_gen;
        break label_3;
      }
      oprt = jj_consume_token(AND);
                   /* keep only last used operator */
      if (queries == null) {
        queries = new ArrayList();
        queries.add(q);
      }
      q = NotQuery();
      queries.add(q);
    }
   {if (true) return (queries == null) ? q : getAndQuery(queries, true /* infix */, oprt);}
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryFieldsQuery()

  SrndQuery q;
  ArrayList fieldNames;
    fieldNames = OptionalFields();
    q = OrQuery();
   {if (true) return (fieldNames == null) ? q : getFieldsQuery(q, fieldNames);}
    throw new Error("Missing return statement in function");
  
public final java.util.ListFieldsQueryList()

  SrndQuery q;
  ArrayList queries = new ArrayList();
    jj_consume_token(LPAREN);
    q = FieldsQuery();
                     queries.add(q);
    label_7:
    while (true) {
      jj_consume_token(COMMA);
      q = FieldsQuery();
                              queries.add(q);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case COMMA:
        ;
        break;
      default:
        jj_la1[7] = jj_gen;
        break label_7;
      }
    }
    jj_consume_token(RPAREN);
   {if (true) return queries;}
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryNQuery()

  SrndQuery q;
  ArrayList queries;
  Token dt;
    q = WQuery();
    label_5:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case N:
        ;
        break;
      default:
        jj_la1[3] = jj_gen;
        break label_5;
      }
      dt = jj_consume_token(N);
      queries = new ArrayList();
      queries.add(q); /* left associative */

      q = WQuery();
      queries.add(q);
      q = getDistanceQuery(queries, true /* infix */, dt, false /* not ordered */);
    }
   {if (true) return q;}
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryNotQuery()

  SrndQuery q;
  ArrayList queries = null;
  Token oprt = null;
    q = NQuery();
    label_4:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case NOT:
        ;
        break;
      default:
        jj_la1[2] = jj_gen;
        break label_4;
      }
      oprt = jj_consume_token(NOT);
                    /* keep only last used operator */
      if (queries == null) {
        queries = new ArrayList();
        queries.add(q);
      }
      q = NQuery();
      queries.add(q);
    }
   {if (true) return (queries == null) ? q : getNotQuery(queries, oprt);}
    throw new Error("Missing return statement in function");
  
public final java.util.ArrayListOptionalFields()

  Token fieldName;
  ArrayList fieldNames = null;
    label_1:
    while (true) {
      if (jj_2_1(2)) {
        ;
      } else {
        break label_1;
      }
      // to the colon
          fieldName = jj_consume_token(TERM);
      jj_consume_token(COLON);
      if (fieldNames == null) {
        fieldNames = new ArrayList();
      }
      fieldNames.add(fieldName.image);
    }
   {if (true) return fieldNames;}
    throw new Error("Missing return statement in function");
  
public final voidOptionalWeights(org.apache.lucene.queryParser.surround.query.SrndQuery q)

  Token weight=null;
    label_8:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case CARAT:
        ;
        break;
      default:
        jj_la1[9] = jj_gen;
        break label_8;
      }
      jj_consume_token(CARAT);
      weight = jj_consume_token(NUMBER);
      float f;
      try {
        f = Float.valueOf(weight.image).floatValue();
      } catch (Exception floatExc) {
        {if (true) throw new ParseException(boostErrorMessage + weight.image + " (" + floatExc + ")");}
      }
      if (f <= 0.0) {
        {if (true) throw new ParseException(boostErrorMessage + weight.image);}
      }
      q.setWeight(f * q.getWeight()); /* left associative, fwiw */

    }
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryOrQuery()

  SrndQuery q;
  ArrayList queries = null;
  Token oprt = null;
    q = AndQuery();
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case OR:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_2;
      }
      oprt = jj_consume_token(OR);
                  /* keep only last used operator */
      if (queries == null) {
        queries = new ArrayList();
        queries.add(q);
      }
      q = AndQuery();
      queries.add(q);
    }
   {if (true) return (queries == null) ? q : getOrQuery(queries, true /* infix */, oprt);}
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryPrefixOperatorQuery()

  Token oprt;
  List queries;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case OR:
      oprt = jj_consume_token(OR);
      /* prefix OR */
          queries = FieldsQueryList();
     {if (true) return getOrQuery(queries, false /* not infix */, oprt);}
      break;
    case AND:
      oprt = jj_consume_token(AND);
      /* prefix AND */
          queries = FieldsQueryList();
     {if (true) return getAndQuery(queries, false /* not infix */, oprt);}
      break;
    case N:
      oprt = jj_consume_token(N);
      /* prefix N */
          queries = FieldsQueryList();
     {if (true) return getDistanceQuery(queries, false /* not infix */, oprt, false /* not ordered */);}
      break;
    case W:
      oprt = jj_consume_token(W);
      /* prefix W */
          queries = FieldsQueryList();
     {if (true) return getDistanceQuery(queries, false  /* not infix */, oprt, true /* ordered */);}
      break;
    default:
      jj_la1[6] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryPrimaryQuery()

                             /* bracketed weighted query or weighted term */
  SrndQuery q;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case LPAREN:
      jj_consume_token(LPAREN);
      q = FieldsQuery();
      jj_consume_token(RPAREN);
      break;
    case OR:
    case AND:
    case W:
    case N:
      q = PrefixOperatorQuery();
      break;
    case TRUNCQUOTED:
    case QUOTED:
    case SUFFIXTERM:
    case TRUNCTERM:
    case TERM:
      q = SimpleTerm();
      break;
    default:
      jj_la1[5] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    OptionalWeights(q);
   {if (true) return q;}
    throw new Error("Missing return statement in function");
  
public voidReInit(CharStream stream)

    token_source.ReInit(stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 10; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  
public voidReInit(QueryParserTokenManager tm)

    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 10; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  
public final org.apache.lucene.queryParser.surround.query.SrndQuerySimpleTerm()

  Token term;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case TERM:
      term = jj_consume_token(TERM);
     {if (true) return getTermQuery(term.image, false /* not quoted */);}
      break;
    case QUOTED:
      term = jj_consume_token(QUOTED);
     {if (true) return getTermQuery(term.image.substring(1, term.image.length()-1), true /* quoted */);}
      break;
    case SUFFIXTERM:
      term = jj_consume_token(SUFFIXTERM);
                        /* ending in * */
      if (! allowedSuffix(term.image)) {
        {if (true) throw new ParseException(truncationErrorMessage + term.image);}
      }
      {if (true) return getPrefixQuery(term.image.substring(0, term.image.length()-1), false /* not quoted */);}
      break;
    case TRUNCTERM:
      term = jj_consume_token(TRUNCTERM);
                       /* with at least one * or ? */
      if (! allowedTruncation(term.image)) {
        {if (true) throw new ParseException(truncationErrorMessage + term.image);}
      }
      {if (true) return getTruncQuery(term.image);}
      break;
    case TRUNCQUOTED:
      term = jj_consume_token(TRUNCQUOTED);
                         /* eg. "9b-b,m"* */
      if ((term.image.length() - 3) < minimumPrefixLength) {
        {if (true) throw new ParseException(truncationErrorMessage + term.image);}
      }
      {if (true) return getPrefixQuery(term.image.substring(1, term.image.length()-2), true /* quoted */);}
      break;
    default:
      jj_la1[8] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryTopSrndQuery()

  SrndQuery q;
    q = FieldsQuery();
    jj_consume_token(0);
   {if (true) return q;}
    throw new Error("Missing return statement in function");
  
public final org.apache.lucene.queryParser.surround.query.SrndQueryWQuery()

  SrndQuery q;
  ArrayList queries;
  Token wt;
    q = PrimaryQuery();
    label_6:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case W:
        ;
        break;
      default:
        jj_la1[4] = jj_gen;
        break label_6;
      }
      wt = jj_consume_token(W);
      queries = new ArrayList();
      queries.add(q); /* left associative */

      q = PrimaryQuery();
      queries.add(q);
      q = getDistanceQuery(queries, true /* infix */, wt, true /* ordered */);
    }
   {if (true) return q;}
    throw new Error("Missing return statement in function");
  
protected booleanallowedSuffix(java.lang.String suffixed)

    return (suffixed.length() - 1) >= minimumPrefixLength;
  
protected booleanallowedTruncation(java.lang.String truncated)

    /* At least 3 normal characters needed. */
    int nrNormalChars = 0;
    for (int i = 0; i < truncated.length(); i++) {
      char c = truncated.charAt(i);
      if ((c != truncator) && (c != anyChar)) {
        nrNormalChars++;
      }
    }
    return nrNormalChars >= minimumCharsInTrunc;
  
protected static voidcheckDistanceSubQueries(org.apache.lucene.queryParser.surround.query.DistanceQuery distq, java.lang.String opName)

    String m = distq.distanceSubQueryNotAllowed();
    if (m != null) {
      throw new ParseException("Operator " + opName + ": " + m);
    }
  
public final voiddisable_tracing()

  
public final voidenable_tracing()

  
public ParseExceptiongenerateParseException()

    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[24];
    for (int i = 0; i < 24; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 10; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 24; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    jj_endpos = 0;
    jj_rescan_token();
    jj_add_error_token(0, 0);
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.elementAt(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetAndQuery(java.util.List queries, boolean infix, Token andToken)

    return new AndQuery( queries, infix, andToken.image);
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetDistanceQuery(java.util.List queries, boolean infix, Token dToken, boolean ordered)

    DistanceQuery dq = new DistanceQuery(queries,
                                        infix,
                                        getOpDistance(dToken.image),
                                        dToken.image,
                                        ordered);
    checkDistanceSubQueries(dq, dToken.image);
    return dq;
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetFieldsQuery(org.apache.lucene.queryParser.surround.query.SrndQuery q, java.util.ArrayList fieldNames)

    /* FIXME: check acceptable subquery: at least one subquery should not be
     * a fields query.
     */
    return new FieldsQuery(q, fieldNames, fieldOperator);
  
public final TokengetNextToken()

    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetNotQuery(java.util.List queries, Token notToken)

    return new NotQuery( queries, notToken.image);
  
protected static intgetOpDistance(java.lang.String distanceOp)

    /* W, 2W, 3W etc -> 1, 2 3, etc. Same for N, 2N ... */
    return distanceOp.length() == 1
      ? 1
      : Integer.parseInt( distanceOp.substring( 0, distanceOp.length() - 1));
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetOrQuery(java.util.List queries, boolean infix, Token orToken)

    return new OrQuery(queries, infix, orToken.image);
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetPrefixQuery(java.lang.String prefix, boolean quoted)

    return new SrndPrefixQuery(prefix, quoted, truncator);
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetTermQuery(java.lang.String term, boolean quoted)

    return new SrndTermQuery(term, quoted);
  
public final TokengetToken(int index)

    Token t = lookingAhead ? jj_scanpos : token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  
protected org.apache.lucene.queryParser.surround.query.SrndQuerygetTruncQuery(java.lang.String truncated)

    return new SrndTruncQuery(truncated, truncator, anyChar);
  
private final booleanjj_2_1(int xla)

    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_1(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(0, xla); }
  
private final booleanjj_3_1()

    if (jj_scan_token(TERM)) return true;
    if (jj_scan_token(COLON)) return true;
    return false;
  
private voidjj_add_error_token(int kind, int pos)


        
    if (pos >= 100) return;
    if (pos == jj_endpos + 1) {
      jj_lasttokens[jj_endpos++] = kind;
    } else if (jj_endpos != 0) {
      jj_expentry = new int[jj_endpos];
      for (int i = 0; i < jj_endpos; i++) {
        jj_expentry[i] = jj_lasttokens[i];
      }
      boolean exists = false;
      for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) {
        int[] oldentry = (int[])(e.nextElement());
        if (oldentry.length == jj_expentry.length) {
          exists = true;
          for (int i = 0; i < jj_expentry.length; i++) {
            if (oldentry[i] != jj_expentry[i]) {
              exists = false;
              break;
            }
          }
          if (exists) break;
        }
      }
      if (!exists) jj_expentries.addElement(jj_expentry);
      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
    }
  
private final Tokenjj_consume_token(int kind)

    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      if (++jj_gc > 100) {
        jj_gc = 0;
        for (int i = 0; i < jj_2_rtns.length; i++) {
          JJCalls c = jj_2_rtns[i];
          while (c != null) {
            if (c.gen < jj_gen) c.first = null;
            c = c.next;
          }
        }
      }
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  
private static voidjj_la1_0()

   
      jj_la1_0();
   
      jj_la1_0 = new int[] {0x100,0x200,0x400,0x1000,0x800,0x7c3b00,0x1b00,0x8000,0x7c0000,0x20000,};
   
private final intjj_ntk()

    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  
private final voidjj_rescan_token()

    jj_rescan = true;
    for (int i = 0; i < 1; i++) {
      JJCalls p = jj_2_rtns[i];
      do {
        if (p.gen > jj_gen) {
          jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
          switch (i) {
            case 0: jj_3_1(); break;
          }
        }
        p = p.next;
      } while (p != null);
    }
    jj_rescan = false;
  
private final voidjj_save(int index, int xla)

    JJCalls p = jj_2_rtns[index];
    while (p.gen > jj_gen) {
      if (p.next == null) { p = p.next = new JJCalls(); break; }
      p = p.next;
    }
    p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  
private final booleanjj_scan_token(int kind)

       
    if (jj_scanpos == jj_lastpos) {
      jj_la--;
      if (jj_scanpos.next == null) {
        jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
      } else {
        jj_lastpos = jj_scanpos = jj_scanpos.next;
      }
    } else {
      jj_scanpos = jj_scanpos.next;
    }
    if (jj_rescan) {
      int i = 0; Token tok = token;
      while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
      if (tok != null) jj_add_error_token(kind, i);
    }
    if (jj_scanpos.kind != kind) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
    return false;
  
public static org.apache.lucene.queryParser.surround.query.SrndQueryparse(java.lang.String query)

 /* weight oparator */

         
    QueryParser parser = new QueryParser();
    return parser.parse2(query);
  
public org.apache.lucene.queryParser.surround.query.SrndQueryparse2(java.lang.String query)

    ReInit(new FastCharStream(new StringReader(query)));
    try {
      return TopSrndQuery();
    } catch (TokenMgrError tme) {
      throw new ParseException(tme.getMessage());
    }