yaml is a submodule
This commit is contained in:
parent
37ad3c045b
commit
95cbf51b95
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -16,3 +16,6 @@
|
||||
[submodule "tagstream-conduit"]
|
||||
path = tagstream-conduit
|
||||
url = https://github.com/snoyberg/tagstream-conduit
|
||||
[submodule "yaml"]
|
||||
path = yaml
|
||||
url = https://github.com/snoyberg/yaml
|
||||
|
||||
1
yaml
Submodule
1
yaml
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 3a46fa34bdd799f587664e6a18efe378695859d1
|
||||
2
yaml/.gitignore
vendored
2
yaml/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
dist
|
||||
*.swp
|
||||
@ -1,266 +0,0 @@
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE DeriveDataTypeable #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE PatternGuards #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
module Data.Yaml
|
||||
( -- * Types
|
||||
Value (..)
|
||||
, Parser
|
||||
, Object
|
||||
, Array
|
||||
-- * Constructors and accessors
|
||||
, object
|
||||
, array
|
||||
, (.=)
|
||||
, (.:)
|
||||
, (.:?)
|
||||
, (.!=)
|
||||
-- * Parsing
|
||||
, parseMonad
|
||||
, parseEither
|
||||
, parseMaybe
|
||||
-- * Classes
|
||||
, ToJSON (..)
|
||||
, FromJSON (..)
|
||||
-- * Encoding/decoding
|
||||
, encode
|
||||
, encodeFile
|
||||
, decode
|
||||
, decodeFile
|
||||
-- ** Better error information
|
||||
, decodeEither
|
||||
) where
|
||||
|
||||
import qualified Text.Libyaml as Y
|
||||
import Data.Aeson
|
||||
( Value (..), ToJSON (..), FromJSON (..), object
|
||||
, (.=) , (.:) , (.:?) , (.!=)
|
||||
, Object, Array
|
||||
)
|
||||
import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
|
||||
import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString.Char8 as S8
|
||||
import qualified Data.Map as Map
|
||||
import System.IO.Unsafe (unsafePerformIO)
|
||||
import Control.Exception (try, throwIO, fromException, Exception)
|
||||
import Control.Monad.Trans.State
|
||||
import qualified Data.Conduit as C
|
||||
import qualified Data.Conduit.List as CL
|
||||
import Control.Monad.Trans.Class (MonadTrans, lift)
|
||||
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||
import Control.Monad (liftM)
|
||||
import qualified Data.Vector as V
|
||||
import Data.Text (Text, pack)
|
||||
import Data.Text.Read (signed, decimal, double)
|
||||
import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
|
||||
import Data.Text.Encoding.Error (lenientDecode)
|
||||
import qualified Data.HashMap.Strict as M
|
||||
import Data.Typeable
|
||||
import Data.Attoparsec.Number
|
||||
|
||||
encode :: ToJSON a => a -> ByteString
|
||||
encode obj = unsafePerformIO $
|
||||
C.runResourceT $ CL.sourceList (objToEvents $ toJSON obj)
|
||||
C.$$ Y.encode
|
||||
|
||||
encodeFile :: ToJSON a => FilePath -> a -> IO ()
|
||||
encodeFile fp obj = C.runResourceT
|
||||
$ CL.sourceList (objToEvents $ toJSON obj)
|
||||
C.$$ Y.encodeFile fp
|
||||
|
||||
objToEvents :: Value -> [Y.Event]
|
||||
objToEvents o = (:) EventStreamStart
|
||||
. (:) EventDocumentStart
|
||||
$ objToEvents' o
|
||||
[ EventDocumentEnd
|
||||
, EventStreamEnd
|
||||
]
|
||||
|
||||
{- FIXME
|
||||
scalarToEvent :: YamlScalar -> Event
|
||||
scalarToEvent (YamlScalar v t s) = EventScalar v t s Nothing
|
||||
-}
|
||||
|
||||
objToEvents' :: Value -> [Y.Event] -> [Y.Event]
|
||||
--objToEvents' (Scalar s) rest = scalarToEvent s : rest
|
||||
objToEvents' (Array list) rest =
|
||||
EventSequenceStart Nothing
|
||||
: foldr ($) (EventSequenceEnd : rest) (map objToEvents' $ V.toList list)
|
||||
objToEvents' (Object pairs) rest =
|
||||
EventMappingStart Nothing
|
||||
: foldr ($) (EventMappingEnd : rest) (map pairToEvents $ M.toList pairs)
|
||||
objToEvents' (String s) rest = EventScalar (encodeUtf8 s) NoTag Any Nothing : rest
|
||||
objToEvents' Null rest = EventScalar "null" NoTag Literal Nothing : rest
|
||||
objToEvents' (Bool True) rest = EventScalar "true" NoTag Literal Nothing : rest
|
||||
objToEvents' (Bool False) rest = EventScalar "false" NoTag Literal Nothing : rest
|
||||
objToEvents' (Number n) rest = EventScalar (S8.pack $ show n) NoTag Literal Nothing : rest
|
||||
|
||||
pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
|
||||
pairToEvents (k, v) rest =
|
||||
EventScalar (encodeUtf8 k) NoTag Any Nothing
|
||||
: objToEvents' v rest
|
||||
|
||||
-- Parsing
|
||||
|
||||
data ParseException = NonScalarKey
|
||||
| UnknownAlias { _anchorName :: Y.AnchorName }
|
||||
| UnexpectedEvent { _received :: Maybe Event
|
||||
, _expected :: Maybe Event
|
||||
}
|
||||
| InvalidYaml (Maybe String)
|
||||
deriving (Show, Typeable)
|
||||
instance Exception ParseException
|
||||
|
||||
newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
|
||||
instance Monad m => Monad (PErrorT m) where
|
||||
return = PErrorT . return . Right
|
||||
(PErrorT m) >>= f = PErrorT $ do
|
||||
e <- m
|
||||
case e of
|
||||
Left e' -> return $ Left e'
|
||||
Right a -> runPErrorT $ f a
|
||||
instance MonadTrans PErrorT where
|
||||
lift = PErrorT . liftM Right
|
||||
instance MonadIO m => MonadIO (PErrorT m) where
|
||||
liftIO = lift . liftIO
|
||||
|
||||
type Parse = StateT (Map.Map String Value) (C.ResourceT IO)
|
||||
|
||||
requireEvent :: Event -> C.Sink Event Parse ()
|
||||
requireEvent e = do
|
||||
f <- CL.head
|
||||
if f == Just e
|
||||
then return ()
|
||||
else liftIO $ throwIO $ UnexpectedEvent f $ Just e
|
||||
|
||||
parse :: C.Sink Event Parse Value
|
||||
parse = do
|
||||
requireEvent EventStreamStart
|
||||
requireEvent EventDocumentStart
|
||||
res <- parseO
|
||||
requireEvent EventDocumentEnd
|
||||
requireEvent EventStreamEnd
|
||||
return res
|
||||
|
||||
parseScalar :: ByteString -> Anchor
|
||||
-> C.Sink Event Parse Text
|
||||
parseScalar v a = do
|
||||
let res = decodeUtf8With lenientDecode v
|
||||
case a of
|
||||
Nothing -> return res
|
||||
Just an -> do
|
||||
lift $ modify (Map.insert an $ textToValue res)
|
||||
return res
|
||||
|
||||
textToValue :: Text -> Value -- FIXME check for quoting style?
|
||||
textToValue "true" = Bool True
|
||||
textToValue "false" = Bool False
|
||||
textToValue "null" = Null
|
||||
textToValue t
|
||||
| Right (x, "") <- signed decimal t = Number $ I x
|
||||
| Right (x, "") <- double t = Number $ D x
|
||||
| otherwise = String t
|
||||
|
||||
parseO :: C.Sink Event Parse Value
|
||||
parseO = do
|
||||
me <- CL.head
|
||||
case me of
|
||||
Just (EventScalar v _t _s a) -> fmap textToValue $ parseScalar v a
|
||||
Just (EventSequenceStart a) -> parseS a id
|
||||
Just (EventMappingStart a) -> parseM a M.empty
|
||||
Just (EventAlias an) -> do
|
||||
m <- lift get
|
||||
case Map.lookup an m of
|
||||
Nothing -> liftIO $ throwIO $ UnknownAlias an
|
||||
Just v -> return v
|
||||
_ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
|
||||
|
||||
parseS :: Y.Anchor
|
||||
-> ([Value] -> [Value])
|
||||
-> C.Sink Event Parse Value
|
||||
parseS a front = do
|
||||
me <- CL.peek
|
||||
case me of
|
||||
Just EventSequenceEnd -> do
|
||||
CL.drop 1
|
||||
let res = Array $ V.fromList $ front []
|
||||
case a of
|
||||
Nothing -> return res
|
||||
Just an -> do
|
||||
lift $ modify $ Map.insert an res
|
||||
return res
|
||||
_ -> do
|
||||
o <- parseO
|
||||
parseS a $ front . (:) o
|
||||
|
||||
parseM :: Y.Anchor
|
||||
-> M.HashMap Text Value
|
||||
-> C.Sink Event Parse Value
|
||||
parseM a front = do
|
||||
me <- CL.peek
|
||||
case me of
|
||||
Just EventMappingEnd -> do
|
||||
CL.drop 1
|
||||
let res = Object front
|
||||
case a of
|
||||
Nothing -> return res
|
||||
Just an -> do
|
||||
lift $ modify $ Map.insert an res
|
||||
return res
|
||||
_ -> do
|
||||
CL.drop 1
|
||||
s <- case me of
|
||||
Just (EventScalar v _ _ a') -> parseScalar v a'
|
||||
_ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
|
||||
o <- parseO
|
||||
|
||||
let al = M.insert s o front
|
||||
al' = if s == pack "<<"
|
||||
then case o of
|
||||
Object l -> M.union al l
|
||||
Array l -> M.union al $ foldl merge' M.empty $ V.toList l
|
||||
_ -> al
|
||||
else al
|
||||
parseM a $ M.insert s o al'
|
||||
where merge' al (Object om) = M.union al om
|
||||
merge' al _ = al
|
||||
|
||||
decode :: FromJSON a
|
||||
=> ByteString
|
||||
-> Maybe a
|
||||
decode bs = unsafePerformIO
|
||||
$ fmap (either (const Nothing) (either (const Nothing) Just))
|
||||
$ decodeHelper (Y.decode bs)
|
||||
|
||||
decodeFile :: FromJSON a
|
||||
=> FilePath
|
||||
-> IO (Maybe a)
|
||||
decodeFile fp = decodeHelper (Y.decodeFile fp) >>= either throwIO (return . either (const Nothing) id)
|
||||
|
||||
decodeEither :: FromJSON a => ByteString -> Either String a
|
||||
decodeEither bs = unsafePerformIO
|
||||
$ fmap (either (Left . show) id)
|
||||
$ decodeHelper (Y.decode bs)
|
||||
|
||||
decodeHelper :: FromJSON a
|
||||
=> C.Source Parse Y.Event
|
||||
-> IO (Either ParseException (Either String a))
|
||||
decodeHelper src = do
|
||||
x <- try $ C.runResourceT $ flip evalStateT Map.empty $ src C.$$ parse
|
||||
case x of
|
||||
Left e
|
||||
| Just pe <- fromException e -> return $ Left pe
|
||||
| Just ye <- fromException e -> return $ Left $ InvalidYaml $ Just $ show (ye :: YamlException)
|
||||
| otherwise -> throwIO e
|
||||
Right y -> return $ Right $ parseEither parseJSON y
|
||||
|
||||
array :: [Value] -> Value
|
||||
array = Array . V.fromList
|
||||
|
||||
parseMonad :: Monad m => (a -> Parser b) -> a -> m b
|
||||
parseMonad p = either fail return . parseEither p
|
||||
25
yaml/LICENSE
25
yaml/LICENSE
@ -1,25 +0,0 @@
|
||||
The following license covers this documentation, and the source code, except
|
||||
where otherwise indicated.
|
||||
|
||||
Copyright 2008, Michael Snoyman. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@ -1,4 +0,0 @@
|
||||
libhelper.so : helper.o
|
||||
gcc -shared -Wl,-soname,libhelper.so -o libhelper.so helper.o
|
||||
helper.o : c/helper.c
|
||||
gcc -c -fPIC c/helper.c -o helper.o
|
||||
@ -1,6 +0,0 @@
|
||||
Support for serialising Haskell to and from Yaml.
|
||||
|
||||
This is a low level library. Unless you know what you are doing,
|
||||
you probably want to use data-objects-yaml instead:
|
||||
|
||||
http://github.com/snoyberg/data-object-yaml
|
||||
@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env runhaskell
|
||||
|
||||
> module Main where
|
||||
> import Distribution.Simple
|
||||
|
||||
> main :: IO ()
|
||||
> main = defaultMain
|
||||
@ -1,595 +0,0 @@
|
||||
{-# LANGUAGE EmptyDataDecls #-}
|
||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
{-# LANGUAGE DeriveDataTypeable #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE PackageImports #-}
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE RankNTypes #-}
|
||||
{-# LANGUAGE CPP #-}
|
||||
|
||||
-- | Low-level, streaming YAML interface. For a higher-level interface, see
|
||||
-- "Data.Yaml".
|
||||
module Text.Libyaml
|
||||
( -- * The event stream
|
||||
Event (..)
|
||||
, Style (..)
|
||||
, Tag (..)
|
||||
, AnchorName
|
||||
, Anchor
|
||||
-- * Encoding and decoding
|
||||
, encode
|
||||
, decode
|
||||
, encodeFile
|
||||
, decodeFile
|
||||
-- * Exception
|
||||
, YamlException (..)
|
||||
) where
|
||||
|
||||
import qualified Data.ByteString.Internal as B
|
||||
import qualified Data.ByteString.Char8 as B8
|
||||
import qualified Data.ByteString
|
||||
import qualified Data.ByteString.Unsafe as BU
|
||||
import Data.ByteString (ByteString, packCStringLen)
|
||||
import Control.Monad
|
||||
import Foreign.C
|
||||
import Foreign.Ptr
|
||||
import Foreign.ForeignPtr
|
||||
import Foreign.Marshal.Alloc
|
||||
import Data.Data
|
||||
|
||||
import Control.Monad.IO.Class
|
||||
|
||||
import Control.Exception (throwIO, Exception, finally)
|
||||
import Control.Applicative
|
||||
import Control.Monad.Trans.Resource
|
||||
import qualified Data.Conduit as C
|
||||
import Control.Exception (mask_)
|
||||
|
||||
data Event =
|
||||
EventStreamStart
|
||||
| EventStreamEnd
|
||||
| EventDocumentStart
|
||||
| EventDocumentEnd
|
||||
| EventAlias !AnchorName
|
||||
| EventScalar !ByteString !Tag !Style !Anchor
|
||||
| EventSequenceStart !Anchor
|
||||
| EventSequenceEnd
|
||||
| EventMappingStart !Anchor
|
||||
| EventMappingEnd
|
||||
deriving (Show, Eq)
|
||||
|
||||
data Style = Any
|
||||
| Plain
|
||||
| SingleQuoted
|
||||
| DoubleQuoted
|
||||
| Literal
|
||||
| Folded
|
||||
deriving (Show, Read, Eq, Enum, Bounded, Ord, Data, Typeable)
|
||||
|
||||
data Tag = StrTag
|
||||
| FloatTag
|
||||
| NullTag
|
||||
| BoolTag
|
||||
| SetTag
|
||||
| IntTag
|
||||
| SeqTag
|
||||
| MapTag
|
||||
| UriTag String
|
||||
| NoTag
|
||||
deriving (Show, Eq, Read, Data, Typeable)
|
||||
|
||||
type AnchorName = String
|
||||
type Anchor = Maybe AnchorName
|
||||
|
||||
tagToString :: Tag -> String
|
||||
tagToString StrTag = "tag:yaml.org,2002:str"
|
||||
tagToString FloatTag = "tag:yaml.org,2002:float"
|
||||
tagToString NullTag = "tag:yaml.org,2002:null"
|
||||
tagToString BoolTag = "tag:yaml.org,2002:bool"
|
||||
tagToString SetTag = "tag:yaml.org,2002:set"
|
||||
tagToString IntTag = "tag:yaml.org,2002:int"
|
||||
tagToString SeqTag = "tag:yaml.org,2002:seq"
|
||||
tagToString MapTag = "tag:yaml.org,2002:map"
|
||||
tagToString (UriTag s) = s
|
||||
tagToString NoTag = ""
|
||||
|
||||
bsToTag :: ByteString -> Tag
|
||||
bsToTag = stringToTag . B8.unpack
|
||||
|
||||
stringToTag :: String -> Tag
|
||||
stringToTag "tag:yaml.org,2002:str" = StrTag
|
||||
stringToTag "tag:yaml.org,2002:float" = FloatTag
|
||||
stringToTag "tag:yaml.org,2002:null" = NullTag
|
||||
stringToTag "tag:yaml.org,2002:bool" = BoolTag
|
||||
stringToTag "tag:yaml.org,2002:set" = SetTag
|
||||
stringToTag "tag:yaml.org,2002:int" = IntTag
|
||||
stringToTag "tag:yaml.org,2002:seq" = SeqTag
|
||||
stringToTag "tag:yaml.org,2002:map" = MapTag
|
||||
stringToTag "" = NoTag
|
||||
stringToTag s = UriTag s
|
||||
|
||||
data ParserStruct
|
||||
type Parser = Ptr ParserStruct
|
||||
parserSize :: Int
|
||||
parserSize = 480
|
||||
|
||||
data EventRawStruct
|
||||
type EventRaw = Ptr EventRawStruct
|
||||
eventSize :: Int
|
||||
eventSize = 104
|
||||
|
||||
foreign import ccall unsafe "yaml_parser_initialize"
|
||||
c_yaml_parser_initialize :: Parser -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_parser_delete"
|
||||
c_yaml_parser_delete :: Parser -> IO ()
|
||||
|
||||
foreign import ccall unsafe "yaml_parser_set_input_string"
|
||||
c_yaml_parser_set_input_string :: Parser
|
||||
-> Ptr CUChar
|
||||
-> CULong
|
||||
-> IO ()
|
||||
|
||||
foreign import ccall unsafe "yaml_parser_set_input_file"
|
||||
c_yaml_parser_set_input_file :: Parser
|
||||
-> File
|
||||
-> IO ()
|
||||
|
||||
data FileStruct
|
||||
type File = Ptr FileStruct
|
||||
|
||||
foreign import ccall unsafe "fopen"
|
||||
c_fopen :: Ptr CChar
|
||||
-> Ptr CChar
|
||||
-> IO File
|
||||
|
||||
foreign import ccall unsafe "fclose"
|
||||
c_fclose :: File
|
||||
-> IO ()
|
||||
|
||||
foreign import ccall unsafe "fclose_helper"
|
||||
c_fclose_helper :: File -> IO ()
|
||||
|
||||
foreign import ccall unsafe "yaml_parser_parse"
|
||||
c_yaml_parser_parse :: Parser -> EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_event_delete"
|
||||
c_yaml_event_delete :: EventRaw -> IO ()
|
||||
|
||||
foreign import ccall "get_parser_error_problem"
|
||||
c_get_parser_error_problem :: Parser -> IO (Ptr CUChar)
|
||||
|
||||
foreign import ccall "get_parser_error_context"
|
||||
c_get_parser_error_context :: Parser -> IO (Ptr CUChar)
|
||||
|
||||
foreign import ccall unsafe "get_parser_error_offset"
|
||||
c_get_parser_error_offset :: Parser -> IO CULong
|
||||
|
||||
makeString :: MonadIO m => (a -> m (Ptr CUChar)) -> a -> m String
|
||||
makeString f a = do
|
||||
cchar <- castPtr `liftM` f a
|
||||
if cchar == nullPtr
|
||||
then return ""
|
||||
else liftIO $ peekCString cchar
|
||||
|
||||
data EventType = YamlNoEvent
|
||||
| YamlStreamStartEvent
|
||||
| YamlStreamEndEvent
|
||||
| YamlDocumentStartEvent
|
||||
| YamlDocumentEndEvent
|
||||
| YamlAliasEvent
|
||||
| YamlScalarEvent
|
||||
| YamlSequenceStartEvent
|
||||
| YamlSequenceEndEvent
|
||||
| YamlMappingStartEvent
|
||||
| YamlMappingEndEvent
|
||||
deriving (Enum,Show)
|
||||
|
||||
foreign import ccall unsafe "get_event_type"
|
||||
c_get_event_type :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "get_scalar_value"
|
||||
c_get_scalar_value :: EventRaw -> IO (Ptr CUChar)
|
||||
|
||||
foreign import ccall unsafe "get_scalar_length"
|
||||
c_get_scalar_length :: EventRaw -> IO CULong
|
||||
|
||||
foreign import ccall unsafe "get_scalar_tag"
|
||||
c_get_scalar_tag :: EventRaw -> IO (Ptr CUChar)
|
||||
|
||||
foreign import ccall unsafe "get_scalar_tag_len"
|
||||
c_get_scalar_tag_len :: EventRaw -> IO CULong
|
||||
|
||||
foreign import ccall unsafe "get_scalar_style"
|
||||
c_get_scalar_style :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "get_scalar_anchor"
|
||||
c_get_scalar_anchor :: EventRaw -> IO CString
|
||||
|
||||
foreign import ccall unsafe "get_sequence_start_anchor"
|
||||
c_get_sequence_start_anchor :: EventRaw -> IO CString
|
||||
|
||||
foreign import ccall unsafe "get_mapping_start_anchor"
|
||||
c_get_mapping_start_anchor :: EventRaw -> IO CString
|
||||
|
||||
foreign import ccall unsafe "get_alias_anchor"
|
||||
c_get_alias_anchor :: EventRaw -> IO CString
|
||||
|
||||
getEvent :: EventRaw -> IO (Maybe Event)
|
||||
getEvent er = do
|
||||
et <- c_get_event_type er
|
||||
case toEnum $ fromEnum et of
|
||||
YamlNoEvent -> return Nothing
|
||||
YamlStreamStartEvent -> return $ Just EventStreamStart
|
||||
YamlStreamEndEvent -> return $ Just EventStreamEnd
|
||||
YamlDocumentStartEvent -> return $ Just EventDocumentStart
|
||||
YamlDocumentEndEvent -> return $ Just EventDocumentEnd
|
||||
YamlAliasEvent -> do
|
||||
yanchor <- c_get_alias_anchor er
|
||||
anchor <- if yanchor == nullPtr
|
||||
then error "got YamlAliasEvent with empty anchor"
|
||||
else peekCString yanchor
|
||||
return $ Just $ EventAlias anchor
|
||||
YamlScalarEvent -> do
|
||||
yvalue <- c_get_scalar_value er
|
||||
ylen <- c_get_scalar_length er
|
||||
ytag <- c_get_scalar_tag er
|
||||
ytag_len <- c_get_scalar_tag_len er
|
||||
ystyle <- c_get_scalar_style er
|
||||
let ytag_len' = fromEnum ytag_len
|
||||
let yvalue' = castPtr yvalue
|
||||
let ytag' = castPtr ytag
|
||||
let ylen' = fromEnum ylen
|
||||
bs <- packCStringLen (yvalue', ylen')
|
||||
tagbs <-
|
||||
if ytag_len' == 0
|
||||
then return Data.ByteString.empty
|
||||
else packCStringLen (ytag', ytag_len')
|
||||
let style = toEnum $ fromEnum ystyle
|
||||
yanchor <- c_get_scalar_anchor er
|
||||
anchor <- if yanchor == nullPtr
|
||||
then return Nothing
|
||||
else fmap Just $ peekCString yanchor
|
||||
return $ Just $ EventScalar bs (bsToTag tagbs) style anchor
|
||||
YamlSequenceStartEvent -> do
|
||||
yanchor <- c_get_sequence_start_anchor er
|
||||
anchor <- if yanchor == nullPtr
|
||||
then return Nothing
|
||||
else fmap Just $ peekCString yanchor
|
||||
return $ Just $ EventSequenceStart anchor
|
||||
YamlSequenceEndEvent -> return $ Just EventSequenceEnd
|
||||
YamlMappingStartEvent -> do
|
||||
yanchor <- c_get_mapping_start_anchor er
|
||||
anchor <- if yanchor == nullPtr
|
||||
then return Nothing
|
||||
else fmap Just $ peekCString yanchor
|
||||
return $ Just $ EventMappingStart anchor
|
||||
YamlMappingEndEvent -> return $ Just EventMappingEnd
|
||||
|
||||
-- Emitter
|
||||
|
||||
data EmitterStruct
|
||||
type Emitter = Ptr EmitterStruct
|
||||
emitterSize :: Int
|
||||
emitterSize = 432
|
||||
|
||||
foreign import ccall unsafe "yaml_emitter_initialize"
|
||||
c_yaml_emitter_initialize :: Emitter -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_emitter_delete"
|
||||
c_yaml_emitter_delete :: Emitter -> IO ()
|
||||
|
||||
data BufferStruct
|
||||
type Buffer = Ptr BufferStruct
|
||||
bufferSize :: Int
|
||||
bufferSize = 16
|
||||
|
||||
foreign import ccall unsafe "buffer_init"
|
||||
c_buffer_init :: Buffer -> IO ()
|
||||
|
||||
foreign import ccall unsafe "get_buffer_buff"
|
||||
c_get_buffer_buff :: Buffer -> IO (Ptr CUChar)
|
||||
|
||||
foreign import ccall unsafe "get_buffer_used"
|
||||
c_get_buffer_used :: Buffer -> IO CULong
|
||||
|
||||
foreign import ccall unsafe "my_emitter_set_output"
|
||||
c_my_emitter_set_output :: Emitter -> Buffer -> IO ()
|
||||
|
||||
foreign import ccall unsafe "yaml_emitter_set_output_file"
|
||||
c_yaml_emitter_set_output_file :: Emitter -> File -> IO ()
|
||||
|
||||
foreign import ccall unsafe "yaml_emitter_emit"
|
||||
c_yaml_emitter_emit :: Emitter -> EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_stream_start_event_initialize"
|
||||
c_yaml_stream_start_event_initialize :: EventRaw -> CInt -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_stream_end_event_initialize"
|
||||
c_yaml_stream_end_event_initialize :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_scalar_event_initialize"
|
||||
c_yaml_scalar_event_initialize
|
||||
:: EventRaw
|
||||
-> Ptr CUChar -- anchor
|
||||
-> Ptr CUChar -- tag
|
||||
-> Ptr CUChar -- value
|
||||
-> CInt -- length
|
||||
-> CInt -- plain_implicit
|
||||
-> CInt -- quoted_implicit
|
||||
-> CInt -- style
|
||||
-> IO CInt
|
||||
|
||||
foreign import ccall unsafe "simple_document_start"
|
||||
c_simple_document_start :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_document_end_event_initialize"
|
||||
c_yaml_document_end_event_initialize :: EventRaw -> CInt -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_sequence_start_event_initialize"
|
||||
c_yaml_sequence_start_event_initialize
|
||||
:: EventRaw
|
||||
-> Ptr CUChar
|
||||
-> Ptr CUChar
|
||||
-> CInt
|
||||
-> CInt
|
||||
-> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_sequence_end_event_initialize"
|
||||
c_yaml_sequence_end_event_initialize :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_mapping_start_event_initialize"
|
||||
c_yaml_mapping_start_event_initialize
|
||||
:: EventRaw
|
||||
-> Ptr CUChar
|
||||
-> Ptr CUChar
|
||||
-> CInt
|
||||
-> CInt
|
||||
-> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_mapping_end_event_initialize"
|
||||
c_yaml_mapping_end_event_initialize :: EventRaw -> IO CInt
|
||||
|
||||
foreign import ccall unsafe "yaml_alias_event_initialize"
|
||||
c_yaml_alias_event_initialize
|
||||
:: EventRaw
|
||||
-> Ptr CUChar
|
||||
-> IO CInt
|
||||
|
||||
toEventRaw :: Event -> (EventRaw -> IO a) -> IO a
|
||||
toEventRaw e f = allocaBytes eventSize $ \er -> do
|
||||
ret <- case e of
|
||||
EventStreamStart ->
|
||||
c_yaml_stream_start_event_initialize
|
||||
er
|
||||
0 -- YAML_ANY_ENCODING
|
||||
EventStreamEnd ->
|
||||
c_yaml_stream_end_event_initialize er
|
||||
EventDocumentStart ->
|
||||
c_simple_document_start er
|
||||
EventDocumentEnd ->
|
||||
c_yaml_document_end_event_initialize er 1
|
||||
EventScalar bs thetag style anchor -> do
|
||||
BU.unsafeUseAsCStringLen bs $ \(value, len) -> do
|
||||
let value' = castPtr value :: Ptr CUChar
|
||||
len' = fromIntegral len :: CInt
|
||||
let thetag' = tagToString thetag
|
||||
withCString thetag' $ \tag' -> do
|
||||
let style' = toEnum $ fromEnum style
|
||||
tagP = castPtr tag'
|
||||
qi = if null thetag' then 1 else 0
|
||||
case anchor of
|
||||
Nothing ->
|
||||
c_yaml_scalar_event_initialize
|
||||
er
|
||||
nullPtr -- anchor
|
||||
tagP -- tag
|
||||
value' -- value
|
||||
len' -- length
|
||||
0 -- plain_implicit
|
||||
qi -- quoted_implicit
|
||||
style' -- style
|
||||
Just anchor' ->
|
||||
withCString anchor' $ \anchorP' -> do
|
||||
let anchorP = castPtr anchorP'
|
||||
c_yaml_scalar_event_initialize
|
||||
er
|
||||
anchorP -- anchor
|
||||
tagP -- tag
|
||||
value' -- value
|
||||
len' -- length
|
||||
0 -- plain_implicit
|
||||
qi -- quoted_implicit
|
||||
style' -- style
|
||||
EventSequenceStart Nothing ->
|
||||
c_yaml_sequence_start_event_initialize
|
||||
er
|
||||
nullPtr
|
||||
nullPtr
|
||||
1
|
||||
0 -- YAML_ANY_SEQUENCE_STYLE
|
||||
EventSequenceStart (Just anchor) ->
|
||||
withCString anchor $ \anchor' -> do
|
||||
let anchorP = castPtr anchor'
|
||||
c_yaml_sequence_start_event_initialize
|
||||
er
|
||||
anchorP
|
||||
nullPtr
|
||||
1
|
||||
0 -- YAML_ANY_SEQUENCE_STYLE
|
||||
EventSequenceEnd ->
|
||||
c_yaml_sequence_end_event_initialize er
|
||||
EventMappingStart Nothing ->
|
||||
c_yaml_mapping_start_event_initialize
|
||||
er
|
||||
nullPtr
|
||||
nullPtr
|
||||
1
|
||||
0 -- YAML_ANY_SEQUENCE_STYLE
|
||||
EventMappingStart (Just anchor) ->
|
||||
withCString anchor $ \anchor' -> do
|
||||
let anchorP = castPtr anchor'
|
||||
c_yaml_mapping_start_event_initialize
|
||||
er
|
||||
anchorP
|
||||
nullPtr
|
||||
1
|
||||
0 -- YAML_ANY_SEQUENCE_STYLE
|
||||
EventMappingEnd ->
|
||||
c_yaml_mapping_end_event_initialize er
|
||||
EventAlias anchor ->
|
||||
withCString anchor $ \anchorP' -> do
|
||||
let anchorP = castPtr anchorP'
|
||||
c_yaml_alias_event_initialize
|
||||
er
|
||||
anchorP
|
||||
unless (ret == 1) $ throwIO $ ToEventRawException ret
|
||||
f er
|
||||
|
||||
newtype ToEventRawException = ToEventRawException CInt
|
||||
deriving (Show, Typeable)
|
||||
instance Exception ToEventRawException
|
||||
|
||||
decode :: C.MonadResource m => B.ByteString -> C.Source m Event
|
||||
decode bs =
|
||||
C.sourceIO alloc cleanup (runParser . fst)
|
||||
where
|
||||
alloc = mask_ $ do
|
||||
ptr <- mallocBytes parserSize
|
||||
res <- c_yaml_parser_initialize ptr
|
||||
if res == 0
|
||||
then do
|
||||
c_yaml_parser_delete ptr
|
||||
free ptr
|
||||
throwIO $ YamlException "Yaml out of memory"
|
||||
else do
|
||||
let (bsfptr, offset, len) = B.toForeignPtr bs
|
||||
let bsptrOrig = unsafeForeignPtrToPtr bsfptr
|
||||
let bsptr = castPtr bsptrOrig `plusPtr` offset
|
||||
c_yaml_parser_set_input_string ptr bsptr (fromIntegral len)
|
||||
return (ptr, bsfptr)
|
||||
cleanup (ptr, bsfptr) = do
|
||||
touchForeignPtr bsfptr
|
||||
c_yaml_parser_delete ptr
|
||||
free ptr
|
||||
|
||||
decodeFile :: C.MonadResource m => FilePath -> C.Source m Event
|
||||
decodeFile file =
|
||||
C.sourceIO alloc cleanup (runParser . fst)
|
||||
where
|
||||
alloc = mask_ $ do
|
||||
ptr <- mallocBytes parserSize
|
||||
res <- c_yaml_parser_initialize ptr
|
||||
if res == 0
|
||||
then do
|
||||
c_yaml_parser_delete ptr
|
||||
free ptr
|
||||
throwIO $ YamlException "Yaml out of memory"
|
||||
else do
|
||||
file' <- liftIO
|
||||
$ withCString file $ \file' -> withCString "r" $ \r' ->
|
||||
c_fopen file' r'
|
||||
if file' == nullPtr
|
||||
then do
|
||||
c_fclose_helper file'
|
||||
c_yaml_parser_delete ptr
|
||||
free ptr
|
||||
throwIO $ YamlException
|
||||
$ "Yaml file not found: " ++ file
|
||||
else do
|
||||
c_yaml_parser_set_input_file ptr file'
|
||||
return (ptr, file')
|
||||
cleanup (ptr, file') = do
|
||||
c_fclose_helper file'
|
||||
c_yaml_parser_delete ptr
|
||||
free ptr
|
||||
|
||||
runParser :: C.MonadResource m => Parser -> m (C.SourceIOResult Event)
|
||||
runParser parser = liftIO $ do
|
||||
e <- parserParseOne' parser
|
||||
case e of
|
||||
Left err -> throwIO $ YamlException err
|
||||
Right Nothing -> return $ C.IOClosed
|
||||
Right (Just ev) -> return $ C.IOOpen ev
|
||||
|
||||
parserParseOne' :: Parser
|
||||
-> IO (Either String (Maybe Event))
|
||||
parserParseOne' parser = allocaBytes eventSize $ \er -> do
|
||||
res <- liftIO $ c_yaml_parser_parse parser er
|
||||
flip finally (c_yaml_event_delete er) $
|
||||
if res == 0
|
||||
then do
|
||||
problem <- makeString c_get_parser_error_problem parser
|
||||
context <- makeString c_get_parser_error_context parser
|
||||
offset <- c_get_parser_error_offset parser
|
||||
return $ Left $ concat
|
||||
[ "YAML parse error: "
|
||||
, problem
|
||||
, "\nContext: "
|
||||
, context
|
||||
, "\nOffset: "
|
||||
, show offset
|
||||
, "\n"
|
||||
]
|
||||
else Right <$> getEvent er
|
||||
|
||||
encode :: C.MonadResource m => C.Sink Event m ByteString
|
||||
encode =
|
||||
runEmitter alloc close
|
||||
where
|
||||
alloc emitter = do
|
||||
fbuf <- mallocForeignPtrBytes bufferSize
|
||||
withForeignPtr fbuf c_buffer_init
|
||||
withForeignPtr fbuf $ c_my_emitter_set_output emitter
|
||||
return fbuf
|
||||
close fbuf = withForeignPtr fbuf $ \b -> do
|
||||
ptr' <- c_get_buffer_buff b
|
||||
len <- c_get_buffer_used b
|
||||
fptr <- newForeignPtr_ $ castPtr ptr'
|
||||
return $ B.fromForeignPtr fptr 0 $ fromIntegral len
|
||||
|
||||
encodeFile :: C.MonadResource m
|
||||
=> FilePath
|
||||
-> C.Sink Event m ()
|
||||
encodeFile filePath =
|
||||
C.PipeM msink (return ())
|
||||
where
|
||||
msink = do
|
||||
(_releaseKey, file) <- flip allocate c_fclose $ do
|
||||
file <- liftIO $ withCString filePath $
|
||||
\filePath' -> withCString "w" $
|
||||
\w' -> c_fopen filePath' w'
|
||||
if (file == nullPtr)
|
||||
then throwIO $ YamlException $ "could not open file for write: " ++ filePath
|
||||
else return file
|
||||
return $ runEmitter (alloc file) (return) -- FIXME close file early
|
||||
alloc file emitter = do
|
||||
c_yaml_emitter_set_output_file emitter file
|
||||
return ()
|
||||
|
||||
runEmitter :: C.MonadResource m
|
||||
=> (Emitter -> IO a) -- ^ alloc
|
||||
-> (a -> IO b) -- ^ close
|
||||
-> C.Sink Event m b
|
||||
runEmitter allocI closeI =
|
||||
C.sinkIO alloc cleanup push close
|
||||
where
|
||||
alloc = mask_ $ do
|
||||
emitter <- mallocBytes emitterSize
|
||||
res <- c_yaml_emitter_initialize emitter
|
||||
when (res == 0) $ throwIO $ YamlException "c_yaml_emitter_initialize failed"
|
||||
a <- allocI emitter
|
||||
return (emitter, a)
|
||||
cleanup (emitter, _) = do
|
||||
c_yaml_emitter_delete emitter
|
||||
free emitter
|
||||
push (emitter, _) e = do
|
||||
_ <- liftIO $ toEventRaw e $ c_yaml_emitter_emit emitter
|
||||
return C.IOProcessing
|
||||
close (_, a) = liftIO $ closeI a
|
||||
|
||||
data YamlException = YamlException String
|
||||
deriving (Show, Typeable)
|
||||
instance Exception YamlException
|
||||
142
yaml/c/helper.c
142
yaml/c/helper.c
@ -1,142 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "helper.h"
|
||||
|
||||
void buffer_init(buffer_t *buffer)
|
||||
{
|
||||
buffer->buff = 0;
|
||||
buffer->size = buffer->used = 0;
|
||||
}
|
||||
|
||||
int buffer_append(void *ext, unsigned char *str, size_t size)
|
||||
{
|
||||
buffer_t *b = ext;
|
||||
int new_size, new_used;
|
||||
char *tmp;
|
||||
|
||||
new_used = b->used + size;
|
||||
for (new_size = b->size || 8; new_size < new_used; new_size *= 2);
|
||||
|
||||
if (new_size != b->size) {
|
||||
tmp = realloc(b->buff, new_size);
|
||||
if (!tmp) return 0;
|
||||
b->buff = tmp;
|
||||
b->size = new_size;
|
||||
}
|
||||
|
||||
memcpy(b->buff + b->used, str, size);
|
||||
b->used = new_used;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char * get_buffer_buff(buffer_t *b)
|
||||
{
|
||||
return b->buff;
|
||||
}
|
||||
|
||||
unsigned int get_buffer_used(buffer_t *b)
|
||||
{
|
||||
return b->used;
|
||||
}
|
||||
|
||||
void my_emitter_set_output(yaml_emitter_t *e, buffer_t *b)
|
||||
{
|
||||
yaml_emitter_set_output(e, buffer_append, b);
|
||||
}
|
||||
|
||||
unsigned char const * get_parser_error_problem(yaml_parser_t *p)
|
||||
{
|
||||
return p->problem;
|
||||
}
|
||||
|
||||
unsigned char const * get_parser_error_context(yaml_parser_t *p)
|
||||
{
|
||||
return p->context;
|
||||
}
|
||||
|
||||
unsigned int get_parser_error_offset(yaml_parser_t *p)
|
||||
{
|
||||
return p->offset;
|
||||
}
|
||||
|
||||
unsigned char const * get_emitter_error(yaml_emitter_t *e)
|
||||
{
|
||||
return e->problem;
|
||||
}
|
||||
|
||||
int simple_document_start(yaml_event_t *e)
|
||||
{
|
||||
return yaml_document_start_event_initialize
|
||||
(e,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1);
|
||||
}
|
||||
|
||||
int get_event_type(yaml_event_t *e)
|
||||
{
|
||||
return e->type;
|
||||
}
|
||||
|
||||
unsigned char * get_scalar_value(yaml_event_t *e)
|
||||
{
|
||||
return e->data.scalar.value;
|
||||
}
|
||||
|
||||
unsigned long get_scalar_length(yaml_event_t *e)
|
||||
{
|
||||
return e->data.scalar.length;
|
||||
}
|
||||
|
||||
unsigned char * get_scalar_tag(yaml_event_t *e)
|
||||
{
|
||||
unsigned char *s = e->data.scalar.tag;
|
||||
if (!s) s = "";
|
||||
return s;
|
||||
}
|
||||
|
||||
unsigned long get_scalar_tag_len(yaml_event_t *e)
|
||||
{
|
||||
return strlen(get_scalar_tag(e));
|
||||
}
|
||||
|
||||
int get_scalar_style(yaml_event_t *e)
|
||||
{
|
||||
return e->data.scalar.style;
|
||||
}
|
||||
|
||||
unsigned char * get_scalar_anchor(yaml_event_t *e)
|
||||
{
|
||||
return e->data.scalar.anchor;
|
||||
}
|
||||
|
||||
unsigned char * get_sequence_start_anchor(yaml_event_t *e)
|
||||
{
|
||||
return e->data.sequence_start.anchor;
|
||||
}
|
||||
|
||||
unsigned char * get_mapping_start_anchor(yaml_event_t *e)
|
||||
{
|
||||
return e->data.mapping_start.anchor;
|
||||
}
|
||||
|
||||
unsigned char * get_alias_anchor(yaml_event_t *e)
|
||||
{
|
||||
return e->data.alias.anchor;
|
||||
}
|
||||
|
||||
int yaml_parser_set_input_filename(yaml_parser_t *parser, const char *filename)
|
||||
{
|
||||
FILE *in = fopen(filename, "r");
|
||||
if (!in) return 0;
|
||||
yaml_parser_set_input_file(parser, in);
|
||||
}
|
||||
|
||||
int fclose_helper(FILE *file)
|
||||
{
|
||||
if (! file) return 0;
|
||||
return fclose(file);
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
#ifndef __HELPER_H__
|
||||
#define __HELPER_H__
|
||||
|
||||
#include <yaml.h>
|
||||
|
||||
typedef struct buffer_s {
|
||||
unsigned char *buff;
|
||||
unsigned int size, used;
|
||||
} buffer_t;
|
||||
void buffer_init(buffer_t *buffer);
|
||||
int buffer_append(void *ext, unsigned char *str, size_t size);
|
||||
unsigned char * get_buffer_buff(buffer_t *b);
|
||||
unsigned int get_buffer_used(buffer_t *b);
|
||||
|
||||
void my_emitter_set_output(yaml_emitter_t *e, buffer_t *b);
|
||||
|
||||
unsigned char const * get_parser_error_problem(yaml_parser_t *p);
|
||||
unsigned char const * get_parser_error_context(yaml_parser_t *p);
|
||||
unsigned int get_parser_error_offset(yaml_parser_t *p);
|
||||
|
||||
unsigned char const * get_emitter_error(yaml_emitter_t *e);
|
||||
|
||||
int simple_document_start(yaml_event_t *e);
|
||||
|
||||
int get_event_type(yaml_event_t *e);
|
||||
|
||||
unsigned char * get_scalar_value(yaml_event_t *e);
|
||||
unsigned long get_scalar_length(yaml_event_t *e);
|
||||
|
||||
unsigned char * get_scalar_tag(yaml_event_t *e);
|
||||
unsigned long get_scalar_tag_len(yaml_event_t *e);
|
||||
|
||||
int get_scalar_style(yaml_event_t *e);
|
||||
|
||||
unsigned char * get_scalar_anchor(yaml_event_t *e);
|
||||
unsigned char * get_sequence_start_anchor(yaml_event_t *e);
|
||||
unsigned char * get_mapping_start_anchor(yaml_event_t *e);
|
||||
unsigned char * get_alias_anchor(yaml_event_t *e);
|
||||
|
||||
#endif /* __HELPER_H__ */
|
||||
@ -1,19 +0,0 @@
|
||||
Copyright (c) 2006 Kirill Simonov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
1390
yaml/libyaml/api.c
1390
yaml/libyaml/api.c
File diff suppressed because it is too large
Load Diff
@ -1,394 +0,0 @@
|
||||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* API functions.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_open(yaml_emitter_t *emitter);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_close(yaml_emitter_t *emitter);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document);
|
||||
|
||||
/*
|
||||
* Clean up functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_delete_document_and_anchors(yaml_emitter_t *emitter);
|
||||
|
||||
/*
|
||||
* Anchor functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_anchor_node(yaml_emitter_t *emitter, int index);
|
||||
|
||||
static yaml_char_t *
|
||||
yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id);
|
||||
|
||||
|
||||
/*
|
||||
* Serialize functions.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_node(yaml_emitter_t *emitter, int index);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_alias(yaml_emitter_t *emitter, yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_scalar(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_sequence(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
/*
|
||||
* Issue a STREAM-START event.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_open(yaml_emitter_t *emitter)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(!emitter->opened); /* Emitter should not be opened yet. */
|
||||
|
||||
STREAM_START_EVENT_INIT(event, YAML_ANY_ENCODING, mark, mark);
|
||||
|
||||
if (!yaml_emitter_emit(emitter, &event)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
emitter->opened = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Issue a STREAM-END event.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_close(yaml_emitter_t *emitter)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(emitter->opened); /* Emitter should be opened. */
|
||||
|
||||
if (emitter->closed) return 1;
|
||||
|
||||
STREAM_END_EVENT_INIT(event, mark, mark);
|
||||
|
||||
if (!yaml_emitter_emit(emitter, &event)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
emitter->closed = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Dump a YAML document.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(document); /* Non-NULL emitter object is expected. */
|
||||
|
||||
emitter->document = document;
|
||||
|
||||
if (!emitter->opened) {
|
||||
if (!yaml_emitter_open(emitter)) goto error;
|
||||
}
|
||||
|
||||
if (STACK_EMPTY(emitter, document->nodes)) {
|
||||
if (!yaml_emitter_close(emitter)) goto error;
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
return 1;
|
||||
}
|
||||
|
||||
assert(emitter->opened); /* Emitter should be opened. */
|
||||
|
||||
emitter->anchors = yaml_malloc(sizeof(*(emitter->anchors))
|
||||
* (document->nodes.top - document->nodes.start));
|
||||
if (!emitter->anchors) goto error;
|
||||
memset(emitter->anchors, 0, sizeof(*(emitter->anchors))
|
||||
* (document->nodes.top - document->nodes.start));
|
||||
|
||||
DOCUMENT_START_EVENT_INIT(event, document->version_directive,
|
||||
document->tag_directives.start, document->tag_directives.end,
|
||||
document->start_implicit, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) goto error;
|
||||
|
||||
yaml_emitter_anchor_node(emitter, 1);
|
||||
if (!yaml_emitter_dump_node(emitter, 1)) goto error;
|
||||
|
||||
DOCUMENT_END_EVENT_INIT(event, document->end_implicit, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) goto error;
|
||||
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up the emitter object after a document is dumped.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_delete_document_and_anchors(yaml_emitter_t *emitter)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (!emitter->anchors) {
|
||||
yaml_document_delete(emitter->document);
|
||||
emitter->document = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
for (index = 0; emitter->document->nodes.start + index
|
||||
< emitter->document->nodes.top; index ++) {
|
||||
yaml_node_t node = emitter->document->nodes.start[index];
|
||||
if (!emitter->anchors[index].serialized) {
|
||||
yaml_free(node.tag);
|
||||
if (node.type == YAML_SCALAR_NODE) {
|
||||
yaml_free(node.data.scalar.value);
|
||||
}
|
||||
}
|
||||
if (node.type == YAML_SEQUENCE_NODE) {
|
||||
STACK_DEL(emitter, node.data.sequence.items);
|
||||
}
|
||||
if (node.type == YAML_MAPPING_NODE) {
|
||||
STACK_DEL(emitter, node.data.mapping.pairs);
|
||||
}
|
||||
}
|
||||
|
||||
STACK_DEL(emitter, emitter->document->nodes);
|
||||
yaml_free(emitter->anchors);
|
||||
|
||||
emitter->anchors = NULL;
|
||||
emitter->last_anchor_id = 0;
|
||||
emitter->document = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the references of a node and assign the anchor id if needed.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_anchor_node(yaml_emitter_t *emitter, int index)
|
||||
{
|
||||
yaml_node_t *node = emitter->document->nodes.start + index - 1;
|
||||
yaml_node_item_t *item;
|
||||
yaml_node_pair_t *pair;
|
||||
|
||||
emitter->anchors[index-1].references ++;
|
||||
|
||||
if (emitter->anchors[index-1].references == 1) {
|
||||
switch (node->type) {
|
||||
case YAML_SEQUENCE_NODE:
|
||||
for (item = node->data.sequence.items.start;
|
||||
item < node->data.sequence.items.top; item ++) {
|
||||
yaml_emitter_anchor_node(emitter, *item);
|
||||
}
|
||||
break;
|
||||
case YAML_MAPPING_NODE:
|
||||
for (pair = node->data.mapping.pairs.start;
|
||||
pair < node->data.mapping.pairs.top; pair ++) {
|
||||
yaml_emitter_anchor_node(emitter, pair->key);
|
||||
yaml_emitter_anchor_node(emitter, pair->value);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
else if (emitter->anchors[index-1].references == 2) {
|
||||
emitter->anchors[index-1].anchor = (++ emitter->last_anchor_id);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a textual representation for an anchor.
|
||||
*/
|
||||
|
||||
#define ANCHOR_TEMPLATE "id%03d"
|
||||
#define ANCHOR_TEMPLATE_LENGTH 16
|
||||
|
||||
static yaml_char_t *
|
||||
yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id)
|
||||
{
|
||||
yaml_char_t *anchor = yaml_malloc(ANCHOR_TEMPLATE_LENGTH);
|
||||
|
||||
if (!anchor) return NULL;
|
||||
|
||||
sprintf((char *)anchor, ANCHOR_TEMPLATE, anchor_id);
|
||||
|
||||
return anchor;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_node(yaml_emitter_t *emitter, int index)
|
||||
{
|
||||
yaml_node_t *node = emitter->document->nodes.start + index - 1;
|
||||
int anchor_id = emitter->anchors[index-1].anchor;
|
||||
yaml_char_t *anchor = NULL;
|
||||
|
||||
if (anchor_id) {
|
||||
anchor = yaml_emitter_generate_anchor(emitter, anchor_id);
|
||||
if (!anchor) return 0;
|
||||
}
|
||||
|
||||
if (emitter->anchors[index-1].serialized) {
|
||||
return yaml_emitter_dump_alias(emitter, anchor);
|
||||
}
|
||||
|
||||
emitter->anchors[index-1].serialized = 1;
|
||||
|
||||
switch (node->type) {
|
||||
case YAML_SCALAR_NODE:
|
||||
return yaml_emitter_dump_scalar(emitter, node, anchor);
|
||||
case YAML_SEQUENCE_NODE:
|
||||
return yaml_emitter_dump_sequence(emitter, node, anchor);
|
||||
case YAML_MAPPING_NODE:
|
||||
return yaml_emitter_dump_mapping(emitter, node, anchor);
|
||||
default:
|
||||
assert(0); /* Could not happen. */
|
||||
break;
|
||||
}
|
||||
|
||||
return 0; /* Could not happen. */
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize an alias.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_alias(yaml_emitter_t *emitter, yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
ALIAS_EVENT_INIT(event, anchor, mark, mark);
|
||||
|
||||
return yaml_emitter_emit(emitter, &event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a scalar.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_scalar(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int plain_implicit = (strcmp((char *)node->tag,
|
||||
YAML_DEFAULT_SCALAR_TAG) == 0);
|
||||
int quoted_implicit = (strcmp((char *)node->tag,
|
||||
YAML_DEFAULT_SCALAR_TAG) == 0);
|
||||
|
||||
SCALAR_EVENT_INIT(event, anchor, node->tag, node->data.scalar.value,
|
||||
node->data.scalar.length, plain_implicit, quoted_implicit,
|
||||
node->data.scalar.style, mark, mark);
|
||||
|
||||
return yaml_emitter_emit(emitter, &event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a sequence.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_sequence(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_SEQUENCE_TAG) == 0);
|
||||
|
||||
yaml_node_item_t *item;
|
||||
|
||||
SEQUENCE_START_EVENT_INIT(event, anchor, node->tag, implicit,
|
||||
node->data.sequence.style, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
for (item = node->data.sequence.items.start;
|
||||
item < node->data.sequence.items.top; item ++) {
|
||||
if (!yaml_emitter_dump_node(emitter, *item)) return 0;
|
||||
}
|
||||
|
||||
SEQUENCE_END_EVENT_INIT(event, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a mapping.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_MAPPING_TAG) == 0);
|
||||
|
||||
yaml_node_pair_t *pair;
|
||||
|
||||
MAPPING_START_EVENT_INIT(event, anchor, node->tag, implicit,
|
||||
node->data.mapping.style, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
for (pair = node->data.mapping.pairs.start;
|
||||
pair < node->data.mapping.pairs.top; pair ++) {
|
||||
if (!yaml_emitter_dump_node(emitter, pair->key)) return 0;
|
||||
if (!yaml_emitter_dump_node(emitter, pair->value)) return 0;
|
||||
}
|
||||
|
||||
MAPPING_END_EVENT_INIT(event, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,430 +0,0 @@
|
||||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* API functions.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document);
|
||||
|
||||
/*
|
||||
* Error handling.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error(yaml_parser_t *parser,
|
||||
const char *problem, yaml_mark_t problem_mark);
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error_context(yaml_parser_t *parser,
|
||||
const char *context, yaml_mark_t context_mark,
|
||||
const char *problem, yaml_mark_t problem_mark);
|
||||
|
||||
|
||||
/*
|
||||
* Alias handling.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_register_anchor(yaml_parser_t *parser,
|
||||
int index, yaml_char_t *anchor);
|
||||
|
||||
/*
|
||||
* Clean up functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_parser_delete_aliases(yaml_parser_t *parser);
|
||||
|
||||
/*
|
||||
* Composer functions.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
/*
|
||||
* Load the next document of the stream.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document)
|
||||
{
|
||||
yaml_event_t event;
|
||||
|
||||
assert(parser); /* Non-NULL parser object is expected. */
|
||||
assert(document); /* Non-NULL document object is expected. */
|
||||
|
||||
memset(document, 0, sizeof(yaml_document_t));
|
||||
if (!STACK_INIT(parser, document->nodes, INITIAL_STACK_SIZE))
|
||||
goto error;
|
||||
|
||||
if (!parser->stream_start_produced) {
|
||||
if (!yaml_parser_parse(parser, &event)) goto error;
|
||||
assert(event.type == YAML_STREAM_START_EVENT);
|
||||
/* STREAM-START is expected. */
|
||||
}
|
||||
|
||||
if (parser->stream_end_produced) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) goto error;
|
||||
if (event.type == YAML_STREAM_END_EVENT) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, parser->aliases, INITIAL_STACK_SIZE))
|
||||
goto error;
|
||||
|
||||
parser->document = document;
|
||||
|
||||
if (!yaml_parser_load_document(parser, &event)) goto error;
|
||||
|
||||
yaml_parser_delete_aliases(parser);
|
||||
parser->document = NULL;
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
|
||||
yaml_parser_delete_aliases(parser);
|
||||
yaml_document_delete(document);
|
||||
parser->document = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set composer error.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error(yaml_parser_t *parser,
|
||||
const char *problem, yaml_mark_t problem_mark)
|
||||
{
|
||||
parser->error = YAML_COMPOSER_ERROR;
|
||||
parser->problem = problem;
|
||||
parser->problem_mark = problem_mark;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set composer error with context.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error_context(yaml_parser_t *parser,
|
||||
const char *context, yaml_mark_t context_mark,
|
||||
const char *problem, yaml_mark_t problem_mark)
|
||||
{
|
||||
parser->error = YAML_COMPOSER_ERROR;
|
||||
parser->context = context;
|
||||
parser->context_mark = context_mark;
|
||||
parser->problem = problem;
|
||||
parser->problem_mark = problem_mark;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete the stack of aliases.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_parser_delete_aliases(yaml_parser_t *parser)
|
||||
{
|
||||
while (!STACK_EMPTY(parser, parser->aliases)) {
|
||||
yaml_free(POP(parser, parser->aliases).anchor);
|
||||
}
|
||||
STACK_DEL(parser, parser->aliases);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a document object.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
|
||||
assert(first_event->type == YAML_DOCUMENT_START_EVENT);
|
||||
/* DOCUMENT-START is expected. */
|
||||
|
||||
parser->document->version_directive
|
||||
= first_event->data.document_start.version_directive;
|
||||
parser->document->tag_directives.start
|
||||
= first_event->data.document_start.tag_directives.start;
|
||||
parser->document->tag_directives.end
|
||||
= first_event->data.document_start.tag_directives.end;
|
||||
parser->document->start_implicit
|
||||
= first_event->data.document_start.implicit;
|
||||
parser->document->start_mark = first_event->start_mark;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
if (!yaml_parser_load_node(parser, &event)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
assert(event.type == YAML_DOCUMENT_END_EVENT);
|
||||
/* DOCUMENT-END is expected. */
|
||||
|
||||
parser->document->end_implicit = event.data.document_end.implicit;
|
||||
parser->document->end_mark = event.end_mark;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
switch (first_event->type) {
|
||||
case YAML_ALIAS_EVENT:
|
||||
return yaml_parser_load_alias(parser, first_event);
|
||||
case YAML_SCALAR_EVENT:
|
||||
return yaml_parser_load_scalar(parser, first_event);
|
||||
case YAML_SEQUENCE_START_EVENT:
|
||||
return yaml_parser_load_sequence(parser, first_event);
|
||||
case YAML_MAPPING_START_EVENT:
|
||||
return yaml_parser_load_mapping(parser, first_event);
|
||||
default:
|
||||
assert(0); /* Could not happen. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add an anchor.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_register_anchor(yaml_parser_t *parser,
|
||||
int index, yaml_char_t *anchor)
|
||||
{
|
||||
yaml_alias_data_t data = { anchor, index, { 0, 0, 0 } };
|
||||
yaml_alias_data_t *alias_data;
|
||||
|
||||
if (!anchor) return 1;
|
||||
|
||||
data.mark = parser->document->nodes.start[index-1].start_mark;
|
||||
|
||||
for (alias_data = parser->aliases.start;
|
||||
alias_data != parser->aliases.top; alias_data ++) {
|
||||
if (strcmp((char *)alias_data->anchor, (char *)anchor) == 0) {
|
||||
yaml_free(anchor);
|
||||
return yaml_parser_set_composer_error_context(parser,
|
||||
"found duplicate anchor; first occurence",
|
||||
alias_data->mark, "second occurence", data.mark);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PUSH(parser, parser->aliases, data)) {
|
||||
yaml_free(anchor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a node corresponding to an alias.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_char_t *anchor = first_event->data.alias.anchor;
|
||||
yaml_alias_data_t *alias_data;
|
||||
|
||||
for (alias_data = parser->aliases.start;
|
||||
alias_data != parser->aliases.top; alias_data ++) {
|
||||
if (strcmp((char *)alias_data->anchor, (char *)anchor) == 0) {
|
||||
yaml_free(anchor);
|
||||
return alias_data->index;
|
||||
}
|
||||
}
|
||||
|
||||
yaml_free(anchor);
|
||||
return yaml_parser_set_composer_error(parser, "found undefined alias",
|
||||
first_event->start_mark);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a scalar node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_node_t node;
|
||||
int index;
|
||||
yaml_char_t *tag = first_event->data.scalar.tag;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SCALAR_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
SCALAR_NODE_INIT(node, tag, first_event->data.scalar.value,
|
||||
first_event->data.scalar.length, first_event->data.scalar.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.scalar.anchor)) return 0;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.scalar.anchor);
|
||||
yaml_free(first_event->data.scalar.value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a sequence node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_node_t node;
|
||||
struct {
|
||||
yaml_node_item_t *start;
|
||||
yaml_node_item_t *end;
|
||||
yaml_node_item_t *top;
|
||||
} items = { NULL, NULL, NULL };
|
||||
int index, item_index;
|
||||
yaml_char_t *tag = first_event->data.sequence_start.tag;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, items, INITIAL_STACK_SIZE)) goto error;
|
||||
|
||||
SEQUENCE_NODE_INIT(node, tag, items.start, items.end,
|
||||
first_event->data.sequence_start.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.sequence_start.anchor)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
while (event.type != YAML_SEQUENCE_END_EVENT) {
|
||||
item_index = yaml_parser_load_node(parser, &event);
|
||||
if (!item_index) return 0;
|
||||
if (!PUSH(parser,
|
||||
parser->document->nodes.start[index-1].data.sequence.items,
|
||||
item_index)) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
}
|
||||
|
||||
parser->document->nodes.start[index-1].end_mark = event.end_mark;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.sequence_start.anchor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a mapping node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_node_t node;
|
||||
struct {
|
||||
yaml_node_pair_t *start;
|
||||
yaml_node_pair_t *end;
|
||||
yaml_node_pair_t *top;
|
||||
} pairs = { NULL, NULL, NULL };
|
||||
int index;
|
||||
yaml_node_pair_t pair;
|
||||
yaml_char_t *tag = first_event->data.mapping_start.tag;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_MAPPING_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, pairs, INITIAL_STACK_SIZE)) goto error;
|
||||
|
||||
MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end,
|
||||
first_event->data.mapping_start.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.mapping_start.anchor)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
while (event.type != YAML_MAPPING_END_EVENT) {
|
||||
pair.key = yaml_parser_load_node(parser, &event);
|
||||
if (!pair.key) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
pair.value = yaml_parser_load_node(parser, &event);
|
||||
if (!pair.value) return 0;
|
||||
if (!PUSH(parser,
|
||||
parser->document->nodes.start[index-1].data.mapping.pairs,
|
||||
pair)) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
}
|
||||
|
||||
parser->document->nodes.start[index-1].end_mark = event.end_mark;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.mapping_start.anchor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,460 +0,0 @@
|
||||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* Declarations.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem,
|
||||
size_t offset, int value);
|
||||
|
||||
static int
|
||||
yaml_parser_update_raw_buffer(yaml_parser_t *parser);
|
||||
|
||||
static int
|
||||
yaml_parser_determine_encoding(yaml_parser_t *parser);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
|
||||
|
||||
/*
|
||||
* Set the reader error and return 0.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem,
|
||||
size_t offset, int value)
|
||||
{
|
||||
parser->error = YAML_READER_ERROR;
|
||||
parser->problem = problem;
|
||||
parser->problem_offset = offset;
|
||||
parser->problem_value = value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Byte order marks.
|
||||
*/
|
||||
|
||||
#define BOM_UTF8 "\xef\xbb\xbf"
|
||||
#define BOM_UTF16LE "\xff\xfe"
|
||||
#define BOM_UTF16BE "\xfe\xff"
|
||||
|
||||
/*
|
||||
* Determine the input stream encoding by checking the BOM symbol. If no BOM is
|
||||
* found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_determine_encoding(yaml_parser_t *parser)
|
||||
{
|
||||
/* Ensure that we had enough bytes in the raw buffer. */
|
||||
|
||||
while (!parser->eof
|
||||
&& parser->raw_buffer.last - parser->raw_buffer.pointer < 3) {
|
||||
if (!yaml_parser_update_raw_buffer(parser)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Determine the encoding. */
|
||||
|
||||
if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 2
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF16LE, 2)) {
|
||||
parser->encoding = YAML_UTF16LE_ENCODING;
|
||||
parser->raw_buffer.pointer += 2;
|
||||
parser->offset += 2;
|
||||
}
|
||||
else if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 2
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF16BE, 2)) {
|
||||
parser->encoding = YAML_UTF16BE_ENCODING;
|
||||
parser->raw_buffer.pointer += 2;
|
||||
parser->offset += 2;
|
||||
}
|
||||
else if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 3
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF8, 3)) {
|
||||
parser->encoding = YAML_UTF8_ENCODING;
|
||||
parser->raw_buffer.pointer += 3;
|
||||
parser->offset += 3;
|
||||
}
|
||||
else {
|
||||
parser->encoding = YAML_UTF8_ENCODING;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update the raw buffer.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_update_raw_buffer(yaml_parser_t *parser)
|
||||
{
|
||||
size_t size_read = 0;
|
||||
|
||||
/* Return if the raw buffer is full. */
|
||||
|
||||
if (parser->raw_buffer.start == parser->raw_buffer.pointer
|
||||
&& parser->raw_buffer.last == parser->raw_buffer.end)
|
||||
return 1;
|
||||
|
||||
/* Return on EOF. */
|
||||
|
||||
if (parser->eof) return 1;
|
||||
|
||||
/* Move the remaining bytes in the raw buffer to the beginning. */
|
||||
|
||||
if (parser->raw_buffer.start < parser->raw_buffer.pointer
|
||||
&& parser->raw_buffer.pointer < parser->raw_buffer.last) {
|
||||
memmove(parser->raw_buffer.start, parser->raw_buffer.pointer,
|
||||
parser->raw_buffer.last - parser->raw_buffer.pointer);
|
||||
}
|
||||
parser->raw_buffer.last -=
|
||||
parser->raw_buffer.pointer - parser->raw_buffer.start;
|
||||
parser->raw_buffer.pointer = parser->raw_buffer.start;
|
||||
|
||||
/* Call the read handler to fill the buffer. */
|
||||
|
||||
if (!parser->read_handler(parser->read_handler_data, parser->raw_buffer.last,
|
||||
parser->raw_buffer.end - parser->raw_buffer.last, &size_read)) {
|
||||
return yaml_parser_set_reader_error(parser, "Input error",
|
||||
parser->offset, -1);
|
||||
}
|
||||
parser->raw_buffer.last += size_read;
|
||||
if (!size_read) {
|
||||
parser->eof = 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure that the buffer contains at least `length` characters.
|
||||
* Return 1 on success, 0 on failure.
|
||||
*
|
||||
* The length is supposed to be significantly less that the buffer size.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length)
|
||||
{
|
||||
assert(parser->read_handler); /* Read handler must be set. */
|
||||
|
||||
/* If the EOF flag is set and the raw buffer is empty, do nothing. */
|
||||
|
||||
if (parser->eof && parser->raw_buffer.pointer == parser->raw_buffer.last)
|
||||
return 1;
|
||||
|
||||
/* Return if the buffer contains enough characters. */
|
||||
|
||||
if (parser->unread >= length)
|
||||
return 1;
|
||||
|
||||
/* Determine the input encoding if it is not known yet. */
|
||||
|
||||
if (!parser->encoding) {
|
||||
if (!yaml_parser_determine_encoding(parser))
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Move the unread characters to the beginning of the buffer. */
|
||||
|
||||
if (parser->buffer.start < parser->buffer.pointer
|
||||
&& parser->buffer.pointer < parser->buffer.last) {
|
||||
size_t size = parser->buffer.last - parser->buffer.pointer;
|
||||
memmove(parser->buffer.start, parser->buffer.pointer, size);
|
||||
parser->buffer.pointer = parser->buffer.start;
|
||||
parser->buffer.last = parser->buffer.start + size;
|
||||
}
|
||||
else if (parser->buffer.pointer == parser->buffer.last) {
|
||||
parser->buffer.pointer = parser->buffer.start;
|
||||
parser->buffer.last = parser->buffer.start;
|
||||
}
|
||||
|
||||
/* Fill the buffer until it has enough characters. */
|
||||
|
||||
while (parser->unread < length)
|
||||
{
|
||||
/* Fill the raw buffer. */
|
||||
|
||||
if (!yaml_parser_update_raw_buffer(parser)) return 0;
|
||||
|
||||
/* Decode the raw buffer. */
|
||||
|
||||
while (parser->raw_buffer.pointer != parser->raw_buffer.last)
|
||||
{
|
||||
unsigned int value = 0, value2 = 0;
|
||||
int incomplete = 0;
|
||||
unsigned char octet;
|
||||
unsigned int width = 0;
|
||||
int low, high;
|
||||
size_t k;
|
||||
size_t raw_unread = parser->raw_buffer.last - parser->raw_buffer.pointer;
|
||||
|
||||
/* Decode the next character. */
|
||||
|
||||
switch (parser->encoding)
|
||||
{
|
||||
case YAML_UTF8_ENCODING:
|
||||
|
||||
/*
|
||||
* Decode a UTF-8 character. Check RFC 3629
|
||||
* (http://www.ietf.org/rfc/rfc3629.txt) for more details.
|
||||
*
|
||||
* The following table (taken from the RFC) is used for
|
||||
* decoding.
|
||||
*
|
||||
* Char. number range | UTF-8 octet sequence
|
||||
* (hexadecimal) | (binary)
|
||||
* --------------------+------------------------------------
|
||||
* 0000 0000-0000 007F | 0xxxxxxx
|
||||
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
|
||||
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
|
||||
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
*
|
||||
* Additionally, the characters in the range 0xD800-0xDFFF
|
||||
* are prohibited as they are reserved for use with UTF-16
|
||||
* surrogate pairs.
|
||||
*/
|
||||
|
||||
/* Determine the length of the UTF-8 sequence. */
|
||||
|
||||
octet = parser->raw_buffer.pointer[0];
|
||||
width = (octet & 0x80) == 0x00 ? 1 :
|
||||
(octet & 0xE0) == 0xC0 ? 2 :
|
||||
(octet & 0xF0) == 0xE0 ? 3 :
|
||||
(octet & 0xF8) == 0xF0 ? 4 : 0;
|
||||
|
||||
/* Check if the leading octet is valid. */
|
||||
|
||||
if (!width)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Invalid leading UTF-8 octet",
|
||||
parser->offset, octet);
|
||||
|
||||
/* Check if the raw buffer contains an incomplete character. */
|
||||
|
||||
if (width > raw_unread) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Incomplete UTF-8 octet sequence",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Decode the leading octet. */
|
||||
|
||||
value = (octet & 0x80) == 0x00 ? octet & 0x7F :
|
||||
(octet & 0xE0) == 0xC0 ? octet & 0x1F :
|
||||
(octet & 0xF0) == 0xE0 ? octet & 0x0F :
|
||||
(octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
|
||||
|
||||
/* Check and decode the trailing octets. */
|
||||
|
||||
for (k = 1; k < width; k ++)
|
||||
{
|
||||
octet = parser->raw_buffer.pointer[k];
|
||||
|
||||
/* Check if the octet is valid. */
|
||||
|
||||
if ((octet & 0xC0) != 0x80)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Invalid trailing UTF-8 octet",
|
||||
parser->offset+k, octet);
|
||||
|
||||
/* Decode the octet. */
|
||||
|
||||
value = (value << 6) + (octet & 0x3F);
|
||||
}
|
||||
|
||||
/* Check the length of the sequence against the value. */
|
||||
|
||||
if (!((width == 1) ||
|
||||
(width == 2 && value >= 0x80) ||
|
||||
(width == 3 && value >= 0x800) ||
|
||||
(width == 4 && value >= 0x10000)))
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Invalid length of a UTF-8 sequence",
|
||||
parser->offset, -1);
|
||||
|
||||
/* Check the range of the value. */
|
||||
|
||||
if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Invalid Unicode character",
|
||||
parser->offset, value);
|
||||
|
||||
break;
|
||||
|
||||
case YAML_UTF16LE_ENCODING:
|
||||
case YAML_UTF16BE_ENCODING:
|
||||
|
||||
low = (parser->encoding == YAML_UTF16LE_ENCODING ? 0 : 1);
|
||||
high = (parser->encoding == YAML_UTF16LE_ENCODING ? 1 : 0);
|
||||
|
||||
/*
|
||||
* The UTF-16 encoding is not as simple as one might
|
||||
* naively think. Check RFC 2781
|
||||
* (http://www.ietf.org/rfc/rfc2781.txt).
|
||||
*
|
||||
* Normally, two subsequent bytes describe a Unicode
|
||||
* character. However a special technique (called a
|
||||
* surrogate pair) is used for specifying character
|
||||
* values larger than 0xFFFF.
|
||||
*
|
||||
* A surrogate pair consists of two pseudo-characters:
|
||||
* high surrogate area (0xD800-0xDBFF)
|
||||
* low surrogate area (0xDC00-0xDFFF)
|
||||
*
|
||||
* The following formulas are used for decoding
|
||||
* and encoding characters using surrogate pairs:
|
||||
*
|
||||
* U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF)
|
||||
* U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF)
|
||||
* W1 = 110110yyyyyyyyyy
|
||||
* W2 = 110111xxxxxxxxxx
|
||||
*
|
||||
* where U is the character value, W1 is the high surrogate
|
||||
* area, W2 is the low surrogate area.
|
||||
*/
|
||||
|
||||
/* Check for incomplete UTF-16 character. */
|
||||
|
||||
if (raw_unread < 2) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Incomplete UTF-16 character",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Get the character. */
|
||||
|
||||
value = parser->raw_buffer.pointer[low]
|
||||
+ (parser->raw_buffer.pointer[high] << 8);
|
||||
|
||||
/* Check for unexpected low surrogate area. */
|
||||
|
||||
if ((value & 0xFC00) == 0xDC00)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Unexpected low surrogate area",
|
||||
parser->offset, value);
|
||||
|
||||
/* Check for a high surrogate area. */
|
||||
|
||||
if ((value & 0xFC00) == 0xD800) {
|
||||
|
||||
width = 4;
|
||||
|
||||
/* Check for incomplete surrogate pair. */
|
||||
|
||||
if (raw_unread < 4) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Incomplete UTF-16 surrogate pair",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Get the next character. */
|
||||
|
||||
value2 = parser->raw_buffer.pointer[low+2]
|
||||
+ (parser->raw_buffer.pointer[high+2] << 8);
|
||||
|
||||
/* Check for a low surrogate area. */
|
||||
|
||||
if ((value2 & 0xFC00) != 0xDC00)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Expected low surrogate area",
|
||||
parser->offset+2, value2);
|
||||
|
||||
/* Generate the value of the surrogate pair. */
|
||||
|
||||
value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF);
|
||||
}
|
||||
|
||||
else {
|
||||
width = 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(1); /* Impossible. */
|
||||
}
|
||||
|
||||
/* Check if the raw buffer contains enough bytes to form a character. */
|
||||
|
||||
if (incomplete) break;
|
||||
|
||||
/*
|
||||
* Check if the character is in the allowed range:
|
||||
* #x9 | #xA | #xD | [#x20-#x7E] (8 bit)
|
||||
* | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit)
|
||||
* | [#x10000-#x10FFFF] (32 bit)
|
||||
*/
|
||||
|
||||
if (! (value == 0x09 || value == 0x0A || value == 0x0D
|
||||
|| (value >= 0x20 && value <= 0x7E)
|
||||
|| (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF)
|
||||
|| (value >= 0xE000 && value <= 0xFFFD)
|
||||
|| (value >= 0x10000 && value <= 0x10FFFF)))
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"Control characters are not allowed",
|
||||
parser->offset, value);
|
||||
|
||||
/* Move the raw pointers. */
|
||||
|
||||
parser->raw_buffer.pointer += width;
|
||||
parser->offset += width;
|
||||
|
||||
/* Finally put the character into the buffer. */
|
||||
|
||||
/* 0000 0000-0000 007F -> 0xxxxxxx */
|
||||
if (value <= 0x7F) {
|
||||
*(parser->buffer.last++) = value;
|
||||
}
|
||||
/* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */
|
||||
else if (value <= 0x7FF) {
|
||||
*(parser->buffer.last++) = 0xC0 + (value >> 6);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
/* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */
|
||||
else if (value <= 0xFFFF) {
|
||||
*(parser->buffer.last++) = 0xE0 + (value >> 12);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 6) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
/* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
|
||||
else {
|
||||
*(parser->buffer.last++) = 0xF0 + (value >> 18);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 12) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 6) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
|
||||
parser->unread ++;
|
||||
}
|
||||
|
||||
/* On EOF, put NUL into the buffer and return. */
|
||||
|
||||
if (parser->eof) {
|
||||
*(parser->buffer.last++) = '\0';
|
||||
parser->unread ++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,141 +0,0 @@
|
||||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* Declarations.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_flush(yaml_emitter_t *emitter);
|
||||
|
||||
/*
|
||||
* Set the writer error and return 0.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem)
|
||||
{
|
||||
emitter->error = YAML_WRITER_ERROR;
|
||||
emitter->problem = problem;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Flush the output buffer.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_flush(yaml_emitter_t *emitter)
|
||||
{
|
||||
int low, high;
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is expected. */
|
||||
assert(emitter->write_handler); /* Write handler must be set. */
|
||||
assert(emitter->encoding); /* Output encoding must be set. */
|
||||
|
||||
emitter->buffer.last = emitter->buffer.pointer;
|
||||
emitter->buffer.pointer = emitter->buffer.start;
|
||||
|
||||
/* Check if the buffer is empty. */
|
||||
|
||||
if (emitter->buffer.start == emitter->buffer.last) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* If the output encoding is UTF-8, we don't need to recode the buffer. */
|
||||
|
||||
if (emitter->encoding == YAML_UTF8_ENCODING)
|
||||
{
|
||||
if (emitter->write_handler(emitter->write_handler_data,
|
||||
emitter->buffer.start,
|
||||
emitter->buffer.last - emitter->buffer.start)) {
|
||||
emitter->buffer.last = emitter->buffer.start;
|
||||
emitter->buffer.pointer = emitter->buffer.start;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return yaml_emitter_set_writer_error(emitter, "Write error");
|
||||
}
|
||||
}
|
||||
|
||||
/* Recode the buffer into the raw buffer. */
|
||||
|
||||
low = (emitter->encoding == YAML_UTF16LE_ENCODING ? 0 : 1);
|
||||
high = (emitter->encoding == YAML_UTF16LE_ENCODING ? 1 : 0);
|
||||
|
||||
while (emitter->buffer.pointer != emitter->buffer.last)
|
||||
{
|
||||
unsigned char octet;
|
||||
unsigned int width;
|
||||
unsigned int value;
|
||||
size_t k;
|
||||
|
||||
/*
|
||||
* See the "reader.c" code for more details on UTF-8 encoding. Note
|
||||
* that we assume that the buffer contains a valid UTF-8 sequence.
|
||||
*/
|
||||
|
||||
/* Read the next UTF-8 character. */
|
||||
|
||||
octet = emitter->buffer.pointer[0];
|
||||
|
||||
width = (octet & 0x80) == 0x00 ? 1 :
|
||||
(octet & 0xE0) == 0xC0 ? 2 :
|
||||
(octet & 0xF0) == 0xE0 ? 3 :
|
||||
(octet & 0xF8) == 0xF0 ? 4 : 0;
|
||||
|
||||
value = (octet & 0x80) == 0x00 ? octet & 0x7F :
|
||||
(octet & 0xE0) == 0xC0 ? octet & 0x1F :
|
||||
(octet & 0xF0) == 0xE0 ? octet & 0x0F :
|
||||
(octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
|
||||
|
||||
for (k = 1; k < width; k ++) {
|
||||
octet = emitter->buffer.pointer[k];
|
||||
value = (value << 6) + (octet & 0x3F);
|
||||
}
|
||||
|
||||
emitter->buffer.pointer += width;
|
||||
|
||||
/* Write the character. */
|
||||
|
||||
if (value < 0x10000)
|
||||
{
|
||||
emitter->raw_buffer.last[high] = value >> 8;
|
||||
emitter->raw_buffer.last[low] = value & 0xFF;
|
||||
|
||||
emitter->raw_buffer.last += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Write the character using a surrogate pair (check "reader.c"). */
|
||||
|
||||
value -= 0x10000;
|
||||
emitter->raw_buffer.last[high] = 0xD8 + (value >> 18);
|
||||
emitter->raw_buffer.last[low] = (value >> 10) & 0xFF;
|
||||
emitter->raw_buffer.last[high+2] = 0xDC + ((value >> 8) & 0xFF);
|
||||
emitter->raw_buffer.last[low+2] = value & 0xFF;
|
||||
|
||||
emitter->raw_buffer.last += 4;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write the raw buffer. */
|
||||
|
||||
if (emitter->write_handler(emitter->write_handler_data,
|
||||
emitter->raw_buffer.start,
|
||||
emitter->raw_buffer.last - emitter->raw_buffer.start)) {
|
||||
emitter->buffer.last = emitter->buffer.start;
|
||||
emitter->buffer.pointer = emitter->buffer.start;
|
||||
emitter->raw_buffer.last = emitter->raw_buffer.start;
|
||||
emitter->raw_buffer.pointer = emitter->raw_buffer.start;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return yaml_emitter_set_writer_error(emitter, "Write error");
|
||||
}
|
||||
}
|
||||
|
||||
1973
yaml/libyaml/yaml.h
1973
yaml/libyaml/yaml.h
File diff suppressed because it is too large
Load Diff
@ -1,639 +0,0 @@
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <yaml.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
|
||||
/*
|
||||
* Memory management.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(void *)
|
||||
yaml_malloc(size_t size);
|
||||
|
||||
YAML_DECLARE(void *)
|
||||
yaml_realloc(void *ptr, size_t size);
|
||||
|
||||
YAML_DECLARE(void)
|
||||
yaml_free(void *ptr);
|
||||
|
||||
YAML_DECLARE(yaml_char_t *)
|
||||
yaml_strdup(const yaml_char_t *);
|
||||
|
||||
/*
|
||||
* Reader: Ensure that the buffer contains at least `length` characters.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
|
||||
|
||||
/*
|
||||
* Scanner: Ensure that the token stack contains at least one token ready.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
|
||||
|
||||
/*
|
||||
* The size of the input raw buffer.
|
||||
*/
|
||||
|
||||
#define INPUT_RAW_BUFFER_SIZE 16384
|
||||
|
||||
/*
|
||||
* The size of the input buffer.
|
||||
*
|
||||
* It should be possible to decode the whole raw buffer.
|
||||
*/
|
||||
|
||||
#define INPUT_BUFFER_SIZE (INPUT_RAW_BUFFER_SIZE*3)
|
||||
|
||||
/*
|
||||
* The size of the output buffer.
|
||||
*/
|
||||
|
||||
#define OUTPUT_BUFFER_SIZE 16384
|
||||
|
||||
/*
|
||||
* The size of the output raw buffer.
|
||||
*
|
||||
* It should be possible to encode the whole output buffer.
|
||||
*/
|
||||
|
||||
#define OUTPUT_RAW_BUFFER_SIZE (OUTPUT_BUFFER_SIZE*2+2)
|
||||
|
||||
/*
|
||||
* The size of other stacks and queues.
|
||||
*/
|
||||
|
||||
#define INITIAL_STACK_SIZE 16
|
||||
#define INITIAL_QUEUE_SIZE 16
|
||||
#define INITIAL_STRING_SIZE 16
|
||||
|
||||
/*
|
||||
* Buffer management.
|
||||
*/
|
||||
|
||||
#define BUFFER_INIT(context,buffer,size) \
|
||||
(((buffer).start = yaml_malloc(size)) ? \
|
||||
((buffer).last = (buffer).pointer = (buffer).start, \
|
||||
(buffer).end = (buffer).start+(size), \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define BUFFER_DEL(context,buffer) \
|
||||
(yaml_free((buffer).start), \
|
||||
(buffer).start = (buffer).pointer = (buffer).end = 0)
|
||||
|
||||
/*
|
||||
* String management.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
yaml_char_t *start;
|
||||
yaml_char_t *end;
|
||||
yaml_char_t *pointer;
|
||||
} yaml_string_t;
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_string_extend(yaml_char_t **start,
|
||||
yaml_char_t **pointer, yaml_char_t **end);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_string_join(
|
||||
yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
|
||||
yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end);
|
||||
|
||||
#define NULL_STRING { NULL, NULL, NULL }
|
||||
|
||||
#define STRING(string,length) { (string), (string)+(length), (string) }
|
||||
|
||||
#define STRING_INIT(context,string,size) \
|
||||
(((string).start = yaml_malloc(size)) ? \
|
||||
((string).pointer = (string).start, \
|
||||
(string).end = (string).start+(size), \
|
||||
memset((string).start, 0, (size)), \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define STRING_DEL(context,string) \
|
||||
(yaml_free((string).start), \
|
||||
(string).start = (string).pointer = (string).end = 0)
|
||||
|
||||
#define STRING_EXTEND(context,string) \
|
||||
(((string).pointer+5 < (string).end) \
|
||||
|| yaml_string_extend(&(string).start, \
|
||||
&(string).pointer, &(string).end))
|
||||
|
||||
#define CLEAR(context,string) \
|
||||
((string).pointer = (string).start, \
|
||||
memset((string).start, 0, (string).end-(string).start))
|
||||
|
||||
#define JOIN(context,string_a,string_b) \
|
||||
((yaml_string_join(&(string_a).start, &(string_a).pointer, \
|
||||
&(string_a).end, &(string_b).start, \
|
||||
&(string_b).pointer, &(string_b).end)) ? \
|
||||
((string_b).pointer = (string_b).start, \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
/*
|
||||
* String check operations.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Check the octet at the specified position.
|
||||
*/
|
||||
|
||||
#define CHECK_AT(string,octet,offset) \
|
||||
((string).pointer[offset] == (yaml_char_t)(octet))
|
||||
|
||||
/*
|
||||
* Check the current octet in the buffer.
|
||||
*/
|
||||
|
||||
#define CHECK(string,octet) CHECK_AT((string),(octet),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is an alphabetical
|
||||
* character, a digit, '_', or '-'.
|
||||
*/
|
||||
|
||||
#define IS_ALPHA_AT(string,offset) \
|
||||
(((string).pointer[offset] >= (yaml_char_t) '0' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) '9') || \
|
||||
((string).pointer[offset] >= (yaml_char_t) 'A' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'Z') || \
|
||||
((string).pointer[offset] >= (yaml_char_t) 'a' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'z') || \
|
||||
(string).pointer[offset] == '_' || \
|
||||
(string).pointer[offset] == '-')
|
||||
|
||||
#define IS_ALPHA(string) IS_ALPHA_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is a digit.
|
||||
*/
|
||||
|
||||
#define IS_DIGIT_AT(string,offset) \
|
||||
(((string).pointer[offset] >= (yaml_char_t) '0' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) '9'))
|
||||
|
||||
#define IS_DIGIT(string) IS_DIGIT_AT((string),0)
|
||||
|
||||
/*
|
||||
* Get the value of a digit.
|
||||
*/
|
||||
|
||||
#define AS_DIGIT_AT(string,offset) \
|
||||
((string).pointer[offset] - (yaml_char_t) '0')
|
||||
|
||||
#define AS_DIGIT(string) AS_DIGIT_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is a hex-digit.
|
||||
*/
|
||||
|
||||
#define IS_HEX_AT(string,offset) \
|
||||
(((string).pointer[offset] >= (yaml_char_t) '0' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) '9') || \
|
||||
((string).pointer[offset] >= (yaml_char_t) 'A' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'F') || \
|
||||
((string).pointer[offset] >= (yaml_char_t) 'a' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'f'))
|
||||
|
||||
#define IS_HEX(string) IS_HEX_AT((string),0)
|
||||
|
||||
/*
|
||||
* Get the value of a hex-digit.
|
||||
*/
|
||||
|
||||
#define AS_HEX_AT(string,offset) \
|
||||
(((string).pointer[offset] >= (yaml_char_t) 'A' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'F') ? \
|
||||
((string).pointer[offset] - (yaml_char_t) 'A' + 10) : \
|
||||
((string).pointer[offset] >= (yaml_char_t) 'a' && \
|
||||
(string).pointer[offset] <= (yaml_char_t) 'f') ? \
|
||||
((string).pointer[offset] - (yaml_char_t) 'a' + 10) : \
|
||||
((string).pointer[offset] - (yaml_char_t) '0'))
|
||||
|
||||
#define AS_HEX(string) AS_HEX_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character is ASCII.
|
||||
*/
|
||||
|
||||
#define IS_ASCII_AT(string,offset) \
|
||||
((string).pointer[offset] <= (yaml_char_t) '\x7F')
|
||||
|
||||
#define IS_ASCII(string) IS_ASCII_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character can be printed unescaped.
|
||||
*/
|
||||
|
||||
#define IS_PRINTABLE_AT(string,offset) \
|
||||
(((string).pointer[offset] == 0x0A) /* . == #x0A */ \
|
||||
|| ((string).pointer[offset] >= 0x20 /* #x20 <= . <= #x7E */ \
|
||||
&& (string).pointer[offset] <= 0x7E) \
|
||||
|| ((string).pointer[offset] == 0xC2 /* #0xA0 <= . <= #xD7FF */ \
|
||||
&& (string).pointer[offset+1] >= 0xA0) \
|
||||
|| ((string).pointer[offset] > 0xC2 \
|
||||
&& (string).pointer[offset] < 0xED) \
|
||||
|| ((string).pointer[offset] == 0xED \
|
||||
&& (string).pointer[offset+1] < 0xA0) \
|
||||
|| ((string).pointer[offset] == 0xEE) \
|
||||
|| ((string).pointer[offset] == 0xEF /* #xE000 <= . <= #xFFFD */ \
|
||||
&& !((string).pointer[offset+1] == 0xBB /* && . != #xFEFF */ \
|
||||
&& (string).pointer[offset+2] == 0xBF) \
|
||||
&& !((string).pointer[offset+1] == 0xBF \
|
||||
&& ((string).pointer[offset+2] == 0xBE \
|
||||
|| (string).pointer[offset+2] == 0xBF))))
|
||||
|
||||
#define IS_PRINTABLE(string) IS_PRINTABLE_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is NUL.
|
||||
*/
|
||||
|
||||
#define IS_Z_AT(string,offset) CHECK_AT((string),'\0',(offset))
|
||||
|
||||
#define IS_Z(string) IS_Z_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is BOM.
|
||||
*/
|
||||
|
||||
#define IS_BOM_AT(string,offset) \
|
||||
(CHECK_AT((string),'\xEF',(offset)) \
|
||||
&& CHECK_AT((string),'\xBB',(offset)+1) \
|
||||
&& CHECK_AT((string),'\xBF',(offset)+2)) /* BOM (#xFEFF) */
|
||||
|
||||
#define IS_BOM(string) IS_BOM_AT(string,0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is space.
|
||||
*/
|
||||
|
||||
#define IS_SPACE_AT(string,offset) CHECK_AT((string),' ',(offset))
|
||||
|
||||
#define IS_SPACE(string) IS_SPACE_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is tab.
|
||||
*/
|
||||
|
||||
#define IS_TAB_AT(string,offset) CHECK_AT((string),'\t',(offset))
|
||||
|
||||
#define IS_TAB(string) IS_TAB_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is blank (space or tab).
|
||||
*/
|
||||
|
||||
#define IS_BLANK_AT(string,offset) \
|
||||
(IS_SPACE_AT((string),(offset)) || IS_TAB_AT((string),(offset)))
|
||||
|
||||
#define IS_BLANK(string) IS_BLANK_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character at the specified position is a line break.
|
||||
*/
|
||||
|
||||
#define IS_BREAK_AT(string,offset) \
|
||||
(CHECK_AT((string),'\r',(offset)) /* CR (#xD)*/ \
|
||||
|| CHECK_AT((string),'\n',(offset)) /* LF (#xA) */ \
|
||||
|| (CHECK_AT((string),'\xC2',(offset)) \
|
||||
&& CHECK_AT((string),'\x85',(offset)+1)) /* NEL (#x85) */ \
|
||||
|| (CHECK_AT((string),'\xE2',(offset)) \
|
||||
&& CHECK_AT((string),'\x80',(offset)+1) \
|
||||
&& CHECK_AT((string),'\xA8',(offset)+2)) /* LS (#x2028) */ \
|
||||
|| (CHECK_AT((string),'\xE2',(offset)) \
|
||||
&& CHECK_AT((string),'\x80',(offset)+1) \
|
||||
&& CHECK_AT((string),'\xA9',(offset)+2))) /* PS (#x2029) */
|
||||
|
||||
#define IS_BREAK(string) IS_BREAK_AT((string),0)
|
||||
|
||||
#define IS_CRLF_AT(string,offset) \
|
||||
(CHECK_AT((string),'\r',(offset)) && CHECK_AT((string),'\n',(offset)+1))
|
||||
|
||||
#define IS_CRLF(string) IS_CRLF_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character is a line break or NUL.
|
||||
*/
|
||||
|
||||
#define IS_BREAKZ_AT(string,offset) \
|
||||
(IS_BREAK_AT((string),(offset)) || IS_Z_AT((string),(offset)))
|
||||
|
||||
#define IS_BREAKZ(string) IS_BREAKZ_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character is a line break, space, or NUL.
|
||||
*/
|
||||
|
||||
#define IS_SPACEZ_AT(string,offset) \
|
||||
(IS_SPACE_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset)))
|
||||
|
||||
#define IS_SPACEZ(string) IS_SPACEZ_AT((string),0)
|
||||
|
||||
/*
|
||||
* Check if the character is a line break, space, tab, or NUL.
|
||||
*/
|
||||
|
||||
#define IS_BLANKZ_AT(string,offset) \
|
||||
(IS_BLANK_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset)))
|
||||
|
||||
#define IS_BLANKZ(string) IS_BLANKZ_AT((string),0)
|
||||
|
||||
/*
|
||||
* Determine the width of the character.
|
||||
*/
|
||||
|
||||
#define WIDTH_AT(string,offset) \
|
||||
(((string).pointer[offset] & 0x80) == 0x00 ? 1 : \
|
||||
((string).pointer[offset] & 0xE0) == 0xC0 ? 2 : \
|
||||
((string).pointer[offset] & 0xF0) == 0xE0 ? 3 : \
|
||||
((string).pointer[offset] & 0xF8) == 0xF0 ? 4 : 0)
|
||||
|
||||
#define WIDTH(string) WIDTH_AT((string),0)
|
||||
|
||||
/*
|
||||
* Move the string pointer to the next character.
|
||||
*/
|
||||
|
||||
#define MOVE(string) ((string).pointer += WIDTH((string)))
|
||||
|
||||
/*
|
||||
* Copy a character and move the pointers of both strings.
|
||||
*/
|
||||
|
||||
#define COPY(string_a,string_b) \
|
||||
((*(string_b).pointer & 0x80) == 0x00 ? \
|
||||
(*((string_a).pointer++) = *((string_b).pointer++)) : \
|
||||
(*(string_b).pointer & 0xE0) == 0xC0 ? \
|
||||
(*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++)) : \
|
||||
(*(string_b).pointer & 0xF0) == 0xE0 ? \
|
||||
(*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++)) : \
|
||||
(*(string_b).pointer & 0xF8) == 0xF0 ? \
|
||||
(*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++), \
|
||||
*((string_a).pointer++) = *((string_b).pointer++)) : 0)
|
||||
|
||||
/*
|
||||
* Stack and queue management.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_stack_extend(void **start, void **top, void **end);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_queue_extend(void **start, void **head, void **tail, void **end);
|
||||
|
||||
#define STACK_INIT(context,stack,size) \
|
||||
(((stack).start = yaml_malloc((size)*sizeof(*(stack).start))) ? \
|
||||
((stack).top = (stack).start, \
|
||||
(stack).end = (stack).start+(size), \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define STACK_DEL(context,stack) \
|
||||
(yaml_free((stack).start), \
|
||||
(stack).start = (stack).top = (stack).end = 0)
|
||||
|
||||
#define STACK_EMPTY(context,stack) \
|
||||
((stack).start == (stack).top)
|
||||
|
||||
#define PUSH(context,stack,value) \
|
||||
(((stack).top != (stack).end \
|
||||
|| yaml_stack_extend((void **)&(stack).start, \
|
||||
(void **)&(stack).top, (void **)&(stack).end)) ? \
|
||||
(*((stack).top++) = value, \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define POP(context,stack) \
|
||||
(*(--(stack).top))
|
||||
|
||||
#define QUEUE_INIT(context,queue,size) \
|
||||
(((queue).start = yaml_malloc((size)*sizeof(*(queue).start))) ? \
|
||||
((queue).head = (queue).tail = (queue).start, \
|
||||
(queue).end = (queue).start+(size), \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define QUEUE_DEL(context,queue) \
|
||||
(yaml_free((queue).start), \
|
||||
(queue).start = (queue).head = (queue).tail = (queue).end = 0)
|
||||
|
||||
#define QUEUE_EMPTY(context,queue) \
|
||||
((queue).head == (queue).tail)
|
||||
|
||||
#define ENQUEUE(context,queue,value) \
|
||||
(((queue).tail != (queue).end \
|
||||
|| yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head, \
|
||||
(void **)&(queue).tail, (void **)&(queue).end)) ? \
|
||||
(*((queue).tail++) = value, \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
#define DEQUEUE(context,queue) \
|
||||
(*((queue).head++))
|
||||
|
||||
#define QUEUE_INSERT(context,queue,index,value) \
|
||||
(((queue).tail != (queue).end \
|
||||
|| yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head, \
|
||||
(void **)&(queue).tail, (void **)&(queue).end)) ? \
|
||||
(memmove((queue).head+(index)+1,(queue).head+(index), \
|
||||
((queue).tail-(queue).head-(index))*sizeof(*(queue).start)), \
|
||||
*((queue).head+(index)) = value, \
|
||||
(queue).tail++, \
|
||||
1) : \
|
||||
((context)->error = YAML_MEMORY_ERROR, \
|
||||
0))
|
||||
|
||||
/*
|
||||
* Token initializers.
|
||||
*/
|
||||
|
||||
#define TOKEN_INIT(token,token_type,token_start_mark,token_end_mark) \
|
||||
(memset(&(token), 0, sizeof(yaml_token_t)), \
|
||||
(token).type = (token_type), \
|
||||
(token).start_mark = (token_start_mark), \
|
||||
(token).end_mark = (token_end_mark))
|
||||
|
||||
#define STREAM_START_TOKEN_INIT(token,token_encoding,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_STREAM_START_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.stream_start.encoding = (token_encoding))
|
||||
|
||||
#define STREAM_END_TOKEN_INIT(token,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_STREAM_END_TOKEN,(start_mark),(end_mark)))
|
||||
|
||||
#define ALIAS_TOKEN_INIT(token,token_value,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_ALIAS_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.alias.value = (token_value))
|
||||
|
||||
#define ANCHOR_TOKEN_INIT(token,token_value,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_ANCHOR_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.anchor.value = (token_value))
|
||||
|
||||
#define TAG_TOKEN_INIT(token,token_handle,token_suffix,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_TAG_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.tag.handle = (token_handle), \
|
||||
(token).data.tag.suffix = (token_suffix))
|
||||
|
||||
#define SCALAR_TOKEN_INIT(token,token_value,token_length,token_style,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_SCALAR_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.scalar.value = (token_value), \
|
||||
(token).data.scalar.length = (token_length), \
|
||||
(token).data.scalar.style = (token_style))
|
||||
|
||||
#define VERSION_DIRECTIVE_TOKEN_INIT(token,token_major,token_minor,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_VERSION_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.version_directive.major = (token_major), \
|
||||
(token).data.version_directive.minor = (token_minor))
|
||||
|
||||
#define TAG_DIRECTIVE_TOKEN_INIT(token,token_handle,token_prefix,start_mark,end_mark) \
|
||||
(TOKEN_INIT((token),YAML_TAG_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \
|
||||
(token).data.tag_directive.handle = (token_handle), \
|
||||
(token).data.tag_directive.prefix = (token_prefix))
|
||||
|
||||
/*
|
||||
* Event initializers.
|
||||
*/
|
||||
|
||||
#define EVENT_INIT(event,event_type,event_start_mark,event_end_mark) \
|
||||
(memset(&(event), 0, sizeof(yaml_event_t)), \
|
||||
(event).type = (event_type), \
|
||||
(event).start_mark = (event_start_mark), \
|
||||
(event).end_mark = (event_end_mark))
|
||||
|
||||
#define STREAM_START_EVENT_INIT(event,event_encoding,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_STREAM_START_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.stream_start.encoding = (event_encoding))
|
||||
|
||||
#define STREAM_END_EVENT_INIT(event,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_STREAM_END_EVENT,(start_mark),(end_mark)))
|
||||
|
||||
#define DOCUMENT_START_EVENT_INIT(event,event_version_directive, \
|
||||
event_tag_directives_start,event_tag_directives_end,event_implicit,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_DOCUMENT_START_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.document_start.version_directive = (event_version_directive), \
|
||||
(event).data.document_start.tag_directives.start = (event_tag_directives_start), \
|
||||
(event).data.document_start.tag_directives.end = (event_tag_directives_end), \
|
||||
(event).data.document_start.implicit = (event_implicit))
|
||||
|
||||
#define DOCUMENT_END_EVENT_INIT(event,event_implicit,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_DOCUMENT_END_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.document_end.implicit = (event_implicit))
|
||||
|
||||
#define ALIAS_EVENT_INIT(event,event_anchor,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_ALIAS_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.alias.anchor = (event_anchor))
|
||||
|
||||
#define SCALAR_EVENT_INIT(event,event_anchor,event_tag,event_value,event_length, \
|
||||
event_plain_implicit, event_quoted_implicit,event_style,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_SCALAR_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.scalar.anchor = (event_anchor), \
|
||||
(event).data.scalar.tag = (event_tag), \
|
||||
(event).data.scalar.value = (event_value), \
|
||||
(event).data.scalar.length = (event_length), \
|
||||
(event).data.scalar.plain_implicit = (event_plain_implicit), \
|
||||
(event).data.scalar.quoted_implicit = (event_quoted_implicit), \
|
||||
(event).data.scalar.style = (event_style))
|
||||
|
||||
#define SEQUENCE_START_EVENT_INIT(event,event_anchor,event_tag, \
|
||||
event_implicit,event_style,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_SEQUENCE_START_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.sequence_start.anchor = (event_anchor), \
|
||||
(event).data.sequence_start.tag = (event_tag), \
|
||||
(event).data.sequence_start.implicit = (event_implicit), \
|
||||
(event).data.sequence_start.style = (event_style))
|
||||
|
||||
#define SEQUENCE_END_EVENT_INIT(event,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_SEQUENCE_END_EVENT,(start_mark),(end_mark)))
|
||||
|
||||
#define MAPPING_START_EVENT_INIT(event,event_anchor,event_tag, \
|
||||
event_implicit,event_style,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_MAPPING_START_EVENT,(start_mark),(end_mark)), \
|
||||
(event).data.mapping_start.anchor = (event_anchor), \
|
||||
(event).data.mapping_start.tag = (event_tag), \
|
||||
(event).data.mapping_start.implicit = (event_implicit), \
|
||||
(event).data.mapping_start.style = (event_style))
|
||||
|
||||
#define MAPPING_END_EVENT_INIT(event,start_mark,end_mark) \
|
||||
(EVENT_INIT((event),YAML_MAPPING_END_EVENT,(start_mark),(end_mark)))
|
||||
|
||||
/*
|
||||
* Document initializer.
|
||||
*/
|
||||
|
||||
#define DOCUMENT_INIT(document,document_nodes_start,document_nodes_end, \
|
||||
document_version_directive,document_tag_directives_start, \
|
||||
document_tag_directives_end,document_start_implicit, \
|
||||
document_end_implicit,document_start_mark,document_end_mark) \
|
||||
(memset(&(document), 0, sizeof(yaml_document_t)), \
|
||||
(document).nodes.start = (document_nodes_start), \
|
||||
(document).nodes.end = (document_nodes_end), \
|
||||
(document).nodes.top = (document_nodes_start), \
|
||||
(document).version_directive = (document_version_directive), \
|
||||
(document).tag_directives.start = (document_tag_directives_start), \
|
||||
(document).tag_directives.end = (document_tag_directives_end), \
|
||||
(document).start_implicit = (document_start_implicit), \
|
||||
(document).end_implicit = (document_end_implicit), \
|
||||
(document).start_mark = (document_start_mark), \
|
||||
(document).end_mark = (document_end_mark))
|
||||
|
||||
/*
|
||||
* Node initializers.
|
||||
*/
|
||||
|
||||
#define NODE_INIT(node,node_type,node_tag,node_start_mark,node_end_mark) \
|
||||
(memset(&(node), 0, sizeof(yaml_node_t)), \
|
||||
(node).type = (node_type), \
|
||||
(node).tag = (node_tag), \
|
||||
(node).start_mark = (node_start_mark), \
|
||||
(node).end_mark = (node_end_mark))
|
||||
|
||||
#define SCALAR_NODE_INIT(node,node_tag,node_value,node_length, \
|
||||
node_style,start_mark,end_mark) \
|
||||
(NODE_INIT((node),YAML_SCALAR_NODE,(node_tag),(start_mark),(end_mark)), \
|
||||
(node).data.scalar.value = (node_value), \
|
||||
(node).data.scalar.length = (node_length), \
|
||||
(node).data.scalar.style = (node_style))
|
||||
|
||||
#define SEQUENCE_NODE_INIT(node,node_tag,node_items_start,node_items_end, \
|
||||
node_style,start_mark,end_mark) \
|
||||
(NODE_INIT((node),YAML_SEQUENCE_NODE,(node_tag),(start_mark),(end_mark)), \
|
||||
(node).data.sequence.items.start = (node_items_start), \
|
||||
(node).data.sequence.items.end = (node_items_end), \
|
||||
(node).data.sequence.items.top = (node_items_start), \
|
||||
(node).data.sequence.style = (node_style))
|
||||
|
||||
#define MAPPING_NODE_INIT(node,node_tag,node_pairs_start,node_pairs_end, \
|
||||
node_style,start_mark,end_mark) \
|
||||
(NODE_INIT((node),YAML_MAPPING_NODE,(node_tag),(start_mark),(end_mark)), \
|
||||
(node).data.mapping.pairs.start = (node_pairs_start), \
|
||||
(node).data.mapping.pairs.end = (node_pairs_end), \
|
||||
(node).data.mapping.pairs.top = (node_pairs_start), \
|
||||
(node).data.mapping.style = (node_style))
|
||||
|
||||
#define YAML_VERSION_STRING "0.1.2"
|
||||
#define YAML_VERSION_MAJOR 0
|
||||
#define YAML_VERSION_MINOR 1
|
||||
#define YAML_VERSION_PATCH 2
|
||||
@ -1,6 +0,0 @@
|
||||
this is a long string: but this one is even longer
|
||||
this is shorter:
|
||||
- many
|
||||
- tiny
|
||||
- string
|
||||
one the other hand: this one is just a little bit bigger than the others
|
||||
@ -1,303 +0,0 @@
|
||||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE PackageImports #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
import qualified Text.Libyaml as Y
|
||||
import qualified Data.ByteString.Char8 as B8
|
||||
|
||||
import Test.HUnit hiding (Test, path)
|
||||
|
||||
import qualified Data.Conduit as C
|
||||
import qualified Data.Conduit.List as CL
|
||||
|
||||
import System.Directory
|
||||
import Control.Monad
|
||||
import Control.Exception (try, SomeException)
|
||||
import Test.Hspec.Monadic
|
||||
import Test.Hspec.HUnit ()
|
||||
|
||||
import qualified Data.Yaml as D
|
||||
import Data.Yaml (object, array)
|
||||
import Data.Maybe
|
||||
import qualified Data.HashMap.Strict as M
|
||||
import qualified Data.Text as T
|
||||
|
||||
main :: IO ()
|
||||
main = hspecX $ do
|
||||
describe "streaming" $ do
|
||||
it "count scalars with anchor" caseCountScalarsWithAnchor
|
||||
it "count sequences with anchor" caseCountSequencesWithAnchor
|
||||
it "count mappings with anchor" caseCountMappingsWithAnchor
|
||||
it "count aliases" caseCountAliases
|
||||
it "count scalars" caseCountScalars
|
||||
it "largest string" caseLargestString
|
||||
it "encode/decode" caseEncodeDecode
|
||||
it "encode/decode file" caseEncodeDecodeFile
|
||||
it "interleaved encode/decode" caseInterleave
|
||||
it "decode invalid document (without segfault)" caseDecodeInvalidDocument
|
||||
describe "Data.Yaml" $ do
|
||||
it "encode/decode" caseEncodeDecodeData
|
||||
it "encode/decode file" caseEncodeDecodeFileData
|
||||
it "encode/decode strings" caseEncodeDecodeStrings
|
||||
it "decode invalid file" caseDecodeInvalid
|
||||
it "processes datatypes" caseDataTypes
|
||||
describe "Data.Yaml aliases" $ do
|
||||
it "simple scalar alias" caseSimpleScalarAlias
|
||||
it "simple sequence alias" caseSimpleSequenceAlias
|
||||
it "simple mapping alias" caseSimpleMappingAlias
|
||||
it "mapping alias before anchor" caseMappingAliasBeforeAnchor
|
||||
it "mapping alias inside anchor" caseMappingAliasInsideAnchor
|
||||
it "scalar alias overriding" caseScalarAliasOverriding
|
||||
describe "Data.Yaml merge keys" $ do
|
||||
it "test uniqueness of keys" caseAllKeysShouldBeUnique
|
||||
it "test mapping merge" caseSimpleMappingMerge
|
||||
it "test sequence of mappings merging" caseMergeSequence
|
||||
|
||||
counter :: Monad m => (Y.Event -> Bool) -> C.Sink Y.Event m Int
|
||||
counter pred' =
|
||||
CL.fold (\cnt e -> (if pred' e then 1 else 0) + cnt) 0
|
||||
|
||||
caseHelper :: String
|
||||
-> (Y.Event -> Bool)
|
||||
-> Int
|
||||
-> Assertion
|
||||
caseHelper yamlString pred' expRes = do
|
||||
res <- C.runResourceT $ Y.decode (B8.pack yamlString) C.$$ counter pred'
|
||||
res @?= expRes
|
||||
|
||||
caseCountScalarsWithAnchor :: Assertion
|
||||
caseCountScalarsWithAnchor =
|
||||
caseHelper yamlString isScalarA 1
|
||||
where
|
||||
yamlString = "foo:\n - &anchor bin1\n - bin2\n - bin3"
|
||||
isScalarA (Y.EventScalar _ _ _ (Just _)) = True
|
||||
isScalarA _ = False
|
||||
|
||||
caseCountSequencesWithAnchor :: Assertion
|
||||
caseCountSequencesWithAnchor =
|
||||
caseHelper yamlString isSequenceStartA 1
|
||||
where
|
||||
yamlString = "foo: &anchor\n - bin1\n - bin2\n - bin3"
|
||||
isSequenceStartA (Y.EventSequenceStart (Just _)) = True
|
||||
isSequenceStartA _ = False
|
||||
|
||||
caseCountMappingsWithAnchor :: Assertion
|
||||
caseCountMappingsWithAnchor =
|
||||
caseHelper yamlString isMappingA 1
|
||||
where
|
||||
yamlString = "foo: &anchor\n key1: bin1\n key2: bin2\n key3: bin3"
|
||||
isMappingA (Y.EventMappingStart (Just _)) = True
|
||||
isMappingA _ = False
|
||||
|
||||
caseCountAliases :: Assertion
|
||||
caseCountAliases =
|
||||
caseHelper yamlString isAlias 1
|
||||
where
|
||||
yamlString = "foo: &anchor\n key1: bin1\n key2: bin2\n key3: bin3\nboo: *anchor"
|
||||
isAlias Y.EventAlias{} = True
|
||||
isAlias _ = False
|
||||
|
||||
caseCountScalars :: Assertion
|
||||
caseCountScalars = do
|
||||
res <- C.runResourceT $ Y.decode yamlBS C.$$ CL.fold adder accum
|
||||
res @?= (7, 1, 2)
|
||||
where
|
||||
yamlString = "foo:\n baz: [bin1, bin2, bin3]\nbaz: bazval"
|
||||
yamlBS = B8.pack yamlString
|
||||
adder (s, l, m) (Y.EventScalar{}) = (s + 1, l, m)
|
||||
adder (s, l, m) (Y.EventSequenceStart{}) = (s, l + 1, m)
|
||||
adder (s, l, m) (Y.EventMappingStart{}) = (s, l, m + 1)
|
||||
adder a _ = a
|
||||
accum = (0, 0, 0) :: (Int, Int, Int)
|
||||
|
||||
caseLargestString :: Assertion
|
||||
caseLargestString = do
|
||||
res <- C.runResourceT $ Y.decodeFile filePath C.$$ CL.fold adder accum
|
||||
res @?= (length expected, expected)
|
||||
where
|
||||
expected = "this one is just a little bit bigger than the others"
|
||||
filePath = "test/largest-string.yaml"
|
||||
adder (i, s) (Y.EventScalar bs _ _ _) =
|
||||
let s' = B8.unpack bs
|
||||
i' = length s'
|
||||
in if i' > i then (i', s') else (i, s)
|
||||
adder acc _ = acc
|
||||
accum = (0, "no strings found")
|
||||
|
||||
newtype MyEvent = MyEvent Y.Event deriving Show
|
||||
instance Eq MyEvent where
|
||||
(MyEvent (Y.EventScalar s t _ _)) == (MyEvent (Y.EventScalar s' t' _ _)) =
|
||||
s == s' && t == t'
|
||||
MyEvent e1 == MyEvent e2 = e1 == e2
|
||||
|
||||
caseEncodeDecode :: Assertion
|
||||
caseEncodeDecode = do
|
||||
eList <- C.runResourceT $ Y.decode yamlBS C.$$ CL.consume
|
||||
bs <- C.runResourceT $ CL.sourceList eList C.$$ Y.encode
|
||||
eList2 <- C.runResourceT $ Y.decode bs C.$$ CL.consume
|
||||
map MyEvent eList @=? map MyEvent eList2
|
||||
where
|
||||
yamlString = "foo: bar\nbaz:\n - bin1\n - bin2\n"
|
||||
yamlBS = B8.pack yamlString
|
||||
|
||||
removeFile' :: FilePath -> IO ()
|
||||
removeFile' fp = do
|
||||
x <- doesFileExist fp
|
||||
when x $ removeFile fp
|
||||
|
||||
caseEncodeDecodeFile :: Assertion
|
||||
caseEncodeDecodeFile = do
|
||||
removeFile' tmpPath
|
||||
eList <- C.runResourceT $ Y.decodeFile filePath C.$$ CL.consume
|
||||
C.runResourceT $ CL.sourceList eList C.$$ Y.encodeFile tmpPath
|
||||
eList2 <- C.runResourceT $ Y.decodeFile filePath C.$$ CL.consume
|
||||
map MyEvent eList @=? map MyEvent eList2
|
||||
where
|
||||
filePath = "test/largest-string.yaml"
|
||||
tmpPath = "tmp.yaml"
|
||||
|
||||
caseInterleave :: Assertion
|
||||
caseInterleave = do
|
||||
removeFile' tmpPath
|
||||
removeFile' tmpPath2
|
||||
() <- C.runResourceT $ Y.decodeFile filePath C.$$ Y.encodeFile tmpPath
|
||||
() <- C.runResourceT $ Y.decodeFile tmpPath C.$$ Y.encodeFile tmpPath2
|
||||
f1 <- readFile tmpPath
|
||||
f2 <- readFile tmpPath2
|
||||
f1 @=? f2
|
||||
where
|
||||
filePath = "test/largest-string.yaml"
|
||||
tmpPath = "tmp.yaml"
|
||||
tmpPath2 = "tmp2.yaml"
|
||||
|
||||
caseDecodeInvalidDocument :: Assertion
|
||||
caseDecodeInvalidDocument = do
|
||||
x <- try $ C.runResourceT $ Y.decode yamlBS C.$$ CL.sinkNull
|
||||
case x of
|
||||
Left (_ :: SomeException) -> return ()
|
||||
Right y -> do
|
||||
putStrLn $ "bad return value: " ++ show y
|
||||
assertFailure "expected parsing exception, but got no errors"
|
||||
where
|
||||
yamlString = " - foo\n - baz\nbuz"
|
||||
yamlBS = B8.pack yamlString
|
||||
|
||||
mkScalar :: String -> D.Value
|
||||
mkScalar = mkStrScalar
|
||||
|
||||
mkStrScalar :: String -> D.Value
|
||||
mkStrScalar = D.String . T.pack
|
||||
|
||||
mappingKey :: D.Value-> String -> D.Value
|
||||
mappingKey (D.Object m) k = (fromJust . M.lookup (T.pack k) $ m)
|
||||
mappingKey _ _ = error "expected Object"
|
||||
|
||||
decodeYaml :: String -> Maybe D.Value
|
||||
decodeYaml s = D.decode $ B8.pack s
|
||||
|
||||
sample :: D.Value
|
||||
sample = array
|
||||
[ D.String "foo"
|
||||
, object
|
||||
[ ("bar1", D.String "bar2")
|
||||
]
|
||||
]
|
||||
|
||||
caseEncodeDecodeData :: Assertion
|
||||
caseEncodeDecodeData = do
|
||||
let out = D.decode $ D.encode sample
|
||||
out @?= Just sample
|
||||
|
||||
caseEncodeDecodeFileData :: Assertion
|
||||
caseEncodeDecodeFileData = do
|
||||
let fp = "tmp.yaml"
|
||||
D.encodeFile fp sample
|
||||
out <- D.decodeFile fp
|
||||
out @?= Just sample
|
||||
|
||||
caseEncodeDecodeStrings :: Assertion
|
||||
caseEncodeDecodeStrings = do
|
||||
let out = D.decode $ D.encode sample
|
||||
out @?= Just sample
|
||||
|
||||
caseDecodeInvalid :: Assertion
|
||||
caseDecodeInvalid = do
|
||||
let invalid = B8.pack "\tthis is 'not' valid :-)"
|
||||
Nothing @=? (D.decode invalid :: Maybe D.Value)
|
||||
|
||||
caseSimpleScalarAlias :: Assertion
|
||||
caseSimpleScalarAlias = do
|
||||
let maybeRes = decodeYaml "- &anch foo\n- baz\n- *anch"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
res @?= array [(mkScalar "foo"), (mkScalar "baz"), (mkScalar "foo")]
|
||||
|
||||
caseSimpleSequenceAlias :: Assertion
|
||||
caseSimpleSequenceAlias = do
|
||||
let maybeRes = decodeYaml "seq: &anch\n - foo\n - baz\nseq2: *anch"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
res @?= object [("seq", array [(mkScalar "foo"), (mkScalar "baz")]), ("seq2", array [(mkScalar "foo"), (mkScalar "baz")])]
|
||||
|
||||
caseSimpleMappingAlias :: Assertion
|
||||
caseSimpleMappingAlias = do
|
||||
let maybeRes = decodeYaml "map: &anch\n key1: foo\n key2: baz\nmap2: *anch"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
res @?= object [(T.pack "map", object [("key1", mkScalar "foo"), ("key2", (mkScalar "baz"))]), (T.pack "map2", object [("key1", (mkScalar "foo")), ("key2", mkScalar "baz")])]
|
||||
|
||||
caseMappingAliasBeforeAnchor :: Assertion
|
||||
caseMappingAliasBeforeAnchor = do
|
||||
let res = decodeYaml "map: *anch\nmap2: &anch\n key1: foo\n key2: baz"
|
||||
isNothing res @? "decode should return Nothing due to unknown alias"
|
||||
|
||||
caseMappingAliasInsideAnchor :: Assertion
|
||||
caseMappingAliasInsideAnchor = do
|
||||
let res = decodeYaml "map: &anch\n key1: foo\n key2: *anch"
|
||||
isNothing res @? "decode should return Nothing due to unknown alias"
|
||||
|
||||
caseScalarAliasOverriding :: Assertion
|
||||
caseScalarAliasOverriding = do
|
||||
let maybeRes = decodeYaml "- &anch foo\n- baz\n- *anch\n- &anch boo\n- buz\n- *anch"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
res @?= array [(mkScalar "foo"), (mkScalar "baz"), (mkScalar "foo"), (mkScalar "boo"), (mkScalar "buz"), (mkScalar "boo")]
|
||||
|
||||
caseAllKeysShouldBeUnique :: Assertion
|
||||
caseAllKeysShouldBeUnique = do
|
||||
let maybeRes = decodeYaml "foo1: foo\nfoo2: baz\nfoo1: buz"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
mappingKey res "foo1" @?= (mkScalar "buz")
|
||||
|
||||
caseSimpleMappingMerge :: Assertion
|
||||
caseSimpleMappingMerge = do
|
||||
let maybeRes = decodeYaml "foo1: foo\nfoo2: baz\n<<:\n foo1: buz\n foo3: fuz"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
mappingKey res "foo1" @?= (mkScalar "foo")
|
||||
mappingKey res "foo3" @?= (mkScalar "fuz")
|
||||
|
||||
caseMergeSequence :: Assertion
|
||||
caseMergeSequence = do
|
||||
let maybeRes = decodeYaml "m1: &m1\n k1: !!str 1\n k2: !!str 2\nm2: &m2\n k1: !!str 3\n k3: !!str 4\nfoo1: foo\n<<: [ *m1, *m2 ]"
|
||||
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
|
||||
let res = fromJust maybeRes
|
||||
mappingKey res "foo1" @?= (mkScalar "foo")
|
||||
mappingKey res "k1" @?= (D.Number 1)
|
||||
mappingKey res "k2" @?= (D.Number 2)
|
||||
mappingKey res "k3" @?= (D.Number 4)
|
||||
|
||||
caseDataTypes :: Assertion
|
||||
caseDataTypes =
|
||||
D.decode (D.encode val) @?= Just val
|
||||
where
|
||||
val = object
|
||||
[ ("string", D.String "foo")
|
||||
, ("int", D.Number 5)
|
||||
, ("float", D.Number 4.3)
|
||||
, ("true", D.Bool True)
|
||||
, ("false", D.Bool False)
|
||||
, ("null", D.Null)
|
||||
]
|
||||
@ -1,78 +0,0 @@
|
||||
name: yaml
|
||||
version: 0.7.0
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
|
||||
maintainer: Michael Snoyman <michael@snoyman.com>
|
||||
synopsis: Low-level binding to the libyaml C library.
|
||||
description: Provides support for parsing and emitting Yaml documents.
|
||||
.
|
||||
This package includes the full libyaml C library version 0.1.2 by Kirill
|
||||
Simonov (<http://pyyaml.org/wiki/LibYAML>) in the package so you
|
||||
don't need to worry about any non-Haskell dependencies.
|
||||
category: Web
|
||||
stability: unstable
|
||||
cabal-version: >= 1.8
|
||||
build-type: Simple
|
||||
homepage: http://github.com/snoyberg/yaml/
|
||||
extra-source-files: c/helper.h,
|
||||
libyaml/yaml_private.h,
|
||||
libyaml/yaml.h,
|
||||
libyaml/LICENSE
|
||||
test/main.hs
|
||||
test/largest-string.yaml
|
||||
|
||||
flag system-libyaml
|
||||
description: Use the system-wide libyaml instead of the bundled copy
|
||||
default: False
|
||||
|
||||
library
|
||||
build-depends: base >= 4 && < 5
|
||||
, transformers >= 0.1 && < 0.3
|
||||
, bytestring >= 0.9.1.4 && < 0.10
|
||||
, conduit >= 0.4 && < 0.5
|
||||
, resourcet >= 0.3 && < 0.4
|
||||
, aeson >= 0.5
|
||||
, containers
|
||||
, unordered-containers
|
||||
, vector
|
||||
, text
|
||||
, attoparsec
|
||||
exposed-modules: Text.Libyaml
|
||||
Data.Yaml
|
||||
ghc-options: -Wall
|
||||
c-sources: c/helper.c
|
||||
include-dirs: c
|
||||
if flag(system-libyaml)
|
||||
pkgconfig-depends: yaml-0.1
|
||||
else
|
||||
c-sources: libyaml/api.c,
|
||||
libyaml/dumper.c,
|
||||
libyaml/emitter.c,
|
||||
libyaml/loader.c,
|
||||
libyaml/parser.c,
|
||||
libyaml/reader.c,
|
||||
libyaml/scanner.c,
|
||||
libyaml/writer.c
|
||||
include-dirs: libyaml
|
||||
|
||||
test-suite test
|
||||
type: exitcode-stdio-1.0
|
||||
hs-source-dirs: test
|
||||
main-is: main.hs
|
||||
cpp-options: -DTEST
|
||||
build-depends: hspec
|
||||
, HUnit
|
||||
, directory
|
||||
, base >= 4 && < 5
|
||||
, transformers >= 0.1 && < 0.3
|
||||
, bytestring >= 0.9.1.4 && < 0.10
|
||||
, conduit
|
||||
, yaml
|
||||
, text
|
||||
, unordered-containers
|
||||
ghc-options: -Wall
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: https://github.com/snoyberg/yaml
|
||||
Loading…
Reference in New Issue
Block a user