Parser for very relaxed time literals. Demonstrates parse tree node selection via labels.
*/
public class VCalRRuleParser extends BaseParser {
public String ruleType = null;
public String duration = null;
public String interval = null;
public String endDate = null;
public Rule Head() {
resetRuleState();
return Sequence( this.FirstOf( Daily(),
Weekly() ),
EndDate(),
Eoi()
);
}
// WTF: If this is private then parboiled causes an IllegalStateException!?!?!?!?
void resetRuleState() {
ruleType
= duration
= interval
= endDate
= null;
}
private Action actionInterval = new Action() {
public boolean run(Context context) {
interval =
//""+nodeByLabel("interval").getValue()
prevText()
;
return true;
}};
private Action actionDuration = new Action() {
public boolean run(Context context) {
duration =
//""+nodeByLabel("duration").getValue()
prevText()
;
return true;
}};
private Action actionEndDate = new Action() {
public boolean run(Context context) {
endDate =
//""+nodeByLabel("endDate").getValue()
prevText()
;
return true;
}};
private class ActionRuleType implements Action {
private String t = null;
ActionRuleType(String t) {
this.t = t;
}
public boolean run(Context context) {
ruleType = this.t;
return true;
}
};
private static final Rule WS = new CharSetMatcher( Characters.of(' ','\t') );
public Rule Daily() {
return Sequence( 'D', Interval(),
Optional( Sequence( WS, Duration() ) ),
new ActionRuleType("D")
);
}
public Rule Weekly() {
return Sequence( 'W', Interval(),
// TODO: Weekday List
Optional( Duration() ),
new ActionRuleType("W")
);
}
public Rule MonthlyP() {
return Sequence( 'M', 'P', Interval(),
// TODO: Month List
Optional( Duration() ),
new ActionRuleType("MP")
);
}
public Rule MonthlyD() {
return Sequence( 'M', 'D', Interval(),
// TODO: Day List
Optional( Duration() ),
new ActionRuleType("MD")
);
}
public Rule YearlyP() {
return Sequence( 'Y', 'P', Interval(),
// TODO: Month List
Optional( Duration() ),
new ActionRuleType("MP")
);
}
public Rule YearlyD() {
return Sequence( 'Y', 'D', Interval(),
// TODO: Day List
Optional( Duration() ),
new ActionRuleType("MD")
);
}
public Rule UtcDateTime() {
return Sequence( Digit(), Digit(), Digit(), Digit(), // YYYY
Digit(), Digit(), // mm
Digit(), Digit(), // dd
'T',
Digit(), Digit(), // HH
Digit(), Digit(), // MM
Digit(), Digit(), // SS
'Z' );
}
public Rule EndDate() {
return Optional( Sequence( UtcDateTime(), actionEndDate ) );
}
public Rule Interval() {
return Sequence( IntegerVal(), actionInterval );
}
public Rule Duration() {
return Sequence( '#', IntegerVal(), actionDuration );
}
public Rule Digit() {
return CharRange('0', '9');
}
public Rule IntegerVal() {
return OneOrMore(Digit());
}
}