UPARSE In Mezzanine: Use Experimentally (but sparingly)

I've made UPARSE part of the mezzanine to make it easier to use in the web build.

It's a reminder that zero performance work has been done on it, and it is glacially slow.

But here it is extracting information from a GitLab URL

uparse port.spec.path [
    "/"
    emit user: between <here> "/"
    emit repo: between <here> "/"
    [opt "-/"]
    "raw/"
    emit branch: between <here> "/"
    emit file_path: between <here> <end>
] then x -> [
    replace/all x.file_path "/" "%2F"  ; API uses slashes for its delimiting

    if port.spec.scheme = 'http [
        port.spec.scheme: 'https
        write log:type=warn ["Converting non-HTTPS URL to HTTPS:" x.url]
    ]

    port.spec.path: join-all [
        "/api/v4/projects/"
        x.user "%2F" x.repo  ; surrogate for numeric id, use escaped `/`
        "/repository/files/" x.file_path "/raw?ref=" x.branch
    ]
]

Anyway, this is just an example of some fun usage of BETWEEN and a generated object. It avoids the to "/" skip or to "/" "/" pattern that's so annoying. And it means you can avoid naming locals for all the things you are extracting in a parse.

I'm thinking that use x would be changed to a construct that would pull all x's fields into scope. Right now there's an experiment for that called "using" but I like USE.

I'll do some performance work at some point, here. But I'm packing to relocate today, so I might be busy for a little bit...

Sample raw GitLab links:

https://gitlab.com/Zhaoshirong/nzpower/-/raw/master/nzpower.reb
https://gitlab.com/Zhaoshirong/nzpower/raw/master/nzpower.reb

The port code splits that into a scheme, domain, and path. This code breaks that path into an object:

>> path: "/Zhaoshirong/nzpower/-/raw/master/nzpower.reb"

>> uparse path [
       "/"
       emit user: between <here> "/"
       emit repo: between <here> "/"
       [opt "-/"]
       "raw/"
       emit branch: between <here> "/"
       emit file_path: between <here> <end>
   ]
== make object! [
    user: "Zhaoshirong"
    repo: "nzpower"
    branch: "master"
    file_path: "nzpower.reb"
]
1 Like