sub process-module(Str:D $_ is copy --> Str:D) {
s:g/^^ "subset" \s+ (<:Lu><:L>*) \s+ '=' \s+ (.+?) \s+ "where" \s+ (.+?) \s* $$/&process-declaration(~$0, ~$1, ~$2)/;
s:g/^^ (. .) "subset" \s+ (<:Lu><:L>*) \s* $$/{$0 ~ process-export(~$1)}/;
$_;
}
sub process-declaration(Str:D $name, Str:D $type, Str:D $condition --> Str:D) {
qq:to/EOF/;
newtype $name = $name ($type)
{ctor-name $name} :: ($type) -> Maybe $name
{ctor-name $name} x
| ($condition) x = Just ($name x)
| otherwise = Nothing
{dtor-name $name} :: $name -> ($type)
{dtor-name $name} ($name x) = x
EOF
}
sub process-export(Str:D $name --> Str:D) {
qq:to/EOF/;
$name
, {ctor-name $name}
, {dtor-name $name}
EOF
}
sub ctor-name(Str:D $name --> Str:D) { $name.substr(0, 1).lc ~ $name.substr(1) }
sub dtor-name(Str:D $name --> Str:D) { "un$name" }
sub MAIN {
print process-module($=finish);
}
=finish
module Data.String.NonEmpty
( subset NonEmptyString
) where
import Data.Maybe (Maybe(..))
import Prelude
subset NonEmptyString = String where (_ /= "")