{-# LANGUAGE TypeSynonymInstances #-}
{- |
Module      :  ./CSL/Parse_AS_Basic.hs
Description :  Parser for basic specs
Copyright   :  (c) Dominik Dietrich, DFKI Bremen 2010
License     :  GPLv2 or higher, see LICENSE.txt

Maintainer  :  dominik.dietrich@dfki.de
Stability   :  experimental
Portability :  non-portable

Parser for abstract syntax for CSL
-}

module CSL.Parse_AS_Basic where

import qualified Common.AnnoState as AnnoState
import Common.Id as Id
import Common.Keywords as Keywords
import Common.Lexer as Lexer
import Common.Parsec
import Common.AS_Annotation as AS_Anno
import Common.GlobalAnnotations (PrefixMap)

import CSL.AS_BASIC_CSL
import CSL.ASUtils
import CSL.Print_AS ()
import CSL.Keywords
import CSL.TreePO

import Numeric
import Data.Char

import qualified Data.Set as Set

import Text.ParserCombinators.Parsec as Parsec
import Text.ParserCombinators.Parsec.Error
import Control.Monad
import qualified Control.Monad.Fail as Fail

-- TODO: extract range information for the basic term and command types

-- ---------------------------------------------------------------------------

-- * Interface to the syntax class

-- ---------------------------------------------------------------------------

parseBasicSpec :: Maybe (PrefixMap -> AnnoState.AParser st BASIC_SPEC)
parseBasicSpec :: Maybe (PrefixMap -> AParser st BASIC_SPEC)
parseBasicSpec = (PrefixMap -> AParser st BASIC_SPEC)
-> Maybe (PrefixMap -> AParser st BASIC_SPEC)
forall a. a -> Maybe a
Just (AParser st BASIC_SPEC -> PrefixMap -> AParser st BASIC_SPEC
forall a b. a -> b -> a
const AParser st BASIC_SPEC
forall st. AParser st BASIC_SPEC
basicSpec)

parseSymbItems :: Maybe (GenParser Char st SYMB_ITEMS)
parseSymbItems :: Maybe (GenParser Char st SYMB_ITEMS)
parseSymbItems = GenParser Char st SYMB_ITEMS
-> Maybe (GenParser Char st SYMB_ITEMS)
forall a. a -> Maybe a
Just GenParser Char st SYMB_ITEMS
forall st. GenParser Char st SYMB_ITEMS
symbItems

parseSymbMapItems :: Maybe (GenParser Char st SYMB_MAP_ITEMS)
parseSymbMapItems :: Maybe (GenParser Char st SYMB_MAP_ITEMS)
parseSymbMapItems = GenParser Char st SYMB_MAP_ITEMS
-> Maybe (GenParser Char st SYMB_MAP_ITEMS)
forall a. a -> Maybe a
Just GenParser Char st SYMB_MAP_ITEMS
forall st. GenParser Char st SYMB_MAP_ITEMS
symbMapItems


-- ---------------------------------------------------------------------------

-- * Parser utils

-- ---------------------------------------------------------------------------


addToPosition :: SourcePos -- ^ original position
              -> SourcePos -- ^ relative position
              -> SourcePos -- ^ new position
addToPosition :: SourcePos -> SourcePos -> SourcePos
addToPosition sp1 :: SourcePos
sp1 sp2 :: SourcePos
sp2
    | SourcePos -> Line
Parsec.sourceLine SourcePos
sp2 Line -> Line -> Bool
forall a. Eq a => a -> a -> Bool
== 1 =
        SourcePos -> Line -> SourcePos
setSourceColumn SourcePos
sp1 (Line -> SourcePos) -> Line -> SourcePos
forall a b. (a -> b) -> a -> b
$
                        SourcePos -> Line
Parsec.sourceColumn SourcePos
sp1 Line -> Line -> Line
forall a. Num a => a -> a -> a
+ SourcePos -> Line
Parsec.sourceColumn SourcePos
sp2 Line -> Line -> Line
forall a. Num a => a -> a -> a
- 1
    | Bool
otherwise =
        SourcePos -> Line -> SourcePos
setSourceColumn (SourcePos -> Line -> SourcePos
setSourceLine SourcePos
sp1
                        (Line -> SourcePos) -> Line -> SourcePos
forall a b. (a -> b) -> a -> b
$ SourcePos -> Line
Parsec.sourceLine SourcePos
sp1 Line -> Line -> Line
forall a. Num a => a -> a -> a
+ SourcePos -> Line
Parsec.sourceLine SourcePos
sp2 Line -> Line -> Line
forall a. Num a => a -> a -> a
- 1)
                            (Line -> SourcePos) -> Line -> SourcePos
forall a b. (a -> b) -> a -> b
$ SourcePos -> Line
Parsec.sourceColumn SourcePos
sp2

posInputParser :: a -> GenParser tok st (a, SourcePos, [tok], st)
posInputParser :: a -> GenParser tok st (a, SourcePos, [tok], st)
posInputParser x :: a
x = do
  SourcePos
pos <- ParsecT [tok] st Identity SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  [tok]
inp <- ParsecT [tok] st Identity [tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  st
st <- ParsecT [tok] st Identity st
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  (a, SourcePos, [tok], st)
-> GenParser tok st (a, SourcePos, [tok], st)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, SourcePos
pos, [tok]
inp, st
st)


{- Tests for subparser
let p1 = getState >>= many1 . string
runParser (string "h" >> runSubParser p1 "\n" "sourcename" >>= posInputParser) () "k" "h\n\nghurra"
-}
runSubParser :: GenParser tok st a -> st -> SourceName
             -> GenParser tok st' (Either ParseError (st, a))
runSubParser :: GenParser tok st a
-> st
-> SourceName
-> GenParser tok st' (Either ParseError (st, a))
runSubParser sp :: GenParser tok st a
sp st :: st
st sn :: SourceName
sn = do
  -- save the current state
  SourcePos
pos <- ParsecT [tok] st' Identity SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  [tok]
inp <- ParsecT [tok] st' Identity [tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  case GenParser tok st (a, SourcePos, [tok], st)
-> st
-> SourceName
-> [tok]
-> Either ParseError (a, SourcePos, [tok], st)
forall tok st a.
GenParser tok st a
-> st -> SourceName -> [tok] -> Either ParseError a
runParser (GenParser tok st a
sp GenParser tok st a
-> (a -> GenParser tok st (a, SourcePos, [tok], st))
-> GenParser tok st (a, SourcePos, [tok], st)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> GenParser tok st (a, SourcePos, [tok], st)
forall a tok st. a -> GenParser tok st (a, SourcePos, [tok], st)
posInputParser) st
st SourceName
sn [tok]
inp of
    Left err :: ParseError
err -> Either ParseError (st, a)
-> GenParser tok st' (Either ParseError (st, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ParseError (st, a)
 -> GenParser tok st' (Either ParseError (st, a)))
-> Either ParseError (st, a)
-> GenParser tok st' (Either ParseError (st, a))
forall a b. (a -> b) -> a -> b
$ ParseError -> Either ParseError (st, a)
forall a b. a -> Either a b
Left ParseError
err
    Right (x :: a
x, pos' :: SourcePos
pos', inp' :: [tok]
inp', st' :: st
st') -> do
      SourcePos -> ParsecT [tok] st' Identity ()
forall (m :: * -> *) s u. Monad m => SourcePos -> ParsecT s u m ()
setPosition (SourcePos -> ParsecT [tok] st' Identity ())
-> SourcePos -> ParsecT [tok] st' Identity ()
forall a b. (a -> b) -> a -> b
$ SourcePos -> SourcePos -> SourcePos
addToPosition SourcePos
pos SourcePos
pos'
      [tok] -> ParsecT [tok] st' Identity ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput [tok]
inp'
      Either ParseError (st, a)
-> GenParser tok st' (Either ParseError (st, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ParseError (st, a)
 -> GenParser tok st' (Either ParseError (st, a)))
-> Either ParseError (st, a)
-> GenParser tok st' (Either ParseError (st, a))
forall a b. (a -> b) -> a -> b
$ (st, a) -> Either ParseError (st, a)
forall a b. b -> Either a b
Right (st
st', a
x)


instance OperatorState (AnnoState.AnnoState st) where
    lookupOperator :: AnnoState st -> SourceName -> Line -> Either Bool OpInfo
lookupOperator _ = () -> SourceName -> Line -> Either Bool OpInfo
forall a.
OperatorState a =>
a -> SourceName -> Line -> Either Bool OpInfo
lookupOperator ()
    lookupBinder :: AnnoState st -> SourceName -> Maybe OpInfo
lookupBinder _ = () -> SourceName -> Maybe OpInfo
forall a. OperatorState a => a -> SourceName -> Maybe OpInfo
lookupBinder ()

data OpVarState a = OpVarState a (Set.Set String)

instance OperatorState a => OperatorState (OpVarState a) where
    lookupOperator :: OpVarState a -> SourceName -> Line -> Either Bool OpInfo
lookupOperator (OpVarState x :: a
x _) = a -> SourceName -> Line -> Either Bool OpInfo
forall a.
OperatorState a =>
a -> SourceName -> Line -> Either Bool OpInfo
lookupOperator a
x
    lookupBinder :: OpVarState a -> SourceName -> Maybe OpInfo
lookupBinder (OpVarState x :: a
x _) = a -> SourceName -> Maybe OpInfo
forall a. OperatorState a => a -> SourceName -> Maybe OpInfo
lookupBinder a
x
    addVar :: OpVarState a -> SourceName -> OpVarState a
addVar (OpVarState st :: a
st s :: Set SourceName
s) x :: SourceName
x = a -> Set SourceName -> OpVarState a
forall a. a -> Set SourceName -> OpVarState a
OpVarState a
st (Set SourceName -> OpVarState a) -> Set SourceName -> OpVarState a
forall a b. (a -> b) -> a -> b
$ SourceName -> Set SourceName -> Set SourceName
forall a. Ord a => a -> Set a -> Set a
Set.insert SourceName
x Set SourceName
s
    isVar :: OpVarState a -> SourceName -> Bool
isVar (OpVarState _ s :: Set SourceName
s) x :: SourceName
x = SourceName -> Set SourceName -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member SourceName
x Set SourceName
s


-- call opvar-state-subparser on given state
runWithVars :: OperatorState a => [String] -> CharParser (OpVarState a) res
            -> CharParser a res
runWithVars :: [SourceName] -> CharParser (OpVarState a) res -> CharParser a res
runWithVars l :: [SourceName]
l p :: CharParser (OpVarState a) res
p = do
  a
st <- ParsecT SourceName a Identity a
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Either ParseError (OpVarState a, res)
res <- CharParser (OpVarState a) res
-> OpVarState a
-> SourceName
-> GenParser Char a (Either ParseError (OpVarState a, res))
forall tok st a st'.
GenParser tok st a
-> st
-> SourceName
-> GenParser tok st' (Either ParseError (st, a))
runSubParser CharParser (OpVarState a) res
p (a -> Set SourceName -> OpVarState a
forall a. a -> Set SourceName -> OpVarState a
OpVarState a
st (Set SourceName -> OpVarState a) -> Set SourceName -> OpVarState a
forall a b. (a -> b) -> a -> b
$ [SourceName] -> Set SourceName
forall a. Ord a => [a] -> Set a
Set.fromList [SourceName]
l) ""
  case Either ParseError (OpVarState a, res)
res of
    Left err :: ParseError
err -> SourceName -> CharParser a res
forall st a. SourceName -> CharParser st a
parseError (SourceName -> CharParser a res) -> SourceName -> CharParser a res
forall a b. (a -> b) -> a -> b
$ [SourceName] -> SourceName
unlines ([SourceName] -> SourceName) -> [SourceName] -> SourceName
forall a b. (a -> b) -> a -> b
$ (Message -> SourceName) -> [Message] -> [SourceName]
forall a b. (a -> b) -> [a] -> [b]
map Message -> SourceName
messageString ([Message] -> [SourceName]) -> [Message] -> [SourceName]
forall a b. (a -> b) -> a -> b
$ ParseError -> [Message]
errorMessages ParseError
err
    Right (_, x :: res
x) -> res -> CharParser a res
forall (m :: * -> *) a. Monad m => a -> m a
return res
x

-- TEST: for subparser...


parseError :: String -> CharParser st a
parseError :: SourceName -> CharParser st a
parseError s :: SourceName
s = do
  SourcePos
p <- ParsecT SourceName st Identity SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  a -> CharParser st a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> CharParser st a) -> a -> CharParser st a
forall a b. (a -> b) -> a -> b
$ SourceName -> a
forall a. HasCallStack => SourceName -> a
error
             (SourceName -> a) -> SourceName -> a
forall a b. (a -> b) -> a -> b
$ [SourceName] -> SourceName
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ "Parse error at line: ", Line -> SourceName
forall a. Show a => a -> SourceName
show (SourcePos -> Line
Parsec.sourceLine SourcePos
p)
                      , " col: ", Line -> SourceName
forall a. Show a => a -> SourceName
show (SourcePos -> Line
Parsec.sourceColumn SourcePos
p), "\n", SourceName
s]

pComma :: CharParser st String
pComma :: CharParser st SourceName
pComma = CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string ","

pSemi :: CharParser st String
pSemi :: CharParser st SourceName
pSemi = CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string ";"

lstring :: String -> CharParser st String
lstring :: SourceName -> CharParser st SourceName
lstring = CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> (SourceName -> CharParser st SourceName)
-> SourceName
-> CharParser st SourceName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string

-- | parser for symbols followed by whitechars
lexemeParser :: CharParser st a -> CharParser st a
lexemeParser :: CharParser st a -> CharParser st a
lexemeParser = (CharParser st a
-> ParsecT SourceName st Identity () -> CharParser st a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< ParsecT SourceName st Identity ()
forall st. CharParser st ()
skip)

getOpName :: String -> [OPNAME] -> OPNAME
getOpName :: SourceName -> [OPNAME] -> OPNAME
getOpName s :: SourceName
s l :: [OPNAME]
l = [OPNAME] -> OPNAME
forall p. Show p => [p] -> p
f [OPNAME]
l
    where f :: [p] -> p
f (x :: p
x : xs :: [p]
xs) = if SourceName
s SourceName -> SourceName -> Bool
forall a. Eq a => a -> a -> Bool
== p -> SourceName
forall a. Show a => a -> SourceName
show p
x then p
x else [p] -> p
f [p]
xs
          f [] = SourceName -> p
forall a. HasCallStack => SourceName -> a
error (SourceName -> p) -> SourceName -> p
forall a b. (a -> b) -> a -> b
$ "getOpName: no op found for " SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
s SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ " in " SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ [OPNAME] -> SourceName
forall a. Show a => a -> SourceName
show [OPNAME]
l

mkFromOps :: [OPNAME] -> String -> [EXPRESSION] -> EXPRESSION
mkFromOps :: [OPNAME] -> SourceName -> [EXPRESSION] -> EXPRESSION
mkFromOps l :: [OPNAME]
l s :: SourceName
s = OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp (SourceName -> [OPNAME] -> OPNAME
getOpName SourceName
s [OPNAME]
l)

-- ---------------------------------------------------------------------------

-- * Parser for Expressions

-- ---------------------------------------------------------------------------


{- | parsing of identifiers. an identifier is a letter followed by
     letters, numbers, or _, but not a keyword -}
identifier :: CharParser st Id.Token
identifier :: CharParser st Token
identifier =
  CharParser st Token -> CharParser st Token
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st Token -> CharParser st Token)
-> CharParser st Token -> CharParser st Token
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName -> CharParser st Token
forall st. CharParser st SourceName -> CharParser st Token
Lexer.pToken (CharParser st SourceName -> CharParser st Token)
-> CharParser st SourceName -> CharParser st Token
forall a b. (a -> b) -> a -> b
$ [SourceName]
-> CharParser st SourceName -> CharParser st SourceName
forall st.
[SourceName]
-> CharParser st SourceName -> CharParser st SourceName
reserved [SourceName]
allKeywords CharParser st SourceName
forall st. CharParser st SourceName
Lexer.scanAnyWords

