我试图在我的查询语言中允许这样的字符串:
-some-hyphenated-term
其中第一个-
表示要排除该术语。问题是-
最终包含在令牌文本中,而不是被解析为排除令牌。我确定我错过了一些简单的东西,但我无法弄清楚。
options {
IGNORE_CASE=true;
LOOKAHEAD=2;
STATIC=false;
}
PARSER_BEGIN(SimpleQueryParser)
public class SimpleQueryParser {
Query query = new Query();
public Query getQuery() {
return query;
}
}
PARSER_END(SimpleQueryParser)
SKIP : { " " | "\t" | "\n" | "\r" }
TOKEN : {
<REQUIRE: "+">
| <FORBID: "-">
| <FIND: "find">
| <LPAREN: "(">
| <RPAREN: ")">
| <STRING : (["A"-"Z", "0"-"9", "_", "*", "^", ".", "-"])+ >
| <QUOTED_STRING: "\"" (~["\""])+ "\"" >
}
/** Top level production. */
void request() : {} {
(findClause())? (queryTerm())*
}
void findClause() : {
Find find = new Find();
Token tCategory, tProperty;
} {
<FIND>
":"
tCategory = category() {
find.setCategory(tCategory.image);
}
(":" tProperty = property() {
find.setProperty(tProperty.image);
}
)?
{
query.setFind(find);
}
}
void expression() : {} {
queryTerm() ( queryTerm() )*
}
void queryTerm() : {
Clause clause = new Clause();
Token tString, tCategory, tProperty;
} {
(<FORBID> { clause.setForbid(true); } | <REQUIRE> { clause.setRequire(true); } )?
(( tCategory = category() { clause.setCategory(tCategory.image); } ":" ( tProperty = property() { clause.setProperty(tProperty.image); } ":" )? ) |
( ":" tProperty = property() { clause.setProperty(tProperty.image); } ":" ))?
(tString = <STRING> { clause.setQuery(tString.image); } | tString = <QUOTED_STRING> {
clause.setQuery(tString.image.substring(1, tString.image.length() - 1)); })
{ query.getClauses().add(clause); }|
<LPAREN> expression() <RPAREN>
}
Token category() : {
Token tCategory;
} {
tCategory = term() { return tCategory; }
}
Token property() : {
Token tProperty;
} {
tProperty = term() { return tProperty; }
}
Token term() : {
Token tTerm;
} {
tTerm = <STRING> { return tTerm; }
}