@@ -1,20 +1,22 @@
1−module Parse.Expr (def,term) where
1+module Parse.Expression (def,term) where
3import Control.Applicative ((<$>), (<*>))
3import Control.Applicative ((<$>), (<*>))
5−import Data.Char (isSymbol, isDigit)
6import Data.List (foldl')
4import Data.List (foldl')
7import Text.Parsec hiding (newline,spaces)
5import Text.Parsec hiding (newline,spaces)
8import Text.Parsec.Indent
6import Text.Parsec.Indent
9import qualified Text.Pandoc as Pan
7import qualified Text.Pandoc as Pan
11−import Parse.Library
9+import Parse.Helpers
10+import qualified Parse.Pattern as Pattern
12−import Parse.Patterns
11+import qualified Parse.Type as Type
13−import Parse.Binops
12+import Parse.Binop
14−import Parse.Types
13+import Parse.Literal
16−import Ast
15+import SourceSyntax.Location as Location
17−import Located
16+import SourceSyntax.Pattern hiding (tuple,list)
17+import qualified SourceSyntax.Literal as Literal
18+import SourceSyntax.Expression
19+import SourceSyntax.Declaration (Declaration(Definition))
19import Guid
21import Guid
20import Types.Types (Type (VarT), Scheme (Forall))
22import Types.Types (Type (VarT), Scheme (Forall))
@@ -24,36 +26,19 @@ import System.IO.Unsafe
25-------- Basic Terms --------
27-------- Basic Terms --------
27−numTerm :: IParser Expr
28−numTerm = toExpr <$> (preNum <?> "number")
29− where toExpr n | '.' `elem` n = FloatNum (read n)
30− | otherwise = IntNum (read n)
31− preNum = (++) <$> many1 digit <*> option "" postNum
32− postNum = do try $ lookAhead (string "." >> digit)
34− ('.':) <$> many1 digit
36−strTerm :: IParser Expr
37−strTerm = liftM Str . expecting "string" . betwixt '"' '"' . many $
38− backslashed <|> satisfy (/='"')
40varTerm :: IParser Expr
29varTerm :: IParser Expr
41varTerm = toVar <$> var <?> "variable"
30varTerm = toVar <$> var <?> "variable"
43−toVar v = case v of "True" -> Boolean True
32+toVar v = case v of "True" -> Literal (Literal.Boolean True)
44− "False" -> Boolean False
33+ "False" -> Literal (Literal.Boolean False)
45 _ -> Var v
34 _ -> Var v
47−chrTerm :: IParser Expr
48−chrTerm = Chr <$> betwixt '\'' '\'' (backslashed <|> satisfy (/='\''))
51accessor :: IParser Expr
36accessor :: IParser Expr
52accessor = do
37accessor = do
53 start <- getPosition
38 start <- getPosition
54 lbl <- try (string "." >> rLabel)
39 lbl <- try (string "." >> rLabel)
55 end <- getPosition
40 end <- getPosition
56− let loc e = addLoc ("." ++ lbl) (pos start end e)
41+ let loc e = Location.add ("." ++ lbl) (Location.at start end e)
57 return (Lambda "_" (loc $ Access (loc $ Var "_") lbl))
42 return (Lambda "_" (loc $ Access (loc $ Var "_") lbl))
@@ -76,7 +61,7 @@ parensTerm = parens $ choice
76 [ do start <- getPosition
61 [ do start <- getPosition
77 op <- try anyOp
62 op <- try anyOp
78 end <- getPosition
63 end <- getPosition
79− let loc = pos start end
64+ let loc = Location.at start end
80 return . loc . Lambda "x" . loc . Lambda "y" . loc $
65 return . loc . Lambda "x" . loc . Lambda "y" . loc $
81 Binop op (loc $ Var "x") (loc $ Var "y")
66 Binop op (loc $ Var "x") (loc $ Var "y")
82 , do start <- getPosition
67 , do start <- getPosition
@@ -84,24 +69,24 @@ parensTerm = parens $ choice
84 commas <- comma >> many (whitespace >> comma)
69 commas <- comma >> many (whitespace >> comma)
85 end <- getPosition
70 end <- getPosition
86 let vars = map (('v':) . show) [ 0 .. length commas + 1 ]
71 let vars = map (('v':) . show) [ 0 .. length commas + 1 ]
87− loc = pos start end
72+ loc = Location.at start end
88 return $ foldr (\x e -> loc $ Lambda x e)
73 return $ foldr (\x e -> loc $ Lambda x e)
89 (loc . tuple $ map (loc . Var) vars) vars
74 (loc . tuple $ map (loc . Var) vars) vars
90 , do start <- getPosition
75 , do start <- getPosition
91 es <- commaSep expr
76 es <- commaSep expr
92 end <- getPosition
77 end <- getPosition
93 return $ case es of [e] -> e
78 return $ case es of [e] -> e
94− _ -> pos start end (tuple es)
79+ _ -> Location.at start end (tuple es)
97recordTerm :: IParser LExpr
82recordTerm :: IParser LExpr
98recordTerm = brackets $ choice [ misc, addLocation record ]
83recordTerm = brackets $ choice [ misc, addLocation record ]
99 where field = do
84 where field = do
100− fDefs <- (:) <$> (PVar <$> rLabel) <*> spacePrefix patternTerm
85+ fDefs <- (:) <$> (PVar <$> rLabel) <*> spacePrefix Pattern.term
101 whitespace
86 whitespace
102 e <- string "=" >> whitespace >> expr
87 e <- string "=" >> whitespace >> expr
103 n <- sourceLine <$> getPosition
88 n <- sourceLine <$> getPosition
104− runAt (1000 * n) $ flattenPatterns fDefs e
89+ runAt (1000 * n) $ Pattern.flatten fDefs e
105 extract [ FnDef f args exp ] = return (f,args,exp)
90 extract [ FnDef f args exp ] = return (f,args,exp)
106 extract _ = fail "Improperly formed record field."
91 extract _ = fail "Improperly formed record field."
107 record = Record <$> (mapM extract =<< commaSep field)
92 record = Record <$> (mapM extract =<< commaSep field)
@@ -127,7 +112,7 @@ recordTerm = brackets $ choice [ misc, addLocation record ]
129term :: IParser LExpr
114term :: IParser LExpr
130−term = addLocation (choice [ numTerm, strTerm, chrTerm, listTerm, accessor ])
115+term = addLocation (choice [ Literal <$> literal, listTerm, accessor ])
131 <|> accessible (addLocation varTerm <|> parensTerm <|> recordTerm)
116 <|> accessible (addLocation varTerm <|> parensTerm <|> recordTerm)
132 <?> "basic term (4, x, 'c', etc.)"
117 <?> "basic term (4, x, 'c', etc.)"
@@ -138,7 +123,7 @@ appExpr = do
138 tlist <- spaceSep1 term
123 tlist <- spaceSep1 term
139 return $ case tlist of
124 return $ case tlist of
140 t:[] -> t
125 t:[] -> t
141− t:ts -> foldl' (\f x -> epos f x $ App f x) t ts
126+ t:ts -> foldl' (\f x -> Location.merge f x $ App f x) t ts
143-------- Normal Expressions --------
128-------- Normal Expressions --------
@@ -154,7 +139,7 @@ ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)
154 thenBranch <- expr
139 thenBranch <- expr
155 whitespace <?> "an 'else' branch" ; reserved "else" <?> "an 'else' branch" ; whitespace
140 whitespace <?> "an 'else' branch" ; reserved "else" <?> "an 'else' branch" ; whitespace
156 elseBranch <- expr
141 elseBranch <- expr
157− return $ MultiIf [(bool, thenBranch), (notLocated (Var "otherwise"), elseBranch)]
142+ return $ MultiIf [(bool, thenBranch), (Location.none (Var "otherwise"), elseBranch)]
158 multiIf = MultiIf <$> spaceSep1 iff
143 multiIf = MultiIf <$> spaceSep1 iff
159 where iff = do string "|" ; whitespace
144 where iff = do string "|" ; whitespace
160 b <- expr ; whitespace ; string "->" ; whitespace
145 b <- expr ; whitespace ; string "->" ; whitespace
@@ -163,10 +148,10 @@ ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)
163lambdaExpr :: IParser LExpr
148lambdaExpr :: IParser LExpr
164lambdaExpr = do char '\\' <|> char '\x03BB' <?> "anonymous function"
149lambdaExpr = do char '\\' <|> char '\x03BB' <?> "anonymous function"
165 whitespace
150 whitespace
166− pats <- spaceSep1 patternTerm
151+ pats <- spaceSep1 Pattern.term
167 whitespace ; arrow ; whitespace
152 whitespace ; arrow ; whitespace
168 e <- expr
153 e <- expr
169− return . run $ makeLambda pats e
154+ return . run $ Pattern.makeLambda pats e
171defSet :: IParser [Def]
156defSet :: IParser [Def]
172defSet = concat <$> block (do d <- anyDef ; whitespace ; return d)
157defSet = concat <$> block (do d <- anyDef ; whitespace ; return d)
@@ -182,7 +167,7 @@ caseExpr :: IParser Expr
182caseExpr = do
167caseExpr = do
183 reserved "case"; whitespace; e <- expr; whitespace; reserved "of"; whitespace
168 reserved "case"; whitespace; e <- expr; whitespace; reserved "of"; whitespace
184 Case e <$> (with <|> without)
169 Case e <$> (with <|> without)
185− where case_ = do p <- patternExpr; whitespace; arrow; whitespace
170+ where case_ = do p <- Pattern.expr; whitespace; arrow; whitespace
186 (,) p <$> expr
171 (,) p <$> expr
187 with = brackets (semiSep1 (case_ <?> "cases { x -> ... }"))
172 with = brackets (semiSep1 (case_ <?> "cases { x -> ... }"))
188 without = block (do c <- case_ ; whitespace ; return c)
173 without = block (do c <- case_ ; whitespace ; return c)
@@ -192,15 +177,15 @@ expr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
192 <|> binaryExpr
177 <|> binaryExpr
193 <?> "an expression"
178 <?> "an expression"
195−funcDef = try (do p1 <- try patternTerm ; infics p1 <|> func p1)
180+funcDef = try (do p1 <- try Pattern.term ; infics p1 <|> func p1)
196− <|> ((:[]) <$> patternExpr)
181+ <|> ((:[]) <$> Pattern.expr)
197 <?> "the definition of a variable (x = ...)"
182 <?> "the definition of a variable (x = ...)"
198− where func p@(PVar v) = (p:) <$> spacePrefix patternTerm
183+ where func p@(PVar v) = (p:) <$> spacePrefix Pattern.term
199 func p = do try (lookAhead (whitespace >> string "="))
184 func p = do try (lookAhead (whitespace >> string "="))
200 return [p]
185 return [p]
201 infics p1 = do
186 infics p1 = do
202 o:p <- try (whitespace >> anyOp)
187 o:p <- try (whitespace >> anyOp)
203− p2 <- (whitespace >> patternTerm)
188+ p2 <- (whitespace >> Pattern.term)
204 return $ if o == '`' then [ PVar $ takeWhile (/='`') p, p1, p2 ]
189 return $ if o == '`' then [ PVar $ takeWhile (/='`') p, p1, p2 ]
205 else [ PVar (o:p), p1, p2 ]
190 else [ PVar (o:p), p1, p2 ]
@@ -210,10 +195,10 @@ assignExpr = withPos $ do
210 whitespace
195 whitespace
211 e <- string "=" >> whitespace >> expr
196 e <- string "=" >> whitespace >> expr
212 n <- sourceLine <$> getPosition
197 n <- sourceLine <$> getPosition
213− runAt (1000 * n) $ flattenPatterns fDefs e
198+ runAt (1000 * n) $ Pattern.flatten fDefs e
216− ((\d -> [d]) <$> typeAnnotation) <|>
201+ ((\d -> [d]) <$> Type.annotation) <|>
217 assignExpr
202 assignExpr
219def = map Definition <$> anyDef
204def = map Definition <$> anyDef