2012-12-06 57 views
0

我試圖讓字符串像這樣在我的查詢語言:JavaCC的令牌前綴

-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; } 
} 

回答

1

您需要更改規則<STRING>這樣,如果它不是一個字符串的第一個字符"-"只包括在內。類似於

<STRING : ["A"-"Z", "0"-"9", "_", "*", "^", "."](["A"-"Z", "0"-"9", "_", "*", "^", ".", "-"])* >