on master help sign in
Make the definition parser commit more aggressively
0d71129Evan Czaplicki
src/Elm/Utils.hs+3−2
@@ -12,6 +12,7 @@ import qualified Data.List as List
12import System.Exit (ExitCode(ExitSuccess, ExitFailure))
12import System.Exit (ExitCode(ExitSuccess, ExitFailure))
13import System.Process (readProcessWithExitCode)
13import System.Process (readProcessWithExitCode)
14
14
15+import qualified AST.Expression.Source as Source
15import qualified AST.Pattern as Pattern
16import qualified AST.Pattern as Pattern
16import qualified Parse.Helpers as Parse
17import qualified Parse.Helpers as Parse
17import qualified Parse.Expression as Parse
18import qualified Parse.Expression as Parse
@@ -99,8 +100,8 @@ missingExe command =
99
100
100isDeclaration :: String -> Maybe String
101isDeclaration :: String -> Maybe String
101isDeclaration string =
102isDeclaration string =
102− case Parse.iParse (Parse.definition (,)) string of
103+ case Parse.iParse Parse.def string of
103− Right (A.A _ (pattern, _)) ->
104+ Right (A.A _ (Source.Definition pattern _)) ->
104 Just (List.intercalate "$" (Pattern.boundVarList pattern))
105 Just (List.intercalate "$" (Pattern.boundVarList pattern))
105
106
106 _ ->
107 _ ->
src/Parse/Declaration.hs+1−1
@@ -36,7 +36,7 @@ commentOr parser =
36
36
37definition :: IParser Decl.Raw
37definition :: IParser Decl.Raw
38definition =
38definition =
39− do def <- Expr.def <?> "a value definition"
39+ do def <- Expr.def <?> "a definition or type annotation"
40 return $ Decl.Def def
40 return $ Decl.Def def
41
41
42
42
src/Parse/Expression.hs+30−43
@@ -1,4 +1,4 @@
1−module Parse.Expression (term, annotation, definition, expr, def) where
1+module Parse.Expression (term, expr, def) where
2
2
3import qualified Data.List as List
3import qualified Data.List as List
4import Text.Parsec hiding (newline, spaces)
4import Text.Parsec hiding (newline, spaces)
@@ -15,7 +15,6 @@ import qualified AST.Expression.General as E
15import qualified AST.Expression.Source as Source
15import qualified AST.Expression.Source as Source
16import qualified AST.Literal as L
16import qualified AST.Literal as L
17import qualified AST.Pattern as P
17import qualified AST.Pattern as P
18−import qualified AST.Type as T
19import qualified AST.Variable as Var
18import qualified AST.Variable as Var
20import qualified Reporting.Annotation as A
19import qualified Reporting.Annotation as A
21
20
@@ -267,8 +266,7 @@ letExpr :: IParser Source.Expr'
267letExpr =
266letExpr =
268 do try (reserved "let")
267 do try (reserved "let")
269 whitespace
268 whitespace
270− defs <-
271− Indent.block (def <* whitespace)
269+ defs <- Indent.block (def <* whitespace)
272 whitespace
270 whitespace
273 reserved "in"
271 reserved "in"
274 whitespace
272 whitespace
@@ -277,49 +275,38 @@ letExpr =
277
275
278def :: IParser Source.Def
276def :: IParser Source.Def
279def =
277def =
278+ addLocation $ Indent.withPos $
279+ do start <- defStart
280+ case start of
281+ A.A _ (P.Var name) ->
282+ do whitespace
283+ choice
284+ [ do hasType
285+ whitespace
286+ tipe <- Type.expr
287+ return $ Source.Annotation name tipe
288+ , do args <- spacePrefix Pattern.term
289+ padded equals
290+ body <- expr
291+ return $ Source.Definition start (makeFunction args body)
292+ ]
293+
294+ _ ->
295+ do args <- spacePrefix Pattern.term
296+ padded equals
297+ body <- expr
298+ return $ Source.Definition start (makeFunction args body)
299+
300+
301+
302+defStart :: IParser P.Raw
303+defStart =
280 choice
304 choice
281− [ annotation Source.Annotation
305+ [ addLocation (try (P.Var <$> parens Binop.infixOp))
282− , definition Source.Definition
306+ , Pattern.term
283 ]
307 ]
284
308
285
309
286−
287−-- TYPE ANNOTATION
288−
289−
290−annotation :: (String -> T.Raw -> a) -> IParser (A.Located a)
291−annotation creator =
292− let
293− start =
294− do v <- lowVar <|> parens Binop.infixOp
295− padded hasType
296− return v
297− in
298− addLocation (creator <$> try start <*> Type.expr)
299−
300−
301−
302−-- DEFINITION
303−
304−
305−definition :: (P.Raw -> Source.Expr -> a) -> IParser (A.Located a)
306−definition creator =
307− addLocation $
308− Indent.withPos $
309− do (name, args) <- defStart
310− padded equals
311− body <- expr
312− return . creator name $ makeFunction args body
313−
314−
315makeFunction :: [P.Raw] -> Source.Expr -> Source.Expr
310makeFunction :: [P.Raw] -> Source.Expr -> Source.Expr
316makeFunction args body@(A.A ann _) =
311makeFunction args body@(A.A ann _) =
317 foldr (\arg body' -> A.A ann $ E.Lambda arg body') body args
312 foldr (\arg body' -> A.A ann $ E.Lambda arg body') body args
318−
319−
320−defStart :: IParser (P.Raw, [P.Raw])
321−defStart =
322− expecting "the definition of a value (x = ...)" $
323− do starter <- try Pattern.term <|> addLocation (P.Var <$> parens Binop.infixOp)
324− args <- spacePrefix Pattern.term
325− return (starter, args)