on master help sign in
more detailed error reporting
commitfe08234419a438f118b141db542ccc547da1a68b authorBoris Lykah <lykahb at gmail.com> dateThu 27 Sep 2012, 20:45 (14 years ago) parentbdb70d4
4 files changed+34−19
Data/Yaml.hs+5−2 Setup.lhs Text/Libyaml.hs+17−15 c/helper.c+12−2
Data/Yaml.hs+5−2
@@ -12,6 +12,7 @@ module Data.Yaml
1212 , Parser
1313 , Object
1414 , Array
15+ , ParseException(..)
1516 -- * Constructors and accessors
1617 , object
1718 , array
@@ -33,6 +34,8 @@ module Data.Yaml
3334 , decodeFile
3435 -- ** Better error information
3536 , decodeEither
37+ -- ** More control over decoding
38+ , decodeHelper
3639 ) where
3740
3841import qualified Text.Libyaml as Y
@@ -114,7 +117,7 @@ data ParseException = NonScalarKey
114117 | UnexpectedEvent { _received :: Maybe Event
115118 , _expected :: Maybe Event
116119 }
117− | InvalidYaml (Maybe String)
120+ | InvalidYaml (Maybe YamlException)
118121 deriving (Show, Typeable)
119122instance Exception ParseException
120123
@@ -264,7 +267,7 @@ decodeHelper src = do
264267 case x of
265268 Left e
266269 | Just pe <- fromException e -> return $ Left pe
267− | Just ye <- fromException e -> return $ Left $ InvalidYaml $ Just $ show (ye :: YamlException)
270+ | Just ye <- fromException e -> return $ Left $ InvalidYaml $ Just (ye :: YamlException)
268271 | otherwise -> throwIO e
269272 Right y -> return $ Right $ parseEither parseJSON y
270273
Setup.lhsmode 100755 → 100644

Only the mode changed.

Text/Libyaml.hs+17−15
@@ -164,8 +164,14 @@ foreign import ccall "get_parser_error_problem"
164164foreign import ccall "get_parser_error_context"
165165 c_get_parser_error_context :: Parser -> IO (Ptr CUChar)
166166
167−foreign import ccall unsafe "get_parser_error_offset"
168− c_get_parser_error_offset :: Parser -> IO CULong
167+foreign import ccall unsafe "get_parser_error_index"
168+ c_get_parser_error_index :: Parser -> IO CULong
169+
170+foreign import ccall unsafe "get_parser_error_line"
171+ c_get_parser_error_line :: Parser -> IO CULong
172+
173+foreign import ccall unsafe "get_parser_error_column"
174+ c_get_parser_error_column :: Parser -> IO CULong
169175
170176makeString :: MonadIO m => (a -> m (Ptr CUChar)) -> a -> m String
171177makeString f a = do
@@ -511,12 +517,12 @@ runParser :: MonadResource m => Parser -> GSource m Event
511517runParser parser = do
512518 e <- liftIO $ parserParseOne' parser
513519 case e of
514− Left err -> liftIO $ throwIO $ YamlException err
520+ Left err -> liftIO $ throwIO err
515521 Right Nothing -> return ()
516522 Right (Just ev) -> yield ev >> runParser parser
517523
518524parserParseOne' :: Parser
519− -> IO (Either String (Maybe Event))
525+ -> IO (Either YamlException (Maybe Event))
520526parserParseOne' parser = allocaBytes eventSize $ \er -> do
521527 res <- liftIO $ c_yaml_parser_parse parser er
522528 flip finally (c_yaml_event_delete er) $
@@ -524,16 +530,10 @@ parserParseOne' parser = allocaBytes eventSize $ \er -> do
524530 then do
525531 problem <- makeString c_get_parser_error_problem parser
526532 context <- makeString c_get_parser_error_context parser
527− offset <- c_get_parser_error_offset parser
528− return $ Left $ concat
529− [ "YAML parse error: "
530− , problem
531− , "\nContext: "
532− , context
533− , "\nOffset: "
534− , show offset
535− , "\n"
536− ]
533+ index <- c_get_parser_error_index parser
534+ line <- c_get_parser_error_line parser
535+ column <- c_get_parser_error_column parser
536+ return $ Left $ YamlParseException problem context (fromIntegral index, fromIntegral line, fromIntegral column)
537537 else Right <$> getEvent er
538538
539539encode :: MonadResource m => GSink Event m ByteString
@@ -594,5 +594,7 @@ runEmitter allocI closeI =
594594 close u = liftIO $ closeI u a
595595
596596data YamlException = YamlException String
597+ -- | problem, context, index, position line, position column
598+ | YamlParseException String String (Int, Int, Int)
597599 deriving (Show, Typeable)
598−instance Exception YamlException
600+instance Exception YamlException
c/helper.c+12−2
@@ -56,9 +56,19 @@ unsigned char const * get_parser_error_context(yaml_parser_t *p)
5656 return p->context;
5757}
5858
59−unsigned int get_parser_error_offset(yaml_parser_t *p)
59+unsigned int get_parser_error_index(yaml_parser_t *p)
6060{
61− return p->offset;
61+ return p->problem_mark.index;
62+}
63+
64+unsigned int get_parser_error_line(yaml_parser_t *p)
65+{
66+ return p->problem_mark.line;
67+}
68+
69+unsigned int get_parser_error_column(yaml_parser_t *p)
70+{
71+ return p->problem_mark.column;
6272}
6373
6474unsigned char const * get_emitter_error(yaml_emitter_t *e)