Skip to content

Instantly share code, notes, and snippets.

@Elvecent
Last active March 20, 2021 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elvecent/afd77f6109b6f46489df2a261aae321e to your computer and use it in GitHub Desktop.
Save Elvecent/afd77f6109b6f46489df2a261aae321e to your computer and use it in GitHub Desktop.
Parsing
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