From 4f4e31805ab259fe7c818f0b4f58fce2ab3ee373 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Fri, 18 Sep 2015 15:43:22 +0200 Subject: [PATCH] Parse secure names --- Data/GraphQL/Parser.hs | 17 ++++++++++------- TODO | 4 +--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Data/GraphQL/Parser.hs b/Data/GraphQL/Parser.hs index 99700c0..b4af436 100644 --- a/Data/GraphQL/Parser.hs +++ b/Data/GraphQL/Parser.hs @@ -12,7 +12,7 @@ import Data.Monoid (Monoid, mempty) import Control.Applicative ((<|>), empty, many, optional) import Control.Monad (when) import Data.Char -import Data.Text (Text, pack) +import Data.Text (Text, append) import Data.Attoparsec.Text ( Parser , () @@ -20,25 +20,28 @@ import Data.Attoparsec.Text , decimal , double , endOfLine + , inClass , many1 , manyTill , option , peekChar - , satisfy , sepBy1 , signed + , takeWhile + , takeWhile1 ) import Data.GraphQL.AST -- * Name --- XXX: Handle starting `_` and no number at the beginning: --- https://facebook.github.io/graphql/#sec-Names --- TODO: Use takeWhile1 instead for efficiency. With takeWhile1 there is no --- parsing failure. name :: Parser Name -name = tok $ pack <$> many1 (satisfy isAlphaNum) +name = tok $ append <$> takeWhile1 isA_z + <*> takeWhile ((||) <$> isDigit <*> isA_z) + where + -- `isAlpha` handles many more Unicode Chars + isA_z = inClass $ '_' : ['A'..'Z'] ++ ['a'..'z'] + -- * Document diff --git a/TODO b/TODO index 6e61bf1..488947e 100644 --- a/TODO +++ b/TODO @@ -6,15 +6,13 @@ - Deal with location ## Parser -- Secure Names -- Optimize `name` and `whiteSpace`: `take...`, `T.fold`, ... - Handle escape characters in string literals - Guard for `on` in `FragmentSpread` - Handle `[Const]` grammar parameter. Need examples - Handle `maxBound` Int values. - Diagnostics. Perhaps port to `parsers` and use `trifecta` for diagnostics, and `attoparsec` for performance. -- Improve comment handling: perhaps front the main parser with a lexer. +- Optimize `whiteSpace`, perhaps front the main parser with a lexer. ## Tests