Permalink
Browse files

Make YAML parsing sufficiently lazy

  • Loading branch information...
1 parent 5820fc9 commit 655788aefe31a886d56810bee00388ae43c33592 @shicks committed Oct 25, 2009
Showing with 16 additions and 3 deletions.
  1. +16 −3 YAML.hs
View
19 YAML.hs
@@ -1,12 +1,15 @@
module YAML ( Node(..), YAML(..),
-- buildTree, unEscape, tokenize, tokenizeBytes,
+ singleT,
getScalar, getMapping, getSequence,
getMappingValues, getMappingList, makeTree, makeTokens,
showYaml, readYaml, parseYaml, parseYamlList ) where
import YAML.Reference ( Code(..), tokenize ) -- , tokenizeBytes )
import Data.Maybe ( catMaybes )
import Data.Char ( isHexDigit, isSpace, isAlphaNum )
+import Data.List ( groupBy )
+import Data.Function ( on )
data Node = Leaf String
| List [Node]
@@ -243,11 +246,21 @@ getMeta (MetaT s:xs) = s++getMeta xs
getMeta (_:xs) = getMeta xs
getMeta [] = ""
+tokenizeLazy :: String -> String -> [(Code,String)]
+tokenizeLazy name = concat . map (tokenize name . unlines)
+ . groupBetween "..." . lines
+ where groupBetween :: Eq a => a -> [a] -> [[a]]
+ groupBetween s = uncurry (:) . gb
+ where -- gb :: [a] -> ([a],[[a]])
+ gb [] = ([],[])
+ gb (x:xs) | x == s = let (y,ys) = gb xs in ([],y:ys)
+ | otherwise = let (y,ys) = gb xs in (x:y,ys)
+
parseYaml :: String -> [Node]
-parseYaml = parseStream . buildTree . tokenize "-"
+parseYaml = parseStream . buildTree . tokenizeLazy "-"
-makeTokens = tokenize "-"
-makeTree = buildTree . tokenize "-"
+makeTokens = tokenizeLazy "-"
+makeTree = buildTree . tokenizeLazy "-"
parseYamlList :: String -> [Node]
parseYamlList x = case parseYaml x of

0 comments on commit 655788a

Please sign in to comment.