diff src/luan/modules/lucene/queryparser/SaneQueryParser.java @ 1110:38a42f437fd2

queryparser now uses parsers.Parser
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 02 Aug 2017 13:45:06 -0600
parents 1a68fc55a80c
children 88b5b81cad4a
line wrap: on
line diff
--- a/src/luan/modules/lucene/queryparser/SaneQueryParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/SaneQueryParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -9,6 +9,8 @@
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.SortField;
+import luan.modules.parsers.Parser;
+import luan.modules.parsers.ParseException;
 
 
 public class SaneQueryParser {
@@ -31,11 +33,22 @@
 	private static final String NOT_IN_TERM = " \t\r\n\":[]{}^+-()";
 	private static final String NOT_IN_FIELD = NOT_IN_TERM + ",";
 	private final FieldParser fieldParser;
-	final Parser parser;
+	private final Parser parser;
 
 	private SaneQueryParser(FieldParser fieldParser,String query) {
 		this.fieldParser = fieldParser;
 		this.parser = new Parser(query);
+		parser.begin();
+	}
+
+	ParseException exception(String msg) {
+		parser.failure();
+		return new ParseException(parser,msg);
+	}
+
+	ParseException exception(Exception cause) {
+		parser.failure();
+		return new ParseException(parser,cause);
 	}
 
 	private Query parseQuery() throws ParseException {
@@ -91,7 +104,7 @@
 				float boost = Float.parseFloat(match);
 				query.setBoost(boost);
 			} catch(NumberFormatException e) {
-				throw new ParseException(this,e);
+				throw exception(e);
 			}
 			parser.success();
 			Spaces();
@@ -107,14 +120,14 @@
 		BooleanQuery bq = new BooleanQuery();
 		while( !parser.match(')') ) {
 			if( parser.endOfInput() )
-				throw new ParseException(this,"unclosed parentheses");
+				throw exception("unclosed parentheses");
 			bq.add( Term(field) );
 		}
 		Spaces();
 		BooleanClause[] clauses = bq.getClauses();
 		switch( clauses.length ) {
 		case 0:
-			throw new ParseException(this,"empty parentheses");
+			throw exception("empty parentheses");
 		case 1:
 			{
 				BooleanClause bc = clauses[0];
@@ -136,7 +149,7 @@
 		TO();
 		String maxQuery = SimpleTerm();
 		if( !parser.anyOf("]}") )
-			throw new ParseException(this,"unclosed range");
+			throw exception("unclosed range");
 		boolean includeMax = parser.lastChar() == ']';
 		Spaces();
 		Query query = fieldParser.getRangeQuery(this,field,minQuery,maxQuery,includeMin,includeMax);
@@ -146,7 +159,7 @@
 	private void TO() throws ParseException {
 		parser.begin();
 		if( !(parser.match("TO") && Space()) )
-			throw new ParseException(this,"'TO' expected");
+			throw exception("'TO' expected");
 		Spaces();
 		parser.success();
 	}
@@ -158,7 +171,7 @@
 			int start = parser.currentIndex() - 1;
 			while( !parser.match('"') ) {
 				if( parser.endOfInput() )
-					throw new ParseException(this,"unclosed quotes");
+					throw exception("unclosed quotes");
 				parser.anyChar();
 				checkEscape();
 			}
@@ -168,7 +181,7 @@
 			match = Unquoted(NOT_IN_TERM);
 		}
 		if( match.length() == 0 )
-			throw new ParseException(this);
+			throw exception("invalid input");
 		return parser.success(match);
 	}
 
@@ -225,7 +238,7 @@
 		while( !parser.endOfInput() ) {
 			parser.begin();
 			if( !parser.match(',') )
-				throw new ParseException(this,"',' expected");
+				throw exception("',' expected");
 			Spaces();
 			parser.success();
 			list.add( SortField() );
@@ -237,7 +250,7 @@
 		parser.begin();
 		String field = Field();
 		if( field==null )
-			throw new ParseException(this);
+			throw exception("invalid input");
 		boolean reverse = !parser.matchIgnoreCase("asc") && parser.matchIgnoreCase("desc");
 		Spaces();
 		SortField sf = fieldParser.getSortField(this,field,reverse);