prefixidentifier :: CharParser st Id.Token
prefixidentifier :: CharParser st Token
prefixidentifier =
    CharParser st Token -> CharParser st Token
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st Token -> CharParser st Token)
-> CharParser st Token -> CharParser st Token
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName -> CharParser st Token
forall st. CharParser st SourceName -> CharParser st Token
Lexer.pToken (CharParser st SourceName -> CharParser st Token)
-> CharParser st SourceName -> CharParser st Token
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName
forall st. CharParser st SourceName
Lexer.scanAnySigns CharParser st SourceName
-> CharParser st SourceName -> CharParser st SourceName
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st SourceName
forall st. CharParser st SourceName
Lexer.scanAnyWords

-- | parses a possibly signed number to an EXPRESSION
signednumberExp :: CharParser st EXPRESSION
signednumberExp :: CharParser st EXPRESSION
signednumberExp =
    let f :: (Either APInt SourceName, Range) -> EXPRESSION
f (eN :: Either APInt SourceName
eN, rg :: Range
rg) = (APInt -> EXPRESSION)
-> (SourceName -> EXPRESSION)
-> Either APInt SourceName
-> EXPRESSION
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((APInt -> Range -> EXPRESSION) -> Range -> APInt -> EXPRESSION
forall a b c. (a -> b -> c) -> b -> a -> c
flip APInt -> Range -> EXPRESSION
Int Range
rg) ((APFloat -> Range -> EXPRESSION) -> Range -> APFloat -> EXPRESSION
forall a b c. (a -> b -> c) -> b -> a -> c
flip APFloat -> Range -> EXPRESSION
Rat Range
rg (APFloat -> EXPRESSION)
-> (SourceName -> APFloat) -> SourceName -> EXPRESSION
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> APFloat
readRat) Either APInt SourceName
eN
    in CharParser st (Either APInt SourceName, Range)
forall st. CharParser st (Either APInt SourceName, Range)
signednumber CharParser st (Either APInt SourceName, Range)
-> ((Either APInt SourceName, Range) -> EXPRESSION)
-> CharParser st EXPRESSION
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> (Either APInt SourceName, Range) -> EXPRESSION
f

-- | parses a possibly signed number (both integers and floats)
signednumber :: CharParser st (Either APInt String, Range)
signednumber :: CharParser st (Either APInt SourceName, Range)
signednumber =
    let f :: (SourceName -> a) -> Token -> m (a, Range)
f c :: SourceName -> a
c x :: Token
x = (a, Range) -> m (a, Range)
forall (m :: * -> *) a. Monad m => a -> m a
return (SourceName -> a
c (SourceName -> a) -> SourceName -> a
forall a b. (a -> b) -> a -> b
$ Token -> SourceName
tokStr Token
x, Token -> Range
tokPos Token
x)
        g :: Token -> m (Either a SourceName, Range)
g x :: Token
x | Token -> Bool
isFloating Token
x = (SourceName -> Either a SourceName)
-> Token -> m (Either a SourceName, Range)
forall (m :: * -> *) a.
Monad m =>
(SourceName -> a) -> Token -> m (a, Range)
f SourceName -> Either a SourceName
forall a b. b -> Either a b
Right Token
x
            | Bool
otherwise = (SourceName -> Either a SourceName)
-> Token -> m (Either a SourceName, Range)
forall (m :: * -> *) a.
Monad m =>
(SourceName -> a) -> Token -> m (a, Range)
f (a -> Either a SourceName
forall a b. a -> Either a b
Left (a -> Either a SourceName)
-> (SourceName -> a) -> SourceName -> Either a SourceName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> a
forall a. Read a => SourceName -> a
read) Token
x
    in CharParser st SourceName -> CharParser st Token
forall st. CharParser st SourceName -> CharParser st Token
Lexer.pToken CharParser st SourceName
forall st. CharParser st SourceName
Lexer.scanFloatExt CharParser st Token
-> (Token -> CharParser st (Either APInt SourceName, Range))
-> CharParser st (Either APInt SourceName, Range)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> CharParser st (Either APInt SourceName, Range)
forall (m :: * -> *) a.
(Monad m, Read a) =>
Token -> m (Either a SourceName, Range)
g

readRat :: String -> APFloat
readRat :: SourceName -> APFloat
readRat s :: SourceName
s = case ReadS APFloat
forall a. RealFrac a => ReadS a
readFloat SourceName
fls of
              [(r :: APFloat
r, "")] -> APFloat -> APFloat
forall p. Num p => p -> p
withSgn APFloat
r
              _ -> SourceName -> APFloat
forall a. HasCallStack => SourceName -> a
error (SourceName -> APFloat) -> SourceName -> APFloat
forall a b. (a -> b) -> a -> b
$ "readRat: cannot read float " SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
s
    where withSgn :: p -> p
withSgn x :: p
x = if Bool
sgn then - p
x else p
x
          (sgn :: Bool
sgn, fls :: SourceName
fls) = case (Char -> Bool) -> SourceName -> SourceName
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace SourceName
s of
                         '-' : s' :: SourceName
s' -> (Bool
True, SourceName
s')
                         _ -> (Bool
False, SourceName
s)

readDbl :: String -> Double
readDbl :: SourceName -> Double
readDbl = SourceName -> Double
forall a. Read a => SourceName -> a
read

{- | The version in Common.Lexer is not compatible with floating point numbers
which may start with ".". This one does it.
This version is still not compatible with -! -}
keySignNumCompat :: CharParser st a -> CharParser st a
keySignNumCompat :: CharParser st a -> CharParser st a
keySignNumCompat = CharParser st a -> CharParser st a
forall tok st a. GenParser tok st a -> GenParser tok st a
try (CharParser st a -> CharParser st a)
-> (CharParser st a -> CharParser st a)
-> CharParser st a
-> CharParser st a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CharParser st a
-> ParsecT SourceName st Identity () -> CharParser st a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< ParsecT SourceName st Identity Char
-> ParsecT SourceName st Identity ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (SourceName -> ParsecT SourceName st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m Char
oneOf SourceName
signNumCompatChars))

signNumCompatChars :: String
signNumCompatChars :: SourceName
signNumCompatChars = "!#$&*+-/:<=>?@\\^|~" SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++
    "\161\162\163\167\169\172\176\177\178\179\181\182\183\185\191\215\247"

oneOfKeys :: [String] -> CharParser st String
oneOfKeys :: [SourceName] -> CharParser st SourceName
oneOfKeys l :: [SourceName]
l = CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
keySignNumCompat (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ [CharParser st SourceName] -> CharParser st SourceName
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice ([CharParser st SourceName] -> CharParser st SourceName)
-> [CharParser st SourceName] -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ (SourceName -> CharParser st SourceName)
-> [SourceName] -> [CharParser st SourceName]
forall a b. (a -> b) -> [a] -> [b]
map SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString [SourceName]
l

plusmin :: OperatorState st => CharParser st EXPRESSION
plusmin :: CharParser st EXPRESSION
plusmin = do
  let ops :: [OPNAME]
ops = [OPNAME
OP_plus, OPNAME
OP_minus]
  EXPRESSION
exp1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
factor
  [(SourceName, EXPRESSION)]
exps <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity [(SourceName, EXPRESSION)])
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair ([SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ([SourceName] -> ParsecT SourceName st Identity SourceName)
-> [SourceName] -> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ (OPNAME -> SourceName) -> [OPNAME] -> [SourceName]
forall a b. (a -> b) -> [a] -> [b]
map OPNAME -> SourceName
forall a. Show a => a -> SourceName
show [OPNAME]
ops) CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
factor
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ if [(SourceName, EXPRESSION)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(SourceName, EXPRESSION)]
exps then EXPRESSION
exp1
           else (EXPRESSION -> (SourceName, EXPRESSION) -> EXPRESSION)
-> EXPRESSION -> [(SourceName, EXPRESSION)] -> EXPRESSION
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ a :: EXPRESSION
a b :: (SourceName, EXPRESSION)
b -> [OPNAME] -> SourceName -> [EXPRESSION] -> EXPRESSION
mkFromOps [OPNAME]
ops ((SourceName, EXPRESSION) -> SourceName
forall a b. (a, b) -> a
fst (SourceName, EXPRESSION)
b) [EXPRESSION
a, (SourceName, EXPRESSION) -> EXPRESSION
forall a b. (a, b) -> b
snd (SourceName, EXPRESSION)
b]) EXPRESSION
exp1 [(SourceName, EXPRESSION)]
exps

-- | parse a product of basic expressions
factor :: OperatorState st => CharParser st EXPRESSION
factor :: CharParser st EXPRESSION
factor = do
  let ops :: [OPNAME]
ops = [OPNAME
OP_mult, OPNAME
OP_div]
  EXPRESSION
exp1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
expexp
  [(SourceName, EXPRESSION)]
exps <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity [(SourceName, EXPRESSION)])
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair ([SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ([SourceName] -> ParsecT SourceName st Identity SourceName)
-> [SourceName] -> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ (OPNAME -> SourceName) -> [OPNAME] -> [SourceName]
forall a b. (a -> b) -> [a] -> [b]
map OPNAME -> SourceName
forall a. Show a => a -> SourceName
show [OPNAME]
ops) CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
expexp
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ if [(SourceName, EXPRESSION)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(SourceName, EXPRESSION)]
exps then EXPRESSION
exp1
     else (EXPRESSION -> (SourceName, EXPRESSION) -> EXPRESSION)
-> EXPRESSION -> [(SourceName, EXPRESSION)] -> EXPRESSION
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ a :: EXPRESSION
a b :: (SourceName, EXPRESSION)
b -> [OPNAME] -> SourceName -> [EXPRESSION] -> EXPRESSION
mkFromOps [OPNAME]
ops ((SourceName, EXPRESSION) -> SourceName
forall a b. (a, b) -> a
fst (SourceName, EXPRESSION)
b) [EXPRESSION
a, (SourceName, EXPRESSION) -> EXPRESSION
forall a b. (a, b) -> b
snd (SourceName, EXPRESSION)
b]) EXPRESSION
exp1 [(SourceName, EXPRESSION)]
exps

-- | parse a sequence of exponentiations
expexp :: OperatorState st => CharParser st EXPRESSION
expexp :: CharParser st EXPRESSION
expexp = do
  EXPRESSION
exp1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
expatom
  [(SourceName, EXPRESSION)]
exps <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity [(SourceName, EXPRESSION)])
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair ([SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["**", "^"]) CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
expatom
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ if [(SourceName, EXPRESSION)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(SourceName, EXPRESSION)]
exps then EXPRESSION
exp1
    else (EXPRESSION -> (SourceName, EXPRESSION) -> EXPRESSION)
-> EXPRESSION -> [(SourceName, EXPRESSION)] -> EXPRESSION
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ a :: EXPRESSION
a b :: (SourceName, EXPRESSION)
b -> OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_pow [EXPRESSION
a, (SourceName, EXPRESSION) -> EXPRESSION
forall a b. (a, b) -> b
snd (SourceName, EXPRESSION)
b]) EXPRESSION
exp1 [(SourceName, EXPRESSION)]
exps

-- | parse a basic expression
expatom :: OperatorState st => CharParser st EXPRESSION
expatom :: CharParser st EXPRESSION
expatom = CharParser st EXPRESSION -> CharParser st EXPRESSION
forall tok st a. GenParser tok st a -> GenParser tok st a
try CharParser st EXPRESSION
forall st. CharParser st EXPRESSION
signednumberExp CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (CharParser st Token
forall st. CharParser st Token
oParenT CharParser st Token
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
plusmin CharParser st EXPRESSION
-> CharParser st Token -> CharParser st EXPRESSION
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< CharParser st Token
forall st. CharParser st Token
cParenT)
          CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
listexp CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. CharParser st EXPRESSION
intervalexp CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
expsymbol

expsymbol :: OperatorState st => CharParser st EXPRESSION
expsymbol :: CharParser st EXPRESSION
expsymbol = do
  Token
ident <- CharParser st Token
forall st. CharParser st Token
prefixidentifier  -- EXTENDED
  ([EXTPARAM], [SourceName])
ep <- ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option ([], [])
        (ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
 -> ParsecT SourceName st Identity ([EXTPARAM], [SourceName]))
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall a b. (a -> b) -> a -> b
$ CharParser st Token
forall st. CharParser st Token
oBracketT CharParser st Token
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> GenParser Char st EXTPARAM
-> GenParser Char st SourceName
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy GenParser Char st EXTPARAM
forall st. CharParser st EXTPARAM
extparam GenParser Char st SourceName
forall st. CharParser st SourceName
pComma ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> CharParser st Token
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< CharParser st Token
forall st. CharParser st Token
cBracketT
  ([EXPRESSION], [SourceName])
exps <- ([EXPRESSION], [SourceName])
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option ([], [])
          (ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
 -> ParsecT SourceName st Identity ([EXPRESSION], [SourceName]))
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
forall a b. (a -> b) -> a -> b
$ CharParser st Token
forall st. CharParser st Token
oParenT CharParser st Token
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser st EXPRESSION
-> GenParser Char st SourceName
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression GenParser Char st SourceName
forall st. CharParser st SourceName
pComma ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
-> CharParser st Token
-> ParsecT SourceName st Identity ([EXPRESSION], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< CharParser st Token
forall st. CharParser st Token
cParenT
  st
st <- ParsecT SourceName st Identity st
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  case Bool
-> st
-> SourceName
-> [EXTPARAM]
-> [EXPRESSION]
-> Range
-> Either SourceName EXPRESSION
forall st.
OperatorState st =>
Bool
-> st
-> SourceName
-> [EXTPARAM]
-> [EXPRESSION]
-> Range
-> Either SourceName EXPRESSION
mkAndAnalyzeOp' Bool
True st
st (Token -> SourceName
tokStr Token
ident) (([EXTPARAM], [SourceName]) -> [EXTPARAM]
forall a b. (a, b) -> a
fst ([EXTPARAM], [SourceName])
ep) (([EXPRESSION], [SourceName]) -> [EXPRESSION]
forall a b. (a, b) -> a
fst ([EXPRESSION], [SourceName])
exps)
           (Range -> Either SourceName EXPRESSION)
-> Range -> Either SourceName EXPRESSION
forall a b. (a -> b) -> a -> b
$ Token -> Range
forall a. GetRange a => a -> Range
getRange Token
ident of
    Left s :: SourceName
s -> SourceName -> CharParser st EXPRESSION
forall st a. SourceName -> CharParser st a
parseError (SourceName -> CharParser st EXPRESSION)
-> SourceName -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ "expsymbol at op " SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ Token -> SourceName
tokStr Token
ident
              SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ [EXPRESSION] -> SourceName
forall a. Show a => a -> SourceName
show (([EXPRESSION], [SourceName]) -> [EXPRESSION]
forall a b. (a, b) -> a
fst ([EXPRESSION], [SourceName])
exps) SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ ": " SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
s
    Right e :: EXPRESSION
e -> EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return EXPRESSION
e

opdecl :: OperatorState st => CharParser st OpDecl
opdecl :: CharParser st OpDecl
opdecl = do
  Token
ident <- CharParser st Token
forall st. CharParser st Token
prefixidentifier  -- EXTENDED
  ([EXTPARAM], [SourceName])
ep <- ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option ([], [])
        (ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
 -> ParsecT SourceName st Identity ([EXTPARAM], [SourceName]))
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall a b. (a -> b) -> a -> b
$ CharParser st Token
forall st. CharParser st Token
oBracketT CharParser st Token
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> GenParser Char st EXTPARAM
-> GenParser Char st SourceName
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy GenParser Char st EXTPARAM
forall st. CharParser st EXTPARAM
extparam GenParser Char st SourceName
forall st. CharParser st SourceName
pComma ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
-> CharParser st Token
-> ParsecT SourceName st Identity ([EXTPARAM], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< CharParser st Token
forall st. CharParser st Token
cBracketT
  ([Token], [SourceName])
args <- ([Token], [SourceName])
-> ParsecT SourceName st Identity ([Token], [SourceName])
-> ParsecT SourceName st Identity ([Token], [SourceName])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option ([], [])
          (ParsecT SourceName st Identity ([Token], [SourceName])
 -> ParsecT SourceName st Identity ([Token], [SourceName]))
-> ParsecT SourceName st Identity ([Token], [SourceName])
-> ParsecT SourceName st Identity ([Token], [SourceName])
forall a b. (a -> b) -> a -> b
$ CharParser st Token
forall st. CharParser st Token
oParenT CharParser st Token
-> ParsecT SourceName st Identity ([Token], [SourceName])
-> ParsecT SourceName st Identity ([Token], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser st Token
-> GenParser Char st SourceName
-> ParsecT SourceName st Identity ([Token], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy CharParser st Token
forall st. CharParser st Token
prefixidentifier GenParser Char st SourceName
forall st. CharParser st SourceName
pComma ParsecT SourceName st Identity ([Token], [SourceName])
-> CharParser st Token
-> ParsecT SourceName st Identity ([Token], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< CharParser st Token
forall st. CharParser st Token
cParenT
  let vdl :: [VarDecl]
vdl = (Token -> VarDecl) -> [Token] -> [VarDecl]
forall a b. (a -> b) -> [a] -> [b]
map ((Token -> Maybe Domain -> VarDecl)
-> Maybe Domain -> Token -> VarDecl
forall a b c. (a -> b -> c) -> b -> a -> c
flip Token -> Maybe Domain -> VarDecl
VarDecl Maybe Domain
forall a. Maybe a
Nothing) ([Token] -> [VarDecl]) -> [Token] -> [VarDecl]
forall a b. (a -> b) -> a -> b
$ ([Token], [SourceName]) -> [Token]
forall a b. (a, b) -> a
fst ([Token], [SourceName])
args
  OpDecl -> CharParser st OpDecl
forall (m :: * -> *) a. Monad m => a -> m a
return (OpDecl -> CharParser st OpDecl) -> OpDecl -> CharParser st OpDecl
forall a b. (a -> b) -> a -> b
$ ConstantName -> [EXTPARAM] -> [VarDecl] -> Range -> OpDecl
OpDecl (SourceName -> ConstantName
SimpleConstant (SourceName -> ConstantName) -> SourceName -> ConstantName
forall a b. (a -> b) -> a -> b
$ Token -> SourceName
tokStr Token
ident) (([EXTPARAM], [SourceName]) -> [EXTPARAM]
forall a b. (a, b) -> a
fst ([EXTPARAM], [SourceName])
ep) [VarDecl]
vdl (Range -> OpDecl) -> Range -> OpDecl
forall a b. (a -> b) -> a -> b
$ Token -> Range
tokPos Token
ident

-- | parses a list expression
listexp :: OperatorState st => CharParser st EXPRESSION
listexp :: CharParser st EXPRESSION
listexp = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
keySignNumCompat (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "{"
  ([EXPRESSION], [SourceName])
elems <- CharParser st EXPRESSION
-> CharParser st SourceName
-> GenParser Char st ([EXPRESSION], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression CharParser st SourceName
forall st. CharParser st SourceName
pComma
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
keySignNumCompat (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "}"
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return ([EXPRESSION] -> Range -> EXPRESSION
List (([EXPRESSION], [SourceName]) -> [EXPRESSION]
forall a b. (a, b) -> a
fst ([EXPRESSION], [SourceName])
elems) Range
nullRange)

intervalexp :: CharParser st EXPRESSION
intervalexp :: CharParser st EXPRESSION
intervalexp = do
  ([(Either APInt SourceName, Range)], [SourceName])
nums <- SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
lstring "[" CharParser st SourceName
-> ParsecT
     SourceName
     st
     Identity
     ([(Either APInt SourceName, Range)], [SourceName])
-> ParsecT
     SourceName
     st
     Identity
     ([(Either APInt SourceName, Range)], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> GenParser Char st (Either APInt SourceName, Range)
-> CharParser st SourceName
-> ParsecT
     SourceName
     st
     Identity
     ([(Either APInt SourceName, Range)], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy GenParser Char st (Either APInt SourceName, Range)
forall st. CharParser st (Either APInt SourceName, Range)
signednumber CharParser st SourceName
forall st. CharParser st SourceName
pComma ParsecT
  SourceName
  st
  Identity
  ([(Either APInt SourceName, Range)], [SourceName])
-> CharParser st SourceName
-> ParsecT
     SourceName
     st
     Identity
     ([(Either APInt SourceName, Range)], [SourceName])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
lstring "]"
  let getFloat :: Either APInt SourceName -> Double
getFloat = (APInt -> Double)
-> (SourceName -> Double) -> Either APInt SourceName -> Double
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either APInt -> Double
forall a. Num a => APInt -> a
fromInteger SourceName -> Double
readDbl
  case ([(Either APInt SourceName, Range)], [SourceName])
-> [(Either APInt SourceName, Range)]
forall a b. (a, b) -> a
fst ([(Either APInt SourceName, Range)], [SourceName])
nums of
    [(x :: Either APInt SourceName
x, rg1 :: Range
rg1), (y :: Either APInt SourceName
y, rg2 :: Range
rg2)] -> EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Range -> EXPRESSION
Interval (Either APInt SourceName -> Double
getFloat Either APInt SourceName
x) (Either APInt SourceName -> Double
getFloat Either APInt SourceName
y) (Range -> EXPRESSION) -> Range -> EXPRESSION
forall a b. (a -> b) -> a -> b
$ [Pos] -> Range
Range
                            ([Pos] -> Range) -> [Pos] -> Range
forall a b. (a -> b) -> a -> b
$ [[Pos]] -> [Pos]
joinRanges ([[Pos]] -> [Pos]) -> [[Pos]] -> [Pos]
forall a b. (a -> b) -> a -> b
$ (Range -> [Pos]) -> [Range] -> [[Pos]]
forall a b. (a -> b) -> [a] -> [b]
map Range -> [Pos]
rangeToList [Range
rg1, Range
rg2]
    _ -> SourceName -> CharParser st EXPRESSION
forall st a. SourceName -> CharParser st a
parseError
         "intervalexp: Parse error: interval with other than two arguments"

-- ---------------------------------------------------------------------------

-- ** parser for extended parameter, e.g., [I=0,...]

-- ---------------------------------------------------------------------------

extparam :: CharParser st EXTPARAM
extparam :: CharParser st EXTPARAM
extparam = do
  Token
i <- CharParser st Token
forall st. CharParser st Token
identifier
  (SourceName -> APInt -> EXTPARAM)
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity APInt
-> CharParser st EXTPARAM
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (Token -> SourceName -> APInt -> EXTPARAM
EP Token
i) ([SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["=", "<=", ">=", "!=", "<", ">", "-|"])
    (ParsecT SourceName st Identity APInt -> CharParser st EXTPARAM)
-> ParsecT SourceName st Identity APInt -> CharParser st EXTPARAM
forall a b. (a -> b) -> a -> b
$ APInt
-> ParsecT SourceName st Identity APInt
-> ParsecT SourceName st Identity APInt
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option 0 (ParsecT SourceName st Identity APInt
 -> ParsecT SourceName st Identity APInt)
-> ParsecT SourceName st Identity APInt
-> ParsecT SourceName st Identity APInt
forall a b. (a -> b) -> a -> b
$ CharParser st (Either APInt SourceName, Range)
forall st. CharParser st (Either APInt SourceName, Range)
signednumber
          CharParser st (Either APInt SourceName, Range)
-> ((Either APInt SourceName, Range) -> APInt)
-> ParsecT SourceName st Identity APInt
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> (APInt -> APInt)
-> (SourceName -> APInt) -> Either APInt SourceName -> APInt
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either APInt -> APInt
forall a. a -> a
id (SourceName -> SourceName -> APInt
forall a. HasCallStack => SourceName -> a
error "extparam: floats not supported") (Either APInt SourceName -> APInt)
-> ((Either APInt SourceName, Range) -> Either APInt SourceName)
-> (Either APInt SourceName, Range)
-> APInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either APInt SourceName, Range) -> Either APInt SourceName
forall a b. (a, b) -> a
fst

-- ---------------------------------------------------------------------------

-- * parser for formulas

-- ---------------------------------------------------------------------------

parseVarList :: CharParser st EXPRESSION
parseVarList :: CharParser st EXPRESSION
parseVarList = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keySign (SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "{")
  ([EXPRESSION], [SourceName])
elems <- CharParser st EXPRESSION
-> CharParser st SourceName
-> GenParser Char st ([EXPRESSION], [SourceName])
forall tok st a b.
GenParser tok st a
-> GenParser tok st b -> GenParser tok st ([a], [b])
Lexer.separatedBy CharParser st EXPRESSION
forall st. CharParser st EXPRESSION
parseVar CharParser st SourceName
forall st. CharParser st SourceName
pComma
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keySign (SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "}")
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return ([EXPRESSION] -> Range -> EXPRESSION
List (([EXPRESSION], [SourceName]) -> [EXPRESSION]
forall a b. (a, b) -> a
fst ([EXPRESSION], [SourceName])
elems) Range
nullRange)

parseVar :: CharParser st EXPRESSION
parseVar :: CharParser st EXPRESSION
parseVar = do
  Token
ident <- CharParser st Token
forall st. CharParser st Token
identifier
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> EXPRESSION
Var Token
ident)


quantFormula :: OperatorState st => OPNAME -> CharParser st EXPRESSION
 -- TODO: static analysis requires probably a better representation of quantifiers
quantFormula :: OPNAME -> CharParser st EXPRESSION
quantFormula q :: OPNAME
q = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keySign (SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string (SourceName -> CharParser st SourceName)
-> SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
q)
  CharParser st Token
forall st. CharParser st Token
oParenT
  EXPRESSION
vars <- CharParser st EXPRESSION
forall st. CharParser st EXPRESSION
parseVar CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. CharParser st EXPRESSION
parseVarList
  CharParser st SourceName
forall st. CharParser st SourceName
pComma
  EXPRESSION
expr <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression
  CharParser st Token
forall st. CharParser st Token
cParenT
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
q [EXPRESSION
vars, EXPRESSION
expr])


-- | parser for atoms
truefalseFormula :: OperatorState st => CharParser st EXPRESSION
truefalseFormula :: CharParser st EXPRESSION
truefalseFormula =
    do
      CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keyWord (SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString (SourceName -> CharParser st SourceName)
-> SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_true))
      EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_true [])
    CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|>
    do
      CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keyWord (SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString (SourceName -> CharParser st SourceName)
-> SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_false))
      EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_false [])
    CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> OPNAME -> CharParser st EXPRESSION
forall st. OperatorState st => OPNAME -> CharParser st EXPRESSION
quantFormula OPNAME
OP_ex CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> OPNAME -> CharParser st EXPRESSION
forall st. OperatorState st => OPNAME -> CharParser st EXPRESSION
quantFormula OPNAME
OP_all

-- | parser for predicates
predFormula :: OperatorState st => CharParser st EXPRESSION
predFormula :: CharParser st EXPRESSION
predFormula = do
  let ops :: [OPNAME]
ops = [ OPNAME
OP_leq, OPNAME
OP_geq, OPNAME
OP_neq, OPNAME
OP_eq, OPNAME
OP_lt, OPNAME
OP_gt ]
  EXPRESSION
exp1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
plusmin
  Maybe (SourceName, EXPRESSION)
mExp2 <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity (Maybe (SourceName, EXPRESSION))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe
           (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity (Maybe (SourceName, EXPRESSION)))
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity (Maybe (SourceName, EXPRESSION))
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair ([SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ((OPNAME -> SourceName) -> [OPNAME] -> [SourceName]
forall a b. (a -> b) -> [a] -> [b]
map OPNAME -> SourceName
forall a. Show a => a -> SourceName
show ([OPNAME] -> [SourceName]) -> [OPNAME] -> [SourceName]
forall a b. (a -> b) -> a -> b
$ Line -> [OPNAME] -> [OPNAME]
forall a. Line -> [a] -> [a]
take 3 [OPNAME]
ops) -- the first 3 print as 2-chars
                   ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (ParsecT SourceName st Identity Char
-> ParsecT SourceName st Identity SourceName
forall (m :: * -> *) a. Monad m => m a -> m [a]
single (ParsecT SourceName st Identity Char
 -> ParsecT SourceName st Identity SourceName)
-> ParsecT SourceName st Identity Char
-> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> ParsecT SourceName st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m Char
oneOf "=<>")) CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
plusmin
  case Maybe (SourceName, EXPRESSION)
mExp2 of
    Just (op :: SourceName
op, exp2 :: EXPRESSION
exp2) -> EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ [OPNAME] -> SourceName -> [EXPRESSION] -> EXPRESSION
mkFromOps [OPNAME]
ops SourceName
op [EXPRESSION
exp1, EXPRESSION
exp2]
    _ -> EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return EXPRESSION
exp1

atomicFormula :: OperatorState st => CharParser st EXPRESSION
atomicFormula :: CharParser st EXPRESSION
atomicFormula = CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
truefalseFormula CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
predFormula CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
parenFormula

-- | parser for formulas
aFormula :: OperatorState st => CharParser st EXPRESSION
aFormula :: CharParser st EXPRESSION
aFormula = CharParser st EXPRESSION -> CharParser st EXPRESSION
forall tok st a. GenParser tok st a -> GenParser tok st a
try CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
negFormula CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
impOrFormula

negFormula :: OperatorState st => CharParser st EXPRESSION
negFormula :: CharParser st EXPRESSION
negFormula = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keyWord (SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString (SourceName -> CharParser st SourceName)
-> SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_not))
  EXPRESSION
f <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
atomicFormula
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_not [EXPRESSION
f])

-- | parses a formula within brackets
parenFormula :: OperatorState st => CharParser st EXPRESSION
parenFormula :: CharParser st EXPRESSION
parenFormula = do
  CharParser st Token -> CharParser st Token
forall st a. CharParser st a -> CharParser st a
lexemeParser CharParser st Token
forall st. CharParser st Token
oParenT
  EXPRESSION
f <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
aFormula
  CharParser st Token -> CharParser st Token
forall st a. CharParser st a -> CharParser st a
lexemeParser CharParser st Token
forall st. CharParser st Token
cParenT
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return EXPRESSION
f

-- | parser for implications and ors (same precedence)
impOrFormula :: OperatorState st => CharParser st EXPRESSION
impOrFormula :: CharParser st EXPRESSION
impOrFormula = do
  let ops :: [OPNAME]
ops = [ OPNAME
OP_or, OPNAME
OP_impl ]
  EXPRESSION
f1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
andFormula
  [(SourceName, EXPRESSION)]
opfs <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity [(SourceName, EXPRESSION)])
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair (ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (ParsecT SourceName st Identity SourceName
 -> ParsecT SourceName st Identity SourceName)
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keyWord
                      (ParsecT SourceName st Identity SourceName
 -> ParsecT SourceName st Identity SourceName)
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> ParsecT SourceName st Identity SourceName
forall st. SourceName -> CharParser st SourceName
tryString (OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_or) ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> SourceName -> ParsecT SourceName st Identity SourceName
forall st. SourceName -> CharParser st SourceName
tryString (OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_impl))
          CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
andFormula
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ if [(SourceName, EXPRESSION)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(SourceName, EXPRESSION)]
opfs then EXPRESSION
f1
   else (EXPRESSION -> (SourceName, EXPRESSION) -> EXPRESSION)
-> EXPRESSION -> [(SourceName, EXPRESSION)] -> EXPRESSION
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ a :: EXPRESSION
a (op :: SourceName
op, b :: EXPRESSION
b) -> [OPNAME] -> SourceName -> [EXPRESSION] -> EXPRESSION
mkFromOps [OPNAME]
ops SourceName
op [EXPRESSION
a, EXPRESSION
b]) EXPRESSION
f1 [(SourceName, EXPRESSION)]
opfs

-- | a parser for and sequence of and formulas
andFormula :: OperatorState st => CharParser st EXPRESSION
andFormula :: CharParser st EXPRESSION
andFormula = do
  EXPRESSION
f1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
atomicFormula
  [(SourceName, EXPRESSION)]
opfs <- ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT SourceName st Identity (SourceName, EXPRESSION)
 -> ParsecT SourceName st Identity [(SourceName, EXPRESSION)])
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
-> ParsecT SourceName st Identity [(SourceName, EXPRESSION)]
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity (SourceName, EXPRESSION)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m (a, b)
pair (ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (ParsecT SourceName st Identity SourceName
 -> ParsecT SourceName st Identity SourceName)
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall st a. CharParser st a -> CharParser st a
keyWord (ParsecT SourceName st Identity SourceName
 -> ParsecT SourceName st Identity SourceName)
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> ParsecT SourceName st Identity SourceName
forall st. SourceName -> CharParser st SourceName
tryString (SourceName -> ParsecT SourceName st Identity SourceName)
-> SourceName -> ParsecT SourceName st Identity SourceName
forall a b. (a -> b) -> a -> b
$ OPNAME -> SourceName
forall a. Show a => a -> SourceName
show OPNAME
OP_and)
          CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
atomicFormula
  EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION -> CharParser st EXPRESSION)
-> EXPRESSION -> CharParser st EXPRESSION
forall a b. (a -> b) -> a -> b
$ if [(SourceName, EXPRESSION)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(SourceName, EXPRESSION)]
opfs then EXPRESSION
f1
   else (EXPRESSION -> (SourceName, EXPRESSION) -> EXPRESSION)
-> EXPRESSION -> [(SourceName, EXPRESSION)] -> EXPRESSION
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ b :: EXPRESSION
b a :: (SourceName, EXPRESSION)
a -> (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_and [EXPRESSION
b, (SourceName, EXPRESSION) -> EXPRESSION
forall a b. (a, b) -> b
snd (SourceName, EXPRESSION)
a])) EXPRESSION
f1 [(SourceName, EXPRESSION)]
opfs

-- ---------------------------------------------------------------------------

-- * Parser for Commands

-- ---------------------------------------------------------------------------


formulaorexpression :: OperatorState st => CharParser st EXPRESSION
formulaorexpression :: CharParser st EXPRESSION
formulaorexpression = CharParser st EXPRESSION -> CharParser st EXPRESSION
forall tok st a. GenParser tok st a -> GenParser tok st a
try CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
aFormula CharParser st EXPRESSION
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
plusmin

-- | parser for commands
command :: CharParser (AnnoState.AnnoState st) CMD
command :: CharParser (AnnoState st) CMD
command = CharParser (AnnoState st) CMD
forall st. OperatorState st => CharParser st CMD
reduceCommand CharParser (AnnoState st) CMD
-> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall tok st a. GenParser tok st a -> GenParser tok st a
try CharParser (AnnoState st) CMD
forall st. OperatorState st => CharParser st CMD
assignment CharParser (AnnoState st) CMD
-> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (AnnoState st) CMD
forall st. CharParser (AnnoState st) CMD
repeatExpr CharParser (AnnoState st) CMD
-> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (AnnoState st) CMD
forall st. CharParser (AnnoState st) CMD
caseExpr
          CharParser (AnnoState st) CMD
-> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (AnnoState st) CMD
forall st. CharParser (AnnoState st) CMD
sequenceExpr CharParser (AnnoState st) CMD
-> CharParser (AnnoState st) CMD -> CharParser (AnnoState st) CMD
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> CharParser (AnnoState st) CMD
forall st. OperatorState st => CharParser st CMD
constraint

reduceCommand :: OperatorState st => CharParser st CMD
reduceCommand :: CharParser st CMD
reduceCommand = do
  SourceName
