Allow comments in API QQ.

Closes #29.
This commit is contained in:
Julian K. Arni 2014-11-13 18:22:26 +01:00
parent 4f62dceb78
commit 108f24a5e7
2 changed files with 52 additions and 4 deletions

View file

@ -90,9 +90,11 @@ data Typ = Val String
parseTyp :: Parser Typ parseTyp :: Parser Typ
parseTyp = do parseTyp = do
f <- many (noneOf "-#\n\r") f <- many (noneOf "-{\n\r")
spaces spaces
s <- optionMaybe parseRet s <- optionMaybe (try parseRet)
try $ optional inlineComment
try $ optional blockComment
case s of case s of
Nothing -> return $ Val (stripTr f) Nothing -> return $ Val (stripTr f)
Just s' -> return $ ReqArgVal (stripTr f) (stripTr s') Just s' -> return $ ReqArgVal (stripTr f) (stripTr s')
@ -101,7 +103,7 @@ parseTyp = do
parseRet = do parseRet = do
string "->" string "->"
spaces spaces
many (noneOf "#\n\r") many (noneOf "-{\n\r")
stripTr = reverse . dropWhile (== ' ') . reverse stripTr = reverse . dropWhile (== ' ') . reverse
@ -116,6 +118,18 @@ parseEntry = do
Val s -> return $ url (met s) Val s -> return $ url (met s)
ReqArgVal i o -> return $ url $ reqBody i (met o) ReqArgVal i o -> return $ url $ reqBody i (met o)
blockComment :: Parser ()
blockComment = do
string "{-"
manyTill anyChar (try $ string "-}")
return ()
inlineComment :: Parser ()
inlineComment = do
string "--"
manyTill anyChar (try $ lookAhead eol)
return ()
eol :: Parser String eol :: Parser String
eol = try (string "\n\r") eol = try (string "\n\r")
<|> try (string "\r\n") <|> try (string "\r\n")
@ -124,7 +138,7 @@ eol = try (string "\n\r")
<?> "end of line" <?> "end of line"
eols :: Parser () eols :: Parser ()
eols = skipMany $ void eol eols = skipMany $ void eol <|> blockComment <|> inlineComment
parseAll :: Parser Type parseAll :: Parser Type
parseAll = do parseAll = do

View file

@ -83,6 +83,34 @@ POST hello Bool
GET hello Bool GET hello Bool
|] |]
type TwoPaths' = ("hello" :> Post Bool) :<|> ("hello" :> Get Bool) type TwoPaths' = ("hello" :> Post Bool) :<|> ("hello" :> Get Bool)
type WithInlineComments = [sitemap|
GET hello Bool -- This is a comment
|]
type WithInlineComments' = "hello" :> Get Bool
type WithInlineComments2 = [sitemap|
GET hello Bool
-- This is a comment
|]
type WithInlineComments2' = "hello" :> Get Bool
type WithBlockComments = [sitemap|
GET hello Bool {-
POST hello Bool
-}
|]
type WithBlockComments' = "hello" :> Get Bool
type WithBlockComments2 = [sitemap|
GET hello Bool {-
POST hello Bool
-}
POST hello Bool
|]
type WithBlockComments2' = ("hello" :> Get Bool) :<|> ("hello" :> Post Bool)
-------------------------------------------------------------------------- --------------------------------------------------------------------------
-- Spec -- Spec
-------------------------------------------------------------------------- --------------------------------------------------------------------------
@ -118,6 +146,12 @@ spec = do
(u::SimpleQueryParam) ~= (u::SimpleQueryParam''') ~> False (u::SimpleQueryParam) ~= (u::SimpleQueryParam''') ~> False
it "Handles multiples paths" $ do it "Handles multiples paths" $ do
(u::TwoPaths) ~= (u::TwoPaths') ~> True (u::TwoPaths) ~= (u::TwoPaths') ~> True
it "Ignores inline comments" $ do
(u::WithInlineComments) ~= (u::WithInlineComments') ~> True
(u::WithInlineComments2) ~= (u::WithInlineComments2') ~> True
it "Ignores inline comments" $ do
(u::WithBlockComments) ~= (u::WithBlockComments') ~> True
(u::WithBlockComments2) ~= (u::WithBlockComments2') ~> True
-------------------------------------------------------------------------- --------------------------------------------------------------------------