on master help sign in
Move OpTable into IParser user state
commit6d27c5eee746ec4cb97aa84ceb61bf051a72a99a authorEvan Czaplicki <info@elm-lang.org> dateThu 29 Aug 2013, 00:54 (13 years ago) parentfea6e5d
3 files changed+13−13
compiler/Parse/Binop.hs+7−11 compiler/Parse/Expression.hs+1−1 compiler/Parse/Helpers.hs+5−1
compiler/Parse/Binop.hs+7−11
@@ -11,10 +11,8 @@ import SourceSyntax.Declaration (Assoc(..))
1111import Text.Parsec
1212import Parse.Helpers
1313
14−type OpTable = [(Int, Assoc, String)]
15−
1614preludeTable :: OpTable
17−preludeTable =
15+preludeTable = Map.fromList $ map (\(lvl,assoc,op) -> (op, (lvl,assoc)))
1816 [ (9, R, ".")
1917 , (8, R, "^")
2018 , (7, L, "*"), (7, L, "/"), (7, L, "mod"), (7, L, "div"), (7, L, "rem")
@@ -29,24 +27,22 @@ preludeTable =
2927 ]
3028
3129opLevel :: OpTable -> String -> Int
32−opLevel table op = Map.findWithDefault 9 op dict
33− where dict = Map.fromList (map (\(lvl,_,op) -> (op,lvl)) table)
30+opLevel table op = fst $ Map.findWithDefault (9,R) op table
3431
3532opAssoc :: OpTable -> String -> Assoc
36−opAssoc table op = Map.findWithDefault R op dict
37− where dict = Map.fromList (map (\(_,assoc,op) -> (op,assoc)) table)
33+opAssoc table op = snd $ Map.findWithDefault (9,R) op table
3834
3935hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
4036hasLevel table n (op,_) = opLevel table op == n
4137
42−binops :: OpTable
43− -> IParser (LExpr t v)
38+binops :: IParser (LExpr t v)
4439 -> IParser (LExpr t v)
4540 -> IParser String
4641 -> IParser (LExpr t v)
47−binops table term last anyOp =
42+binops term last anyOp =
4843 do e <- term
49− split (table ++ preludeTable) 0 e =<< nextOps
44+ table <- getState
45+ split (Map.union preludeTable table) 0 e =<< nextOps
5046 where
5147 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
5248 whitespace ; op <- anyOp ; whitespace
compiler/Parse/Expression.hs+1−1
@@ -144,7 +144,7 @@ appExpr = do
144144-------- Normal Expressions --------
145145
146146binaryExpr :: IParser (LExpr t v)
147−binaryExpr = binops [] appExpr lastExpr anyOp
147+binaryExpr = binops appExpr lastExpr anyOp
148148 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
149149 <|> lambdaExpr
150150
compiler/Parse/Helpers.hs+5−1
@@ -5,9 +5,11 @@ import Control.Monad
55import Control.Monad.State
66import Data.Char (isUpper)
77import qualified Data.Set as Set
8+import qualified Data.Map as Map
89import SourceSyntax.Helpers as Help
910import SourceSyntax.Location as Location
1011import SourceSyntax.Expression
12+import SourceSyntax.Declaration (Assoc)
1113import Text.Parsec hiding (newline,spaces,State)
1214import Text.Parsec.Indent
1315
@@ -35,7 +37,9 @@ jsReserveds = Set.fromList
3537
3638expecting = flip (<?>)
3739
38−type IParser a = ParsecT String () (State SourcePos) a
40+type OpTable = Map.Map String (Int, Assoc)
41+
42+type IParser a = ParsecT String OpTable (State SourcePos) a
3943
4044iParse :: IParser a -> SourceName -> String -> Either ParseError a
4145iParse aParser source_name input =