Remove debug spew
[beagle.git] / beagled / Lucene.Net / QueryParser / QueryParser.cs
blob3a732b9d4c489ad81d40c371747c366badcc3e12
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Generated By:JavaCC: Do not edit this line. QueryParser.java */
19 using System;
20 using Lucene.Net.Analysis;
21 using Lucene.Net.Documents;
22 using Term = Lucene.Net.Index.Term;
23 using Lucene.Net.Search;
24 using Searchable = Lucene.Net.Search.Searchable;
25 using Parameter = Lucene.Net.Util.Parameter;
27 namespace Lucene.Net.QueryParsers
30 /// <summary> This class is generated by JavaCC. The most important method is
31 /// {@link #Parse(String)}.
32 ///
33 /// The syntax for query strings is as follows:
34 /// A Query is a series of clauses.
35 /// A clause may be prefixed by:
36 /// <ul>
37 /// <li> a plus (<code>+</code>) or a minus (<code>-</code>) sign, indicating
38 /// that the clause is required or prohibited respectively; or
39 /// <li> a term followed by a colon, indicating the field to be searched.
40 /// This enables one to construct queries which search multiple fields.
41 /// </ul>
42 ///
43 /// A clause may be either:
44 /// <ul>
45 /// <li> a term, indicating all the documents that contain this term; or
46 /// <li> a nested query, enclosed in parentheses. Note that this may be used
47 /// with a <code>+</code>/<code>-</code> prefix to require any of a set of
48 /// terms.
49 /// </ul>
50 ///
51 /// Thus, in BNF, the query grammar is:
52 /// <pre>
53 /// Query ::= ( Clause )*
54 /// Clause ::= ["+", "-"] [&lt;TERM&gt; ":"] ( &lt;TERM&gt; | "(" Query ")" )
55 /// </pre>
56 ///
57 /// <p>
58 /// Examples of appropriately formatted queries can be found in the <a
59 /// href="http://lucene.apache.org/java/docs/queryparsersyntax.html">query syntax
60 /// documentation</a>.
61 /// </p>
62 ///
63 /// <p>Note that QueryParser is <em>not</em> thread-safe.</p>
64 ///
65 /// </summary>
66 /// <author> Brian Goetz
67 /// </author>
68 /// <author> Peter Halacsy
69 /// </author>
70 /// <author> Tatu Saloranta
71 /// </author>
73 public class QueryParser : QueryParserConstants
75 private void InitBlock()
77 fuzzyMinSim = FuzzyQuery.defaultMinSimilarity;
78 fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
79 jj_2_rtns = new JJCalls[1];
80 jj_ls = new LookaheadSuccess();
83 private const int CONJ_NONE = 0;
84 private const int CONJ_AND = 1;
85 private const int CONJ_OR = 2;
87 private const int MOD_NONE = 0;
88 private const int MOD_NOT = 10;
89 private const int MOD_REQ = 11;
91 /// <deprecated> use {@link #OR_OPERATOR} instead
92 /// </deprecated>
93 public const int DEFAULT_OPERATOR_OR = 0;
94 /// <deprecated> use {@link #AND_OPERATOR} instead
95 /// </deprecated>
96 public const int DEFAULT_OPERATOR_AND = 1;
98 // make it possible to call setDefaultOperator() without accessing
99 // the nested class:
100 /// <summary>Alternative form of QueryParser.Operator.AND </summary>
101 public static readonly Operator AND_OPERATOR = Operator.AND;
102 /// <summary>Alternative form of QueryParser.Operator.OR </summary>
103 public static readonly Operator OR_OPERATOR = Operator.OR;
105 /// <summary>The actual operator that parser uses to combine query terms </summary>
106 private Operator operator_Renamed = OR_OPERATOR;
108 internal bool lowercaseExpandedTerms = true;
110 internal Analyzer analyzer;
111 internal System.String field;
112 internal int phraseSlop = 0;
113 internal float fuzzyMinSim;
114 internal int fuzzyPrefixLength;
115 internal System.Globalization.CultureInfo locale = System.Threading.Thread.CurrentThread.CurrentCulture;
117 /// <summary>The default operator for parsing queries.
118 /// Use {@link QueryParser#setDefaultOperator} to change it.
119 /// </summary>
120 [Serializable]
121 public sealed class Operator : Parameter
123 internal Operator(System.String name) : base(name)
126 public static readonly Operator OR = new Operator("OR");
127 public static readonly Operator AND = new Operator("AND");
130 /// <summary>Parses a query string, returning a {@link Lucene.Net.search.Query}.</summary>
131 /// <param name="query"> the query string to be parsed.
132 /// </param>
133 /// <param name="field"> the default field for query terms.
134 /// </param>
135 /// <param name="analyzer"> used to find terms in the query text.
136 /// </param>
137 /// <throws> ParseException if the parsing fails </throws>
138 /// <summary>
139 /// </summary>
140 /// <deprecated> Use an instance of QueryParser and the {@link #Parse(String)} method instead.
141 /// </deprecated>
142 static public Query Parse(System.String query, System.String field, Analyzer analyzer)
144 QueryParser parser = new QueryParser(field, analyzer);
145 return parser.Parse(query);
148 /// <summary>Constructs a query parser.</summary>
149 /// <param name="f"> the default field for query terms.
150 /// </param>
151 /// <param name="a"> used to find terms in the query text.
152 /// </param>
153 public QueryParser(System.String f, Analyzer a) : this(new FastCharStream(new System.IO.StringReader("")))
155 analyzer = a;
156 field = f;
159 /// <summary>Parses a query string, returning a {@link Lucene.Net.search.Query}.</summary>
160 /// <param name="query"> the query string to be parsed.
161 /// </param>
162 /// <throws> ParseException if the parsing fails </throws>
163 public virtual Query Parse(System.String query)
165 ReInit(new FastCharStream(new System.IO.StringReader(query)));
168 return Query(field);
170 catch (TokenMgrError tme)
172 throw new ParseException(tme.Message);
174 catch (BooleanQuery.TooManyClauses)
176 throw new ParseException("Too many boolean clauses");
180 /// <returns> Returns the analyzer.
181 /// </returns>
182 public virtual Analyzer GetAnalyzer()
184 return analyzer;
187 /// <returns> Returns the field.
188 /// </returns>
189 public virtual System.String GetField()
191 return field;
194 /// <summary> Get the minimal similarity for fuzzy queries.</summary>
195 public virtual float GetFuzzyMinSim()
197 return fuzzyMinSim;
200 /// <summary> Set the minimum similarity for fuzzy queries.
201 /// Default is 0.5f.
202 /// </summary>
203 public virtual void SetFuzzyMinSim(float fuzzyMinSim)
205 this.fuzzyMinSim = fuzzyMinSim;
208 /// <summary> Get the prefix length for fuzzy queries. </summary>
209 /// <returns> Returns the fuzzyPrefixLength.
210 /// </returns>
211 public virtual int GetFuzzyPrefixLength()
213 return fuzzyPrefixLength;
216 /// <summary> Set the prefix length for fuzzy queries. Default is 0.</summary>
217 /// <param name="fuzzyPrefixLength">The fuzzyPrefixLength to set.
218 /// </param>
219 public virtual void SetFuzzyPrefixLength(int fuzzyPrefixLength)
221 this.fuzzyPrefixLength = fuzzyPrefixLength;
224 /// <summary> Sets the default slop for phrases. If zero, then exact phrase matches
225 /// are required. Default value is zero.
226 /// </summary>
227 public virtual void SetPhraseSlop(int phraseSlop)
229 this.phraseSlop = phraseSlop;
232 /// <summary> Gets the default slop for phrases.</summary>
233 public virtual int GetPhraseSlop()
235 return phraseSlop;
238 /// <summary> Sets the boolean operator of the QueryParser.
239 /// In default mode (<code>DEFAULT_OPERATOR_OR</code>) terms without any modifiers
240 /// are considered optional: for example <code>capital of Hungary</code> is equal to
241 /// <code>capital OR of OR Hungary</code>.<br/>
242 /// In <code>DEFAULT_OPERATOR_AND</code> terms are considered to be in conjuction: the
243 /// above mentioned query is parsed as <code>capital AND of AND Hungary</code>
244 /// </summary>
245 /// <deprecated> use {@link #SetDefaultOperator(QueryParser.Operator)} instead
246 /// </deprecated>
247 public virtual void SetOperator(int op)
249 if (op == DEFAULT_OPERATOR_AND)
250 this.operator_Renamed = AND_OPERATOR;
251 else if (op == DEFAULT_OPERATOR_OR)
252 this.operator_Renamed = OR_OPERATOR;
253 else
254 throw new System.ArgumentException("Unknown operator " + op);
257 /// <summary> Sets the boolean operator of the QueryParser.
258 /// In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
259 /// are considered optional: for example <code>capital of Hungary</code> is equal to
260 /// <code>capital OR of OR Hungary</code>.<br/>
261 /// In <code>AND_OPERATOR</code> mode terms are considered to be in conjuction: the
262 /// above mentioned query is parsed as <code>capital AND of AND Hungary</code>
263 /// </summary>
264 public virtual void SetDefaultOperator(Operator op)
266 this.operator_Renamed = op;
269 /// <summary> Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
270 /// or DEFAULT_OPERATOR_OR.
271 /// </summary>
272 /// <deprecated> use {@link #GetDefaultOperator()} instead
273 /// </deprecated>
274 public virtual int GetOperator()
276 if (operator_Renamed == AND_OPERATOR)
277 return DEFAULT_OPERATOR_AND;
278 else if (operator_Renamed == OR_OPERATOR)
279 return DEFAULT_OPERATOR_OR;
280 else
282 throw new System.SystemException("Unknown operator " + operator_Renamed);
286 /// <summary> Gets implicit operator setting, which will be either AND_OPERATOR
287 /// or OR_OPERATOR.
288 /// </summary>
289 public virtual Operator GetDefaultOperator()
291 return operator_Renamed;
294 /// <summary> Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
295 /// lower-cased or not. Default is <code>true</code>.
296 /// </summary>
297 /// <deprecated> use {@link #SetLowercaseExpandedTerms(boolean)} instead
298 /// </deprecated>
299 public virtual void SetLowercaseWildcardTerms(bool lowercaseExpandedTerms)
301 this.lowercaseExpandedTerms = lowercaseExpandedTerms;
304 /// <summary> Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
305 /// lower-cased or not. Default is <code>true</code>.
306 /// </summary>
307 public virtual void SetLowercaseExpandedTerms(bool lowercaseExpandedTerms)
309 this.lowercaseExpandedTerms = lowercaseExpandedTerms;
312 /// <deprecated> use {@link #GetLowercaseExpandedTerms()} instead
313 /// </deprecated>
314 public virtual bool GetLowercaseWildcardTerms()
316 return lowercaseExpandedTerms;
319 /// <seealso cref="SetLowercaseExpandedTerms(boolean)">
320 /// </seealso>
321 public virtual bool GetLowercaseExpandedTerms()
323 return lowercaseExpandedTerms;
326 /// <summary> Set locale used by date range parsing.</summary>
327 public virtual void SetLocale(System.Globalization.CultureInfo locale)
329 this.locale = locale;
332 /// <summary> Returns current locale, allowing access by subclasses.</summary>
333 public virtual System.Globalization.CultureInfo GetLocale()
335 return locale;
338 protected internal virtual void AddClause(System.Collections.ArrayList clauses, int conj, int mods, Query q)
340 bool required, prohibited;
342 // If this term is introduced by AND, make the preceding term required,
343 // unless it's already prohibited
344 if (clauses.Count > 0 && conj == CONJ_AND)
346 BooleanClause c = (BooleanClause) clauses[clauses.Count - 1];
347 if (!c.IsProhibited())
348 c.SetOccur(BooleanClause.Occur.MUST);
351 if (clauses.Count > 0 && operator_Renamed == AND_OPERATOR && conj == CONJ_OR)
353 // If this term is introduced by OR, make the preceding term optional,
354 // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
355 // notice if the input is a OR b, first term is parsed as required; without
356 // this modification a OR b would parsed as +a OR b
357 BooleanClause c = (BooleanClause) clauses[clauses.Count - 1];
358 if (!c.IsProhibited())
359 c.SetOccur(BooleanClause.Occur.SHOULD);
362 // We might have been passed a null query; the term might have been
363 // filtered away by the analyzer.
364 if (q == null)
365 return ;
367 if (operator_Renamed == OR_OPERATOR)
369 // We set REQUIRED if we're introduced by AND or +; PROHIBITED if
370 // introduced by NOT or -; make sure not to set both.
371 prohibited = (mods == MOD_NOT);
372 required = (mods == MOD_REQ);
373 if (conj == CONJ_AND && !prohibited)
375 required = true;
378 else
380 // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED
381 // if not PROHIBITED and not introduced by OR
382 prohibited = (mods == MOD_NOT);
383 required = (!prohibited && conj != CONJ_OR);
385 if (required && !prohibited)
386 clauses.Add(new BooleanClause(q, BooleanClause.Occur.MUST));
387 else if (!required && !prohibited)
388 clauses.Add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
389 else if (!required && prohibited)
390 clauses.Add(new BooleanClause(q, BooleanClause.Occur.MUST_NOT));
391 else
392 throw new System.SystemException("Clause cannot be both required and prohibited");
395 /// <summary> Note that parameter analyzer is ignored. Calls inside the parser always
396 /// use class member analyzer.
397 ///
398 /// </summary>
399 /// <exception cref="ParseException">throw in overridden method to disallow
400 /// </exception>
401 /// <deprecated> use {@link #GetFieldQuery(String, String)}
402 /// </deprecated>
403 protected internal virtual Query GetFieldQuery(System.String field, Analyzer analyzer, System.String queryText)
405 return GetFieldQuery(field, queryText);
408 /// <exception cref="ParseException">throw in overridden method to disallow
409 /// </exception>
410 protected internal virtual Query GetFieldQuery(System.String field, System.String queryText)
412 // Use the analyzer to get all the tokens, and then build a TermQuery,
413 // PhraseQuery, or nothing based on the term count
415 TokenStream source = analyzer.TokenStream(field, new System.IO.StringReader(queryText));
416 System.Collections.ArrayList v = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
417 Lucene.Net.Analysis.Token t;
418 int positionCount = 0;
419 bool severalTokensAtSamePosition = false;
421 while (true)
425 t = source.Next();
427 catch (System.IO.IOException e)
429 t = null;
431 if (t == null)
432 break;
433 v.Add(t);
434 if (t.GetPositionIncrement() != 0)
435 positionCount += t.GetPositionIncrement();
436 else
437 severalTokensAtSamePosition = true;
441 source.Close();
443 catch (System.IO.IOException e)
445 // ignore
448 if (v.Count == 0)
449 return null;
450 else if (v.Count == 1)
452 t = (Lucene.Net.Analysis.Token) v[0];
453 return new TermQuery(new Term(field, t.TermText()));
455 else
457 if (severalTokensAtSamePosition)
459 if (positionCount == 1)
461 // no phrase query:
462 BooleanQuery q = new BooleanQuery(true);
463 for (int i = 0; i < v.Count; i++)
465 t = (Lucene.Net.Analysis.Token) v[i];
466 TermQuery currentQuery = new TermQuery(new Term(field, t.TermText()));
467 q.Add(currentQuery, BooleanClause.Occur.SHOULD);
469 return q;
471 else
473 // phrase query:
474 MultiPhraseQuery mpq = new MultiPhraseQuery();
475 System.Collections.ArrayList multiTerms = new System.Collections.ArrayList();
476 for (int i = 0; i < v.Count; i++)
478 t = (Lucene.Net.Analysis.Token) v[i];
479 if (t.GetPositionIncrement() == 1 && multiTerms.Count > 0)
481 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
482 multiTerms.Clear();
484 multiTerms.Add(new Term(field, t.TermText()));
486 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
487 return mpq;
490 else
492 PhraseQuery q = new PhraseQuery();
493 q.SetSlop(phraseSlop);
494 for (int i = 0; i < v.Count; i++)
496 q.Add(new Term(field, ((Lucene.Net.Analysis.Token) v[i]).TermText()));
498 return q;
503 /// <summary> Note that parameter analyzer is ignored. Calls inside the parser always
504 /// use class member analyzer.
505 ///
506 /// </summary>
507 /// <exception cref="ParseException">throw in overridden method to disallow
508 /// </exception>
509 /// <deprecated> use {@link #GetFieldQuery(String, String, int)}
510 /// </deprecated>
511 protected internal virtual Query GetFieldQuery(System.String field, Analyzer analyzer, System.String queryText, int slop)
513 return GetFieldQuery(field, queryText, slop);
516 /// <summary> Base implementation delegates to {@link #GetFieldQuery(String,String)}.
517 /// This method may be overridden, for example, to return
518 /// a SpanNearQuery instead of a PhraseQuery.
519 ///
520 /// </summary>
521 /// <exception cref="ParseException">throw in overridden method to disallow
522 /// </exception>
523 protected internal virtual Query GetFieldQuery(System.String field, System.String queryText, int slop)
525 Query query = GetFieldQuery(field, queryText);
527 if (query is PhraseQuery)
529 ((PhraseQuery) query).SetSlop(slop);
531 if (query is MultiPhraseQuery)
533 ((MultiPhraseQuery) query).SetSlop(slop);
536 return query;
539 /// <summary> Note that parameter analyzer is ignored. Calls inside the parser always
540 /// use class member analyzer.
541 ///
542 /// </summary>
543 /// <exception cref="ParseException">throw in overridden method to disallow
544 /// </exception>
545 /// <deprecated> use {@link #GetRangeQuery(String, String, String, boolean)}
546 /// </deprecated>
547 protected internal virtual Query GetRangeQuery(System.String field, Analyzer analyzer, System.String part1, System.String part2, bool inclusive)
549 return GetRangeQuery(field, part1, part2, inclusive);
552 /// <exception cref="ParseException">throw in overridden method to disallow
553 /// </exception>
554 protected internal virtual Query GetRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
556 if (lowercaseExpandedTerms)
558 part1 = part1.ToLower();
559 part2 = part2.ToLower();
563 System.DateTime d1 = System.DateTime.Parse(part1, locale);
564 System.DateTime d2 = System.DateTime.Parse(part2, locale);
565 part1 = DateField.DateToString(d1);
566 part2 = DateField.DateToString(d2);
568 catch (System.Exception e)
572 return new RangeQuery(new Term(field, part1), new Term(field, part2), inclusive);
575 /// <summary> Factory method for generating query, given a set of clauses.
576 /// By default creates a boolean query composed of clauses passed in.
577 ///
578 /// Can be overridden by extending classes, to modify query being
579 /// returned.
580 ///
581 /// </summary>
582 /// <param name="clauses">Vector that contains {@link BooleanClause} instances
583 /// to join.
584 ///
585 /// </param>
586 /// <returns> Resulting {@link Query} object.
587 /// </returns>
588 /// <exception cref="ParseException">throw in overridden method to disallow
589 /// </exception>
590 protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses)
592 return GetBooleanQuery(clauses, false);
595 /// <summary> Factory method for generating query, given a set of clauses.
596 /// By default creates a boolean query composed of clauses passed in.
597 ///
598 /// Can be overridden by extending classes, to modify query being
599 /// returned.
600 ///
601 /// </summary>
602 /// <param name="clauses">Vector that contains {@link BooleanClause} instances
603 /// to join.
604 /// </param>
605 /// <param name="disableCoord">true if coord scoring should be disabled.
606 ///
607 /// </param>
608 /// <returns> Resulting {@link Query} object.
609 /// </returns>
610 /// <exception cref="ParseException">throw in overridden method to disallow
611 /// </exception>
612 protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses, bool disableCoord)
614 BooleanQuery query = new BooleanQuery(disableCoord);
615 for (int i = 0; i < clauses.Count; i++)
617 query.Add((BooleanClause) clauses[i]);
619 return query;
622 /// <summary> Factory method for generating a query. Called when parser
623 /// parses an input term token that contains one or more wildcard
624 /// characters (? and *), but is not a prefix term token (one
625 /// that has just a single * character at the end)
626 /// <p>
627 /// Depending on settings, prefix term may be lower-cased
628 /// automatically. It will not go through the default Analyzer,
629 /// however, since normal Analyzers are unlikely to work properly
630 /// with wildcard templates.
631 /// <p>
632 /// Can be overridden by extending classes, to provide custom handling for
633 /// wildcard queries, which may be necessary due to missing analyzer calls.
634 ///
635 /// </summary>
636 /// <param name="field">Name of the field query will use.
637 /// </param>
638 /// <param name="termStr">Term token that contains one or more wild card
639 /// characters (? or *), but is not simple prefix term
640 ///
641 /// </param>
642 /// <returns> Resulting {@link Query} built for the term
643 /// </returns>
644 /// <exception cref="ParseException">throw in overridden method to disallow
645 /// </exception>
646 protected internal virtual Query GetWildcardQuery(System.String field, System.String termStr)
648 if (lowercaseExpandedTerms)
650 termStr = termStr.ToLower();
652 Term t = new Term(field, termStr);
653 return new WildcardQuery(t);
656 /// <summary> Factory method for generating a query (similar to
657 /// {@link #getWildcardQuery}). Called when parser parses an input term
658 /// token that uses prefix notation; that is, contains a single '*' wildcard
659 /// character as its last character. Since this is a special case
660 /// of generic wildcard term, and such a query can be optimized easily,
661 /// this usually results in a different query object.
662 /// <p>
663 /// Depending on settings, a prefix term may be lower-cased
664 /// automatically. It will not go through the default Analyzer,
665 /// however, since normal Analyzers are unlikely to work properly
666 /// with wildcard templates.
667 /// <p>
668 /// Can be overridden by extending classes, to provide custom handling for
669 /// wild card queries, which may be necessary due to missing analyzer calls.
670 ///
671 /// </summary>
672 /// <param name="field">Name of the field query will use.
673 /// </param>
674 /// <param name="termStr">Term token to use for building term for the query
675 /// (<b>without</b> trailing '*' character!)
676 ///
677 /// </param>
678 /// <returns> Resulting {@link Query} built for the term
679 /// </returns>
680 /// <exception cref="ParseException">throw in overridden method to disallow
681 /// </exception>
682 protected internal virtual Query GetPrefixQuery(System.String field, System.String termStr)
684 if (lowercaseExpandedTerms)
686 termStr = termStr.ToLower();
688 Term t = new Term(field, termStr);
689 return new PrefixQuery(t);
692 /// <deprecated> use {@link #GetFuzzyQuery(String, String, float)}
693 /// </deprecated>
694 protected internal virtual Query GetFuzzyQuery(System.String field, System.String termStr)
696 return GetFuzzyQuery(field, termStr, fuzzyMinSim);
699 /// <summary> Factory method for generating a query (similar to
700 /// {@link #getWildcardQuery}). Called when parser parses
701 /// an input term token that has the fuzzy suffix (~) appended.
702 ///
703 /// </summary>
704 /// <param name="field">Name of the field query will use.
705 /// </param>
706 /// <param name="termStr">Term token to use for building term for the query
707 ///
708 /// </param>
709 /// <returns> Resulting {@link Query} built for the term
710 /// </returns>
711 /// <exception cref="ParseException">throw in overridden method to disallow
712 /// </exception>
713 protected internal virtual Query GetFuzzyQuery(System.String field, System.String termStr, float minSimilarity)
715 if (lowercaseExpandedTerms)
717 termStr = termStr.ToLower();
719 Term t = new Term(field, termStr);
720 return new FuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
723 /// <summary> Returns a String where the escape char has been
724 /// removed, or kept only once if there was a double escape.
725 /// </summary>
726 private System.String DiscardEscapeChar(System.String input)
728 char[] caSource = input.ToCharArray();
729 char[] caDest = new char[caSource.Length];
730 int j = 0;
731 for (int i = 0; i < caSource.Length; i++)
733 if ((caSource[i] != '\\') || (i > 0 && caSource[i - 1] == '\\'))
735 caDest[j++] = caSource[i];
738 return new System.String(caDest, 0, j);
741 /// <summary> Returns a String where those characters that QueryParser
742 /// expects to be escaped are escaped by a preceding <code>\</code>.
743 /// </summary>
744 public static System.String Escape(System.String s)
746 System.Text.StringBuilder sb = new System.Text.StringBuilder();
747 for (int i = 0; i < s.Length; i++)
749 char c = s[i];
750 // NOTE: keep this in sync with _ESCAPED_CHAR below!
751 if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?')
753 sb.Append('\\');
755 sb.Append(c);
757 return sb.ToString();
760 /// <summary> Command line tool to test QueryParser, using {@link Lucene.Net.analysis.SimpleAnalyzer}.
761 /// Usage:<br>
762 /// <code>java Lucene.Net.queryParser.QueryParser &lt;input&gt;</code>
763 /// </summary>
764 [STAThread]
765 public static void Main(System.String[] args)
767 if (args.Length == 0)
769 System.Console.Out.WriteLine("Usage: java Lucene.Net.queryParser.QueryParser <input>");
770 System.Environment.Exit(0);
772 QueryParser qp = new QueryParser("field", new Lucene.Net.Analysis.SimpleAnalyzer());
773 Query q = qp.Parse(args[0]);
774 System.Console.Out.WriteLine(q.ToString("field"));
777 // * Query ::= ( Clause )*
778 // * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
779 public int Conjunction()
781 int ret = CONJ_NONE;
782 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
785 case Lucene.Net.QueryParsers.QueryParserConstants.AND:
786 case Lucene.Net.QueryParsers.QueryParserConstants.OR:
787 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
790 case Lucene.Net.QueryParsers.QueryParserConstants.AND:
791 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.AND);
792 ret = CONJ_AND;
793 break;
795 case Lucene.Net.QueryParsers.QueryParserConstants.OR:
796 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.OR);
797 ret = CONJ_OR;
798 break;
800 default:
801 jj_la1[0] = jj_gen;
802 Jj_consume_token(- 1);
803 throw new ParseException();
806 break;
808 default:
809 jj_la1[1] = jj_gen;
811 break;
815 if (true)
816 return ret;
818 throw new System.ApplicationException("Missing return statement in function");
821 public int Modifiers()
823 int ret = MOD_NONE;
824 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
827 case Lucene.Net.QueryParsers.QueryParserConstants.NOT:
828 case Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
829 case Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
830 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
833 case Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
834 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.PLUS);
835 ret = MOD_REQ;
836 break;
838 case Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
839 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.MINUS);
840 ret = MOD_NOT;
841 break;
843 case Lucene.Net.QueryParsers.QueryParserConstants.NOT:
844 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NOT);
845 ret = MOD_NOT;
846 break;
848 default:
849 jj_la1[2] = jj_gen;
850 Jj_consume_token(- 1);
851 throw new ParseException();
854 break;
856 default:
857 jj_la1[3] = jj_gen;
859 break;
863 if (true)
864 return ret;
866 throw new System.ApplicationException("Missing return statement in function");
869 public Query Query(System.String field)
871 System.Collections.ArrayList clauses = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
872 Query q, firstQuery = null;
873 int conj, mods;
874 mods = Modifiers();
875 q = Clause(field);
876 AddClause(clauses, CONJ_NONE, mods, q);
877 if (mods == MOD_NONE)
878 firstQuery = q;
879 while (true)
881 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
884 case Lucene.Net.QueryParsers.QueryParserConstants.AND:
885 case Lucene.Net.QueryParsers.QueryParserConstants.OR:
886 case Lucene.Net.QueryParsers.QueryParserConstants.NOT:
887 case Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
888 case Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
889 case Lucene.Net.QueryParsers.QueryParserConstants.LPAREN:
890 case Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
891 case Lucene.Net.QueryParsers.QueryParserConstants.TERM:
892 case Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
893 case Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
894 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
895 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
896 case Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
898 break;
900 default:
901 jj_la1[4] = jj_gen;
902 goto label_1_brk;
905 conj = Conjunction();
906 mods = Modifiers();
907 q = Clause(field);
908 AddClause(clauses, conj, mods, q);
911 label_1_brk: ;
913 if (clauses.Count == 1 && firstQuery != null)
915 if (true)
916 return firstQuery;
918 else
921 if (true)
922 return GetBooleanQuery(clauses);
925 throw new System.ApplicationException("Missing return statement in function");
928 public Query Clause(System.String field)
930 Query q;
931 Token fieldToken = null, boost = null;
932 if (Jj_2_1(2))
934 fieldToken = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.TERM);
935 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.COLON);
936 field = DiscardEscapeChar(fieldToken.image);
938 else
942 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
945 case Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
946 case Lucene.Net.QueryParsers.QueryParserConstants.TERM:
947 case Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
948 case Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
949 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
950 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
951 case Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
952 q = Term(field);
953 break;
955 case Lucene.Net.QueryParsers.QueryParserConstants.LPAREN:
956 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.LPAREN);
957 q = Query(field);
958 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RPAREN);
959 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
962 case Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
963 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
964 boost = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
965 break;
967 default:
968 jj_la1[5] = jj_gen;
970 break;
973 break;
975 default:
976 jj_la1[6] = jj_gen;
977 Jj_consume_token(- 1);
978 throw new ParseException();
981 if (boost != null)
983 float f = (float) 1.0;
986 f = (float) System.Single.Parse(boost.image);
987 q.SetBoost(f);
989 catch (System.Exception ignored)
994 if (true)
995 return q;
997 throw new System.ApplicationException("Missing return statement in function");
1000 public Query Term(System.String field)
1002 Token term, boost = null, fuzzySlop = null, goop1, goop2;
1003 bool prefix = false;
1004 bool wildcard = false;
1005 bool fuzzy = false;
1006 bool rangein = false;
1007 Query q;
1008 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1011 case Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1012 case Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1013 case Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1014 case Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1015 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1018 case Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1019 term = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.TERM);
1020 break;
1022 case Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1023 term = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM);
1024 prefix = true;
1025 break;
1027 case Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1028 term = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM);
1029 wildcard = true;
1030 break;
1032 case Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1033 term = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1034 break;
1036 default:
1037 jj_la1[7] = jj_gen;
1038 Jj_consume_token(- 1);
1039 throw new ParseException();
1042 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1045 case Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1046 fuzzySlop = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1047 fuzzy = true;
1048 break;
1050 default:
1051 jj_la1[8] = jj_gen;
1053 break;
1056 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1059 case Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1060 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1061 boost = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1062 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1065 case Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1066 fuzzySlop = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1067 fuzzy = true;
1068 break;
1070 default:
1071 jj_la1[9] = jj_gen;
1073 break;
1076 break;
1078 default:
1079 jj_la1[10] = jj_gen;
1081 break;
1084 System.String termImage = DiscardEscapeChar(term.image);
1085 if (wildcard)
1087 q = GetWildcardQuery(field, termImage);
1089 else if (prefix)
1091 q = GetPrefixQuery(field, DiscardEscapeChar(term.image.Substring(0, (term.image.Length - 1) - (0))));
1093 else if (fuzzy)
1095 float fms = fuzzyMinSim;
1098 fms = (float) System.Single.Parse(fuzzySlop.image.Substring(1));
1100 catch (System.Exception ignored)
1103 if (fms < 0.0f || fms > 1.0f)
1106 if (true)
1107 throw new ParseException("Minimum similarity for a FuzzyQuery has to be between 0.0f and 1.0f !");
1110 if (fms == fuzzyMinSim)
1111 q = GetFuzzyQuery(field, termImage);
1112 else
1113 q = GetFuzzyQuery(field, termImage, fms);
1115 else
1117 q = GetFieldQuery(field, analyzer, termImage);
1119 break;
1121 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
1122 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START);
1123 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1126 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP:
1127 goop1 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP);
1128 break;
1130 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED:
1131 goop1 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED);
1132 break;
1134 default:
1135 jj_la1[11] = jj_gen;
1136 Jj_consume_token(- 1);
1137 throw new ParseException();
1140 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1143 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_TO:
1144 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_TO);
1145 break;
1147 default:
1148 jj_la1[12] = jj_gen;
1150 break;
1153 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1156 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP:
1157 goop2 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP);
1158 break;
1160 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED:
1161 goop2 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED);
1162 break;
1164 default:
1165 jj_la1[13] = jj_gen;
1166 Jj_consume_token(- 1);
1167 throw new ParseException();
1170 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_END);
1171 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1174 case Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1175 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1176 boost = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1177 break;
1179 default:
1180 jj_la1[14] = jj_gen;
1182 break;
1185 if (goop1.kind == Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED)
1187 goop1.image = goop1.image.Substring(1, (goop1.image.Length - 1) - (1));
1189 else
1191 goop1.image = DiscardEscapeChar(goop1.image);
1193 if (goop2.kind == Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED)
1195 goop2.image = goop2.image.Substring(1, (goop2.image.Length - 1) - (1));
1197 else
1199 goop2.image = DiscardEscapeChar(goop2.image);
1201 q = GetRangeQuery(field, analyzer, goop1.image, goop2.image, true);
1202 break;
1204 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
1205 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START);
1206 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1209 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP:
1210 goop1 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP);
1211 break;
1213 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED:
1214 goop1 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED);
1215 break;
1217 default:
1218 jj_la1[15] = jj_gen;
1219 Jj_consume_token(- 1);
1220 throw new ParseException();
1223 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1226 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_TO:
1227 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_TO);
1228 break;
1230 default:
1231 jj_la1[16] = jj_gen;
1233 break;
1236 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1239 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP:
1240 goop2 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP);
1241 break;
1243 case Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED:
1244 goop2 = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED);
1245 break;
1247 default:
1248 jj_la1[17] = jj_gen;
1249 Jj_consume_token(- 1);
1250 throw new ParseException();
1253 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_END);
1254 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1257 case Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1258 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1259 boost = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1260 break;
1262 default:
1263 jj_la1[18] = jj_gen;
1265 break;
1268 if (goop1.kind == Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED)
1270 goop1.image = goop1.image.Substring(1, (goop1.image.Length - 1) - (1));
1272 else
1274 goop1.image = DiscardEscapeChar(goop1.image);
1276 if (goop2.kind == Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED)
1278 goop2.image = goop2.image.Substring(1, (goop2.image.Length - 1) - (1));
1280 else
1282 goop2.image = DiscardEscapeChar(goop2.image);
1285 q = GetRangeQuery(field, analyzer, goop1.image, goop2.image, false);
1286 break;
1288 case Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
1289 term = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.QUOTED);
1290 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1293 case Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1294 fuzzySlop = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1295 break;
1297 default:
1298 jj_la1[19] = jj_gen;
1300 break;
1303 switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
1306 case Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1307 Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1308 boost = Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1309 break;
1311 default:
1312 jj_la1[20] = jj_gen;
1314 break;
1317 int s = phraseSlop;
1319 if (fuzzySlop != null)
1323 s = (int) System.Single.Parse(fuzzySlop.image.Substring(1));
1325 catch (System.Exception ignored)
1329 q = GetFieldQuery(field, analyzer, term.image.Substring(1, (term.image.Length - 1) - (1)), s);
1330 break;
1332 default:
1333 jj_la1[21] = jj_gen;
1334 Jj_consume_token(- 1);
1335 throw new ParseException();
1338 if (boost != null)
1340 float f = (float) 1.0;
1343 f = (float) System.Single.Parse(boost.image);
1345 catch (System.Exception ignored)
1347 /* Should this be handled somehow? (defaults to "no boost", if
1348 * boost number is invalid)
1352 // avoid boosting null queries, such as those caused by stop words
1353 if (q != null)
1355 q.SetBoost(f);
1359 if (true)
1360 return q;
1362 throw new System.ApplicationException("Missing return statement in function");
1365 private bool Jj_2_1(int xla)
1367 jj_la = xla; jj_lastpos = jj_scanpos = token;
1370 return !Jj_3_1();
1372 catch (LookaheadSuccess ls)
1374 return true;
1376 finally
1378 Jj_save(0, xla);
1382 private bool Jj_3_1()
1384 if (Jj_scan_token(Lucene.Net.QueryParsers.QueryParserConstants.TERM))
1385 return true;
1386 if (Jj_scan_token(Lucene.Net.QueryParsers.QueryParserConstants.COLON))
1387 return true;
1388 return false;
1391 public QueryParserTokenManager token_source;
1392 public Token token, jj_nt;
1393 private int jj_ntk;
1394 private Token jj_scanpos, jj_lastpos;
1395 private int jj_la;
1396 public bool lookingAhead = false;
1397 private bool jj_semLA;
1398 private int jj_gen;
1399 private int[] jj_la1 = new int[22];
1400 private static uint[] jj_la1_0;
1401 private static void Jj_la1_0()
1403 jj_la1_0 = new uint[]{0x180, 0x180, 0xe00, 0xe00, 0xfb1f80, 0x8000, 0xfb1000, 0x9a0000, 0x40000, 0x40000, 0x8000, 0xc000000, 0x1000000, 0xc000000, 0x8000, 0xc0000000, 0x10000000, 0xc0000000, 0x8000, 0x40000, 0x8000, 0xfb0000};
1405 private JJCalls[] jj_2_rtns;
1406 private bool jj_rescan = false;
1407 private int jj_gc = 0;
1409 public QueryParser(CharStream stream)
1411 InitBlock();
1412 token_source = new QueryParserTokenManager(stream);
1413 token = new Token();
1414 jj_ntk = - 1;
1415 jj_gen = 0;
1416 for (int i = 0; i < 22; i++)
1417 jj_la1[i] = - 1;
1418 for (int i = 0; i < jj_2_rtns.Length; i++)
1419 jj_2_rtns[i] = new JJCalls();
1422 public virtual void ReInit(CharStream stream)
1424 token_source.ReInit(stream);
1425 token = new Token();
1426 jj_ntk = - 1;
1427 jj_gen = 0;
1428 for (int i = 0; i < 22; i++)
1429 jj_la1[i] = - 1;
1430 for (int i = 0; i < jj_2_rtns.Length; i++)
1431 jj_2_rtns[i] = new JJCalls();
1434 public QueryParser(QueryParserTokenManager tm)
1436 InitBlock();
1437 token_source = tm;
1438 token = new Token();
1439 jj_ntk = - 1;
1440 jj_gen = 0;
1441 for (int i = 0; i < 22; i++)
1442 jj_la1[i] = - 1;
1443 for (int i = 0; i < jj_2_rtns.Length; i++)
1444 jj_2_rtns[i] = new JJCalls();
1447 public virtual void ReInit(QueryParserTokenManager tm)
1449 token_source = tm;
1450 token = new Token();
1451 jj_ntk = - 1;
1452 jj_gen = 0;
1453 for (int i = 0; i < 22; i++)
1454 jj_la1[i] = - 1;
1455 for (int i = 0; i < jj_2_rtns.Length; i++)
1456 jj_2_rtns[i] = new JJCalls();
1459 private Token Jj_consume_token(int kind)
1461 Token oldToken;
1462 if ((oldToken = token).next != null)
1463 token = token.next;
1464 else
1465 token = token.next = token_source.GetNextToken();
1466 jj_ntk = - 1;
1467 if (token.kind == kind)
1469 jj_gen++;
1470 if (++jj_gc > 100)
1472 jj_gc = 0;
1473 for (int i = 0; i < jj_2_rtns.Length; i++)
1475 JJCalls c = jj_2_rtns[i];
1476 while (c != null)
1478 if (c.gen < jj_gen)
1479 c.first = null;
1480 c = c.next;
1484 return token;
1486 token = oldToken;
1487 jj_kind = kind;
1488 throw GenerateParseException();
1491 [Serializable]
1492 private sealed class LookaheadSuccess:System.ApplicationException
1495 private LookaheadSuccess jj_ls;
1496 private bool Jj_scan_token(int kind)
1498 if (jj_scanpos == jj_lastpos)
1500 jj_la--;
1501 if (jj_scanpos.next == null)
1503 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.GetNextToken();
1505 else
1507 jj_lastpos = jj_scanpos = jj_scanpos.next;
1510 else
1512 jj_scanpos = jj_scanpos.next;
1514 if (jj_rescan)
1516 int i = 0; Token tok = token;
1517 while (tok != null && tok != jj_scanpos)
1519 i++; tok = tok.next;
1521 if (tok != null)
1522 Jj_add_error_token(kind, i);
1524 if (jj_scanpos.kind != kind)
1525 return true;
1526 if (jj_la == 0 && jj_scanpos == jj_lastpos)
1527 throw jj_ls;
1528 return false;
1531 public Token GetNextToken()
1533 if (token.next != null)
1534 token = token.next;
1535 else
1536 token = token.next = token_source.GetNextToken();
1537 jj_ntk = - 1;
1538 jj_gen++;
1539 return token;
1542 public Token GetToken(int index)
1544 Token t = lookingAhead ? jj_scanpos : token;
1545 for (int i = 0; i < index; i++)
1547 if (t.next != null)
1548 t = t.next;
1549 else
1550 t = t.next = token_source.GetNextToken();
1552 return t;
1555 private int Jj_ntk()
1557 if ((jj_nt = token.next) == null)
1558 return (jj_ntk = (token.next = token_source.GetNextToken()).kind);
1559 else
1560 return (jj_ntk = jj_nt.kind);
1563 private System.Collections.ArrayList jj_expentries = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
1564 private int[] jj_expentry;
1565 private int jj_kind = - 1;
1566 private int[] jj_lasttokens = new int[100];
1567 private int jj_endpos;
1569 private void Jj_add_error_token(int kind, int pos)
1571 if (pos >= 100)
1572 return ;
1573 if (pos == jj_endpos + 1)
1575 jj_lasttokens[jj_endpos++] = kind;
1577 else if (jj_endpos != 0)
1579 jj_expentry = new int[jj_endpos];
1580 for (int i = 0; i < jj_endpos; i++)
1582 jj_expentry[i] = jj_lasttokens[i];
1584 bool exists = false;
1585 for (System.Collections.IEnumerator e = jj_expentries.GetEnumerator(); e.MoveNext(); )
1587 int[] oldentry = (int[]) (e.Current);
1588 if (oldentry.Length == jj_expentry.Length)
1590 exists = true;
1591 for (int i = 0; i < jj_expentry.Length; i++)
1593 if (oldentry[i] != jj_expentry[i])
1595 exists = false;
1596 break;
1599 if (exists)
1600 break;
1603 if (!exists)
1604 jj_expentries.Add(jj_expentry);
1605 if (pos != 0)
1606 jj_lasttokens[(jj_endpos = pos) - 1] = kind;
1610 public virtual ParseException GenerateParseException()
1612 jj_expentries.Clear();
1613 bool[] la1tokens = new bool[32];
1614 for (int i = 0; i < 32; i++)
1616 la1tokens[i] = false;
1618 if (jj_kind >= 0)
1620 la1tokens[jj_kind] = true;
1621 jj_kind = - 1;
1623 for (int i = 0; i < 22; i++)
1625 if (jj_la1[i] == jj_gen)
1627 for (int j = 0; j < 32; j++)
1629 if ((jj_la1_0[i] & (1 << j)) != 0)
1631 la1tokens[j] = true;
1636 for (int i = 0; i < 32; i++)
1638 if (la1tokens[i])
1640 jj_expentry = new int[1];
1641 jj_expentry[0] = i;
1642 jj_expentries.Add(jj_expentry);
1645 jj_endpos = 0;
1646 Jj_rescan_token();
1647 Jj_add_error_token(0, 0);
1648 int[][] exptokseq = new int[jj_expentries.Count][];
1649 for (int i = 0; i < jj_expentries.Count; i++)
1651 exptokseq[i] = (int[]) jj_expentries[i];
1653 return new ParseException(token, exptokseq, Lucene.Net.QueryParsers.QueryParserConstants.tokenImage);
1656 public void Enable_tracing()
1660 public void Disable_tracing()
1664 private void Jj_rescan_token()
1666 jj_rescan = true;
1667 for (int i = 0; i < 1; i++)
1669 JJCalls p = jj_2_rtns[i];
1672 if (p.gen > jj_gen)
1674 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
1675 switch (i)
1678 case 0: Jj_3_1(); break;
1681 p = p.next;
1683 while (p != null);
1685 jj_rescan = false;
1688 private void Jj_save(int index, int xla)
1690 JJCalls p = jj_2_rtns[index];
1691 while (p.gen > jj_gen)
1693 if (p.next == null)
1695 p = p.next = new JJCalls(); break;
1697 p = p.next;
1699 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
1702 internal sealed class JJCalls
1704 internal int gen;
1705 internal Token first;
1706 internal int arg;
1707 internal JJCalls next;
1709 static QueryParser()
1712 Jj_la1_0();