3 ms·
Even though I still use regexes in rare circumstances - e.g. inside config files, parser combinators already do a much better job than this (or regexes) when yo
by mrkeen 6y ago
Even though I still use regexes in rare circumstances - e.g. inside config files, parser combinators already do a much better job than this (or regexes) when you are writing maintainable code:
warcEntry = do
header <- warcHeader
crlf
body <- do
contentLength <- getContentLength header
compressionMode <- getCompressionMode header
warcbody contentLength compressionMode
crlf
crlf
return (WarcEntry header body)
If you accept crlf as "carriage-return-line-feed", the rest basically reads as pseudocode. crlf could have just as easily been written (string "\r\n") I guess.
Parser combinators can:
* call out to other parsing functions (e.g. warcHeader) - so you can build your code out of testable units.
* bind results to variables and start using them during the parse, e.g. warcHeader returns data containing contentLength and compressionMode, which is then fed to the warcbody function so it knows what to expect.
- one-punch 6y agoParser combinators showed me what an expressive language (in particular Haskell) can do to let you write code as you intend, so that maintainable code is enjoyable to read, write, and reason about. I have heard that some people first “get” Haskell because of parser combinators.
- Tainnor 6y ago> call out to other parsing functions yes, regular expressions cannot do that because regular languages cannot do that. that is an important limitation of regular languages, otherwise you'd be able to match the language {a^nb^n for any n} (or more specifically, balanced parentheses). The thing you wrote you wouldn't want to parse with a regular expression because it fundamentally doesn't look like a regular language with its hierarchical structure.