5 -- |This is an auxiliary parser utilities for parsing things related
8 -- In general you don't have to use this module directly.
9 module Network.HTTP.Lucu.Parser.Http
24 import Network.HTTP.Lucu.Parser
26 -- |@'isCtl' c@ is 'Prelude.False' iff @0x20 <= @c@ < 0x7F@.
33 -- |@'isSeparator' c@ is 'Prelude.True' iff c is one of HTTP
35 isSeparator :: Char -> Bool
36 isSeparator '(' = True
37 isSeparator ')' = True
38 isSeparator '<' = True
39 isSeparator '>' = True
40 isSeparator '@' = True
41 isSeparator ',' = True
42 isSeparator ';' = True
43 isSeparator ':' = True
44 isSeparator '\\' = True
45 isSeparator '"' = True
46 isSeparator '/' = True
47 isSeparator '[' = True
48 isSeparator ']' = True
49 isSeparator '?' = True
50 isSeparator '=' = True
51 isSeparator '{' = True
52 isSeparator '}' = True
53 isSeparator ' ' = True
54 isSeparator '\t' = True
57 -- |@'isChar' c@ is 'Prelude.True' iff @c <= 0x7f@.
58 isChar :: Char -> Bool
63 -- |@'isToken' c@ is equivalent to @not ('isCtl' c || 'isSeparator'
65 isToken :: Char -> Bool
67 not (isCtl c || isSeparator c)
69 -- |@'listOf' p@ is similar to @'Network.HTTP.Lucu.Parser.sepBy' p
70 -- ('Network.HTTP.Lucu.Parser.char' \',\')@ but it allows any
71 -- occurrences of LWS before and after each tokens.
72 listOf :: Parser a -> Parser [a]
73 listOf !p = do _ <- many lws
74 sepBy p $! do _ <- many lws
78 -- |'token' is equivalent to @'Network.HTTP.Lucu.Parser.many1' $
79 -- 'Network.HTTP.Lucu.Parser.satisfy' 'isToken'@
80 token :: Parser String
81 token = many1 $! satisfy isToken
83 -- |'lws' is an HTTP LWS: @'Network.HTTP.Lucu.Parser.crlf'?
84 -- ('Network.HTTP.Lucu.Parser.sp' | 'Network.HTTP.Lucu.Parser.ht')+@
86 lws = do s <- option "" crlf
87 xs <- many1 (sp <|> ht)
90 -- |'text' accepts one character which doesn't satisfy 'isCtl'.
92 text = satisfy (not . isCtl)
94 -- |'separator' accepts one character which satisfies 'isSeparator'.
95 separator :: Parser Char
96 separator = satisfy isSeparator
98 -- |'quotedStr' accepts a string surrounded by double quotation
99 -- marks. Quotes can be escaped by backslashes.
100 quotedStr :: Parser String
101 quotedStr = do _ <- char '"'
102 xs <- many (qdtext <|> quotedPair)
104 return $ foldr (++) "" xs
106 qdtext = do c <- satisfy (/= '"')
109 quotedPair = do _ <- char '\\'
113 -- |'qvalue' accepts a so-called qvalue.
114 qvalue :: Parser Double
115 qvalue = do x <- char '0'
118 ys <- many digit -- 本當は三文字までに制限
125 ys <- many (char '0') -- 本當は三文字までに制限