Skip to content

Prelude.read: no parse #87

Closed
Closed
@garetxe

Description

@garetxe

When trying to use c2hs 0.17.2 together with language-c 0.4.5 I get an error "Prelude.read: no parse".

This boils down to a change in representation of Ident in language-c, seems like, so the Read instance for Ident in src/C2HS/Gen/Monad.hs fails to work. The new Ident looks like the following

Ident "GIBaseInfo" 436424608 (NodeInfo ("/usr/include/gobject-introspection-1.0/gitypes.h": line 145) (("/usr/include/gobject-introspection-1.0/gitypes.h": line 145),10) (Name {nameId = 56188}))

The following Read instance fixes the issue for me:

-- Remove everything until the next element in the list (given by a
-- ","), the end of the list (marked by "]"), or the end of a record
-- "}". Everything inside parenthesis is ignored.
chopIdent :: String -> String
chopIdent str = goChop 0 str
    where goChop :: Int -> String -> String
          goChop 0 rest@('}':_) = rest
          goChop 0 rest@(',':_) = rest
          goChop 0 rest@(']':_) = rest
          goChop level ('(':rest) = goChop (level+1) rest
          goChop level (')':rest) = goChop (level-1) rest
          goChop level (_  :rest) = goChop level rest
          goChop level [] = []

extractIdent :: String -> (Ident, String)
extractIdent str =
    let isQuote c = c == '\'' || c == '"'
        (ideChars, rest) = span (not . isQuote)
                           . tail
                           . dropWhile (not . isQuote) $ str
    in
      if null ideChars
      then error $ "Could not interpret " ++ show str ++ "as an Ident."
      else (internalIdent ideChars, (chopIdent . tail) rest)

-- super kludgy (depends on Show instance of Ident)
instance Read Ident where
  readsPrec _ str = [extractIdent str]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions