changeset 1110:38a42f437fd2

queryparser now uses parsers.Parser
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 02 Aug 2017 13:45:06 -0600
parents 8c999ab85e33
children 88b5b81cad4a
files src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/queryparser/FieldParser.java src/luan/modules/lucene/queryparser/MultiFieldParser.java src/luan/modules/lucene/queryparser/NumberFieldParser.java src/luan/modules/lucene/queryparser/ParseException.java src/luan/modules/lucene/queryparser/Parser.java src/luan/modules/lucene/queryparser/SaneQueryParser.java src/luan/modules/lucene/queryparser/StringFieldParser.java src/luan/modules/lucene/queryparser/SynonymParser.java src/luan/modules/parsers/ParseException.java src/luan/modules/parsers/Parser.java
diffstat 11 files changed, 50 insertions(+), 209 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Wed Aug 02 13:45:06 2017 -0600
@@ -64,7 +64,7 @@
 import luan.modules.lucene.queryparser.MultiFieldParser;
 import luan.modules.lucene.queryparser.StringFieldParser;
 import luan.modules.lucene.queryparser.NumberFieldParser;
-import luan.modules.lucene.queryparser.ParseException;
+import luan.modules.parsers.ParseException;
 import luan.modules.Utils;
 import luan.Luan;
 import luan.LuanState;
--- a/src/luan/modules/lucene/queryparser/FieldParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/FieldParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -2,6 +2,7 @@
 
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.SortField;
+import luan.modules.parsers.ParseException;
 
 
 public interface FieldParser {
--- a/src/luan/modules/lucene/queryparser/MultiFieldParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/MultiFieldParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -6,6 +6,7 @@
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.SortField;
+import luan.modules.parsers.ParseException;
 
 
 public class MultiFieldParser implements FieldParser {
@@ -34,7 +35,7 @@
 	@Override public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
 		if( field == null ) {
 			if( defaultFieldParser == null )
-				throw new ParseException(qp,"no defaults were specified, so a field is required");
+				throw qp.exception("no defaults were specified, so a field is required");
 			if( defaultFields.length == 1 )
 				return defaultFieldParser.getQuery(qp,defaultFields[0],query);
 			BooleanQuery bq = new BooleanQuery();
@@ -48,14 +49,14 @@
 				return fp.getQuery(qp,field,query);
 			if( allowUnspecifiedFields )
 				return defaultFieldParser.getQuery(qp,field,query);
-			throw new ParseException(qp,"unrecognized field '"+field+"'");
+			throw qp.exception("unrecognized field '"+field+"'");
 		}
 	}
 
 	@Override public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
 		if( field == null ) {
 			if( defaultFieldParser == null )
-				throw new ParseException(qp,"no defaults were specified, so a field is required");
+				throw qp.exception("no defaults were specified, so a field is required");
 			if( defaultFields.length == 1 )
 				return defaultFieldParser.getRangeQuery(qp,defaultFields[0],minQuery,maxQuery,includeMin,includeMax);
 			BooleanQuery bq = new BooleanQuery();
@@ -69,7 +70,7 @@
 				return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax);
 			if( allowUnspecifiedFields )
 				return defaultFieldParser.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax);
-			throw new ParseException(qp,"field '"+field+"' not specified");
+			throw qp.exception("field '"+field+"' not specified");
 		}
 	}
 
@@ -79,7 +80,7 @@
 			return fp.getSortField(qp,field,reverse);
 		if( allowUnspecifiedFields )
 			return defaultFieldParser.getSortField(qp,field,reverse);
-		throw new ParseException(qp,"field '"+field+"' not specified");
+		throw qp.exception("field '"+field+"' not specified");
 	}
 
 }
--- a/src/luan/modules/lucene/queryparser/NumberFieldParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/NumberFieldParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -3,6 +3,7 @@
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.SortField;
+import luan.modules.parsers.ParseException;
 
 
 public abstract class NumberFieldParser implements FieldParser {
@@ -15,7 +16,7 @@
 		try {
 			return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax);
 		} catch(NumberFormatException e) {
-			throw new ParseException(qp,e);
+			throw qp.exception(e);
 		}
 	}
 
