let-def / menhir

Unofficial repository. Experimentations around menhir parser generator
Other
6 stars 0 forks source link

Type parameters for tokens and rules #2

Open lpw25 opened 10 years ago

lpw25 commented 10 years ago

Since you're experimenting with Menhir, and would probably know how feasible this feature is, I thought I would suggest it here.

I would like to parametrise my parsers by type parameters. Essentially, I would like the resulting parse functions to look like:

(Lexing.lexbuf -> 'a token) -> Lexing.lexbuf -> 'a foo

I can get something like this by using:

%parameter <A : sig type t end>

and using a local module to apply the parser, but it is quite ugly.

I think something like the following would work as a definition:

%type_parameter < 'param >

%token < 'param list > PList
%token < 'param * 'param > PPair

%type < 'param option > an_option_rule

%start < 'param list > start_parser

The implementation would be to parametrise the token type by the type parameter, using the name 'param wherever the token type appears in the rule definitions. As long as the definitions of the rule code was careful not to capture the type parameter name I think all the definitions would be appropriately typed.

lpw25 commented 10 years ago

Actually, thinking about it I only need the token and output to be parametrised, not the buffer. I've adjusted the description above accordingly.

let-def commented 10 years ago

In an ideal world, yes it would be possible to handle parsers with type parameters.

The backends are undergoing major changes as parts of this fork are being considered for upstreaming. At the moment we are trying to make things simpler. Introducing parametric polymorphism is likely not going to help.

So while possible, it is not going to happen anytime soon. Is it really a problem to go through a functor application?

lpw25 commented 10 years ago

So while possible, it is not going to happen anytime soon. Is it really a problem to go through a functor application?

Not really, it just annoyed me a bit and I could see how a solution would work so I thought I'd write it down somewhere where it might eventually get acted upon.