Parsers don't have to be complicated(bkaradzic.github.io) |
Parsers don't have to be complicated(bkaradzic.github.io) |
Famous examples: despite so many initial good intentions, html tags don’t need to be closed, JSON numbers are too often encoded as strings, YAML can look like what most people expect or it can look progressively more like JSON… and on and on.
If what you're parsing is within the capacity of humans to interact with (so in the range of tens of kilobytes), a grammar that requires an O(N^2) parser is totally fine.
A simple recursive-descent parser is easy to write by hand and runs in linear time.
Looking at the linked URL parser, why doesn't it look like
url = do scheme
authority
path
query
fragment
where
scheme = ...
authority = ...
etc.
It looks totally ad-hoc.One common way to test it is just to pass ipv6 url: http://[f021:d981:b487:e57d:193e:550e::]/
RFC 3986 Appendix B [1] "Parsing a URI Reference with a Regular Expression":
The following line is the regular expression for breaking-down a well-formed URI reference into its components.
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
scheme = $2
authority = $4
path = $5
query = $7
fragment = $9
Let's test your URI with this regex, shall we? [2]
$2 (scheme) = http
$4 (authority) = [f021:d981:b487:e57d:193e:550e::]
$5 (path) = /
Seems correct to me.[1] https://datatracker.ietf.org/doc/html/rfc3986#appendix-B
Is there a common source of extra \r in malformed inputs, beyond those existing as part of \r\n? Or is this just a dig at Windows-style line endings? If there's something weird going on I think I'd rather fail loudly.
> Bounding the inner scanner to a single line makes “run past the end of a malformed line” unrepresentable rather than merely unlikely.
I don't really see what makes it "unrepresentable", and this reads more like "if you used the right scanning logic, you can't have used the wrong scanning logic".
handles all EOL sequences without backtracking. Or write a non-regex equivalent of that.
The Rust compiler is a common example of a compiler that does a good job here, and I think it is one of only a few.
Is this really ergonomic?
No codegen, just function calling.