I'm working on a parser combinator library, and I'd really like my parser to simply be some callable object:
typedef std::function<parse_result(parse_stream)> parser;
Which makes the parser combinators nice, eg:
parser operator &(parser a, parser b) { return both(a,b); }
but I'd like two features:
1) I'd like string literals to get promoted to a parser automatically so you can do things like:
parser option = "<" & regexp("[^+>]+");
2) I'd like a parser to have a name that I can use for error formatting. In the case of the "both" parser above, I could print that I expected a.name() and b.name() for example.
The two options I've tried so far are
-
a parser class that's callable, this lets me build from strings and std::function instances but a general callable has to be converted to a std::function first and from there to a parser, and C++ won't do two implicit conversions
-
Inheriting from std::function so I can implicitly convert the functions, but this seems to have a lot of gotchas in terms of only converting callables into a parser.
Does anyone have any thoughts on how to structure this?
Aucun commentaire:
Enregistrer un commentaire