annotate src/goodjava/queryparser/SaneQueryParser.java @ 1437:3e185b27f982

Http.request.body
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 07 Jan 2020 21:03:57 -0700
parents e48290f3d9fb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1362
diff changeset
1 package goodjava.queryparser;
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.List;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.ArrayList;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.regex.Pattern;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import org.apache.lucene.search.Query;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import org.apache.lucene.search.MatchAllDocsQuery;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import org.apache.lucene.search.BooleanClause;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import org.apache.lucene.search.BooleanQuery;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import org.apache.lucene.search.Sort;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import org.apache.lucene.search.SortField;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1362
diff changeset
12 import goodjava.parser.Parser;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1362
diff changeset
13 import goodjava.parser.ParseException;
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 public class SaneQueryParser {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public static Query parseQuery(FieldParser fieldParser,String query) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return new SaneQueryParser(fieldParser,query).parseQuery();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
1422
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
22 public static String quote(String s) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
23 s = s.replace("\\","\\\\");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
24 s = s.replace("\b","\\b");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
25 s = s.replace("\f","\\f");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
26 s = s.replace("\n","\\n");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
27 s = s.replace("\r","\\r");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
28 s = s.replace("\t","\\t");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
29 s = s.replace("\"","\\\"");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
30 return "\""+s+"\"";
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 public static Sort parseSort(FieldParser fieldParser,String sort) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return new SaneQueryParser(fieldParser,sort).parseSort();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
38 private static final String NOT_IN_RANGE = " \t\r\n\":[]{}^+()";
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
39 private static final String NOT_IN_TERM = NOT_IN_RANGE + "-";
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 private static final String NOT_IN_FIELD = NOT_IN_TERM + ",";
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 private final FieldParser fieldParser;
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
42 private final Parser parser;
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 private SaneQueryParser(FieldParser fieldParser,String query) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 this.fieldParser = fieldParser;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 this.parser = new Parser(query);
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
47 parser.begin();
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
48 }
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
49
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
50 ParseException exception(String msg) {
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
51 parser.failure();
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
52 return new ParseException(parser,msg);
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
53 }
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
54
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
55 ParseException exception(Exception cause) {
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
56 parser.failure();
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
57 return new ParseException(parser,cause);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 private Query parseQuery() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 BooleanQuery bq = new BooleanQuery();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 while( !parser.endOfInput() ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 bq.add( Term(null) );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 BooleanClause[] clauses = bq.getClauses();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 switch( clauses.length ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 case 0:
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 return new MatchAllDocsQuery();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 case 1:
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 BooleanClause bc = clauses[0];
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 return bc.getQuery();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 default:
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 return bq;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 private BooleanClause Term(String defaultField) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 BooleanClause.Occur occur;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 if( parser.match('+') ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 occur = BooleanClause.Occur.MUST;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 } else if( parser.match('-') ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 occur = BooleanClause.Occur.MUST_NOT;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 } else {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 occur = BooleanClause.Occur.SHOULD;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 String field = QueryField();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 if( field == null )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 field = defaultField;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 Query query = NestedTerm(field);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 if( query == null )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 query = RangeTerm(field);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 if( query == null ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 parser.begin();
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
100 String match = SimpleTerm(NOT_IN_TERM);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 query = fieldParser.getQuery(this,field,match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 parser.success();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 if( parser.match('^') ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 int start = parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 try {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 while( parser.anyOf("0123456789.") );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 String match = parser.textFrom(start);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 float boost = Float.parseFloat(match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 query.setBoost(boost);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 } catch(NumberFormatException e) {
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
113 throw exception(e);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 parser.success();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 BooleanClause bc = new BooleanClause(query,occur);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 return bc;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 private Query NestedTerm(String field) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 if( !parser.match('(') )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 return parser.failure(null);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 BooleanQuery bq = new BooleanQuery();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 while( !parser.match(')') ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 if( parser.endOfInput() )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
129 throw exception("unclosed parentheses");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 bq.add( Term(field) );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 BooleanClause[] clauses = bq.getClauses();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 switch( clauses.length ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 case 0:
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
136 throw exception("empty parentheses");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 case 1:
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 BooleanClause bc = clauses[0];
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 if( bc.getOccur() != BooleanClause.Occur.MUST_NOT )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 return parser.success(bc.getQuery());
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 default:
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 return parser.success(bq);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 private Query RangeTerm(String field) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 if( !parser.anyOf("[{") )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 return parser.failure(null);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 boolean includeMin = parser.lastChar() == '[';
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 Spaces();
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
154 String minQuery = SimpleTerm(NOT_IN_RANGE);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 TO();
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
156 String maxQuery = SimpleTerm(NOT_IN_RANGE);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 if( !parser.anyOf("]}") )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
158 throw exception("unclosed range");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 boolean includeMax = parser.lastChar() == ']';
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 Query query = fieldParser.getRangeQuery(this,field,minQuery,maxQuery,includeMin,includeMax);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 return parser.success(query);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 private void TO() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 if( !(parser.match("TO") && Space()) )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
168 throw exception("'TO' expected");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 parser.success();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
173 private String SimpleTerm(String exclude) throws ParseException {
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 parser.begin();
1422
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
175 String match = Quoted();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
176 if( match==null )
1362
77f2d091f17f minor - SaneQueryParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1344
diff changeset
177 match = Unquoted(exclude);
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 if( match.length() == 0 )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
179 throw exception("invalid input");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 return parser.success(match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 private String QueryField() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 String match = Field();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 if( match==null || !parser.match(':') )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 return parser.failure((String)null);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 return parser.success(match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 private String Field() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 String match = Unquoted(NOT_IN_FIELD);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 if( match.length()==0 )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 return parser.failure((String)null);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 match = StringFieldParser.escape(this,match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 return parser.success(match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200
1422
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
201 private String Quoted() throws ParseException {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
202 parser.begin();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
203 if( !parser.match('"') )
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
204 return parser.failure(null);
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
205 StringBuilder sb = new StringBuilder();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
206 while( parser.anyChar() ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
207 char c = parser.lastChar();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
208 switch(c) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
209 case '"':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
210 return parser.success(sb.toString());
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
211 case '\\':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
212 if( parser.anyChar() ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
213 c = parser.lastChar();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
214 switch(c) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
215 case '"':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
216 case '\\':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
217 sb.append(c);
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
218 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
219 case 'b':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
220 sb.append('\b');
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
221 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
222 case 'f':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
223 sb.append('\f');
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
224 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
225 case 'n':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
226 sb.append('\n');
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
227 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
228 case 'r':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
229 sb.append('\r');
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
230 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
231 case 't':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
232 sb.append('\t');
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
233 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
234 case 'u':
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
235 int n = 0;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
236 for( int i=0; i<4; i++ ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
237 int d;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
238 if( parser.inCharRange('0','9') ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
239 d = parser.lastChar() - '0';
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
240 } else if( parser.inCharRange('a','f') ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
241 d = parser.lastChar() - 'a' + 10;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
242 } else if( parser.inCharRange('A','F') ) {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
243 d = parser.lastChar() - 'A' + 10;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
244 } else {
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
245 throw exception("invalid hex digit");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
246 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
247 n = 16*n + d;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
248 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
249 sb.append((char)n);
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
250 continue;
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
251 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
252 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
253 throw exception("invalid escape char");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
254 default:
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
255 sb.append(c);
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
256 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
257 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
258 parser.failure();
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
259 throw exception("unclosed string");
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
260 }
e48290f3d9fb better quoting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
261
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262 private String Unquoted(String exclude) throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 int start = parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 while( parser.noneOf(exclude) ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 checkEscape();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 String match = parser.textFrom(start);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 return parser.success(match);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 private void checkEscape() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 if( parser.lastChar() == '\\' )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274 parser.anyChar();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 private void Spaces() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 while( Space() );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 private boolean Space() {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
282 return parser.anyOf(" \t\r\n");
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
283 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
286 // sort
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
287
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
288 private Sort parseSort() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
290 if( parser.endOfInput() )
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
291 return null;
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
292 List<SortField> list = new ArrayList<SortField>();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
293 list.add( SortField() );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
294 while( !parser.endOfInput() ) {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
295 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
296 if( !parser.match(',') )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
297 throw exception("',' expected");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
298 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
299 parser.success();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300 list.add( SortField() );
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
301 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
302 return new Sort(list.toArray(new SortField[0]));
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
303 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
304
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
305 private SortField SortField() throws ParseException {
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
306 parser.begin();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
307 String field = Field();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
308 if( field==null )
1110
38a42f437fd2 queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
309 throw exception("invalid input");
730
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
310 boolean reverse = !parser.matchIgnoreCase("asc") && parser.matchIgnoreCase("desc");
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311 Spaces();
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
312 SortField sf = fieldParser.getSortField(this,field,reverse);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
313 return parser.success(sf);
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
314 }
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
315
01e68da6983b add sane-lucene-queryparser source to luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
316 }