Add escape character to query parser (backslash); add unit test for escape character; wrap TokenMgrError with ParseException in QueryParser.parse

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149736 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brian Goetz 2002-05-06 21:59:44 +00:00
parent fe527de308
commit 2a96b329a8
3 changed files with 24 additions and 8 deletions

View File

@ -198,6 +198,7 @@
<!-- ================================================================== --> <!-- ================================================================== -->
<target name="demo" depends="compile" if="javacc.present"> <target name="demo" depends="compile" if="javacc.present">
<mkdir dir="${build.demo}"/> <mkdir dir="${build.demo}"/>
<mkdir dir="${build.demo.src}" />
<copy todir="${build.demo.src}"> <copy todir="${build.demo.src}">
<fileset dir="${demo.src}"> <fileset dir="${demo.src}">
@ -211,7 +212,6 @@
javacchome="${javacc.zip.dir}" javacchome="${javacc.zip.dir}"
outputdirectory="${build.demo.src}/org/apache/lucene/demo/html" outputdirectory="${build.demo.src}/org/apache/lucene/demo/html"
/> />
<mkdir dir="${build.demo.classes}"/> <mkdir dir="${build.demo.classes}"/>
<javac <javac

View File

@ -111,12 +111,16 @@ public class QueryParser {
* @param field the default field for query terms. * @param field the default field for query terms.
* @param analyzer used to find terms in the query text. * @param analyzer used to find terms in the query text.
* @throws ParseException if the parsing fails * @throws ParseException if the parsing fails
* @throws TokenMgrError if the parsing fails
*/ */
static public Query parse(String query, String field, Analyzer analyzer) static public Query parse(String query, String field, Analyzer analyzer)
throws ParseException, TokenMgrError { throws ParseException {
QueryParser parser = new QueryParser(field, analyzer); try {
return parser.parse(query); QueryParser parser = new QueryParser(field, analyzer);
return parser.parse(query);
}
catch (TokenMgrError tme) {
throw new ParseException(tme.getMessage());
}
} }
Analyzer analyzer; Analyzer analyzer;
@ -269,9 +273,12 @@ PARSER_END(QueryParser)
<*> TOKEN : { <*> TOKEN : {
<#_NUM_CHAR: ["0"-"9"] > <#_NUM_CHAR: ["0"-"9"] >
| <#_TERM_START_CHAR: ~[ " ", "\t", "+", "-", "!", "(", ")", ":", "^", | <#_ESCAPED_CHAR: "\\" [ "\\", "+", "-", "!", "(", ")", ":", "^",
"[", "]", "\"", "{", "}", "~", "*" ] > "[", "]", "\"", "{", "}", "~", "*" ] >
| <#_TERM_CHAR: <_TERM_START_CHAR> > | <#_TERM_START_CHAR: ( ~[ " ", "\t", "+", "-", "!", "(", ")", ":", "^",
"[", "]", "\"", "{", "}", "~", "*" ]
| <_ESCAPED_CHAR> ) >
| <#_TERM_CHAR: ( <_TERM_START_CHAR> | <_ESCAPED_CHAR> ) >
| <#_WHITESPACE: ( " " | "\t" ) > | <#_WHITESPACE: ( " " | "\t" ) >
} }

View File

@ -246,4 +246,13 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}"); assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}");
assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})"); assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})");
} }
public void testEscaped() throws Exception {
Analyzer a = new WhitespaceAnalyzer();
assertQueryEquals("\\[brackets", a, "\\[brackets");
assertQueryEquals("\\[brackets", null, "brackets");
assertQueryEquals("\\\\", a, "\\\\");
assertQueryEquals("\\+blah", a, "\\+blah");
assertQueryEquals("\\(blah", a, "\\(blah");
}
} }