Last active
March 20, 2021 09:54
-
-
Save Elvecent/afd77f6109b6f46489df2a261aae321e to your computer and use it in GitHub Desktop.
Parsing
This file contains 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
import Data.Void | |
import Text.Megaparsec | |
import Text.Megaparsec.Char | |
type Parser = Parsec Void String | |
main :: IO () | |
main = case runParser p "bruh" "\"123\", \"321\" , ," of | |
Left err -> print err | |
Right res -> print res | |
p :: Parser [String] | |
p = do | |
res <- (spaced $ inQuotes anyStringUntilQuote) | |
`sepBy` | |
char ',' | |
eof | |
pure res | |
where | |
spaced = between space space | |
inQuotes = between (char '\"') (char '\"') | |
anyStringUntilQuote = many $ anySingleBut '\"' | |
{- examples: | |
> parseTest p "\"1\" , \"2,2\" , \"\"" | |
["1","2,2",""] | |
> parseTest p "\"1\" \"2,2\" , \"\"" | |
1:9: | |
| | |
1 | "1" "2,2" , "" | |
| ^ | |
unexpected '"' | |
expecting ',', end of input, or white space | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment