{- -----------------------------------------------------------------------------
Copyright 2019-2020 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Safe #-}

module Parser.DefinedCategory (
) where

import Control.Monad (when)
import Prelude hiding (pi)
import Text.Parsec

import Base.CompileError
import Parser.Common
import Parser.Procedure ()
import Parser.TypeCategory
import Parser.TypeInstance ()
import Types.DefinedCategory
import Types.Procedure
import Types.TypeCategory
import Types.TypeInstance
import Types.Variance


instance ParseFromSource (DefinedCategory SourcePos) where
  sourceParser :: ParserE m (DefinedCategory SourcePos)
sourceParser = String
-> ParserE m (DefinedCategory SourcePos)
-> ParserE m (DefinedCategory SourcePos)
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"defined concrete category" (ParserE m (DefinedCategory SourcePos)
 -> ParserE m (DefinedCategory SourcePos))
-> ParserE m (DefinedCategory SourcePos)
-> ParserE m (DefinedCategory SourcePos)
forall a b. (a -> b) -> a -> b
$ do
    SourcePos
c <- ParsecT String () m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
    ParserE m ()
forall (m :: * -> *). Monad m => ParserE m ()
kwDefine
    CategoryName
n <- ParserE m CategoryName
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
    ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
"{")
    ([ValueRefine SourcePos]
ds,[ValueDefine SourcePos]
rs) <- ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
parseRefinesDefines
    ([ValueParam SourcePos]
pi,[ParamFilter SourcePos]
fi) <- ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
parseInternalParams ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
forall (m :: * -> *) a. Monad m => a -> m a
return ([],[])
    ([DefinedMember SourcePos]
ms,[ExecutableProcedure SourcePos]
ps,[ScopedFunction SourcePos]
fs) <- CategoryName
-> ParserE
     m
     ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
      [ScopedFunction SourcePos])
forall (m :: * -> *).
CompileErrorM m =>
CategoryName
-> ParserE
     m
     ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
      [ScopedFunction SourcePos])
parseMemberProcedureFunction CategoryName
n
    ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
"}")
    DefinedCategory SourcePos -> ParserE m (DefinedCategory SourcePos)
forall (m :: * -> *) a. Monad m => a -> m a
return (DefinedCategory SourcePos
 -> ParserE m (DefinedCategory SourcePos))
-> DefinedCategory SourcePos
-> ParserE m (DefinedCategory SourcePos)
forall a b. (a -> b) -> a -> b
$ [SourcePos]
-> CategoryName
-> [ValueParam SourcePos]
-> [ValueRefine SourcePos]
-> [ValueDefine SourcePos]
-> [ParamFilter SourcePos]
-> [DefinedMember SourcePos]
-> [ExecutableProcedure SourcePos]
-> [ScopedFunction SourcePos]
-> DefinedCategory SourcePos
forall c.
[c]
-> CategoryName
-> [ValueParam c]
-> [ValueRefine c]
-> [ValueDefine c]
-> [ParamFilter c]
-> [DefinedMember c]
-> [ExecutableProcedure c]
-> [ScopedFunction c]
-> DefinedCategory c
DefinedCategory [SourcePos
c] CategoryName
n [ValueParam SourcePos]
pi [ValueRefine SourcePos]
ds [ValueDefine SourcePos]
rs [ParamFilter SourcePos]
fi [DefinedMember SourcePos]
ms [ExecutableProcedure SourcePos]
ps [ScopedFunction SourcePos]
fs
    where
      parseRefinesDefines :: ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
parseRefinesDefines = ([([ValueRefine SourcePos], [ValueDefine SourcePos])]
 -> ([ValueRefine SourcePos], [ValueDefine SourcePos]))
-> ParsecT
     String () m [([ValueRefine SourcePos], [ValueDefine SourcePos])]
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [([ValueRefine SourcePos], [ValueDefine SourcePos])]
-> ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall (f :: * -> *) a b.
(Foldable f, Monoid a, Monoid b) =>
f (a, b) -> (a, b)
merge2 (ParsecT
   String () m [([ValueRefine SourcePos], [ValueDefine SourcePos])]
 -> ParsecT
      String () m ([ValueRefine SourcePos], [ValueDefine SourcePos]))
-> ParsecT
     String () m [([ValueRefine SourcePos], [ValueDefine SourcePos])]
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall a b. (a -> b) -> a -> b
$ ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
-> ParserE m ()
-> ParsecT
     String () m [([ValueRefine SourcePos], [ValueDefine SourcePos])]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepBy ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
refineOrDefine ParserE m ()
forall (m :: * -> *). Monad m => ParserE m ()
optionalSpace
      refineOrDefine :: ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
refineOrDefine = String
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"refine or define" (ParsecT
   String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
 -> ParsecT
      String () m ([ValueRefine SourcePos], [ValueDefine SourcePos]))
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall a b. (a -> b) -> a -> b
$ ParsecT String () m (ValueRefine SourcePos)
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall (m :: * -> *) a b.
(Functor m, Monad m) =>
m a -> m ([a], [b])
put12 ParsecT String () m (ValueRefine SourcePos)
forall (m :: * -> *).
CompileErrorM m =>
ParserE m (ValueRefine SourcePos)
singleRefine ParsecT
  String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT String () m (ValueDefine SourcePos)
-> ParsecT
     String () m ([ValueRefine SourcePos], [ValueDefine SourcePos])
forall (m :: * -> *) b a.
(Functor m, Monad m) =>
m b -> m ([a], [b])
put22 ParsecT String () m (ValueDefine SourcePos)
forall (m :: * -> *).
CompileErrorM m =>
ParserE m (ValueDefine SourcePos)
singleDefine
      parseInternalParams :: ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
parseInternalParams = String
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"internal params" (ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
 -> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos]))
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
forall a b. (a -> b) -> a -> b
$ do
        ParserE m () -> ParserE m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParserE m ()
forall (m :: * -> *). Monad m => ParserE m ()
kwTypes
        [ValueParam SourcePos]
pi <- ParserE m ()
-> ParserE m ()
-> ParsecT String () m [ValueParam SourcePos]
-> ParsecT String () m [ValueParam SourcePos]
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between (ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (ParserE m () -> ParserE m ()) -> ParserE m () -> ParserE m ()
forall a b. (a -> b) -> a -> b
$ String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
"<")
                      (ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (ParserE m () -> ParserE m ()) -> ParserE m () -> ParserE m ()
forall a b. (a -> b) -> a -> b
$ String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
">")
                      (ParsecT String () m (ValueParam SourcePos)
-> ParserE m () -> ParsecT String () m [ValueParam SourcePos]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepBy ParsecT String () m (ValueParam SourcePos)
singleParam (ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (ParserE m () -> ParserE m ()) -> ParserE m () -> ParserE m ()
forall a b. (a -> b) -> a -> b
$ String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
","))
        [ParamFilter SourcePos]
fi <- ParsecT String () m [ParamFilter SourcePos]
parseInternalFilters
        ([ValueParam SourcePos], [ParamFilter SourcePos])
-> ParserE m ([ValueParam SourcePos], [ParamFilter SourcePos])
forall (m :: * -> *) a. Monad m => a -> m a
return ([ValueParam SourcePos]
pi,[ParamFilter SourcePos]
fi)
      parseInternalFilters :: ParsecT String () m [ParamFilter SourcePos]
parseInternalFilters = do
        ParserE m () -> ParserE m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParserE m () -> ParserE m ()) -> ParserE m () -> ParserE m ()
forall a b. (a -> b) -> a -> b
$ ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
"{")
        [ParamFilter SourcePos]
fi <- ParsecT String () m [ParamFilter SourcePos]
forall (m :: * -> *).
CompileErrorM m =>
ParserE m [ParamFilter SourcePos]
parseFilters
        ParserE m () -> ParserE m ()
forall (m :: * -> *) a. Monad m => ParserE m a -> ParserE m a
sepAfter (String -> ParserE m ()
forall (m :: * -> *). Monad m => String -> ParserE m ()
string_ String
"}")
        [ParamFilter SourcePos]
-> ParsecT String () m [ParamFilter SourcePos]
forall (m :: * -> *) a. Monad m => a -> m a
return [ParamFilter SourcePos]
fi
      singleParam :: ParsecT String () m (ValueParam SourcePos)
