3 ms·
I think you'd be hard pressed to find someone who doesn't know parser combinators tell you the snippet above is readable. First of all, what language is this?
by incepted 11y ago
I think you'd be hard pressed to find someone who doesn't know parser combinators tell you the snippet above is readable.
First of all, what language is this? Well, I know, but you seem to forget that the parser combinator syntax varies per language. What's the equivalent syntax for Java? Or for Python? What about a language that doesn't have a parser combinator library? Or one that has several ones, all slightly different?
I think you're falling prey to the specialist fallacy: you're obviously very comfortable with parser combinators but you've forgotten how long it took you to get there and you now see them as an ultimate solution to all problems without realizing their downsides.
- lmm 11y ago> First of all, what language is this? Well, I know, but you seem to forget that the parser combinator syntax varies per language. What's the equivalent syntax for Java? Or for Python? What about a language that doesn't have a parser combinator library? Or one that has several ones, all slightly different? I very deliberately didn't mention the language or the library, because I think the snippet is readable without knowing that. Minor syntax differences between libraries matter when writing, but not when reading, and reading is more important. (And it's not like there aren't several slightly different implementations of regexes) I'm not that committed to parser combinators - I'd be happy to consider alternatives - but anything where you a) name the things you're capturing b) can easily combine several small parsers to make a bigger parser will have a huge readability, testability and maintainability advantage over regexes.
- omaranto 11y ago> anything where you a) name the things you're capturing b) can easily combine several small parsers to make a bigger parser will have a huge readability, testability and maintainability advantage over regexes I think many regex libraries have named captures and in most languages you can either concatenate regexes or build regexes from strings which in turn can be built from concatenation. Furthermore, I thought building regexes by concatenation of smaller pieces was a commonly recommended technique for improving readability (I have no data on whether the recommendation is commonly followed or not, of course).
- lmm 11y agoI've never seen either of them in real-world code. Concatenation is something but I think it's a lot less flexible - if you have a parser for something and want to make a parser for a comma-separated list of that thing, with parser combinators that's one call, whereas with a regex I don't think the string-manipulation is straightforward, and would the named capture groups still work if they were now being hit multiple times?
- omaranto 11y agoYou're definitely right that concatenation is less composable, and I agree that parser combinators are more powerful. I was just pointing out there are somethings you can do in regexes to make things more readable. And about captures inside repetition: I don't see any reason they couldn't capture a list of strings instead of a string in dynamically typed languages but in all regex libraries I'm aware of they do something that seems useless to me: they only capture the last occurrence! (They probably just overwrite the capture on each repetition.)