Advertisement
Guest User

Untitled

a guest
May 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.81 KB | None | 0 0
  1. slava@dogbert:~/code/perl6$ cat grammar.pl6 ; ./grammar.pl6
  2. #!/home/slava/bin/perl6
  3.  
  4. my @str =
  5.     'http://localhost',
  6.     'http://127.0.0.1',
  7.     'https://127.0.0.1',
  8. ;
  9.  
  10. grammar URL {
  11.     rule TOP {
  12.         <protocol>'://'<host>
  13.     }
  14.  
  15.     proto token protocol {*}
  16.     token protocol:sym<http> { <sym> }
  17.     token protocol:sym<https> { <sym> }
  18.     token protocol:sym<ftp> { <sym> }
  19.     token protocol:sym<ftps> { <sym> }
  20.  
  21.     proto token host {*}
  22.     token host:sym<ipv4> { <ipv4> }
  23.  
  24.     token ipv4 {
  25.         [<octet>\.]**1..3 <octet>
  26.     }
  27.     token octet {
  28.         (\d**1..3) <?{ 0 <= $0 <= 255 }>
  29.     }
  30. }
  31.  
  32. for @str -> $s {
  33.     if URL.parse($s) {
  34.         $/<protocol>.say;
  35.         $/<host>.say;
  36.     }
  37.     else {
  38.         "no match!".say;
  39.     }
  40.     say '-------------';
  41. }
  42.  
  43. no match!
  44. -------------
  45. no match!
  46. -------------
  47. no match!
  48. -------------
  49. slava@dogbert:~/code/perl6$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement