stedolan / crowbar

Property fuzzing for OCaml
MIT License
180 stars 31 forks source link

Improving grammar concatenation - string - 'a #34

Closed FooB4r closed 1 year ago

FooB4r commented 6 years ago

String grammar generation could be improved and made easier when concatenating a list of generator

Sentence = Subject Verb Location
...

in order to make my sentence i have to do something like: map [subj; verb; loc] (fun subj verb loc -> subj ^ verb ^ loc) or map [subj; verb; loc] (fun subj verb loc -> String.concat separator [subj; verb; loc]) if i need a separator. The more generators you want to concatenate the more unreadable it gets !

The concatenation isn't well supported, this would be great for generating grammar such as http. I suggest adding functions for strings

(* concatenate a List of string gen inserting the separator string sep between each *)
let concat_gen_list sep l =
  List.fold_left (fun acc e ->
    map [acc; sep; e] (fun acc sep e -> acc ^ e ^ sep)
  ) empty l

(* Concatenate a Crowbar.list equivalent String.concat on Crowbar.list gen *)
let concat_list_gen sep l =
  map [sep; l] String.concat

We could even improve the concept by adding the operator as an argument

(* concatenate a List of 'a gen inserting the separator sep between each *)
let concat_gen_list sep l op =
  List.fold_left (fun acc e ->
    map [acc; sep; e] (fun acc sep e -> op (op acc e) sep )
  ) empty l

This is just an idea, PR to come if you approve !

stedolan commented 6 years ago

This looks good! Mostly I've been using map with generators of different types, I hadn't realised how awkward it was when they're all the same type.

stedolan commented 1 year ago

(I think this was done in #42)