Paste: Ugly URL parser

Author: slava
Mode: factor
Date: Fri, 26 Sep 2008 23:25:52
Plain Text |
<PRIVATE

EBNF: parse-url

protocol = [a-z]+                   => [[ url-decode ]]
username = [^/:@#?]+                => [[ url-decode ]]
password = [^/:@#?]+                => [[ url-decode ]]
pathname = [^#?]+                   => [[ url-decode ]]
query    = [^#]+                    => [[ query>assoc ]]
anchor   = .+                       => [[ url-decode ]]

hostname = [^/#?]+                  => [[ url-decode ]]

hostname-spec = hostname ("/"|!(.)) => [[ first ]]

auth     = (username (":" password  => [[ second ]])? "@"
                                    => [[ first2 2array ]])?

url      = ((protocol "://")        => [[ first ]] auth hostname)?
           (pathname)?
           ("?" query               => [[ second ]])?
           ("#" anchor              => [[ second ]])?

;EBNF

PRIVATE>

M: string >url
    parse-url {
        [
            first [
                [ first ] ! protocol
                [
                    second
                    [ first [ first2 ] [ f f ] if* ] ! username, password
                    [ second parse-host ] ! host, port
                    bi
                ] bi
            ] [ f f f f f ] if*
        ]
        [ second ] ! pathname
        [ third ] ! query
        [ fourth ] ! anchor
    } cleave url boa
    dup host>> [ [ "/" or ] change-path ] when ;

New Annotation

Summary:
Author:
Mode:
Body: