ysaie

31 / ⚧ / code, music, art, games

──────────────────────────────
🌸 many-shaped creature
✨ too many projects
🚀 cannot be stopped
🌙 stayed up too late
:eggbug: eggbug enjoyer
──────────────────────────────
header image: chapter 8 complete from celeste
avatar: made using this character builder


📩 email
contact@echowritescode.dev

it's very slick, and it works fine (barring some mildly annoying -Wunused-do-bind warnings), but i am totally mystified as to how the following parser figures out that "\"" means "please parse a " character":

parseStringLiteral :: Parser Text
parseStringLiteral = parseThenAdvance $ StringLiteral <$> do
  "\""
  text <- Megaparsec.takeWhileP (Just "character") (/= '"')
  "\""
  return text

my best guess is there's an instance for Text of some typeclass in Megaparsec that lets it treat a plain Text as a parser? i don't feel like i have enough context/instinct to dig deeper into this though... like, if this works, why does megaparsec even have the string combinator?


You must log in to comment.

in reply to @ysaie's post:

That's what the OverloadedStrings extension does: it implicitly wraps all Text literals in an implicit call to the IsString class, so when you write something like:

{-# LANGUAGE OverloadedStrings #-}

parseStringLiteral :: Parser Text
parseStringLiteral = parseThenAdvance $ StringLiteral <$> do
  "\""
  text <- Megaparsec.takeWhileP (Just "character") (/= '"')
  "\""
  return text

… that gets translated to:

parseStringLiteral :: Parser Text
parseStringLiteral = parseThenAdvance $ StringLiteral <$> do
  fromString "\""
  text <- Megaparsec.takeWhileP (Just (fromString "character")) (/= '"')
  fromString "\""
  return text