Are there some ‘date parser’ library that does for dates what FParsec does to strings ?
That is, you either specify rules and it will match against them to recognize the supplied patterns.
Conversely, are there any libraries to generate dates based on some parsing rules ?
The idea would be to supply user with a ‘real time’ completion to guide him to a valid future fparsec matching.
(does this problem of generative parsing has a name in the secluded parsing circles ?)
You can define a simple domain specific language (DSL) to express these kinds of rules. The type corresponding to your “parser” is actually just a function that takes a date and returns boolean:
You can easily define some simple functions:
To specify your condition – “the next wednesday after the 5th of the month” – you’ll need a helper that generates function that succeeds on any day following 5th, which can be done like this (this is a bit inefficient, but it is composition using existing primitives, which is nice):
Your specification (or “parser”) that only succeeds for the day you described is then: