1 -- |This is an auxiliary parser utilities for parsing things related
4 -- In general you don't have to use this module directly.
5 module Network.HTTP.Lucu.Parser.Http
20 import Network.HTTP.Lucu.Parser
22 -- |@'isCtl' c@ is 'Prelude.False' iff @0x20 <= @c@ < 0x7F@.
29 -- |@'isSeparator' c@ is 'Prelude.True' iff c is one of HTTP
31 isSeparator :: Char -> Bool
32 isSeparator '(' = True
33 isSeparator ')' = True
34 isSeparator '<' = True
35 isSeparator '>' = True
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 '\t' = True
53 -- |@'isChar' c@ is 'Prelude.True' iff @c <= 0x7f@.
54 isChar :: Char -> Bool
59 -- |@'isToken' c@ is equivalent to @not ('isCtl' c || 'isSeparator'
61 isToken :: Char -> Bool
63 not (isCtl c || isSeparator c)
65 -- |@'listOf' p@ is similar to @'Network.HTTP.Lucu.Parser.sepBy' p
66 -- ('Network.HTTP.Lucu.Parser.char' \',\')@ but it allows any
67 -- occurrences of LWS before and after each tokens.
68 listOf :: Parser a -> Parser [a]
69 listOf !p = do _ <- many lws
70 sepBy p $! do _ <- many lws
74 -- |'token' is equivalent to @'Network.HTTP.Lucu.Parser.many1' $
75 -- 'Network.HTTP.Lucu.Parser.satisfy' 'isToken'@
76 token :: Parser String
77 token = many1 $! satisfy isToken
79 -- |'lws' is an HTTP LWS: @'Network.HTTP.Lucu.Parser.crlf'?
80 -- ('Network.HTTP.Lucu.Parser.sp' | 'Network.HTTP.Lucu.Parser.ht')+@
82 lws = do s <- option "" crlf
83 xs <- many1 (sp <|> ht)
86 -- |'text' accepts one character which doesn't satisfy 'isCtl'.
88 text = satisfy (not . isCtl)
90 -- |'separator' accepts one character which satisfies 'isSeparator'.
91 separator :: Parser Char
92 separator = satisfy isSeparator
94 -- |'quotedStr' accepts a string surrounded by double quotation
95 -- marks. Quotes can be escaped by backslashes.
96 quotedStr :: Parser String
97 quotedStr = do _ <- char '"'
98 xs <- many (qdtext <|> quotedPair)
100 return $ foldr (++) "" xs
102 qdtext = do c <- satisfy (/= '"')
105 quotedPair = do _ <- char '\\'
109 -- |'qvalue' accepts a so-called qvalue.
110 qvalue :: Parser Double
111 qvalue = do x <- char '0'
114 ys <- many digit -- 本當は三文字までに制限
121 ys <- many (char '0') -- 本當は三文字までに制限