Advertisement
Guest User

Untitled

a guest
May 22nd, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.89 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.     'https://123123',
  9. ;
  10.  
  11. grammar URL {
  12.     token TOP {
  13.         <protocol>'://'<host>
  14.     }
  15.  
  16.     proto token protocol {*}
  17.     token protocol:sym<http> { <sym> }
  18.     token protocol:sym<https> { <sym> }
  19.     token protocol:sym<ftp> { <sym> }
  20.     token protocol:sym<ftps> { <sym> }
  21.  
  22.     proto token host {*}
  23.     token host:sym<ipv4> { \d+ }
  24.  
  25.     token ipv42 {
  26.         [<octet>\.]**1..3 <octet>
  27.     }
  28.     token octet {
  29.         (\d**1..3) <?{ 0 <= $0 <= 255 }>
  30.     }
  31. }
  32.  
  33. for @str -> $s {
  34.     if URL.parse($s) {
  35.         $/<protocol>.say;
  36.         $/<host>.say;
  37.     }
  38.     else {
  39.         "no match!".say;
  40.     }
  41.     say '-------------';
  42. }
  43.  
  44. no match!
  45. -------------
  46. no match!
  47. -------------
  48. no match!
  49. -------------
  50. 「https」
  51.  sym => 「https」
  52.  
  53. 123123
  54.  
  55. -------------
  56. slava@dogbert:~/code/perl6$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement