Version 7 (modified by malcolm.wallace@…, 12 years ago) (diff)

Replacing the Read Class


replace the Read class

Problems with the Haskell 98 class

  • the backtracking parser is very inefficient
  • writing instances is painful, compared to a monadic interface
  • on failure, parsers give no indication of what went wrong

Proposed replacements

  • parsers based on ReadP, from Parallel Parsing Processes by Koen Classen, JFP 2004.
    • abstract type with monadic interface, invoked with
      readP_to_S :: ReadP a -> String -> [(a, String)]
      (The internal representation is more complex.)
    • choice operator supports full nondeterminism by parsing alternatives in parallel. (There is also a committed choice operator.)
    • mostly deterministic parsers are much more efficient than ReadS
    • returns no information on failure, but could be altered to do so, as it knows the position at which all alternatives are exhausted.
    • requires PolymorphicComponents
    GHC has used a new Read class based on these parsers since 5.04 (July 2002).
  • Malcolm's experimental Poly TextParser in the development version of HaXml.
    • monadic interface, invoked with
      runParser :: TextParser a -> String -> (Either String a, String)
      (The internal representation is more general, parameterised on token type.)
    • returns only the first successful alternative, but with internal backtracking, and an explicit commit operator to prevent backtracking where necessary
    • on failure, returns an error string and the unparsed input
    • failure messages can be defined/combined/nested with convenient combinators
  • Could also use something based on Daan's Parsec combinators
    • monadic interface, invoked with (simplifying slightly)
      parse :: Parser a -> SourceName -> String -> Either ParseError a
    • mostly deterministic, with explicit 'try' operator for non-determinism
    • on failure, reports an error message including the position at which the error occured.
    • already widely used for other parsing tasks