Case Insensitive Literals #40
Answered
by
drhagen
dennis-barrett
asked this question in
Q&A
-
Hello, I'm really enjoying using Parsita, thanks for creating it 🙂 Is there an easy way to ignore case in literals. For example: from parsita import *
class CaseInsensitiveParsers(TextParsers):
# Would be nice to specify that this is case-insensitive:
for_parser = lit('for')
# This is the only way I can think of doing it right now (or with a regex):
for_parser_ci = lit('for', 'For', 'FOr', 'FOR', 'fOr', 'fOR', 'foR', 'FoR')
CaseInsensitiveParsers.for_parser_ci.parse('FOR').or_die() # works
CaseInsensitiveParsers.for_parser.parse('FOR').or_die() # obv doesn't work, but it would be nice |
Beta Was this translation helpful? Give feedback.
Answered by
drhagen
Nov 4, 2022
Replies: 1 comment 1 reply
-
Yeah, you need to use from parsita import *
class CaseInsensitiveParsers(TextParsers):
for_parser_reg = reg('(?i)for')
CaseInsensitiveParsers.for_parser_reg.parse('FOR').or_die() # also works This seems pretty ergonomic to me, but if you have a lot of these you could make a helper function: from parsita import *
def cilit(token: str) -> Parser[str, str]:
return reg("(?i)" + token)
class CaseInsensitiveParsers(TextParsers):
for_parser_cilit = cilit('for')
CaseInsensitiveParsers.for_parser_cilit.parse('FOR').or_die() # also works |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
dennis-barrett
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, you need to use
reg
for this:This seems pretty ergonomic to me, but if you have a lot of these you could make a helper function: