Created
August 2, 2017 00:49
-
-
Save frasertweedale/f8cadce6e78ddcf2d3739fd4c089abaf to your computer and use it in GitHub Desktop.
Parsing ranges with Attoparsec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| λ> let offset = T.Parser $ \t pos more lose succ -> succ t pos more (T.fromPos pos) | |
| λ> data Range = Range Int Int deriving (Eq, Show) | |
| λ> let skipTill s = manyTill anyWord8 (lookAhead $ string s) *> pure () | |
| λ> let range s = Range <$> offset <* skipTill s <*> offset | |
| λ> parseOnly (range "--") "....--" | |
| Right (Range 0 4) | |
| λ> parseOnly (range "--" <* string "--") "....--" | |
| Right (Range 0 4) | |
| λ> parseOnly (range "--" <* string "--") "....--..." | |
| Right (Range 0 4) | |
| λ> parseOnly (many' (range "--" <* string "--")) "....--..." | |
| Right [Range 0 4] | |
| λ> parseOnly (many' (range "--" <* string "--")) "....--..--" | |
| Right [Range 0 4,Range 6 8] | |
| λ> parseOnly (many' (range "--" <* string "--")) "....---..--" | |
| Right [Range 0 4,Range 6 9] | |
| λ> parseOnly (many' (range "--" <* string "--")) "....-...--....--..----" | |
| Right [Range 0 8,Range 10 14,Range 16 18,Range 20 20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment