GMPParser.hs revision f36b9db17fd6c5a36f1a0549968f4d7748080a15
1a879e0f576d139dcd52e6d8ec958b4a3c169846Jens Elkner----------------------------------------------------------------
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-- the Generic Model Parser Abstract Syntax
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-- Copyright 2007, Lutz Schroeder and Georgel Calin
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa----------------------------------------------------------------
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
1a879e0f576d139dcd52e6d8ec958b4a3c169846Jens Elknermodule GMPParser where
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Text.ParserCombinators.Parsec
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport qualified Text.ParserCombinators.Parsec.Token as P
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Text.ParserCombinators.Parsec.Expr
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport Text.ParserCombinators.Parsec.Language
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksaimport GMPAS
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-----------------------------------------------------------
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-- The Parser Things
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa-----------------------------------------------------------
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksapar5er :: Parser String
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksapar5er = do
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa do try(string "F"); return "F"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "T"); return "T"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "~"); return "Not"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string"/\\") ; return "And"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "\\/"); return "Or"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "->"); return "If"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "<-"); return "Fi"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do try(string "<->"); return "Iff"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <?> "GMPParser.par5er"
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa{- the parser should actually look somewhat like the following
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksapar5er :: Parser Formula
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksapar5er = do Text.ParserCombinators.Parsec.try(string "F");
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return F
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do Text.ParserCombinators.Parsec.try(string "T");
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return T
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do Text.ParserCombinators.Parsec.try(string "~");
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return Neg
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do Text.ParserCombinators.Parsec.try(string "/\\");
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return And
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do Text.ParserCombinators.Parsec.try(string "\\/");
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return Or
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa <|> do Text.ParserCombinators.Parsec.try(string "->");
1a879e0f576d139dcd52e6d8ec958b4a3c169846Jens Elkner return If
<|> do try(string "<-");
return Fi
-}
runLex :: Show a => Parser a -> String -> IO ()
runLex p input = run (do {
whiteSpace
; x <- p
; eof
; return x
}) input
run :: Show a => Parser a -> String -> IO ()
run p input
= case (parse p "" input) of
Left err -> do{ putStr "parse error at "
; print err
}
Right x -> print x
----------------------------------------------------------------
-- Formulae
----------------------------------------------------------------
{-
form :: Parser Formula
form = choice
[ not
, and
, or
, ifimpl
, fiimpl
, iffimpl
, mop
]
<?> "formulae"
not = do
try (symbol "~") ;
f <- form ;
return Not(f)
and = do
try (symbol "/"; symbol "\\") ;
f <- form ;
-- not complete
-}
----------------------------------------------------------------
-- The lexer
----------------------------------------------------------------
lexer = P.makeTokenParser gmpDef
lexeme = P.lexeme lexer
parens = P.parens lexer
braces = P.braces lexer
semiSep = P.semiSep lexer
semiSep1 = P.semiSep1 lexer
commaSep = P.commaSep lexer
commaSep1 = P.commaSep1 lexer
whiteSpace = P.whiteSpace lexer
symbol = P.symbol lexer
identifier = P.identifier lexer
reserved = P.reserved lexer
natural = P.natural lexer
gmpDef
= haskellStyle
{ identStart = letter
, identLetter = alphaNum <|> oneOf "_'" -- ???
, opStart = opLetter gmpDef
, opLetter = oneOf "\\->/~[]"
, reservedOpNames = ["~","->","<-","<->","/\\","\\/","[]"]
}
----------------------------------------------------------------
-- ???
----------------------------------------------------------------