elm-community / parser-combinators

A parser combinator library for Elm.
http://package.elm-lang.org/packages/elm-community/parser-combinators/latest
BSD 3-Clause "New" or "Revised" License
104 stars 13 forks source link

Case insensitive parser #26

Closed Keyamoon closed 6 years ago

Keyamoon commented 7 years ago

Perhaps I'm missing something, but I didn't find any way to make a case insensitive string/regex parser.

stil4m commented 7 years ago

@Keyamoon Currently that is not supported. The library compiles its own regex and it does this to control the fact that the regex only has to match the beginning of the input.

The result is that simply passing in a Regex that is case insensitive is not an option, because the package will lose the control it needs. The current workaround is to use the regex function in this package with regexes having case insensitive character classes. For example: [A-Za-z] or [A-z].

The string is case sensitive by design. Using an or (or (string "foo") (string "FOO")) or choice may help you in this.

Does this work for you?

Otherwise, can you state what you try to achieve with and provide a real example?

Keyamoon commented 7 years ago

I think you meant to say that string is case sensitive by design.

I'm trying to parse an HTML doctype declaration. I'm currently using an or for parsing "doctype" or "DOCTYPE", but something like "docTyPe" would be valid too.

Keyamoon commented 7 years ago

Just realized I could use [dD][oO][cC][tT][yY][pP][eE] too.

stil4m commented 7 years ago

Yes I meant to say sensitive.

The 'ugly' but working solution for now would be a regex that matches each char case insensitive:

(d|D)(o|O)(c|C) ...

But this is suboptimal.

Adding one or two new functions would be an option regexWithOptions and/or caseInsensitiveString, but I would like more feedback from other people before adding these parsers.

stil4m commented 7 years ago

@Keyamoon When you only encounter this in the doctype string, I think this is a good workaround.

In addition: Maybe look into elm-xml. This library may contain what you are trying to do.

Keyamoon commented 7 years ago

Yeah, it's a good workaround for now. Thank you.

It's definitely better to get more feedback before jumping to conclusion and adding new functions.

stil4m commented 7 years ago

I'll leave the issue open for now. I'll check back in after a while.

stil4m commented 6 years ago

Will close due to inactivity