gdritter repos ndbl / 6b65fa9
Finished comments; clarified text Getty Ritter 8 years ago
2 changed file(s) with 36 addition(s) and 10 deletion(s). Collapse all Expand all
22 {-# LANGUAGE LambdaCase #-}
33
44 module Data.NDBL.Parse (Document, Group, Pair, pNDBL) where
5
6 import Data.Char (isPrint, isSpace)
57
68 type Document = [Group]
79 type Group = [Pair]
2527
2628 pQString :: Parse String
2729 pQString = go
28 where go ('\\':x:xs) = (x:) `over` go xs
30 where go ('\\':x:xs)
31 | x == '\\' ||
32 x == '"' = (x:) `over` go xs
33 | otherwise = throw $ "Unrecognized escape: \"\\" ++ [x] ++ "\""
2934 go ('"':xs) = return (xs, "")
30 go (x:xs) = (x:) `over` go xs
35 go (x:xs)
36 | isPrint x || isSpace x = (x:) `over` go xs
37 | otherwise = throw $ "Non-printable character: " ++ show x
38 go [] = throw $ "End of document while still inside string"
3139
3240 pWord :: Parse String
3341 pWord s@(x:xs)
34 | not (isSep x) = (x:) `over` pWord xs
42 | isPrint x && not (isSep x) = (x:) `over` pWord xs
43 | isSep x = return (s, "")
44 | not (isPrint x) = throw $ "Non-printable character: " ++ show x
3545 pWord s = return (s, "")
3646
3747 pWord1 :: Parse String
3848 pWord1 (x:xs)
39 | not (isSep x) = (x:) `over` pWord xs
49 | isPrint x && not (isSep x) = (x:) `over` pWord xs
50 | isSep x = throw $ "`=` without previous key"
51 | not (isPrint x) = throw $ "Non-printable character: " ++ show x
4052 pWord1 s = throw $ "Expected word; found " ++ show s
4153
4254
5264 isVSpace :: Char -> Bool
5365 isVSpace c = c == '\n' || c == '\r'
5466
67 pComment :: Parse Bool
68 pComment s@(x:xs)
69 | isVSpace x = pSkip s
70 | otherwise = pComment xs
71
5572 pSkip :: Parse Bool
56 pSkip (y:[]) = return ("", False)
73 pSkip "" = return ("", False)
74 pSkip (y:"") = return ("", False)
75 pSkip ('#':xs) = pComment xs
5776 pSkip (y:s@(x:xs))
5877 | isVSpace y && isHSpace x = pSkip xs
5978 | isVSpace y = return (s, False)
6060 A _comment_ is introduced by any whitespace (including newlines)
6161 followed by a pound sign (`#`) and lasts until the end of a line. This
6262 means that a key cannot begin with the `#` character, but that a `#`
63 character can occur as a constitutent of a key-value pair.
63 character can occur as a constitutent of a key-value pair, including
64 as a trailing character.
6465
6566 A _key-value pair_ consists of a string of at least length one, followed
6667 by a equals sign (`=`) and subsequently by a string of at least zero.
67 The value may be quoted, in which case it is allowed to contain any
68 The key must be a bare string, and can contain any printable non-whitespace
69 character except the equals sign (`=`) and additionally must not begin
70 with a pound sign (`#`). It is acceptable for a key to contains a
71 pound sign if it is not the first character of the key.
72 The value may be quoted with double quotes (`"`), in which case it is allowed to contain any
6873 printable character, including the equals sign, whitespace, and newlines.
69 An unquoted value is allowed to contain any non-whitespace character
70 except the equals sign. The value can be zero length. No spaces are
74 Quoted values understand the escape sequences `\\` for a backslash and
75 `\"` for a double quote; no other escape sequences are provided.
76 An unquoted value is allowed to contain any printable non-whitespace
77 character except the equals sign. The value can be zero length. No spaces are
7178 allowed around the equals sign.
7279
73 A _group_ is a multiset of key-value pairs. A group is introduced by a
80 A _group_ is a sequence of key-value pairs. A group is introduced by a
7481 non-indented key-value pair; all subsequent key-value pairs on the same
7582 line, as well as any key-value pairs on subsequent indented lines, belong
7683 to the same group.