@@ -1,17 +1,37 @@
1−module Parse.Binop (binops, OpTable) where
1+module Parse.Binop (infixOp, binops, OpTable) where
22
3+import Control.Monad (guard)
34import qualified Data.List as List
45import qualified Data.Map as Map
5−import Text.Parsec ((<|>), choice, getState, try)
6+import Text.Parsec ((<|>), choice, getState, lower, many1, notFollowedBy, satisfy, try)
67
78import AST.Declaration (Assoc(L, N, R))
89import AST.Expression.General (Expr'(Binop))
910import qualified AST.Expression.Source as Source
11+import qualified AST.Helpers as Help
1012import qualified AST.Variable as Var
11−import Parse.Helpers (IParser, OpTable, commitIf, failure, whitespace)
13+import Parse.Helpers (IParser, OpTable, commitIf, expecting, failure, whitespace)
1214import qualified Reporting.Annotation as A
1315
1416
17+
18+-- INFIX OPERATORS
19+
20+
21+infixOp :: IParser String
22+infixOp =
23+ expecting "an infix operator like (+)" $
24+ do op <- many1 (satisfy (\c -> Help.isSymbol c && c /= '`'))
25+ guard (op `notElem` [ "=", "..", "->", "--", "|", ":" ])
26+ case op of
27+ "." -> notFollowedBy lower >> return op
28+ _ -> return op
29+
30+
31+
32+-- BINARY EXPRESSIONS
33+
34+
1535opLevel :: OpTable -> String -> Int
1636opLevel table op =
1737 fst $ Map.findWithDefault (9,L) op table
@@ -30,18 +50,17 @@ hasLevel table n (op,_) =
3050binops
3151 :: IParser Source.Expr
3252 -> IParser Source.Expr
33− -> IParser String
3453 -> IParser Source.Expr
35−binops term last anyOp =
54+binops term last =
3655 do e <- term
3756 table <- getState
3857 split table 0 e =<< nextOps
3958 where
4059 nextOps =
4160 choice
42− [ commitIf (whitespace >> anyOp) $
61+ [ commitIf (whitespace >> infixOp) $
4362 do whitespace
44− op <- anyOp
63+ op <- infixOp
4564 whitespace
4665 expr <- Left <$> try term <|> Right <$> last
4766 case expr of