rust-bakery / nom

Rust parser combinator framework
MIT License
9.38k stars 804 forks source link

feat: Add separated_list_m_n #1722

Open jtojnar opened 8 months ago

jtojnar commented 8 months ago

This is useful to naïvely represent some grammar productions (e.g. IPv6address from RFC 3986):

IPv6address /= [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32

turns to

 tuple((
-    opt(tuple((many_m_n(0, 2, tuple((h16, char(':')))), h16))),
+    separated_list_m_n(0, 3, char(':'), h16),
     tag("::"),
     many_m_n(2, 2, tuple((h16, char(':')))),
     ls32,
 ))
cenodis commented 6 months ago

Now that https://github.com/rust-bakery/nom/pull/1608 is merged I think it would be better to implement the proper range syntax instead of adding yet another variant for a specific format. Geal has already mentioned seperated_list as a candidate in https://github.com/rust-bakery/nom/issues/1613, so this variant would be made redundant soon anyway.