@@ -4,13 +4,12 @@ import Control.Applicative ((<$>), (<*>))
44import Control.Monad
55import Data.Char (isSymbol, isDigit)
66import Data.List (foldl',intercalate)
7−import qualified Data.Map as Map
87import Text.Parsec hiding (newline,spaces)
98import qualified Text.PrettyPrint as P
109
1110import qualified SourceSyntax.Module as S
1211import Parse.Helpers
13−import Parse.Binop (infixStmt, OpTable, preludeTable)
12+import Parse.Binop (infixStmt, OpTable)
1413import Parse.Expression
1514import Parse.Type
1615import Parse.Module
@@ -23,8 +22,8 @@ freshDef = commitIf (freshLine >> (letter <|> char '_')) $ do
2322decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
2423 (d:) <$> many freshDef
2524
26−program :: OpTable -> String -> Either [P.Doc] (S.Module t v)
27−program table = setupParserWithTable table $ do
25+program :: String -> Either [P.Doc] (S.Module t v)
26+program = setupParser $ do
2827 optional freshLine
2928 (names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
3029 is <- (do try (lookAhead $ reserved "import")
@@ -41,43 +40,8 @@ dependencies =
4140 (,) <$> option "Main" (getName <$> moduleDef `followedBy` freshLine)
4241 <*> option [] (map fst <$> imports `followedBy` freshLine)
4342
44−setupParserWithTable :: OpTable -> IParser a -> String -> Either [P.Doc] a
45−setupParserWithTable table p source =
46− do let initialTable = Map.union preludeTable table
47− localTable <- setupParser parseFixities source
48− case Map.intersection initialTable localTable of
49− overlap | not (Map.null overlap) -> Left [ msg overlap ]
50− | otherwise ->
51− flip setupParser source $ do
52− putState preludeTable --(Map.union initialTable localTable)
53− p
54− where
55− msg overlap =
56− P.vcat [ P.text "Parse error:"
57− , P.text $ "Overlapping definitions for infix operators: " ++
58− intercalate " " (Map.keys overlap)
59− ]
60−
61−parseFixities = infixes []
62− where
63− nil p = const () <$> p
64− manyTil p end = end <|> (p >> manyTil p end)
65−
66− eatLine = manyTil (nil multiComment <|> nil anyChar)
67− (const False <$> simpleNewline <|> const True <$> eof)
68−
69− infixes stmts = do
70− next <- Left <$> infixStmt <|> Right <$> eatLine
71− case next of
72− Left stmt -> infixes (stmt:stmts)
73− Right True -> return $ format stmts
74− Right False -> infixes stmts
75−
76− format = Map.fromList . map (\(lvl,assoc,op) -> (op, (lvl,assoc)))
77−
78−
7943setupParser :: IParser a -> String -> Either [P.Doc] a
8044setupParser p source =
81− case iParse p source of
45+ case iParse p "" source of
8246 Right result -> Right result
8347 Left err -> Left [ P.text $ "Parse error at " ++ show err ]