--- a/src/luan/modules/lucene/queryparser/ParseException.java	Wed Aug 02 12:36:28 2017 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-package luan.modules.lucene.queryparser;
-
-
-public final class ParseException extends Exception {
-	public final String text;
-	public final int errorIndex;
-	public final int highIndex;
-
-	public ParseException(SaneQueryParser qp) {
-		this(qp,"Invalid input");
-	}
-
-	public ParseException(SaneQueryParser qp,String msg) {
-		super(msg+" at position "+(qp.parser.errorIndex()+1));
-		Parser parser = qp.parser;
-		this.text = parser.text;
-		this.errorIndex = parser.errorIndex();
-		this.highIndex = parser.highIndex();
-	}
-
-	public ParseException(SaneQueryParser qp,Exception cause) {
-		this(qp,cause.getMessage(),cause);
-	}
-
-	public ParseException(SaneQueryParser qp,String msg,Exception cause) {
-		super(msg+" at position "+(qp.parser.errorIndex()+1),cause);
-		Parser parser = qp.parser;
-		this.text = parser.text;
-		this.errorIndex = parser.errorIndex();
-		this.highIndex = parser.highIndex();
-	}
-
-}
--- a/src/luan/modules/lucene/queryparser/Parser.java	Wed Aug 02 12:36:28 2017 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,155 +0,0 @@
-package luan.modules.lucene.queryparser;
-
-/**
- * A general parser utility class.
- * This class is not needed to use SaneQueryParser.
- */
-public class Parser {
-	public final String text;
-	private final int len;
-	private int[] stack = new int[256];
-	private int frame = 0;
-	private int iHigh;
-
-	Parser(String text) {
-		this.text = text;
-		this.len = text.length();
-	}
-
-	private int i() {
-		return stack[frame];
-	}
-
-	private void i(int i) {
-		stack[frame] += i;
-		if( iHigh < stack[frame] )
-			iHigh = stack[frame];
-	}
-
-	public int begin() {
-		frame++;
-		if( frame == stack.length ) {
-			int[] a = new int[2*frame];
-			System.arraycopy(stack,0,a,0,frame);
-			stack = a;
-		}
-		stack[frame] = stack[frame-1];
-		return i();
-	}
-
-	public void rollback() {
-		stack[frame] = stack[frame-1];
-	}
-
-	public <T> T success(T t) {
-		success();
-		return t;
-	}
-
-	public boolean success() {
-		frame--;
-		stack[frame] = stack[frame+1];
-		return true;
-	}
-
-	public <T> T failure(T t) {
-		failure();
-		return t;
-	}
-
-	public boolean failure() {
-		frame--;
-		return false;
-	}
-
-	public int currentIndex() {
-		return i();
-	}
-
-	public int errorIndex() {
-		return frame > 0 ? stack[frame-1] : 0;
-	}
-
-	public int highIndex() {
-		return iHigh;
-	}
-
-	public char lastChar() {
-		return text.charAt(i()-1);
-	}
-
-	public char currentChar() {
-		return text.charAt(i());
-	}
-
-	public boolean endOfInput() {
-		return i() >= len;
-	}
-
-	public boolean match(char c) {
-		if( endOfInput() || text.charAt(i()) != c )
-			return false;
-		i(1);
-		return true;
-	}
-
-	public boolean match(String s) {
-		int n = s.length();
-		if( !text.regionMatches(i(),s,0,n) )
-			return false;
-		i(n);
-		return true;
-	}
-
-	public boolean matchIgnoreCase(String s) {
-		int n = s.length();
-		if( !text.regionMatches(true,i(),s,0,n) )
-			return false;
-		i(n);
-		return true;
-	}
-
-	public boolean anyOf(String s) {
-		if( endOfInput() || s.indexOf(text.charAt(i())) == -1 )
-			return false;
-		i(1);
-		return true;
-	}
-
-	public boolean noneOf(String s) {
-		if( endOfInput() || s.indexOf(text.charAt(i())) != -1 )
-			return false;
-		i(1);
-		return true;
-	}
-
-	public boolean inCharRange(char cLow, char cHigh) {
-		if( endOfInput() )
-			return false;
-		char c = text.charAt(i());
-		if( !(cLow <= c && c <= cHigh) )
-			return false;
-		i(1);
-		return true;
-	}
-
-	public boolean anyChar() {
-		if( endOfInput() )
-			return false;
-		i(1);
-		return true;
-	}
-
-	public boolean test(char c) {
-		return !endOfInput() && text.charAt(i()) == c;
-	}
-
-	public boolean test(String s) {
-		return text.regionMatches(i(),s,0,s.length());
-	}
-
-	public String textFrom(int start) {
-		return text.substring(start,i());
-	}
-
-}
--- 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);
--- a/src/luan/modules/lucene/queryparser/StringFieldParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/StringFieldParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -14,6 +14,7 @@
 import org.apache.lucene.search.PrefixQuery;
 import org.apache.lucene.search.SortField;
 import org.apache.lucene.index.Term;
+import luan.modules.parsers.ParseException;
 
 
 public class StringFieldParser implements FieldParser {
@@ -75,7 +76,7 @@
 			char c = a[i];
 			if( c == '\\' ) {
 				if( ++i == a.length )
-					throw new ParseException(qp,"ends with '\\'");
+					throw qp.exception("ends with '\\'");
 				c = a[i];
 			}
 			sb.append(c);
@@ -95,7 +96,7 @@
 				hasWildcard = true;
 			if( c == '\\' ) {
 				if( ++i == a.length )
-					throw new ParseException(qp,"ends with '\\'");
+					throw qp.exception("ends with '\\'");
 				c = a[i];
 				if( c=='?' || c=='*' )
 					sb.append('\\');
--- a/src/luan/modules/lucene/queryparser/SynonymParser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/lucene/queryparser/SynonymParser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -5,6 +5,7 @@
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.SortField;
+import luan.modules.parsers.ParseException;
 
 
 public class SynonymParser implements FieldParser {
--- a/src/luan/modules/parsers/ParseException.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/parsers/ParseException.java	Wed Aug 02 13:45:06 2017 -0600
@@ -6,13 +6,24 @@
 	public final int errorIndex;
 	public final int highIndex;
 
-	ParseException(Parser parser,String msg) {
+	public ParseException(Parser parser,String msg) {
 		super(msg);
 		this.text = parser.text;
 		this.errorIndex = parser.currentIndex();
 		this.highIndex = parser.highIndex();
 	}
 
+	public ParseException(Parser parser,Exception cause) {
+		this(parser,cause.getMessage(),cause);
+	}
+
+	public ParseException(Parser parser,String msg,Exception cause) {
+		super(msg,cause);
+		this.text = parser.text;
+		this.errorIndex = parser.currentIndex();
+		this.highIndex = parser.highIndex();
+	}
+
 	private class Location {
 		final int line;
 		final int pos;
--- a/src/luan/modules/parsers/Parser.java	Wed Aug 02 12:36:28 2017 -0600
+++ b/src/luan/modules/parsers/Parser.java	Wed Aug 02 13:45:06 2017 -0600
@@ -8,7 +8,7 @@
 	private int frame = 0;
 	private int iHigh;
 
-	Parser(String text) {
+	public Parser(String text) {
 		this.text = text;
 		this.len = text.length();
 	}