@@ -4,11 +4,13 @@ import Control.Applicative ((<$>), (<*>))
44import Control.Monad
55import Data.Char (isSymbol, isDigit)
66import Data.List (foldl',intercalate)
7+import qualified Data.Map as Map
78import Text.Parsec hiding (newline,spaces)
89import qualified Text.PrettyPrint as P
910
1011import qualified SourceSyntax.Module as S
1112import Parse.Helpers
13+import Parse.Binop (infixStmt, OpTable)
1214import Parse.Expression
1315import Parse.Type
1416import Parse.Module
@@ -21,8 +23,8 @@ freshDef = commitIf (freshLine >> (letter <|> char '_')) $ do
2123decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
2224 (d:) <$> many freshDef
2325
24−program :: String -> Either [P.Doc] (S.Module t v)
25−program = setupParser $ do
26+program :: OpTable -> String -> Either [P.Doc] (S.Module t v)
27+program table = setupParserWithTable table $ do
2628 optional freshLine
2729 (names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
2830 is <- (do try (lookAhead $ reserved "import")
@@ -39,6 +41,40 @@ dependencies =
3941 (,) <$> option "Main" (getName <$> moduleDef `followedBy` freshLine)
4042 <*> option [] (map fst <$> imports `followedBy` freshLine)
4143
44+setupParserWithTable :: OpTable -> IParser a -> String -> Either [P.Doc] a
45+setupParserWithTable table p source =
46+ do localTable <- setupParser parseFixities source
47+ case Map.intersection table localTable of
48+ overlap | not (Map.null overlap) -> Left [ msg overlap ]
49+ | otherwise ->
50+ flip setupParser source $ do
51+ putState (Map.union table localTable)
52+ p
53+ where
54+ msg overlap =
55+ P.vcat [ P.text "Parse error:"
56+ , P.text $ "Overlapping definitions for infix operators: " ++
57+ intercalate " " (Map.keys overlap)
58+ ]
59+
60+parseFixities = infixes []
61+ where
62+ nil p = const () <$> p
63+ manyTil p end = end <|> (p >> manyTil p end)
64+
65+ eatLine = manyTil (nil multiComment <|> nil anyChar)
66+ (const False <$> simpleNewline <|> const True <$> eof)
67+
68+ infixes stmts = do
69+ next <- Left <$> infixStmt <|> Right <$> eatLine
70+ case next of
71+ Left stmt -> infixes (stmt:stmts)
72+ Right True -> return $ format stmts
73+ Right False -> infixes stmts
74+
75+ format = Map.fromList . map (\(lvl,assoc,op) -> (op, (lvl,assoc)))
76+
77+
4278setupParser :: IParser a -> String -> Either [P.Doc] a
4379setupParser p source =
4480 case iParse p source of