singleParam = String
-> ParsecT String () m (ValueParam SourcePos)
-> ParsecT String () m (ValueParam SourcePos)
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"param declaration" (ParsecT String () m (ValueParam SourcePos)
 -> ParsecT String () m (ValueParam SourcePos))
-> ParsecT String () m (ValueParam SourcePos)
-> ParsecT String () m (ValueParam SourcePos)
forall a b. (a -> b) -> a -> b
$ do
        SourcePos
c <- ParsecT String () m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
        ParamName
n <- ParserE m ParamName
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
        ValueParam SourcePos -> ParsecT String () m (ValueParam SourcePos)
forall (m :: * -> *) a. Monad m => a -> m a
return (ValueParam SourcePos
 -> ParsecT String () m (ValueParam SourcePos))
-> ValueParam SourcePos
-> ParsecT String () m (ValueParam SourcePos)
forall a b. (a -> b) -> a -> b
$ [SourcePos] -> ParamName -> Variance -> ValueParam SourcePos
forall c. [c] -> ParamName -> Variance -> ValueParam c
ValueParam [SourcePos
c] ParamName
n Variance
Invariant

instance ParseFromSource (DefinedMember SourcePos) where
  sourceParser :: ParserE m (DefinedMember SourcePos)
sourceParser = String
-> ParserE m (DefinedMember SourcePos)
-> ParserE m (DefinedMember SourcePos)
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"defined member" (ParserE m (DefinedMember SourcePos)
 -> ParserE m (DefinedMember SourcePos))
-> ParserE m (DefinedMember SourcePos)
-> ParserE m (DefinedMember SourcePos)
forall a b. (a -> b) -> a -> b
$ do
    SourcePos
c <- ParsecT String () m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
    (SymbolScope
s,ValueType
t) <- ParsecT String () m (SymbolScope, ValueType)
-> ParsecT String () m (SymbolScope, ValueType)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT String () m (SymbolScope, ValueType)
parseType
    VariableName
n <- ParserE m VariableName
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
    Maybe (Expression SourcePos)
e <- if SymbolScope
s SymbolScope -> SymbolScope -> Bool
forall a. Eq a => a -> a -> Bool
== SymbolScope
ValueScope
            then Maybe (Expression SourcePos)
-> ParsecT String () m (Maybe (Expression SourcePos))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Expression SourcePos)
forall a. Maybe a
Nothing
            else ParsecT String () m (Maybe (Expression SourcePos))
parseInit
    DefinedMember SourcePos -> ParserE m (DefinedMember SourcePos)
forall (m :: * -> *) a. Monad m => a -> m a
return (DefinedMember SourcePos -> ParserE m (DefinedMember SourcePos))
-> DefinedMember SourcePos -> ParserE m (DefinedMember SourcePos)
forall a b. (a -> b) -> a -> b
$ [SourcePos]
-> SymbolScope
-> ValueType
-> VariableName
-> Maybe (Expression SourcePos)
-> DefinedMember SourcePos
forall c.
[c]
-> SymbolScope
-> ValueType
-> VariableName
-> Maybe (Expression c)
-> DefinedMember c
DefinedMember [SourcePos
c] SymbolScope
s ValueType
t VariableName
n Maybe (Expression SourcePos)
e
    where
      parseInit :: ParsecT String () m (Maybe (Expression SourcePos))
parseInit = String
-> ParsecT String () m (Maybe (Expression SourcePos))
-> ParsecT String () m (Maybe (Expression SourcePos))
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"member initializer" (ParsecT String () m (Maybe (Expression SourcePos))
 -> ParsecT String () m (Maybe (Expression SourcePos)))
-> ParsecT String () m (Maybe (Expression SourcePos))
-> ParsecT String () m (Maybe (Expression SourcePos))
forall a b. (a -> b) -> a -> b
$ do
        ParserE m ()
forall (m :: * -> *). Monad m => ParserE m ()
assignOperator
        Expression SourcePos
e <- ParserE m (Expression SourcePos)
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
        Maybe (Expression SourcePos)
-> ParsecT String () m (Maybe (Expression SourcePos))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Expression SourcePos)
 -> ParsecT String () m (Maybe (Expression SourcePos)))
-> Maybe (Expression SourcePos)
-> ParsecT String () m (Maybe (Expression SourcePos))
forall a b. (a -> b) -> a -> b
$ Expression SourcePos -> Maybe (Expression SourcePos)
forall a. a -> Maybe a
Just Expression SourcePos
e
      parseType :: ParsecT String () m (SymbolScope, ValueType)
parseType = do
        SymbolScope
s <- ParserE m SymbolScope
forall (m :: * -> *). Monad m => ParserE m SymbolScope
parseScope
        ValueType
t <- ParserE m ValueType
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
        (SymbolScope, ValueType)
-> ParsecT String () m (SymbolScope, ValueType)
forall (m :: * -> *) a. Monad m => a -> m a
return (SymbolScope
s,ValueType
t)

parseMemberProcedureFunction :: CompileErrorM m =>
  CategoryName -> ParserE m ([DefinedMember SourcePos],
                             [ExecutableProcedure SourcePos],
                             [ScopedFunction SourcePos])
parseMemberProcedureFunction :: CategoryName
-> ParserE
     m
     ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
      [ScopedFunction SourcePos])
parseMemberProcedureFunction CategoryName
n = do
  ([DefinedMember SourcePos]
ms,[ExecutableProcedure SourcePos]
ps,[(ScopedFunction SourcePos, ExecutableProcedure SourcePos)]
fs) <- ParserE m (DefinedMember SourcePos)
-> ParserE m (ExecutableProcedure SourcePos)
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ParserE
     m
     ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
      [(ScopedFunction SourcePos, ExecutableProcedure SourcePos)])
forall (m :: * -> *) a b c.
Monad m =>
ParserE m a
-> ParserE m b -> ParserE m c -> ParserE m ([a], [b], [c])
parseAny3 (ParserE m (DefinedMember SourcePos)
forall a. ParserE m a
catchUnscopedType ParserE m (DefinedMember SourcePos)
-> ParserE m (DefinedMember SourcePos)
-> ParserE m (DefinedMember SourcePos)
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParserE m (DefinedMember SourcePos)
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser) ParserE m (ExecutableProcedure SourcePos)
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser ParserE m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
singleFunction
  let ps2 :: [ExecutableProcedure SourcePos]
ps2 = [ExecutableProcedure SourcePos]
ps [ExecutableProcedure SourcePos]
-> [ExecutableProcedure SourcePos]
-> [ExecutableProcedure SourcePos]
forall a. [a] -> [a] -> [a]
++ ((ScopedFunction SourcePos, ExecutableProcedure SourcePos)
 -> ExecutableProcedure SourcePos)
