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">
<mkdir dir="${build.demo}"/>
<mkdir dir="${build.demo.src}" />
<copy todir="${build.demo.src}">
<fileset dir="${demo.src}">
@ -211,7 +212,6 @@
javacchome="${javacc.zip.dir}"
outputdirectory="${build.demo.src}/org/apache/lucene/demo/html"
/>
<mkdir dir="${build.demo.classes}"/>
<javac

View File

@ -111,13 +111,17 @@ public class QueryParser {
* @param field the default field for query terms.
* @param analyzer used to find terms in the query text.
* @throws ParseException if the parsing fails
* @throws TokenMgrError if the parsing fails
*/
static public Query parse(String query, String field, Analyzer analyzer)
throws ParseException, TokenMgrError {
throws ParseException {
try {
QueryParser parser = new QueryParser(field, analyzer);
return parser.parse(query);
}
catch (TokenMgrError tme) {
throw new ParseException(tme.getMessage());
}
}
Analyzer analyzer;
String field;
@ -269,9 +273,12 @@ PARSER_END(QueryParser)
<*> TOKEN : {
<#_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" ) >
}

View File

@ -246,4 +246,13 @@ public class TestQueryParser extends TestCase {
assertQueryEquals("( bar blar { a z}) ", null, "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");
}
}