on master help sign in
Actually use infixes in parser
11f5f52Evan Czaplicki
compiler/Initialize.hs+4−1
@@ -23,7 +23,10 @@ import qualified Transform.Canonicalize as Canonical
23buildFromSource :: Bool -> Interfaces -> String -> Either [Doc] (MetadataModule () ())
23buildFromSource :: Bool -> Interfaces -> String -> Either [Doc] (MetadataModule () ())
24buildFromSource noPrelude interfaces source =
24buildFromSource noPrelude interfaces source =
25 do let add = if noPrelude then id else Prelude.add
25 do let add = if noPrelude then id else Prelude.add
26+ infixes = Map.fromList . map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
27+ . concatMap iFixities $ Map.elems interfaces
28+
26− modul@(Module _ _ _ decls') <- add `fmap` Parse.program source
29+ modul@(Module _ _ _ decls') <- add `fmap` Parse.program infixes source
27
30
28 -- check for structural errors
31 -- check for structural errors
29 Module names exs ims decls <-
32 Module names exs ims decls <-
compiler/Parse/Parse.hs+38−2
@@ -4,11 +4,13 @@ import Control.Applicative ((<$>), (<*>))
4import Control.Monad
4import Control.Monad
5import Data.Char (isSymbol, isDigit)
5import Data.Char (isSymbol, isDigit)
6import Data.List (foldl',intercalate)
6import Data.List (foldl',intercalate)
7+import qualified Data.Map as Map
7import Text.Parsec hiding (newline,spaces)
8import Text.Parsec hiding (newline,spaces)
8import qualified Text.PrettyPrint as P
9import qualified Text.PrettyPrint as P
9
10
10import qualified SourceSyntax.Module as S
11import qualified SourceSyntax.Module as S
11import Parse.Helpers
12import Parse.Helpers
13+import Parse.Binop (infixStmt, OpTable)
12import Parse.Expression
14import Parse.Expression
13import Parse.Type
15import Parse.Type
14import Parse.Module
16import Parse.Module
@@ -21,8 +23,8 @@ freshDef = commitIf (freshLine >> (letter <|> char '_')) $ do
21decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
23decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
22 (d:) <$> many freshDef
24 (d:) <$> many freshDef
23
25
24−program :: String -> Either [P.Doc] (S.Module t v)
26+program :: OpTable -> String -> Either [P.Doc] (S.Module t v)
25−program = setupParser $ do
27+program table = setupParserWithTable table $ do
26 optional freshLine
28 optional freshLine
27 (names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
29 (names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
28 is <- (do try (lookAhead $ reserved "import")
30 is <- (do try (lookAhead $ reserved "import")
@@ -39,6 +41,40 @@ dependencies =
39 (,) <$> option "Main" (getName <$> moduleDef `followedBy` freshLine)
41 (,) <$> option "Main" (getName <$> moduleDef `followedBy` freshLine)
40 <*> option [] (map fst <$> imports `followedBy` freshLine)
42 <*> option [] (map fst <$> imports `followedBy` freshLine)
41
43
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+
42setupParser :: IParser a -> String -> Either [P.Doc] a
78setupParser :: IParser a -> String -> Either [P.Doc] a
43setupParser p source =
79setupParser p source =
44 case iParse p source of
80 case iParse p source of
compiler/Transform/Canonicalize.hs+1−1
@@ -22,7 +22,7 @@ interface moduleName iface =
22 { iTypes = Map.mapKeys prefix (Map.map renameType' (iTypes iface))
22 { iTypes = Map.mapKeys prefix (Map.map renameType' (iTypes iface))
23 , iAdts = map (both prefix renameCtors) (iAdts iface)
23 , iAdts = map (both prefix renameCtors) (iAdts iface)
24 , iAliases = map (both prefix renameType') (iAliases iface)
24 , iAliases = map (both prefix renameType') (iAliases iface)
25− , iFixities = map (both id prefix) (iFixities iface)
25+ , iFixities = iFixities iface -- cannot have canonicalized operators while parsing
26 }
26 }
27 where
27 where
28 both f g (a,b,c) = (f a, b, g c)
28 both f g (a,b,c) = (f a, b, g c)