Reduce usage of the opt parser

opt directives = some directive
All other occurrences of opt parse an optional list withing some
delimiters (braces, parens).
This commit is contained in:
Eugen Wissner 2020-01-13 08:11:22 +01:00
parent 6d951491be
commit f4f076fa59

View File

@ -32,8 +32,8 @@ operationDefinition = SelectionSet <$> selectionSet
operationDefinition' operationDefinition'
= OperationDefinition <$> operationType = OperationDefinition <$> operationType
<*> optional name <*> optional name
<*> opt variableDefinitions <*> variableDefinitions
<*> opt directives <*> directives
<*> selectionSet <*> selectionSet
operationType :: Parser OperationType operationType :: Parser OperationType
@ -47,7 +47,7 @@ selectionSet :: Parser SelectionSet
selectionSet = braces $ NonEmpty.some selection selectionSet = braces $ NonEmpty.some selection
selectionSetOpt :: Parser SelectionSetOpt selectionSetOpt :: Parser SelectionSetOpt
selectionSetOpt = braces $ some selection selectionSetOpt = listOptIn braces selection
selection :: Parser Selection selection :: Parser Selection
selection = field selection = field
@ -61,9 +61,9 @@ field :: Parser Selection
field = Field field = Field
<$> optional alias <$> optional alias
<*> name <*> name
<*> opt arguments <*> arguments
<*> opt directives <*> directives
<*> opt selectionSetOpt <*> selectionSetOpt
alias :: Parser Alias alias :: Parser Alias
alias = try $ name <* colon alias = try $ name <* colon
@ -71,7 +71,7 @@ alias = try $ name <* colon
-- * Arguments -- * Arguments
arguments :: Parser [Argument] arguments :: Parser [Argument]
arguments = parens $ some argument arguments = listOptIn parens argument
argument :: Parser Argument argument :: Parser Argument
argument = Argument <$> name <* colon <*> value argument = Argument <$> name <* colon <*> value
@ -82,13 +82,13 @@ fragmentSpread :: Parser Selection
fragmentSpread = FragmentSpread fragmentSpread = FragmentSpread
<$ spread <$ spread
<*> fragmentName <*> fragmentName
<*> opt directives <*> directives
inlineFragment :: Parser Selection inlineFragment :: Parser Selection
inlineFragment = InlineFragment inlineFragment = InlineFragment
<$ spread <$ spread
<*> optional typeCondition <*> optional typeCondition
<*> opt directives <*> directives
<*> selectionSet <*> selectionSet
fragmentDefinition :: Parser FragmentDefinition fragmentDefinition :: Parser FragmentDefinition
@ -96,7 +96,7 @@ fragmentDefinition = FragmentDefinition
<$ symbol "fragment" <$ symbol "fragment"
<*> name <*> name
<*> typeCondition <*> typeCondition
<*> opt directives <*> directives
<*> selectionSet <*> selectionSet
fragmentName :: Parser Name fragmentName :: Parser Name
@ -139,13 +139,15 @@ objectField = ObjectField <$> name <* symbol ":" <*> value
-- * Variables -- * Variables
variableDefinitions :: Parser [VariableDefinition] variableDefinitions :: Parser [VariableDefinition]
variableDefinitions = parens $ some variableDefinition variableDefinitions = listOptIn parens variableDefinition
variableDefinition :: Parser VariableDefinition variableDefinition :: Parser VariableDefinition
variableDefinition = VariableDefinition <$> variable variableDefinition = VariableDefinition
<* colon <$> variable
<*> type_ <* colon
<*> optional defaultValue <*> type_
<*> optional defaultValue
variable :: Parser Name variable :: Parser Name
variable = dollar *> name variable = dollar *> name
@ -168,18 +170,18 @@ nonNullType = NonNullTypeNamed <$> name <* bang
-- * Directives -- * Directives
directives :: Parser [Directive] directives :: Parser [Directive]
directives = some directive directives = many directive
directive :: Parser Directive directive :: Parser Directive
directive = Directive directive = Directive
<$ at <$ at
<*> name <*> name
<*> opt arguments <*> arguments
-- * Internal -- * Internal
opt :: Monoid a => Parser a -> Parser a listOptIn :: (Parser [a] -> Parser [a]) -> Parser a -> Parser [a]
opt = option mempty listOptIn surround = option [] . surround . some
-- Hack to reverse parser success -- Hack to reverse parser success
but :: Parser a -> Parser () but :: Parser a -> Parser ()