on master help sign in
Switch format of OpTables, and add iParse that takes a table
fb64557Evan Czaplicki
compiler/Docs.hs+1−1
@@ -45,7 +45,7 @@ config = Config { confIndent = 2, confCompare = keyOrder keys }
45
45
46parseFile path = do
46parseFile path = do
47 source <- readFile path
47 source <- readFile path
48− case iParse docs "" source of
48+ case iParse docs source of
49 Right json -> do
49 Right json -> do
50 putStrLn $ "Documenting " ++ path
50 putStrLn $ "Documenting " ++ path
51 let docPath = "docs" </> replaceExtension path ".json"
51 let docPath = "docs" </> replaceExtension path ".json"
compiler/Parse/Binop.hs+6−11
@@ -11,9 +11,6 @@ import SourceSyntax.Declaration (Assoc(..))
11import Text.Parsec
11import Text.Parsec
12import Parse.Helpers
12import Parse.Helpers
13
13
14−type OpTable = [(Int, Assoc, String)]
15−
16−preludeTable :: OpTable
17preludeTable =
14preludeTable =
18 [ (9, R, ".")
15 [ (9, R, ".")
19 , (8, R, "^")
16 , (8, R, "^")
@@ -29,24 +26,22 @@ preludeTable =
29 ]
26 ]
30
27
31opLevel :: OpTable -> String -> Int
28opLevel :: OpTable -> String -> Int
32−opLevel table op = Map.findWithDefault 9 op dict
29+opLevel table op = fst $ Map.findWithDefault (9,L) op table
33− where dict = Map.fromList (map (\(lvl,_,op) -> (op,lvl)) table)
34
30
35opAssoc :: OpTable -> String -> Assoc
31opAssoc :: OpTable -> String -> Assoc
36−opAssoc table op = Map.findWithDefault L op dict
32+opAssoc table op = snd $ Map.findWithDefault (9,L) op table
37− where dict = Map.fromList (map (\(_,assoc,op) -> (op,assoc)) table)
38
33
39hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
34hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
40hasLevel table n (op,_) = opLevel table op == n
35hasLevel table n (op,_) = opLevel table op == n
41
36
42−binops :: OpTable
43− -> IParser (LExpr t v)
37+binops :: IParser (LExpr t v)
44 -> IParser (LExpr t v)
38 -> IParser (LExpr t v)
45 -> IParser String
39 -> IParser String
46 -> IParser (LExpr t v)
40 -> IParser (LExpr t v)
47−binops table term last anyOp =
41+binops term last anyOp =
48 do e <- term
42 do e <- term
43+ table <- getState
49− split (table ++ preludeTable) 0 e =<< nextOps
44+ split table 0 e =<< nextOps
50 where
45 where
51 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
46 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
52 whitespace ; op <- anyOp ; whitespace
47 whitespace ; op <- anyOp ; whitespace
compiler/Parse/Expression.hs+1−1
@@ -156,7 +156,7 @@ appExpr = do
156-------- Normal Expressions --------
156-------- Normal Expressions --------
157
157
158binaryExpr :: IParser (LExpr t v)
158binaryExpr :: IParser (LExpr t v)
159−binaryExpr = binops [] appExpr lastExpr anyOp
159+binaryExpr = binops appExpr lastExpr anyOp
160 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
160 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
161 <|> lambdaExpr
161 <|> lambdaExpr
162
162
compiler/Parse/Helpers.hs+10−4
@@ -5,9 +5,11 @@ import Control.Monad
5import Control.Monad.State
5import Control.Monad.State
6import Data.Char (isUpper)
6import Data.Char (isUpper)
7import qualified Data.Set as Set
7import qualified Data.Set as Set
8+import qualified Data.Map as Map
8import SourceSyntax.Helpers as Help
9import SourceSyntax.Helpers as Help
9import SourceSyntax.Location as Location
10import SourceSyntax.Location as Location
10import SourceSyntax.Expression
11import SourceSyntax.Expression
12+import SourceSyntax.Declaration (Assoc)
11import Text.Parsec hiding (newline,spaces,State)
13import Text.Parsec hiding (newline,spaces,State)
12import Text.Parsec.Indent
14import Text.Parsec.Indent
13
15
@@ -35,11 +37,15 @@ jsReserveds = Set.fromList
35
37
36expecting = flip (<?>)
38expecting = flip (<?>)
37
39
40+type OpTable = Map.Map String (Int, Assoc)
38−type IParser a = ParsecT String () (State SourcePos) a
41+type IParser a = ParsecT String OpTable (State SourcePos) a
39
42
43+iParse :: IParser a -> String -> Either ParseError a
44+iParse = iParseWithTable "" Map.empty
45+
40−iParse :: IParser a -> SourceName -> String -> Either ParseError a
46+iParseWithTable :: SourceName -> OpTable -> IParser a -> String -> Either ParseError a
41−iParse aParser source_name input =
47+iParseWithTable sourceName table aParser input =
42− runIndent source_name $ runParserT aParser () source_name input
48+ runIndent sourceName $ runParserT aParser table sourceName input
43
49
44backslashed :: IParser Char
50backslashed :: IParser Char
45backslashed = do { char '\\'; c <- anyChar
51backslashed = do { char '\\'; c <- anyChar
compiler/Parse/Module.hs+1−1
@@ -13,7 +13,7 @@ varList = parens $ commaSep1 (var <|> parens symOp)
13
13
14getModuleName :: String -> Maybe String
14getModuleName :: String -> Maybe String
15getModuleName source =
15getModuleName source =
16− case iParse getModuleName "" source of
16+ case iParse getModuleName source of
17 Right name -> Just name
17 Right name -> Just name
18 Left _ -> Nothing
18 Left _ -> Nothing
19 where
19 where
compiler/Parse/Parse.hs+1−1
@@ -41,6 +41,6 @@ dependencies =
41
41
42setupParser :: IParser a -> String -> Either [P.Doc] a
42setupParser :: IParser a -> String -> Either [P.Doc] a
43setupParser p source =
43setupParser p source =
44− case iParse p "" source of
44+ case iParse p source of
45 Right result -> Right result
45 Right result -> Right result
46 Left err -> Left [ P.text $ "Parse error at " ++ show err ]
46 Left err -> Left [ P.text $ "Parse error at " ++ show err ]