Skip to content

Instantly share code, notes, and snippets.

@hjkatz
Created March 10, 2015 16:27
Show Gist options
  • Save hjkatz/e3b8f79dbcf23e16cd81 to your computer and use it in GitHub Desktop.
Save hjkatz/e3b8f79dbcf23e16cd81 to your computer and use it in GitHub Desktop.
grammar URL
{
token TOP
{
<schema> '://'
[<ip> | <hostname> ]
[ ':' <port>]?
'/' <path>?
}
token byte
{
(\d**1..3) <?{ $0 < 256 }>
}
token ip
{
<byte> [\. <byte> ] ** 3
}
token schema
{
\w+
}
token hostname
{
(\w+) ( \. \w+ )*
}
token port
{
\d+
}
token path
{
<[ a..z A..Z 0..9 \-_.!~*'():@&=+$,/ ]>+
}
}
subset NonEmptyString of Str where *.chars >= 0;
subset UrlToken of NonEmptyString where { $_ ~~ /^TOP$/ | /^byte$/ | /^ip$/ | /^schema$/ | /^hostname$/ | /^port$/ | /^path$/ };
sub MAIN( NonEmptyString $string, UrlToken $token )
{
my $match = URL.parse($string);
say $match{$token};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment