on master help sign in
Properly parse all user-defined infix declarations
commitc2eecc2084790a13421112ef109b061d6d3c7aba authorEvan Czaplicki <info@elm-lang.org> dateSun 15 Sep 2013, 14:34 (13 years ago) parenta40cd3c
3 files changed+14−39
compiler/Parse/Binop.hs+1−25 compiler/Parse/Parse.hs+9−14 libraries/Basics.elm+4
compiler/Parse/Binop.hs+1−25
@@ -1,4 +1,4 @@
1−module Parse.Binop (binops, infixStmt, OpTable) where
1+module Parse.Binop (binops, OpTable) where
22
33import Control.Applicative ((<$>))
44import Control.Monad.Error
@@ -11,20 +11,6 @@ import SourceSyntax.Declaration (Assoc(..))
1111import Text.Parsec
1212import Parse.Helpers
1313
14−preludeTable =
15− [ (9, R, ".")
16− , (8, R, "^")
17− , (7, L, "*"), (7, L, "/"), (7, L, "mod"), (7, L, "div"), (7, L, "rem")
18− , (6, L, "+"), (6, L, "-")
19− , (5, R, "::"), (5, R, "++")
20− , (4, N, "<="), (4, N, ">="), (4, N, "<")
21− , (4, N, "=="), (4, N, "/="), (4, N, ">")
22− , (4, L, "~"), (4, L, "<~")
23− , (3, R, "&&")
24− , (2, R, "||")
25− , (0, R, "<|"), (0, L, "|>")
26− ]
27−
2814opLevel :: OpTable -> String -> Int
2915opLevel table op = fst $ Map.findWithDefault (9,L) op table
3016
@@ -96,13 +82,3 @@ getAssoc table n eops
9682 concat [ "Conflicting " ++ problem ++ " for binary operators ("
9783 , intercalate ", " (map fst eops), "). "
9884 , "Consider adding parentheses to disambiguate." ]
99−
100−infixStmt :: IParser (Int, Assoc, String)
101−infixStmt =
102− let infx str assoc = try (reserved ("infix" ++ str) >> return assoc) in
103− do assoc <- choice [ infx "l" L, infx "r" R, infx "" N ]
104− whitespace
105− prec <- do n <- digit ; return (read [n] :: Int)
106− whitespace
107− op <- anyOp
108− return (prec, assoc, op)
compiler/Parse/Parse.hs+9−14
@@ -9,9 +9,11 @@ import Text.Parsec hiding (newline,spaces)
99import qualified Text.PrettyPrint as P
1010
1111import qualified SourceSyntax.Module as S
12+import SourceSyntax.Declaration (Declaration(Fixity))
1213import Parse.Helpers
13−import Parse.Binop (infixStmt, OpTable)
14+import Parse.Binop (OpTable)
1415import Parse.Expression
16+import Parse.Declaration (infixDecl)
1517import Parse.Type
1618import Parse.Module
1719import qualified Parse.Declaration as Decl
@@ -59,22 +61,15 @@ setupParserWithTable table p source =
5961
6062parseFixities = infixes []
6163 where
62− nil p = const () <$> p
63− manyTil p end = end <|> (p >> manyTil p end)
64+ eatLine = anyThen (const False <$> simpleNewline <|> const True <$> eof)
6465
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
66+ infixes ops = do
67+ next <- Left <$> infixDecl <|> Right <$> eatLine
7068 case next of
71− Left stmt -> infixes (stmt:stmts)
72− Right True -> return $ format stmts
73− Right False -> infixes stmts
69+ Left (Fixity assoc lvl op) -> infixes ((op,(lvl,assoc)) : ops)
70+ Right True -> return $ Map.fromList ops
71+ Right False -> infixes ops
7472
75− format = Map.fromList . map (\(lvl,assoc,op) -> (op, (lvl,assoc)))
76−
77−
7873setupParser :: IParser a -> String -> Either [P.Doc] a
7974setupParser p source =
8075 case iParse p source of
libraries/Basics.elm+4
@@ -96,6 +96,10 @@ infixl 7 *
9696infixl 7 /
9797infixl 8 ^
9898
99+infixl 7 `div`
100+infixl 7 `mod`
101+infixl 7 `rem`
102+
99103{-| Integer division, remainder is discarded. -}
100104div : Int -> Int -> Int
101105div = Native.Basics.div