Skip to content

Commit

Permalink
Intermediate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jul 30, 2016
1 parent 46b8ce4 commit bf9f3fd
Showing 1 changed file with 64 additions and 14 deletions.
78 changes: 64 additions & 14 deletions src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -277,30 +277,37 @@ enumTypeDeclaration = do
char ';'
return $ TypeDeclaration typename (EnumType memberSet) docs'

fields :: Parser [Field]
fields = do
fieldType <- typeExpression <?> "field type"
fieldsOrParameters :: (String, String)
-> (Name -> TypeExpression -> Maybe Docs -> a)
-> Parser [a]
fieldsOrParameters (label, pluralLabel) make = do
type' <- typeExpression <?> (label ++ " type")
spaces1
fieldName <- name <?> "field name"
name' <- name <?> (label ++ " name")
spaces
let mkField = Field fieldName fieldType
followedByComma mkField <|> do
d <- optional docs' <?> "field docs"
return [mkField d]
let makeWithDocs = make name' type'
followedByComma makeWithDocs <|> do
d <- optional docs' <?> (label ++ " docs")
return [makeWithDocs d]
where
followedByComma :: (Maybe Docs -> Field) -> Parser [Field]
followedByComma mkField = do
recur :: Parser [a]
recur = fieldsOrParameters (label, pluralLabel) make
followedByComma :: (Maybe Docs -> a) -> Parser [a]
followedByComma makeWithDocs = do
char ','
spaces
d <- optional docs' <?> "field docs"
rest <- option [] fields <?> "rest of fields"
return $ mkField d : rest
d <- optional docs' <?> (label ++ " docs")
rest <- option [] recur <?> ("rest of " ++ pluralLabel)
return $ makeWithDocs d : rest
docs' :: Parser Docs
docs' = do
d <- docs <?> "field docs"
d <- docs <?> (label ++ " docs")
spaces
return d

fields :: Parser [Field]
fields = fieldsOrParameters ("label", "labels") Field

fieldSet :: Parser (DeclarationSet Field)
fieldSet = do
fields' <- fields <?> "fields"
Expand Down Expand Up @@ -370,6 +377,49 @@ typeDeclaration =
unionTypeDeclaration
) <?> "type declaration (e.g. boxed, enum, record, union)"

parameters :: Parser [Parameter]
parameters = fieldsOrParameters ("parameter", "parameters") Parameter

parameterSet :: Parser (DeclarationSet Parameter)
parameterSet = do
params <- parameters <?> "method parameters"
handleNameDuplication "parameter" params return

methods :: Parser [Method]
methods = do
returnType <- typeExpression <?> "method return type"
spaces
methodName <- name <?> "method name"
spaces
char '('
spaces
char ')'
-------------------------------------------------------------------------------------------------------------

methodSet :: Parser (DeclarationSet Method)
methodSet = do
methods' <- methods <?> "service methods"
handleNameDuplication "method" methods' return

serviceDeclaration :: Parser ServiceDeclaration
serviceDeclaration = do
string "service" <?> "service keyword"
spaces
serviceName <- name <?> "service name"
spaces
char '('
spaces
docs' <- optional $ do
d <- docs <?> "service docs"
spaces
return d
methods' <- methodSet <?> "service methods"
spaces
char ')'
spaces
char ';'
return $ ServiceDeclaration serviceName (Service methods') docs'

modulePath :: Parser ModulePath
modulePath = do
idents <- sepBy1 (identifier <?> "module identifier")
Expand Down

0 comments on commit bf9f3fd

Please sign in to comment.