好吧,这可能很简单,但无论我涂抹多少咖啡,我的大脑都不会激活。
我需要将以下模式与 ANTLR 匹配:
5 YEARS
5 YEARS 2 MONTHS
5 YEARS 2 MONTHS 3 DAYS
2 MONTHS 3 DAYS
5 YEARS 3 DAYS
etc
所以,我从以下规则开始:
atom returns [Object value]
// start w/ a duration. Returned value will be a Joda Period object
: (INTEGER ('YEAR'|'YEARS'))? (INTEGER ('MONTH'|'MONTHS'))? (INTEGER ('DAY'|'DAYS')?
;
显然那是行不通的。我知道这很简单,但我现在有一个主要的大脑 404。
(一旦我得到正确的规则,我会将定义移至 Lexer 定义)
更新: 由于之前提供的输入,以下规则集有效。再次感谢。
datePeriod returns [Object value]
: year month? week? day? EOF
{
$value = new Period($year.num, $month.num, $week.num, $day.num,0,0,0,0);
}
| month week? day? EOF
{
$value = new Period(0, $month.num, $week.num, $day.num,0,0,0,0);
}
| week day? EOF
{
$value = new Period(0,0, $week.num, $day.num,0,0,0,0);
}
| day EOF
{
$value = new Period(0, 0, 0, $day.num,0,0,0,0);
}
;
year returns [int num]
: INTEGER YEAR
{
$num = $INTEGER.int;
}
;
month returns [int num]
: INTEGER MONTH
{
$num = $INTEGER.int;
}
;
week returns [int num]
: INTEGER WEEK
{
$num = $INTEGER.int;
}
;
day returns [int num]
: INTEGER DAY
{
$num = $INTEGER.int;
}
;
YEAR: ('YEAR'|'YEARS');
MONTH: ('MONTH'|'MONTHS');
WEEK: ('WEEK'|'WEEKS');
DAY: ('DAY'|'DAYS');
不幸的是,我是 ANTLR IDEA 现在正在抛出警告,例如:
Decision can match input such as "INTEGER MONTH" using multiple alternatives: 1, 2
杰森