on master help sign in
support cross-module OpTables
commitd7c89ee07c2c6237774514a27be27fd9e75dc00d authorEvan Czaplicki <info@elm-lang.org> dateThu 29 Aug 2013, 02:53 (13 years ago) parentf1a8897
4 files changed+46−7
compiler/Initialize.hs+4−1 compiler/Parse/Binop.hs+2−2 compiler/Parse/Parse.hs+39−3 compiler/Transform/Canonicalize.hs+1−1
compiler/Initialize.hs+4−1
@@ -23,7 +23,10 @@ import qualified Transform.Canonicalize as Canonical
2323buildFromSource :: Bool -> Interfaces -> String -> Either [Doc] (MetadataModule () ())
2424buildFromSource noPrelude interfaces source =
2525 do let add = if noPrelude then id else Prelude.add
26− modul@(Module _ _ _ decls') <- add `fmap` Parse.program source
26+ infixes = Map.fromList . map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
27+ . concatMap iFixities $ Map.elems interfaces
28+
29+ modul@(Module _ _ _ decls') <- add `fmap` Parse.program infixes source
2730
2831 -- check for structural errors
2932 Module names exs ims decls <-
compiler/Parse/Binop.hs+2−2
@@ -1,4 +1,4 @@
1−module Parse.Binop (binops, infixStmt, OpTable) where
1+module Parse.Binop (binops, infixStmt, OpTable, preludeTable) where
22
33import Control.Applicative ((<$>))
44import Control.Monad.Error
@@ -42,7 +42,7 @@ binops :: IParser (LExpr t v)
4242binops term last anyOp =
4343 do e <- term
4444 table <- getState
45− split (Map.union preludeTable table) 0 e =<< nextOps
45+ split table 0 e =<< nextOps
4646 where
4747 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
4848 whitespace ; op <- anyOp ; whitespace
compiler/Parse/Parse.hs+39−3
@@ -4,12 +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
12−import Parse.Binop (infixStmt, OpTable)
13+import Parse.Binop (infixStmt, OpTable, preludeTable)
1314import Parse.Expression
1415import Parse.Type
1516import Parse.Module
@@ -22,8 +23,8 @@ freshDef = commitIf (freshLine >> (letter <|> char '_')) $ do
2223decls = do d <- Decl.declaration <?> "at least one datatype or variable definition"
2324 (d:) <$> many freshDef
2425
25−program :: String -> Either [P.Doc] (S.Module t v)
26−program = setupParser $ do
26+program :: OpTable -> String -> Either [P.Doc] (S.Module t v)
27+program table = setupParserWithTable table $ do
2728 optional freshLine
2829 (names,exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
2930 is <- (do try (lookAhead $ reserved "import")
@@ -40,6 +41,41 @@ dependencies =
4041 (,) <$> option "Main" (getName <$> moduleDef `followedBy` freshLine)
4142 <*> option [] (map fst <$> imports `followedBy` freshLine)
4243
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 (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+
4379setupParser :: IParser a -> String -> Either [P.Doc] a
4480setupParser p source =
4581 case iParse p source of
compiler/Transform/Canonicalize.hs+1−1
@@ -22,7 +22,7 @@ interface moduleName iface =
2222 { iTypes = Map.mapKeys prefix (Map.map renameType' (iTypes iface))
2323 , iAdts = map (both prefix renameCtors) (iAdts iface)
2424 , 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
2626 }
2727 where
2828 both f g (a,b,c) = (f a, b, g c)