Handle escaped quotes for GraphQL String Values

This also includes a new type for Value String.

The tests fail now, although it parses successfully. I'll use a pretty
printer in next commit so that it's easier to spot the differences.
Onces this is working I'll add the rest of the escaped characters.
This commit is contained in:
Danny Navarro 2015-09-18 18:11:11 +02:00
parent cb9977141d
commit 899fa1b531
2 changed files with 17 additions and 3 deletions

View File

@ -66,12 +66,14 @@ data Value = ValueVariable Variable
-- GraphQL Float is double precison -- GraphQL Float is double precison
| ValueFloat Double | ValueFloat Double
| ValueBoolean Bool | ValueBoolean Bool
| ValueString Text | ValueString StringValue
| ValueEnum Name | ValueEnum Name
| ValueList ListValue | ValueList ListValue
| ValueObject ObjectValue | ValueObject ObjectValue
deriving (Eq,Show) deriving (Eq,Show)
newtype StringValue = StringValue Text deriving (Eq,Show)
newtype ListValue = ListValue [Value] deriving (Eq,Show) newtype ListValue = ListValue [Value] deriving (Eq,Show)
newtype ObjectValue = ObjectValue [ObjectField] deriving (Eq,Show) newtype ObjectValue = ObjectValue [ObjectField] deriving (Eq,Show)

View File

@ -14,6 +14,7 @@ import Data.Char
import Data.Foldable (traverse_) import Data.Foldable (traverse_)
import Data.Text (Text, append) import Data.Text (Text, append)
import qualified Data.Text as T
import Data.Attoparsec.Text import Data.Attoparsec.Text
( Parser ( Parser
, (<?>) , (<?>)
@ -28,6 +29,7 @@ import Data.Attoparsec.Text
, peekChar , peekChar
, sepBy1 , sepBy1
, signed , signed
, takeText
, takeWhile , takeWhile
, takeWhile1 , takeWhile1
) )
@ -156,13 +158,23 @@ value = ValueVariable <$> variable
<|> ValueInt <$> tok (signed decimal) <|> ValueInt <$> tok (signed decimal)
<|> ValueFloat <$> tok (signed double) <|> ValueFloat <$> tok (signed double)
<|> ValueBoolean <$> bool <|> ValueBoolean <$> bool
-- TODO: Handle escape characters, unicode, etc <|> ValueString <$> stringValue
<|> ValueString <$> quotes name
-- `true` and `false` have been tried before -- `true` and `false` have been tried before
<|> ValueEnum <$> name <|> ValueEnum <$> name
<|> ValueList <$> listValue <|> ValueList <$> listValue
<|> ValueObject <$> objectValue <|> ValueObject <$> objectValue
stringValue :: Parser StringValue
stringValue = StringValue <$> quotes (T.foldl' step mempty <$> takeText)
where
-- TODO: Handle unicode and the rest of escaped chars.
step acc c
| T.null acc = T.singleton c
| T.last acc == '\\' = if c == '"' then T.init acc `T.snoc` '"'
else acc `T.snoc` c
| otherwise = acc `T.snoc` c
-- Notice it can be empty -- Notice it can be empty
listValue :: Parser ListValue listValue :: Parser ListValue
listValue = ListValue <$> brackets (many value) listValue = ListValue <$> brackets (many value)