cmd <- CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keyWord (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ [CharParser st SourceName] -> CharParser st SourceName
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice ([CharParser st SourceName] -> CharParser st SourceName)
-> [CharParser st SourceName] -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ (SourceName -> CharParser st SourceName)
-> [SourceName] -> [CharParser st SourceName]
forall a b. (a -> b) -> [a] -> [b]
map SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString
         ["solve", "simplify", "divide", "int", "rlqe", "factorize", "print"]
  CharParser st Token
forall st. CharParser st Token
oParenT
  EXPRESSION
arg1 <- CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression
  [EXPRESSION]
args <- CharParser st EXPRESSION
-> ParsecT SourceName st Identity [EXPRESSION]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (CharParser st EXPRESSION
 -> ParsecT SourceName st Identity [EXPRESSION])
-> CharParser st EXPRESSION
-> ParsecT SourceName st Identity [EXPRESSION]
forall a b. (a -> b) -> a -> b
$ CharParser st SourceName
forall st. CharParser st SourceName
pComma CharParser st SourceName
-> CharParser st EXPRESSION -> CharParser st EXPRESSION
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression
  CharParser st Token
forall st. CharParser st Token
cParenT
  CMD -> CharParser st CMD
forall (m :: * -> *) a. Monad m => a -> m a
return (CMD -> CharParser st CMD) -> CMD -> CharParser st CMD
forall a b. (a -> b) -> a -> b
$ SourceName -> [EXPRESSION] -> CMD
Cmd SourceName
cmd ([EXPRESSION] -> CMD) -> [EXPRESSION] -> CMD
forall a b. (a -> b) -> a -> b
$ EXPRESSION
arg1 EXPRESSION -> [EXPRESSION] -> [EXPRESSION]
forall a. a -> [a] -> [a]
: [EXPRESSION]
args

assignment :: OperatorState st => CharParser st CMD
assignment :: CharParser st CMD
assignment = do
  ident :: OpDecl
ident@(OpDecl _ _ vdl :: [VarDecl]
vdl _) <- CharParser st OpDecl
forall st. OperatorState st => CharParser st OpDecl
opdecl
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ [CharParser st SourceName] -> CharParser st SourceName
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice [SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString ":=", SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString "="]
  EXPRESSION
exp' <- [SourceName]
-> CharParser (OpVarState st) EXPRESSION
-> CharParser st EXPRESSION
forall a res.
OperatorState a =>
[SourceName] -> CharParser (OpVarState a) res -> CharParser a res
runWithVars ((VarDecl -> SourceName) -> [VarDecl] -> [SourceName]
forall a b. (a -> b) -> [a] -> [b]
map VarDecl -> SourceName
varDeclName [VarDecl]
vdl) CharParser (OpVarState st) EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
plusmin
  CMD -> CharParser st CMD
forall (m :: * -> *) a. Monad m => a -> m a
return (CMD -> CharParser st CMD) -> CMD -> CharParser st CMD
forall a b. (a -> b) -> a -> b
$ OpDecl -> EXPRESSION -> CMD
Ass OpDecl
ident EXPRESSION
exp'

constraint :: OperatorState st => CharParser st CMD
constraint :: CharParser st CMD
constraint = do
  EXPRESSION
exp' <- GenParser Char st EXPRESSION -> GenParser Char st EXPRESSION
forall tok st a. GenParser tok st a -> GenParser tok st a
try GenParser Char st EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
aFormula
  case EXPRESSION
exp' of
    Op {} ->
        CMD -> CharParser st CMD
forall (m :: * -> *) a. Monad m => a -> m a
return (CMD -> CharParser st CMD) -> CMD -> CharParser st CMD
forall a b. (a -> b) -> a -> b
$ SourceName -> [EXPRESSION] -> CMD
Cmd "constraint" [EXPRESSION
exp']
    _ -> SourceName -> CharParser st CMD
forall (m :: * -> *) a. MonadFail m => SourceName -> m a
Fail.fail "Malformed constraint"


sequenceExpr :: CharParser (AnnoState.AnnoState st) CMD
sequenceExpr :: CharParser (AnnoState st) CMD
sequenceExpr = do
  SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "sequence"
  [Annoted CMD]
statements <- ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity [Annoted CMD]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (AParser st Token
forall st. AParser st Token
AnnoState.dotT AParser st Token
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser (AnnoState st) CMD
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall st a. AParser st a -> AParser st (Annoted a)
AnnoState.allAnnoParser CharParser (AnnoState st) CMD
forall st. CharParser (AnnoState st) CMD
command)
  SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "end"
  CMD -> CharParser (AnnoState st) CMD
forall (m :: * -> *) a. Monad m => a -> m a
return (CMD -> CharParser (AnnoState st) CMD)
-> CMD -> CharParser (AnnoState st) CMD
forall a b. (a -> b) -> a -> b
$ [CMD] -> CMD
Sequence ([CMD] -> CMD) -> [CMD] -> CMD
forall a b. (a -> b) -> a -> b
$ (Annoted CMD -> CMD) -> [Annoted CMD] -> [CMD]
forall a b. (a -> b) -> [a] -> [b]
map Annoted CMD -> CMD
forall a. Annoted a -> a
AS_Anno.item [Annoted CMD]
statements

repeatExpr :: CharParser (AnnoState.AnnoState st) CMD
repeatExpr :: CharParser (AnnoState st) CMD
repeatExpr = do
  SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "repeat"
  [Annoted CMD]
statements <- ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity [Annoted CMD]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (AParser st Token
forall st. AParser st Token
AnnoState.dotT AParser st Token
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser (AnnoState st) CMD
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall st a. AParser st a -> AParser st (Annoted a)
AnnoState.allAnnoParser CharParser (AnnoState st) CMD
forall st. CharParser (AnnoState st) CMD
command)
  SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "until"
  EXPRESSION
cstr <- CharParser (AnnoState st) EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
aFormula
  CMD -> CharParser (AnnoState st) CMD
forall (m :: * -> *) a. Monad m => a -> m a
return (CMD -> CharParser (AnnoState st) CMD)
-> CMD -> CharParser (AnnoState st) CMD
forall a b. (a -> b) -> a -> b
$ EXPRESSION -> [CMD] -> CMD
Repeat EXPRESSION
cstr ([CMD] -> CMD) -> [CMD] -> CMD
forall a b. (a -> b) -> a -> b
$ (Annoted CMD -> CMD) -> [Annoted CMD] -> [CMD]
forall a b. (a -> b) -> [a] -> [b]
map Annoted CMD -> CMD
forall a. Annoted a -> a
AS_Anno.item [Annoted CMD]
statements

singleCase :: CharParser (AnnoState.AnnoState st) (EXPRESSION, [CMD])
singleCase :: CharParser (AnnoState st) (EXPRESSION, [CMD])
singleCase =
    let p1 :: ParsecT SourceName st Identity EXPRESSION
p1 = SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
lstring "else" CharParser st SourceName
-> ParsecT SourceName st Identity EXPRESSION
-> ParsecT SourceName st Identity EXPRESSION
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> EXPRESSION -> ParsecT SourceName st Identity EXPRESSION
forall (m :: * -> *) a. Monad m => a -> m a
return (OPNAME -> [EXPRESSION] -> EXPRESSION
mkPredefOp OPNAME
OP_true [])
    in do
      SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "case"
      EXPRESSION
cond <- [ParsecT SourceName (AnnoState st) Identity EXPRESSION]
-> ParsecT SourceName (AnnoState st) Identity EXPRESSION
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice [ParsecT SourceName (AnnoState st) Identity EXPRESSION
-> ParsecT SourceName (AnnoState st) Identity EXPRESSION
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT SourceName (AnnoState st) Identity EXPRESSION
forall st. CharParser st EXPRESSION
p1, ParsecT SourceName (AnnoState st) Identity EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
aFormula]
      SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring ":"
      [Annoted CMD]
statements <- ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity [Annoted CMD]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (AParser st Token
forall st. AParser st Token
AnnoState.dotT AParser st Token
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> AParser st CMD
-> ParsecT SourceName (AnnoState st) Identity (Annoted CMD)
forall st a. AParser st a -> AParser st (Annoted a)
AnnoState.allAnnoParser AParser st CMD
forall st. CharParser (AnnoState st) CMD
command)
      (EXPRESSION, [CMD])
-> CharParser (AnnoState st) (EXPRESSION, [CMD])
forall (m :: * -> *) a. Monad m => a -> m a
return (EXPRESSION
cond, (Annoted CMD -> CMD) -> [Annoted CMD] -> [CMD]
forall a b. (a -> b) -> [a] -> [b]
map Annoted CMD -> CMD
forall a. Annoted a -> a
AS_Anno.item [Annoted CMD]
statements)


caseExpr :: CharParser (AnnoState.AnnoState st) CMD
caseExpr :: CharParser (AnnoState st) CMD
caseExpr = ParsecT SourceName (AnnoState st) Identity (EXPRESSION, [CMD])
-> ParsecT SourceName (AnnoState st) Identity [(EXPRESSION, [CMD])]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT SourceName (AnnoState st) Identity (EXPRESSION, [CMD])
forall st. CharParser (AnnoState st) (EXPRESSION, [CMD])
singleCase ParsecT SourceName (AnnoState st) Identity [(EXPRESSION, [CMD])]
-> ([(EXPRESSION, [CMD])] -> CMD) -> CharParser (AnnoState st) CMD
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [(EXPRESSION, [CMD])] -> CMD
Cond CharParser (AnnoState st) CMD
-> ParsecT SourceName (AnnoState st) Identity SourceName
-> CharParser (AnnoState st) CMD
forall (m :: * -> *) a b. Monad m => m a -> m b -> m a
<< SourceName -> ParsecT SourceName (AnnoState st) Identity SourceName
forall st. SourceName -> CharParser st SourceName
lstring "end"

-- ---------------------------------------------------------------------------

-- * parser spec entries

-- ---------------------------------------------------------------------------


-- | parser for operator declarations: example: operator a,b,c
opItem :: CharParser st OP_ITEM
opItem :: CharParser st OP_ITEM
opItem = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
Lexer.keySign (CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
tryString "operator"))
  [Token]
vars <- ParsecT SourceName st Identity Token
-> CharParser st SourceName
-> ParsecT SourceName st Identity [Token]
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]
sepBy1 ParsecT SourceName st Identity Token
forall st. CharParser st Token
identifier CharParser st SourceName
forall st. CharParser st SourceName
pComma
  OP_ITEM -> CharParser st OP_ITEM
forall (m :: * -> *) a. Monad m => a -> m a
return (OP_ITEM -> CharParser st OP_ITEM)
-> OP_ITEM -> CharParser st OP_ITEM
forall a b. (a -> b) -> a -> b
$ [Token] -> Range -> OP_ITEM
Op_item [Token]
vars Range
nullRange


-- | Parser for variable declarations: example: vars x,y in {1,2}; z in [-1,1]
varItems :: CharParser st [VAR_ITEM]
varItems :: CharParser st [VAR_ITEM]
varItems = [SourceName] -> CharParser st SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["vars", "var"] CharParser st SourceName
-> CharParser st [VAR_ITEM] -> CharParser st [VAR_ITEM]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT SourceName st Identity VAR_ITEM
-> CharParser st SourceName -> CharParser st [VAR_ITEM]
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]
sepBy1 ParsecT SourceName st Identity VAR_ITEM
forall st. CharParser st VAR_ITEM
varItem CharParser st SourceName
forall st. CharParser st SourceName
pSemi

-- | Parser for a variable declaration: example: vars x,y in {1,2}
varItem :: CharParser st VAR_ITEM
varItem :: CharParser st VAR_ITEM
varItem = do
  [Token]
vars <- ParsecT SourceName st Identity Token
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity [Token]
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]
sepBy1 ParsecT SourceName st Identity Token
forall st. CharParser st Token
identifier ParsecT SourceName st Identity SourceName
forall st. CharParser st SourceName
pComma
  [SourceName] -> ParsecT SourceName st Identity SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["in"]
  Domain
dom <- CharParser st Domain
forall st. CharParser st Domain
parseDomain
  VAR_ITEM -> CharParser st VAR_ITEM
forall (m :: * -> *) a. Monad m => a -> m a
return (VAR_ITEM -> CharParser st VAR_ITEM)
-> VAR_ITEM -> CharParser st VAR_ITEM
forall a b. (a -> b) -> a -> b
$ [Token] -> Domain -> Range -> VAR_ITEM
Var_item [Token]
vars Domain
dom Range
nullRange


{- | Parser for extended parameter declarations:
example: I in [1,2]; -}
epDecl :: CharParser st (Id.Token, EPDomain)
epDecl :: CharParser st (Token, EPDomain)
epDecl = do
  Token
epId <- CharParser st Token
forall st. CharParser st Token
identifier
  [SourceName] -> CharParser st SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["in"]
  CharParser st EPDomain
forall st. CharParser st EPDomain
parseEPDomain CharParser st EPDomain
-> (EPDomain -> (Token, EPDomain))
-> CharParser st (Token, EPDomain)
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> (,) Token
epId

{- | Parser for extended parameter default values and domain variable
declarations: example: I = 1; n=2 -}
epNumValAss :: CharParser st (Id.Token, APInt)
epNumValAss :: CharParser st (Token, APInt)
epNumValAss = do
  Token
epId <- CharParser st Token
forall st. CharParser st Token
identifier
  [SourceName] -> CharParser st SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["="]
  CharParser st SourceName
forall st. CharParser st SourceName
getSignedNumber CharParser st SourceName
-> (SourceName -> (Token, APInt)) -> CharParser st (Token, APInt)
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> (,) Token
epId (APInt -> (Token, APInt))
-> (SourceName -> APInt) -> SourceName -> (Token, APInt)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> APInt
forall a. Read a => SourceName -> a
read


parseDomain :: CharParser st Domain
parseDomain :: CharParser st Domain
parseDomain = do
  Char
lp <- CharParser st Char -> CharParser st Char
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st Char -> CharParser st Char)
-> CharParser st Char -> CharParser st Char
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st Char
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m Char
oneOf "{[]"
  [GroundConstant]
gcl <- ParsecT SourceName st Identity GroundConstant
-> ParsecT SourceName st Identity SourceName
-> ParsecT SourceName st Identity [GroundConstant]
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]
sepBy1 (CharParser st (Either APInt SourceName, Range)
forall st. CharParser st (Either APInt SourceName, Range)
signednumber CharParser st (Either APInt SourceName, Range)
-> ((Either APInt SourceName, Range) -> GroundConstant)
-> ParsecT SourceName st Identity GroundConstant
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> (APInt -> GroundConstant)
-> (SourceName -> GroundConstant)
-> Either APInt SourceName
-> GroundConstant
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either APInt -> GroundConstant
GCI (APFloat -> GroundConstant
GCR (APFloat -> GroundConstant)
-> (SourceName -> APFloat) -> SourceName -> GroundConstant
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> APFloat
readRat) (Either APInt SourceName -> GroundConstant)
-> ((Either APInt SourceName, Range) -> Either APInt SourceName)
-> (Either APInt SourceName, Range)
-> GroundConstant
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either APInt SourceName, Range) -> Either APInt SourceName
forall a b. (a, b) -> a
fst) ParsecT SourceName st Identity SourceName
forall st. CharParser st SourceName
pComma
  Char
rp <- CharParser st Char -> CharParser st Char
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st Char -> CharParser st Char)
-> CharParser st Char -> CharParser st Char
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st Char
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m Char
oneOf "[]}"
  let f :: Bool -> Bool -> ParsecT SourceName st Identity Domain
f o :: Bool
o c :: Bool
c = case [GroundConstant]
gcl of
                [lb :: GroundConstant
lb, rb :: GroundConstant
rb] -> Domain -> ParsecT SourceName st Identity Domain
forall (m :: * -> *) a. Monad m => a -> m a
return (Domain -> ParsecT SourceName st Identity Domain)
-> Domain -> ParsecT SourceName st Identity Domain
forall a b. (a -> b) -> a -> b
$ (GroundConstant, Bool) -> (GroundConstant, Bool) -> Domain
forall a. (a, Bool) -> (a, Bool) -> SetOrInterval a
IntVal (GroundConstant
lb, Bool
o) (GroundConstant
rb, Bool
c)
                _ -> SourceName -> ParsecT SourceName st Identity Domain
forall st a. SourceName -> CharParser st a
parseError "parseDomain: incorrect interval-list"
  case [Char
lp, Char
rp] of
    "{}" -> Domain -> CharParser st Domain
forall (m :: * -> *) a. Monad m => a -> m a
return (Domain -> CharParser st Domain) -> Domain -> CharParser st Domain
forall a b. (a -> b) -> a -> b
$ Set GroundConstant -> Domain
forall a. Set a -> SetOrInterval a
Set (Set GroundConstant -> Domain) -> Set GroundConstant -> Domain
forall a b. (a -> b) -> a -> b
$ [GroundConstant] -> Set GroundConstant
forall a. Ord a => [a] -> Set a
Set.fromList [GroundConstant]
gcl
    "[]" -> Bool -> Bool -> CharParser st Domain
forall st. Bool -> Bool -> ParsecT SourceName st Identity Domain
f Bool
True Bool
True
    "[[" -> Bool -> Bool -> CharParser st Domain
forall st. Bool -> Bool -> ParsecT SourceName st Identity Domain
f Bool
True Bool
False
    "][" -> Bool -> Bool -> CharParser st Domain
forall st. Bool -> Bool -> ParsecT SourceName st Identity Domain
f Bool
False Bool
False
    "]]" -> Bool -> Bool -> CharParser st Domain
forall st. Bool -> Bool -> ParsecT SourceName st Identity Domain
f Bool
False Bool
True
    _ -> SourceName -> CharParser st Domain
forall st a. SourceName -> CharParser st a
parseError "parseDomain: malformed domain parens"

parseEPVal :: CharParser st EPVal
parseEPVal :: CharParser st EPVal
parseEPVal = do
  Maybe Token
mId <- ParsecT SourceName st Identity Token
-> ParsecT SourceName st Identity (Maybe Token)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe ParsecT SourceName st Identity Token
forall st. CharParser st Token
identifier
  case Maybe Token
mId of
    Just n :: Token
n -> EPVal -> CharParser st EPVal
forall (m :: * -> *) a. Monad m => a -> m a
return (EPVal -> CharParser st EPVal) -> EPVal -> CharParser st EPVal
forall a b. (a -> b) -> a -> b
$ Token -> EPVal
EPConstRef Token
n
    _ -> CharParser st SourceName
forall st. CharParser st SourceName
getSignedNumber CharParser st SourceName
-> (SourceName -> EPVal) -> CharParser st EPVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> APInt -> EPVal
EPVal (APInt -> EPVal) -> (SourceName -> APInt) -> SourceName -> EPVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> APInt
forall a. Read a => SourceName -> a
read

parseEPDomain :: CharParser st EPDomain
parseEPDomain :: CharParser st EPDomain
parseEPDomain = do
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "["
  EPVal
l <- CharParser st EPVal
forall st. CharParser st EPVal
parseEPVal
  CharParser st SourceName
forall st. CharParser st SourceName
pComma
  EPVal
r <- CharParser st EPVal
forall st. CharParser st EPVal
parseEPVal
  CharParser st SourceName -> CharParser st SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser st SourceName -> CharParser st SourceName)
-> CharParser st SourceName -> CharParser st SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "]"
  EPDomain -> CharParser st EPDomain
forall (m :: * -> *) a. Monad m => a -> m a
return (EPDomain -> CharParser st EPDomain)
-> EPDomain -> CharParser st EPDomain
forall a b. (a -> b) -> a -> b
$ EPVal -> EPVal -> EPDomain
forall a. a -> a -> ClosedInterval a
ClosedInterval EPVal
l EPVal
r


-- | Toplevel parser for basic specs
basicSpec :: AnnoState.AParser st BASIC_SPEC
basicSpec :: AParser st BASIC_SPEC
basicSpec =
  AParser st BASIC_ITEM -> AParser st [Annoted BASIC_ITEM]
forall st a. AParser st a -> AParser st [Annoted a]
AnnoState.annosParser AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseBasicItems AParser st [Annoted BASIC_ITEM]
-> ([Annoted BASIC_ITEM] -> BASIC_SPEC) -> AParser st BASIC_SPEC
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [Annoted BASIC_ITEM] -> BASIC_SPEC
Basic_spec
  AParser st BASIC_SPEC
-> AParser st BASIC_SPEC -> AParser st BASIC_SPEC
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (CharParser (AnnoState st) Token
forall st. CharParser st Token
Lexer.oBraceT CharParser (AnnoState st) Token
-> CharParser (AnnoState st) Token
-> CharParser (AnnoState st) Token
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharParser (AnnoState st) Token
forall st. CharParser st Token
Lexer.cBraceT CharParser (AnnoState st) Token
-> AParser st BASIC_SPEC -> AParser st BASIC_SPEC
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BASIC_SPEC -> AParser st BASIC_SPEC
forall (m :: * -> *) a. Monad m => a -> m a
return ([Annoted BASIC_ITEM] -> BASIC_SPEC
Basic_spec []))

-- | Parser for basic items
parseBasicItems :: AnnoState.AParser st BASIC_ITEM
parseBasicItems :: AParser st BASIC_ITEM
parseBasicItems = AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseOpDecl AParser st BASIC_ITEM
-> AParser st BASIC_ITEM -> AParser st BASIC_ITEM
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseVarDecl AParser st BASIC_ITEM
-> AParser st BASIC_ITEM -> AParser st BASIC_ITEM
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseEPDefValOrDomDecl
                  AParser st BASIC_ITEM
-> AParser st BASIC_ITEM -> AParser st BASIC_ITEM
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseEPDecl AParser st BASIC_ITEM
-> AParser st BASIC_ITEM -> AParser st BASIC_ITEM
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> AParser st BASIC_ITEM
forall st. AParser st BASIC_ITEM
parseAxItems

-- | parser for operator declarations
parseOpDecl :: AnnoState.AParser st BASIC_ITEM
parseOpDecl :: AParser st BASIC_ITEM
parseOpDecl = CharParser (AnnoState st) OP_ITEM
forall st. CharParser st OP_ITEM
opItem CharParser (AnnoState st) OP_ITEM
-> (OP_ITEM -> BASIC_ITEM) -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> OP_ITEM -> BASIC_ITEM
Op_decl

-- | parser for variable declarations
parseVarDecl :: AnnoState.AParser st BASIC_ITEM
parseVarDecl :: AParser st BASIC_ITEM
parseVarDecl = CharParser (AnnoState st) [VAR_ITEM]
forall st. CharParser st [VAR_ITEM]
varItems CharParser (AnnoState st) [VAR_ITEM]
-> ([VAR_ITEM] -> BASIC_ITEM) -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [VAR_ITEM] -> BASIC_ITEM
Var_decls

{- | parser for extended parameter declarations, one of:
default value for an extended parameter (I=2)
a domain variable declaration (n=10) -}
parseEPDefValOrDomDecl :: AnnoState.AParser st BASIC_ITEM
parseEPDefValOrDomDecl :: AParser st BASIC_ITEM
parseEPDefValOrDomDecl = do
  SourceName -> CharParser (AnnoState st) SourceName
forall st. SourceName -> CharParser st SourceName
lstring "set"
  Maybe SourceName
mDef <- CharParser (AnnoState st) SourceName
-> ParsecT SourceName (AnnoState st) Identity (Maybe SourceName)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (CharParser (AnnoState st) SourceName
 -> ParsecT SourceName (AnnoState st) Identity (Maybe SourceName))
-> CharParser (AnnoState st) SourceName
-> ParsecT SourceName (AnnoState st) Identity (Maybe SourceName)
forall a b. (a -> b) -> a -> b
$ CharParser (AnnoState st) SourceName
-> CharParser (AnnoState st) SourceName
forall tok st a. GenParser tok st a -> GenParser tok st a
try (CharParser (AnnoState st) SourceName
 -> CharParser (AnnoState st) SourceName)
-> CharParser (AnnoState st) SourceName
-> CharParser (AnnoState st) SourceName
forall a b. (a -> b) -> a -> b
$ CharParser (AnnoState st) SourceName
-> CharParser (AnnoState st) SourceName
forall st a. CharParser st a -> CharParser st a
lexemeParser (CharParser (AnnoState st) SourceName
 -> CharParser (AnnoState st) SourceName)
-> CharParser (AnnoState st) SourceName
-> CharParser (AnnoState st) SourceName
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser (AnnoState st) SourceName
forall s (m :: * -> *) u.
Stream s m Char =>
SourceName -> ParsecT s u m SourceName
string "default"
  case Maybe SourceName
mDef of
    Nothing -> ParsecT SourceName (AnnoState st) Identity (Token, APInt)
-> CharParser (AnnoState st) SourceName
-> ParsecT SourceName (AnnoState st) Identity [(Token, APInt)]
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]
sepBy1 ParsecT SourceName (AnnoState st) Identity (Token, APInt)
forall st. CharParser st (Token, APInt)
epNumValAss CharParser (AnnoState st) SourceName
forall st. CharParser st SourceName
pSemi ParsecT SourceName (AnnoState st) Identity [(Token, APInt)]
-> ([(Token, APInt)] -> BASIC_ITEM) -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [(Token, APInt)] -> BASIC_ITEM
EP_domdecl
    _ -> ParsecT SourceName (AnnoState st) Identity (Token, APInt)
-> CharParser (AnnoState st) SourceName
-> ParsecT SourceName (AnnoState st) Identity [(Token, APInt)]
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]
sepBy1 ParsecT SourceName (AnnoState st) Identity (Token, APInt)
forall st. CharParser st (Token, APInt)
epNumValAss CharParser (AnnoState st) SourceName
forall st. CharParser st SourceName
pSemi ParsecT SourceName (AnnoState st) Identity [(Token, APInt)]
-> ([(Token, APInt)] -> BASIC_ITEM) -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [(Token, APInt)] -> BASIC_ITEM
EP_defval

-- | parser for extended parameter declarations
parseEPDecl :: AnnoState.AParser st BASIC_ITEM
parseEPDecl :: AParser st BASIC_ITEM
parseEPDecl = [SourceName] -> CharParser (AnnoState st) SourceName
forall st. [SourceName] -> CharParser st SourceName
oneOfKeys ["eps", "ep"] CharParser (AnnoState st) SourceName
-> AParser st BASIC_ITEM -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT SourceName (AnnoState st) Identity (Token, EPDomain)
-> CharParser (AnnoState st) SourceName
-> ParsecT SourceName (AnnoState st) Identity [(Token, EPDomain)]
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]
sepBy1 ParsecT SourceName (AnnoState st) Identity (Token, EPDomain)
forall st. CharParser st (Token, EPDomain)
epDecl CharParser (AnnoState st) SourceName
forall st. CharParser st SourceName
pSemi ParsecT SourceName (AnnoState st) Identity [(Token, EPDomain)]
-> ([(Token, EPDomain)] -> BASIC_ITEM) -> AParser st BASIC_ITEM
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> [(Token, EPDomain)] -> BASIC_ITEM
EP_decl

-- | parser for Axiom_item
parseAxItems :: AnnoState.AParser st BASIC_ITEM
parseAxItems :: AParser st BASIC_ITEM
parseAxItems = do
  AParser st Token
forall st. AParser st Token
AnnoState.dotT
  Annoted CMD
cmd <- AParser st CMD -> AParser st (Annoted CMD)
forall st a. AParser st a -> AParser st (Annoted a)
AnnoState.allAnnoParser AParser st CMD
forall st. CharParser (AnnoState st) CMD
command
  BASIC_ITEM -> AParser st BASIC_ITEM
forall (m :: * -> *) a. Monad m => a -> m a
return (BASIC_ITEM -> AParser st BASIC_ITEM)
-> BASIC_ITEM -> AParser st BASIC_ITEM
forall a b. (a -> b) -> a -> b
$ Annoted CMD -> BASIC_ITEM
Axiom_item Annoted CMD
cmd


-- ---------------------------------------------------------------------------

-- * parser for symbol maps etc.

-- ---------------------------------------------------------------------------

-- | parsing a prop symbol
symb :: GenParser Char st SYMB
symb :: GenParser Char st SYMB
symb = CharParser st Token
forall st. CharParser st Token
identifier CharParser st Token -> (Token -> SYMB) -> GenParser Char st SYMB
forall (m :: * -> *) a b. Monad m => m a -> (a -> b) -> m b
>-> Token -> SYMB
Symb_id


-- | parsing one symbol or a mapping of one to a second symbol
symbMap :: GenParser Char st SYMB_OR_MAP
symbMap :: GenParser Char st SYMB_OR_MAP
symbMap = do
  SYMB
s <- GenParser Char st SYMB
forall st. GenParser Char st SYMB
symb
  do Token
f <- CharParser st SourceName -> CharParser st Token
forall st. CharParser st SourceName -> CharParser st Token
pToken (CharParser st SourceName -> CharParser st Token)
-> CharParser st SourceName -> CharParser st Token
forall a b. (a -> b) -> a -> b
$ SourceName -> CharParser st SourceName
forall st. SourceName -> CharParser st SourceName
toKey SourceName
mapsTo
     SYMB
t <- GenParser Char st SYMB
forall st. GenParser Char st SYMB
symb
     SYMB_OR_MAP -> GenParser Char st SYMB_OR_MAP
forall (m :: * -> *) a. Monad m => a -> m a
return (SYMB -> SYMB -> Range -> SYMB_OR_MAP
Symb_map SYMB
s SYMB
t (Range -> SYMB_OR_MAP) -> Range -> SYMB_OR_MAP
forall a b. (a -> b) -> a -> b
$ Token -> Range
tokPos Token
f)
    GenParser Char st SYMB_OR_MAP
-> GenParser Char st SYMB_OR_MAP -> GenParser Char st SYMB_OR_MAP
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> SYMB_OR_MAP -> GenParser Char st SYMB_OR_MAP
forall (m :: * -> *) a. Monad m => a -> m a
return (SYMB -> SYMB_OR_MAP
Symb SYMB
s)

-- | Parse a list of comma separated symbols.
symbItems :: GenParser Char st SYMB_ITEMS
symbItems :: GenParser Char st SYMB_ITEMS
symbItems = do
  (is :: [SYMB]
is, ps :: [Token]
ps) <- GenParser Char st ([SYMB], [Token])
forall st. GenParser Char st ([SYMB], [Token])
symbs
  SYMB_ITEMS -> GenParser Char st SYMB_ITEMS
forall (m :: * -> *) a. Monad m => a -> m a
return ([SYMB] -> Range -> SYMB_ITEMS
Symb_items [SYMB]
is (Range -> SYMB_ITEMS) -> Range -> SYMB_ITEMS
forall a b. (a -> b) -> a -> b
$ [Token] -> Range
catRange [Token]
ps)

-- | parse a comma separated list of symbols
symbs :: GenParser Char st ([SYMB], [Token])
symbs :: GenParser Char st ([SYMB], [Token])
symbs = do
       SYMB
s <- GenParser Char st SYMB
forall st. GenParser Char st SYMB
symb
       do Token
c <- CharParser st Token
forall st. CharParser st Token
commaT CharParser st Token
-> GenParser Char st SYMB -> CharParser st Token
forall tok st a b.
GenParser tok st a -> GenParser tok st b -> GenParser tok st a
`followedWith` GenParser Char st SYMB
forall st. GenParser Char st SYMB
symb
          (is :: [SYMB]
is, ps :: [Token]
ps) <- GenParser Char st ([SYMB], [Token])
forall st. GenParser Char st ([SYMB], [Token])
symbs
          ([SYMB], [Token]) -> GenParser Char st ([SYMB], [Token])
forall (m :: * -> *) a. Monad m => a -> m a
return (SYMB
s SYMB -> [SYMB] -> [SYMB]
forall a. a -> [a] -> [a]
: [SYMB]
is, Token
c Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token]
ps)
         GenParser Char st ([SYMB], [Token])
-> GenParser Char st ([SYMB], [Token])
-> GenParser Char st ([SYMB], [Token])
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ([SYMB], [Token]) -> GenParser Char st ([SYMB], [Token])
forall (m :: * -> *) a. Monad m => a -> m a
return ([SYMB
s], [])

-- | parse a list of symbol mappings
symbMapItems :: GenParser Char st SYMB_MAP_ITEMS
symbMapItems :: GenParser Char st SYMB_MAP_ITEMS
symbMapItems = do
  (is :: [SYMB_OR_MAP]
is, ps :: [Token]
ps) <- GenParser Char st ([SYMB_OR_MAP], [Token])
forall st. GenParser Char st ([SYMB_OR_MAP], [Token])
symbMaps
  SYMB_MAP_ITEMS -> GenParser Char st SYMB_MAP_ITEMS
forall (m :: * -> *) a. Monad m => a -> m a
return ([SYMB_OR_MAP] -> Range -> SYMB_MAP_ITEMS
Symb_map_items [SYMB_OR_MAP]
is (Range -> SYMB_MAP_ITEMS) -> Range -> SYMB_MAP_ITEMS
forall a b. (a -> b) -> a -> b
$ [Token] -> Range
catRange [Token]
ps)

-- | parse a comma separated list of symbol mappings
symbMaps :: GenParser Char st ([SYMB_OR_MAP], [Token])
symbMaps :: GenParser Char st ([SYMB_OR_MAP], [Token])
symbMaps = do
  SYMB_OR_MAP
s <- GenParser Char st SYMB_OR_MAP
forall st. GenParser Char st SYMB_OR_MAP
symbMap
  do Token
c <- CharParser st Token
forall st. CharParser st Token
commaT CharParser st Token
-> GenParser Char st SYMB -> CharParser st Token
forall tok st a b.
GenParser tok st a -> GenParser tok st b -> GenParser tok st a
`followedWith` GenParser Char st SYMB
forall st. GenParser Char st SYMB
symb
     (is :: [SYMB_OR_MAP]
is, ps :: [Token]
ps) <- GenParser Char st ([SYMB_OR_MAP], [Token])
forall st. GenParser Char st ([SYMB_OR_MAP], [Token])
symbMaps
     ([SYMB_OR_MAP], [Token])
-> GenParser Char st ([SYMB_OR_MAP], [Token])
forall (m :: * -> *) a. Monad m => a -> m a
return (SYMB_OR_MAP
s SYMB_OR_MAP -> [SYMB_OR_MAP] -> [SYMB_OR_MAP]
forall a. a -> [a] -> [a]
: [SYMB_OR_MAP]
is, Token
c Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token]
ps)
    GenParser Char st ([SYMB_OR_MAP], [Token])
-> GenParser Char st ([SYMB_OR_MAP], [Token])
-> GenParser Char st ([SYMB_OR_MAP], [Token])
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ([SYMB_OR_MAP], [Token])
-> GenParser Char st ([SYMB_OR_MAP], [Token])
forall (m :: * -> *) a. Monad m => a -> m a
return ([SYMB_OR_MAP
s], [])

parseCommand :: String -> Maybe CMD
parseCommand :: SourceName -> Maybe CMD
parseCommand inp :: SourceName
inp =
    case GenParser Char (AnnoState ()) CMD
-> AnnoState ()
-> SourceName
-> SourceName
-> Either ParseError CMD
forall tok st a.
GenParser tok st a
-> st -> SourceName -> [tok] -> Either ParseError a
runParser GenParser Char (AnnoState ()) CMD
forall st. CharParser (AnnoState st) CMD
command (() -> AnnoState ()
forall st. st -> AnnoState st
AnnoState.emptyAnnos ()) "" SourceName
inp of
      Left _ -> Maybe CMD
forall a. Maybe a
Nothing
      Right s :: CMD
s -> CMD -> Maybe CMD
forall a. a -> Maybe a
Just CMD
s

parseExpression :: OperatorState a => a -> String -> Maybe EXPRESSION
parseExpression :: a -> SourceName -> Maybe EXPRESSION
parseExpression st :: a
st inp :: SourceName
inp =
    case GenParser Char a EXPRESSION
-> a -> SourceName -> SourceName -> Either ParseError EXPRESSION
forall tok st a.
GenParser tok st a
-> st -> SourceName -> [tok] -> Either ParseError a
runParser GenParser Char a EXPRESSION
forall st. OperatorState st => CharParser st EXPRESSION
formulaorexpression a
st "" SourceName
inp of
      Left _ -> Maybe EXPRESSION
forall a. Maybe a
Nothing
      Right s :: EXPRESSION
s -> EXPRESSION -> Maybe EXPRESSION
forall a. a -> Maybe a
Just EXPRESSION
s