on master help sign in
Switch format of OpTables, and add iParse that takes a table
commitfb64557f755887253bbcb4e89b0bc3142bbe4f53 authorEvan Czaplicki <info@elm-lang.org> dateSun 15 Sep 2013, 13:42 (13 years ago) parentd71b957
6 files changed+20−19
compiler/Docs.hs+1−1 compiler/Parse/Binop.hs+6−11 compiler/Parse/Expression.hs+1−1 compiler/Parse/Helpers.hs+10−4 compiler/Parse/Module.hs+1−1 compiler/Parse/Parse.hs+1−1
compiler/Docs.hs+1−1
@@ -45,7 +45,7 @@ config = Config { confIndent = 2, confCompare = keyOrder keys }
4545
4646parseFile path = do
4747 source <- readFile path
48− case iParse docs "" source of
48+ case iParse docs source of
4949 Right json -> do
5050 putStrLn $ "Documenting " ++ path
5151 let docPath = "docs" </> replaceExtension path ".json"
compiler/Parse/Binop.hs+6−11
@@ -11,9 +11,6 @@ import SourceSyntax.Declaration (Assoc(..))
1111import Text.Parsec
1212import Parse.Helpers
1313
14−type OpTable = [(Int, Assoc, String)]
15−
16−preludeTable :: OpTable
1714preludeTable =
1815 [ (9, R, ".")
1916 , (8, R, "^")
@@ -29,24 +26,22 @@ preludeTable =
2926 ]
3027
3128opLevel :: OpTable -> String -> Int
32−opLevel table op = Map.findWithDefault 9 op dict
33− where dict = Map.fromList (map (\(lvl,_,op) -> (op,lvl)) table)
29+opLevel table op = fst $ Map.findWithDefault (9,L) op table
3430
3531opAssoc :: OpTable -> String -> Assoc
36−opAssoc table op = Map.findWithDefault L op dict
37− where dict = Map.fromList (map (\(_,assoc,op) -> (op,assoc)) table)
32+opAssoc table op = snd $ Map.findWithDefault (9,L) op table
3833
3934hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
4035hasLevel table n (op,_) = opLevel table op == n
4136
42−binops :: OpTable
43− -> IParser (LExpr t v)
37+binops :: IParser (LExpr t v)
4438 -> IParser (LExpr t v)
4539 -> IParser String
4640 -> IParser (LExpr t v)
47−binops table term last anyOp =
41+binops term last anyOp =
4842 do e <- term
49− split (table ++ preludeTable) 0 e =<< nextOps
43+ table <- getState
44+ split table 0 e =<< nextOps
5045 where
5146 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
5247 whitespace ; op <- anyOp ; whitespace
compiler/Parse/Expression.hs+1−1
@@ -156,7 +156,7 @@ appExpr = do
156156-------- Normal Expressions --------
157157
158158binaryExpr :: IParser (LExpr t v)
159−binaryExpr = binops [] appExpr lastExpr anyOp
159+binaryExpr = binops appExpr lastExpr anyOp
160160 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
161161 <|> lambdaExpr
162162
compiler/Parse/Helpers.hs+10−4
@@ -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,11 +37,15 @@ 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+type IParser a = ParsecT String OpTable (State SourcePos) a
3942
40−iParse :: IParser a -> SourceName -> String -> Either ParseError a
41−iParse aParser source_name input =
42− runIndent source_name $ runParserT aParser () source_name input
43+iParse :: IParser a -> String -> Either ParseError a
44+iParse = iParseWithTable "" Map.empty
45+
46+iParseWithTable :: SourceName -> OpTable -> IParser a -> String -> Either ParseError a
47+iParseWithTable sourceName table aParser input =
48+ runIndent sourceName $ runParserT aParser table sourceName input
4349
4450backslashed :: IParser Char
4551backslashed = do { char '\\'; c <- anyChar
compiler/Parse/Module.hs+1−1
@@ -13,7 +13,7 @@ varList = parens $ commaSep1 (var <|> parens symOp)
1313
1414getModuleName :: String -> Maybe String
1515getModuleName source =
16− case iParse getModuleName "" source of
16+ case iParse getModuleName source of
1717 Right name -> Just name
1818 Left _ -> Nothing
1919 where
compiler/Parse/Parse.hs+1−1
@@ -41,6 +41,6 @@ dependencies =
4141
4242setupParser :: IParser a -> String -> Either [P.Doc] a
4343setupParser p source =
44− case iParse p "" source of
44+ case iParse p source of
4545 Right result -> Right result
4646 Left err -> Left [ P.text $ "Parse error at " ++ show err ]