summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Navarro <j@dannynavarro.net>2015-09-14 15:43:09 +0200
committerDanny Navarro <j@dannynavarro.net>2015-09-14 15:43:09 +0200
commitec018db73affd8e7287ff2c8384f5d424248140a (patch)
treebbebf39b6517d8fbed9717160d9c58d6aaa27a42
parent3084b188dd36739b291e5352b2baf34f96bee6d1 (diff)
downloadgraphql-ec018db73affd8e7287ff2c8384f5d424248140a.tar.gz
Handle comments in whitespace
-rw-r--r--Data/GraphQL/Parser.hs20
-rw-r--r--TODO3
2 files changed, 11 insertions, 12 deletions
diff --git a/Data/GraphQL/Parser.hs b/Data/GraphQL/Parser.hs
index 1cd5cb6..1f7254b 100644
--- a/Data/GraphQL/Parser.hs
+++ b/Data/GraphQL/Parser.hs
@@ -1,8 +1,10 @@
{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE LambdaCase #-}
module Data.GraphQL.Parser where
import Prelude hiding (takeWhile)
import Control.Applicative (Alternative, (<|>), empty, many, optional)
+import Control.Monad (when)
import Data.Char
import Data.Text (Text, pack)
@@ -18,6 +20,7 @@ import Data.Attoparsec.Text
, many1
, manyTill
, option
+ , peekChar
, satisfy
, sepBy
, sepBy1
@@ -45,8 +48,8 @@ name = tok $ pack <$> many1 (satisfy isAlphaNum)
-- * Document
document :: Parser Document
-document = whiteSpace *>
- (Document <$> many1 definition)
+document = whiteSpace
+ *> (Document <$> many1 definition)
-- Try SelectionSet when no definition
<|> (Document . pure
. DefinitionOperation
@@ -322,11 +325,8 @@ optempty = option mempty
-- ** WhiteSpace
--
whiteSpace :: Parser ()
-whiteSpace =
- skipMany (satisfy (\c -> isSpace c || ',' == c || isEndOfLine c))
-
-skipComments :: Parser ()
-skipComments = skipMany comment
-
-comment :: Parser Text
-comment = "#" *> (pack <$> manyTill anyChar endOfLine)
+whiteSpace = peekChar >>= \case
+ Just c -> if isSpace c || c == ','
+ then anyChar *> whiteSpace
+ else when (c == '#') $ manyTill anyChar endOfLine *> whiteSpace
+ _ -> return ()
diff --git a/TODO b/TODO
index 7f88296..c8e5bc3 100644
--- a/TODO
+++ b/TODO
@@ -5,9 +5,8 @@
- Deal with Location
## Parser
-- Handle comments
- Secure Names
-- Optimize `name`: `take...`, `T.fold`, ...
+- Optimize `name` and `whiteSpace`: `take...`, `T.fold`, ...
- Handle escape characters in string literals
- Guard for `on` in `FragmentSpread`
- Tests!