2013-07-16 12:39:26 +00:00
|
|
|
module Parse.Parser (parseProgram) where
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2013-06-14 03:25:00 +00:00
|
|
|
import SourceSyntax.Module
|
2012-06-10 04:21:16 +00:00
|
|
|
import Control.Applicative ((<$>), (<*>))
|
|
|
|
import Control.Monad
|
|
|
|
import Data.Char (isSymbol, isDigit)
|
2013-02-27 07:33:47 +00:00
|
|
|
import Data.List (foldl',intercalate)
|
2012-06-10 04:21:16 +00:00
|
|
|
import Text.Parsec hiding (newline,spaces)
|
2013-07-16 12:39:26 +00:00
|
|
|
import qualified Text.PrettyPrint as P
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2013-06-14 03:25:00 +00:00
|
|
|
import Parse.Helpers
|
|
|
|
import Parse.Binop (infixStmt, OpTable)
|
|
|
|
import Parse.Expression
|
|
|
|
import Parse.Type
|
|
|
|
import Parse.Module
|
2013-07-07 20:11:16 +00:00
|
|
|
import qualified Parse.Declaration as Decl
|
2012-08-01 23:37:37 +00:00
|
|
|
|
2012-06-12 06:28:45 +00:00
|
|
|
freshDef = commitIf (freshLine >> (letter <|> char '_')) $ do
|
|
|
|
freshLine
|
2013-07-07 20:11:16 +00:00
|
|
|
Decl.declaration <?> "another datatype or variable definition"
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2013-07-07 20:11:16 +00:00
|
|
|
decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
|
|
|
|
(d:) <$> many freshDef
|
2012-06-10 04:21:16 +00:00
|
|
|
|
2012-06-11 13:11:15 +00:00
|
|
|
program = do
|
|
|
|
optional freshLine
|
2012-06-28 08:52:47 +00:00
|
|
|
(names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
|
2012-06-12 06:28:45 +00:00
|
|
|
is <- (do try (lookAhead $ reserved "import")
|
|
|
|
imports `followedBy` freshLine) <|> return []
|
2013-07-07 20:11:16 +00:00
|
|
|
declarations <- decls
|
2012-06-10 04:21:16 +00:00
|
|
|
optional freshLine ; optional spaces ; eof
|
2013-07-07 20:11:16 +00:00
|
|
|
return $ Module names exports is declarations
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2013-07-16 12:39:26 +00:00
|
|
|
parseProgram :: String -> Either [P.Doc] (Module t v)
|
2013-02-27 07:33:47 +00:00
|
|
|
parseProgram = setupParser program
|
|
|
|
|
2013-07-16 12:39:26 +00:00
|
|
|
setupParser :: IParser a -> String -> Either [P.Doc] a
|
2013-02-27 07:33:47 +00:00
|
|
|
setupParser p source =
|
|
|
|
case iParse p "" source of
|
2012-06-11 13:11:15 +00:00
|
|
|
Right result -> Right result
|
2013-07-16 12:39:26 +00:00
|
|
|
Left err -> Left [ P.sep . map P.text . words $ "Parse error at " ++ show err ]
|