-
Hello! A quick question about the whitespace argument in parsita: Say I have the following code snippet: class PrimitiveParsers(TextParsers, whitespace=None):
int = reg(r'[-+]?[0-9]+') > int
class PairParsers(TextParsers, whitespace=r'[ ]*'):
a = PrimitiveParsers.int
b = PrimitiveParsers.int
pair = a & b
if __name__ == "__main__":
result = PairParsers.pair.parse('23 84')
if isinstance(result, Success):
print(result.value)
elif isinstance(result, Failure):
print(result.message) If I run this, I get the following output:
If I change class PrimitiveParsers(TextParsers):
int = reg(r'[-+]?[0-9]+') > int
class PairParsers(TextParsers, whitespace=r'[ ]*'):
a = PrimitiveParsers.int
b = PrimitiveParsers.int
pair = a & b
if __name__ == "__main__":
result = PairParsers.pair.parse('23 84')
if isinstance(result, Success):
print(result.value)
elif isinstance(result, Failure):
print(result.message)
Is it expected that changing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The fundamental issue is that I would have to see more of your problem before deciding the best solution to your problem. (Thanks for the minimized problem.) Without seeing your real problem, I would suspect that the things in You probably already know that one workaround is to throw empty strings in. Depending on the number of locations where this is an issue in your parser, this may be satisfactory: from parsita import *
class PrimitiveParsers(TextParsers, whitespace=None):
int = reg(r'[-+]?[0-9]+') > int
class PairParsers(TextParsers, whitespace=r'[ ]*'):
a = PrimitiveParsers.int
b = PrimitiveParsers.int
pair = a << "" & b
if __name__ == "__main__":
result = PairParsers.pair.parse('23 84')
if isinstance(result, Success):
print(result.value)
elif isinstance(result, Failure):
print(result.message) |
Beta Was this translation helpful? Give feedback.
The fundamental issue is that
whitespace
affects onlylit
andreg
within its context. The whitespace regex is not invoked by other operators. I'd like to say this is for performance reasons, but truthfully, I had simply not considered this edge case where parsers from one context were used in another context with no delimiters. I'll have to think about this a bit longer before I decide if the current behavior is philosophically incorrect. But regardless, I agree that it is unexpected.I would have to see more of your problem before deciding the best solution to your problem. (Thanks for the minimized problem.) Without seeing your real problem, I would suspect that the things in
PrimitiveP…