Version 6 (modified by ross@…, 12 years ago) (diff)

some details

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 parser in the development version of HaXml.
    • monadic interface, invoked with
      runParser :: Parser t a -> [t] -> (Either String a, [t])
      (This is essentially the internal representation.)
    • choice operator commits to the first successful alternative
    • on failure, returns an error string and the unparsed input
  • 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