J-F-Liu / pom

PEG parser combinators using operator overloading without macros.
MIT License
496 stars 30 forks source link

JSON Parser explanation #67

Closed homersimpsons closed 6 months ago

homersimpsons commented 6 months ago

(I do not know where to ask for this question)

In the README there is an example for a JSON parser. I do not really uderstand how this line works:

let integer = one_of(b"123456789") - one_of(b"0123456789").repeat(0..) | sym(b'0');

Especially about the - in one_of(b"123456789") - one_of(b"0123456789").repeat(0..).

Reading the documentation above I see:

p - q | Match p and q, if both succeed return result of p.

So here I would expect the integer to be only the first char, for instance given 1234, integer would be only 1.

Technically I would have expected a + here, because the documentation says:

p + q | Match p and q, if both succeed return a pair of results.

But using a + gives a compilation error (even with parentheses).

How does that work with a - here ?

J-F-Liu commented 6 months ago

The final string is created by number.collect(), so results of internal parts of number are irrelevant.

homersimpsons commented 6 months ago

Oh okay, so the goal of all the parsers here is just to match a number-like sequence. Then the whole matched byte sequence is converted.

Thank you for your explanation! I could not wrap my head around this...