-> [(ScopedFunction SourcePos, ExecutableProcedure SourcePos)]
-> [ExecutableProcedure SourcePos]
forall a b. (a -> b) -> [a] -> [b]
map (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ExecutableProcedure SourcePos
forall a b. (a, b) -> b
snd [(ScopedFunction SourcePos, ExecutableProcedure SourcePos)]
fs
  let fs2 :: [ScopedFunction SourcePos]
fs2 = ((ScopedFunction SourcePos, ExecutableProcedure SourcePos)
 -> ScopedFunction SourcePos)
-> [(ScopedFunction SourcePos, ExecutableProcedure SourcePos)]
-> [ScopedFunction SourcePos]
forall a b. (a -> b) -> [a] -> [b]
map (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ScopedFunction SourcePos
forall a b. (a, b) -> a
fst [(ScopedFunction SourcePos, ExecutableProcedure SourcePos)]
fs
  ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
 [ScopedFunction SourcePos])
-> ParserE
     m
     ([DefinedMember SourcePos], [ExecutableProcedure SourcePos],
      [ScopedFunction SourcePos])
forall (m :: * -> *) a. Monad m => a -> m a
return ([DefinedMember SourcePos]
ms,[ExecutableProcedure SourcePos]
ps2,[ScopedFunction SourcePos]
fs2) where
    singleFunction :: ParserE m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
singleFunction = String
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"function" (ParserE
   m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
 -> ParserE
      m (ScopedFunction SourcePos, ExecutableProcedure SourcePos))
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
forall a b. (a -> b) -> a -> b
$ do
      ScopedFunction SourcePos
f <- ParserE m SymbolScope
-> ParserE m CategoryName -> ParserE m (ScopedFunction SourcePos)
forall (m :: * -> *).
CompileErrorM m =>
ParserE m SymbolScope
-> ParserE m CategoryName -> ParserE m (ScopedFunction SourcePos)
parseScopedFunction ParserE m SymbolScope
forall (m :: * -> *). Monad m => ParserE m SymbolScope
parseScope (CategoryName -> ParserE m CategoryName
forall (m :: * -> *) a. Monad m => a -> m a
return CategoryName
n)
      SourcePos
c <- ParsecT String () m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
      ExecutableProcedure SourcePos
p <- String
-> ParserE m (ExecutableProcedure SourcePos)
-> ParserE m (ExecutableProcedure SourcePos)
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled (String
"definition of function " String -> String -> String
forall a. [a] -> [a] -> [a]
++ FunctionName -> String
forall a. Show a => a -> String
show (ScopedFunction SourcePos -> FunctionName
forall c. ScopedFunction c -> FunctionName
sfName ScopedFunction SourcePos
f)) (ParserE m (ExecutableProcedure SourcePos)
 -> ParserE m (ExecutableProcedure SourcePos))
-> ParserE m (ExecutableProcedure SourcePos)
-> ParserE m (ExecutableProcedure SourcePos)
forall a b. (a -> b) -> a -> b
$ ParserE m (ExecutableProcedure SourcePos)
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser
      Bool -> ParsecT String () m () -> ParsecT String () m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ScopedFunction SourcePos -> FunctionName
forall c. ScopedFunction c -> FunctionName
sfName ScopedFunction SourcePos
f FunctionName -> FunctionName -> Bool
forall a. Eq a => a -> a -> Bool
/= ExecutableProcedure SourcePos -> FunctionName
forall c. ExecutableProcedure c -> FunctionName
epName ExecutableProcedure SourcePos
p) (ParsecT String () m () -> ParsecT String () m ())
-> ParsecT String () m () -> ParsecT String () m ()
forall a b. (a -> b) -> a -> b
$
        SourcePos -> String -> ParsecT String () m ()
forall (m :: * -> *) a.
CompileErrorM m =>
SourcePos -> String -> ParserE m a
parseErrorM SourcePos
c (String -> ParsecT String () m ())
-> String -> ParsecT String () m ()
forall a b. (a -> b) -> a -> b
$ String
"expecting definition of function " String -> String -> String
forall a. [a] -> [a] -> [a]
++ FunctionName -> String
forall a. Show a => a -> String
show (ScopedFunction SourcePos -> FunctionName
forall c. ScopedFunction c -> FunctionName
sfName ScopedFunction SourcePos
f) String -> String -> String
forall a. [a] -> [a] -> [a]
++
                        String
" but got definition of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ FunctionName -> String
forall a. Show a => a -> String
show (ExecutableProcedure SourcePos -> FunctionName
forall c. ExecutableProcedure c -> FunctionName
epName ExecutableProcedure SourcePos
p)
      (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
-> ParserE
     m (ScopedFunction SourcePos, ExecutableProcedure SourcePos)
forall (m :: * -> *) a. Monad m => a -> m a
return (ScopedFunction SourcePos
f,ExecutableProcedure SourcePos
p)
    catchUnscopedType :: ParserE m a
catchUnscopedType = String -> ParserE m a -> ParserE m a
forall (m :: * -> *) a.
Monad m =>
String -> ParserE m a -> ParserE m a
labeled String
"" (ParserE m a -> ParserE m a) -> ParserE m a -> ParserE m a
forall a b. (a -> b) -> a -> b
$ do
      SourcePos
c <- ParsecT String () m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
      ValueType
_ <- ParsecT String () m ValueType -> ParsecT String () m ValueType
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT String () m ValueType
forall a (m :: * -> *).
(ParseFromSource a, CompileErrorM m) =>
ParserE m a
sourceParser :: CompileErrorM m => ParserE m ValueType
      SourcePos -> String -> ParserE m a
forall (m :: * -> *) a.
CompileErrorM m =>
SourcePos -> String -> ParserE m a
parseErrorM SourcePos
c String
"members must have an explicit @value or @category scope"