1 /* Generated By:JavaCC: Do not edit this line. QueryParser.java */
2 package org.apache.lucene.queryParser.surround.parser;
4 import java.util.ArrayList;
6 import java.io.StringReader;
9 import org.apache.lucene.analysis.TokenStream;
11 import org.apache.lucene.queryParser.surround.query.SrndQuery;
12 import org.apache.lucene.queryParser.surround.query.FieldsQuery;
13 import org.apache.lucene.queryParser.surround.query.OrQuery;
14 import org.apache.lucene.queryParser.surround.query.AndQuery;
15 import org.apache.lucene.queryParser.surround.query.NotQuery;
16 import org.apache.lucene.queryParser.surround.query.DistanceQuery;
17 import org.apache.lucene.queryParser.surround.query.SrndTermQuery;
18 import org.apache.lucene.queryParser.surround.query.SrndPrefixQuery;
19 import org.apache.lucene.queryParser.surround.query.SrndTruncQuery;
22 * This class is generated by JavaCC. The only method that clients should need
23 * to call is <a href="#parse">parse()</a>.
26 public class QueryParser implements QueryParserConstants {
27 final int minimumPrefixLength = 3;
28 final int minimumCharsInTrunc = 3;
29 final String truncationErrorMessage = "Too unrestrictive truncation: ";
30 final String boostErrorMessage = "Cannot handle boost value: ";
32 /* CHECKME: These should be the same as for the tokenizer. How? */
33 final char truncator = '*';
34 final char anyChar = '?';
35 final char quote = '\"';
36 final char fieldOperator = ':';
37 final char comma = ','; /* prefix list separator */
38 final char carat = '^'; /* weight operator */
40 static public SrndQuery parse(String query) throws ParseException {
41 QueryParser parser = new QueryParser();
42 return parser.parse2(query);
45 public QueryParser() {
46 this(new FastCharStream(new StringReader("")));
49 public SrndQuery parse2(String query) throws ParseException {
50 ReInit(new FastCharStream(new StringReader(query)));
52 return TopSrndQuery();
53 } catch (TokenMgrError tme) {
54 throw new ParseException(tme.getMessage());
58 protected SrndQuery getFieldsQuery(
59 SrndQuery q, ArrayList<String> fieldNames) {
60 /* FIXME: check acceptable subquery: at least one subquery should not be
63 return new FieldsQuery(q, fieldNames, fieldOperator);
66 protected SrndQuery getOrQuery(List<SrndQuery> queries, boolean infix, Token orToken) {
67 return new OrQuery(queries, infix, orToken.image);
70 protected SrndQuery getAndQuery(List<SrndQuery> queries, boolean infix, Token andToken) {
71 return new AndQuery( queries, infix, andToken.image);
74 protected SrndQuery getNotQuery(List<SrndQuery> queries, Token notToken) {
75 return new NotQuery( queries, notToken.image);
78 protected static int getOpDistance(String distanceOp) {
79 /* W, 2W, 3W etc -> 1, 2 3, etc. Same for N, 2N ... */
80 return distanceOp.length() == 1
82 : Integer.parseInt( distanceOp.substring( 0, distanceOp.length() - 1));
85 protected static void checkDistanceSubQueries(DistanceQuery distq, String opName)
86 throws ParseException {
87 String m = distq.distanceSubQueryNotAllowed();
89 throw new ParseException("Operator " + opName + ": " + m);
93 protected SrndQuery getDistanceQuery(
94 List<SrndQuery> queries,
97 boolean ordered) throws ParseException {
98 DistanceQuery dq = new DistanceQuery(queries,
100 getOpDistance(dToken.image),
103 checkDistanceSubQueries(dq, dToken.image);
107 protected SrndQuery getTermQuery(
108 String term, boolean quoted) {
109 return new SrndTermQuery(term, quoted);
112 protected boolean allowedSuffix(String suffixed) {
113 return (suffixed.length() - 1) >= minimumPrefixLength;
116 protected SrndQuery getPrefixQuery(
117 String prefix, boolean quoted) {
118 return new SrndPrefixQuery(prefix, quoted, truncator);
121 protected boolean allowedTruncation(String truncated) {
122 /* At least 3 normal characters needed. */
123 int nrNormalChars = 0;
124 for (int i = 0; i < truncated.length(); i++) {
125 char c = truncated.charAt(i);
126 if ((c != truncator) && (c != anyChar)) {
130 return nrNormalChars >= minimumCharsInTrunc;
133 protected SrndQuery getTruncQuery(String truncated) {
134 return new SrndTruncQuery(truncated, truncator, anyChar);
137 final public SrndQuery TopSrndQuery() throws ParseException {
141 {if (true) return q;}
142 throw new Error("Missing return statement in function");
145 final public SrndQuery FieldsQuery() throws ParseException {
147 ArrayList<String> fieldNames;
148 fieldNames = OptionalFields();
150 {if (true) return (fieldNames == null) ? q : getFieldsQuery(q, fieldNames);}
151 throw new Error("Missing return statement in function");
154 final public ArrayList<String> OptionalFields() throws ParseException {
156 ArrayList<String> fieldNames = null;
165 fieldName = jj_consume_token(TERM);
166 jj_consume_token(COLON);
167 if (fieldNames == null) {
168 fieldNames = new ArrayList<String>();
170 fieldNames.add(fieldName.image);
172 {if (true) return fieldNames;}
173 throw new Error("Missing return statement in function");
176 final public SrndQuery OrQuery() throws ParseException {
178 ArrayList<SrndQuery> queries = null;
183 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
191 oprt = jj_consume_token(OR);
192 /* keep only last used operator */
193 if (queries == null) {
194 queries = new ArrayList<SrndQuery>();
200 {if (true) return (queries == null) ? q : getOrQuery(queries, true /* infix */, oprt);}
201 throw new Error("Missing return statement in function");
204 final public SrndQuery AndQuery() throws ParseException {
206 ArrayList<SrndQuery> queries = null;
211 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
219 oprt = jj_consume_token(AND);
220 /* keep only last used operator */
221 if (queries == null) {
222 queries = new ArrayList<SrndQuery>();
228 {if (true) return (queries == null) ? q : getAndQuery(queries, true /* infix */, oprt);}
229 throw new Error("Missing return statement in function");
232 final public SrndQuery NotQuery() throws ParseException {
234 ArrayList<SrndQuery> queries = null;
239 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
247 oprt = jj_consume_token(NOT);
248 /* keep only last used operator */
249 if (queries == null) {
250 queries = new ArrayList<SrndQuery>();
256 {if (true) return (queries == null) ? q : getNotQuery(queries, oprt);}
257 throw new Error("Missing return statement in function");
260 final public SrndQuery NQuery() throws ParseException {
262 ArrayList<SrndQuery> queries;
267 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
275 dt = jj_consume_token(N);
276 queries = new ArrayList<SrndQuery>();
277 queries.add(q); /* left associative */
281 q = getDistanceQuery(queries, true /* infix */, dt, false /* not ordered */);
283 {if (true) return q;}
284 throw new Error("Missing return statement in function");
287 final public SrndQuery WQuery() throws ParseException {
289 ArrayList<SrndQuery> queries;
294 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
302 wt = jj_consume_token(W);
303 queries = new ArrayList<SrndQuery>();
304 queries.add(q); /* left associative */
308 q = getDistanceQuery(queries, true /* infix */, wt, true /* ordered */);
310 {if (true) return q;}
311 throw new Error("Missing return statement in function");
314 final public SrndQuery PrimaryQuery() throws ParseException {
315 /* bracketed weighted query or weighted term */
317 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
319 jj_consume_token(LPAREN);
321 jj_consume_token(RPAREN);
327 q = PrefixOperatorQuery();
338 jj_consume_token(-1);
339 throw new ParseException();
342 {if (true) return q;}
343 throw new Error("Missing return statement in function");
346 final public SrndQuery PrefixOperatorQuery() throws ParseException {
348 List<SrndQuery> queries;
349 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
351 oprt = jj_consume_token(OR);
353 queries = FieldsQueryList();
354 {if (true) return getOrQuery(queries, false /* not infix */, oprt);}
357 oprt = jj_consume_token(AND);
359 queries = FieldsQueryList();
360 {if (true) return getAndQuery(queries, false /* not infix */, oprt);}
363 oprt = jj_consume_token(N);
365 queries = FieldsQueryList();
366 {if (true) return getDistanceQuery(queries, false /* not infix */, oprt, false /* not ordered */);}
369 oprt = jj_consume_token(W);
371 queries = FieldsQueryList();
372 {if (true) return getDistanceQuery(queries, false /* not infix */, oprt, true /* ordered */);}
376 jj_consume_token(-1);
377 throw new ParseException();
379 throw new Error("Missing return statement in function");
382 final public List<SrndQuery> FieldsQueryList() throws ParseException {
384 ArrayList<SrndQuery> queries = new ArrayList<SrndQuery>();
385 jj_consume_token(LPAREN);
390 jj_consume_token(COMMA);
393 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
402 jj_consume_token(RPAREN);
403 {if (true) return queries;}
404 throw new Error("Missing return statement in function");
407 final public SrndQuery SimpleTerm() throws ParseException {
409 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
411 term = jj_consume_token(TERM);
412 {if (true) return getTermQuery(term.image, false /* not quoted */);}
415 term = jj_consume_token(QUOTED);
416 {if (true) return getTermQuery(term.image.substring(1, term.image.length()-1), true /* quoted */);}
419 term = jj_consume_token(SUFFIXTERM);
421 if (! allowedSuffix(term.image)) {
422 {if (true) throw new ParseException(truncationErrorMessage + term.image);}
424 {if (true) return getPrefixQuery(term.image.substring(0, term.image.length()-1), false /* not quoted */);}
427 term = jj_consume_token(TRUNCTERM);
428 /* with at least one * or ? */
429 if (! allowedTruncation(term.image)) {
430 {if (true) throw new ParseException(truncationErrorMessage + term.image);}
432 {if (true) return getTruncQuery(term.image);}
435 term = jj_consume_token(TRUNCQUOTED);
437 if ((term.image.length() - 3) < minimumPrefixLength) {
438 {if (true) throw new ParseException(truncationErrorMessage + term.image);}
440 {if (true) return getPrefixQuery(term.image.substring(1, term.image.length()-2), true /* quoted */);}
444 jj_consume_token(-1);
445 throw new ParseException();
447 throw new Error("Missing return statement in function");
450 final public void OptionalWeights(SrndQuery q) throws ParseException {
454 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
462 jj_consume_token(CARAT);
463 weight = jj_consume_token(NUMBER);
466 f = Float.valueOf(weight.image).floatValue();
467 } catch (Exception floatExc) {
468 {if (true) throw new ParseException(boostErrorMessage + weight.image + " (" + floatExc + ")");}
471 {if (true) throw new ParseException(boostErrorMessage + weight.image);}
473 q.setWeight(f * q.getWeight()); /* left associative, fwiw */
478 private boolean jj_2_1(int xla) {
479 jj_la = xla; jj_lastpos = jj_scanpos = token;
480 try { return !jj_3_1(); }
481 catch(LookaheadSuccess ls) { return true; }
482 finally { jj_save(0, xla); }
485 private boolean jj_3_1() {
486 if (jj_scan_token(TERM)) return true;
487 if (jj_scan_token(COLON)) return true;
491 /** Generated Token Manager. */
492 public QueryParserTokenManager token_source;
493 /** Current token. */
498 private Token jj_scanpos, jj_lastpos;
501 final private int[] jj_la1 = new int[10];
502 static private int[] jj_la1_0;
506 private static void jj_la1_init_0() {
507 jj_la1_0 = new int[] {0x100,0x200,0x400,0x1000,0x800,0x7c3b00,0x1b00,0x8000,0x7c0000,0x20000,};
509 final private JJCalls[] jj_2_rtns = new JJCalls[1];
510 private boolean jj_rescan = false;
511 private int jj_gc = 0;
513 /** Constructor with user supplied CharStream. */
514 public QueryParser(CharStream stream) {
515 token_source = new QueryParserTokenManager(stream);
519 for (int i = 0; i < 10; i++) jj_la1[i] = -1;
520 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
524 public void ReInit(CharStream stream) {
525 token_source.ReInit(stream);
529 for (int i = 0; i < 10; i++) jj_la1[i] = -1;
530 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
533 /** Constructor with generated Token Manager. */
534 public QueryParser(QueryParserTokenManager tm) {
539 for (int i = 0; i < 10; i++) jj_la1[i] = -1;
540 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
544 public void ReInit(QueryParserTokenManager tm) {
549 for (int i = 0; i < 10; i++) jj_la1[i] = -1;
550 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
553 private Token jj_consume_token(int kind) throws ParseException {
555 if ((oldToken = token).next != null) token = token.next;
556 else token = token.next = token_source.getNextToken();
558 if (token.kind == kind) {
562 for (int i = 0; i < jj_2_rtns.length; i++) {
563 JJCalls c = jj_2_rtns[i];
565 if (c.gen < jj_gen) c.first = null;
574 throw generateParseException();
577 static private final class LookaheadSuccess extends java.lang.Error { }
578 final private LookaheadSuccess jj_ls = new LookaheadSuccess();
579 private boolean jj_scan_token(int kind) {
580 if (jj_scanpos == jj_lastpos) {
582 if (jj_scanpos.next == null) {
583 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
585 jj_lastpos = jj_scanpos = jj_scanpos.next;
588 jj_scanpos = jj_scanpos.next;
591 int i = 0; Token tok = token;
592 while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
593 if (tok != null) jj_add_error_token(kind, i);
595 if (jj_scanpos.kind != kind) return true;
596 if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
601 /** Get the next Token. */
602 final public Token getNextToken() {
603 if (token.next != null) token = token.next;
604 else token = token.next = token_source.getNextToken();
610 /** Get the specific Token. */
611 final public Token getToken(int index) {
613 for (int i = 0; i < index; i++) {
614 if (t.next != null) t = t.next;
615 else t = t.next = token_source.getNextToken();
620 private int jj_ntk() {
621 if ((jj_nt=token.next) == null)
622 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
624 return (jj_ntk = jj_nt.kind);
627 private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
628 private int[] jj_expentry;
629 private int jj_kind = -1;
630 private int[] jj_lasttokens = new int[100];
631 private int jj_endpos;
633 private void jj_add_error_token(int kind, int pos) {
634 if (pos >= 100) return;
635 if (pos == jj_endpos + 1) {
636 jj_lasttokens[jj_endpos++] = kind;
637 } else if (jj_endpos != 0) {
638 jj_expentry = new int[jj_endpos];
639 for (int i = 0; i < jj_endpos; i++) {
640 jj_expentry[i] = jj_lasttokens[i];
642 jj_entries_loop: for (java.util.Iterator it = jj_expentries.iterator(); it.hasNext();) {
643 int[] oldentry = (int[])(it.next());
644 if (oldentry.length == jj_expentry.length) {
645 for (int i = 0; i < jj_expentry.length; i++) {
646 if (oldentry[i] != jj_expentry[i]) {
647 continue jj_entries_loop;
650 jj_expentries.add(jj_expentry);
651 break jj_entries_loop;
654 if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
658 /** Generate ParseException. */
659 public ParseException generateParseException() {
660 jj_expentries.clear();
661 boolean[] la1tokens = new boolean[24];
663 la1tokens[jj_kind] = true;
666 for (int i = 0; i < 10; i++) {
667 if (jj_la1[i] == jj_gen) {
668 for (int j = 0; j < 32; j++) {
669 if ((jj_la1_0[i] & (1<<j)) != 0) {
675 for (int i = 0; i < 24; i++) {
677 jj_expentry = new int[1];
679 jj_expentries.add(jj_expentry);
684 jj_add_error_token(0, 0);
685 int[][] exptokseq = new int[jj_expentries.size()][];
686 for (int i = 0; i < jj_expentries.size(); i++) {
687 exptokseq[i] = jj_expentries.get(i);
689 return new ParseException(token, exptokseq, tokenImage);
692 /** Enable tracing. */
693 final public void enable_tracing() {
696 /** Disable tracing. */
697 final public void disable_tracing() {
700 private void jj_rescan_token() {
702 for (int i = 0; i < 1; i++) {
704 JJCalls p = jj_2_rtns[i];
706 if (p.gen > jj_gen) {
707 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
709 case 0: jj_3_1(); break;
714 } catch(LookaheadSuccess ls) { }
719 private void jj_save(int index, int xla) {
720 JJCalls p = jj_2_rtns[index];
721 while (p.gen > jj_gen) {
722 if (p.next == null) { p = p.next = new JJCalls(); break; }
725 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
728 static final class JJCalls {