@@ -18,8 +18,6 @@ import qualified AST.Literal as L
1818import qualified AST.Pattern as P
1919import qualified AST.Variable as Var
2020import qualified Reporting.Annotation as A
21−import qualified Reporting.Crash as Crash
22−import qualified Reporting.Region as R
2321
2422
2523-------- Basic Terms --------
@@ -136,65 +134,40 @@ parensTerm =
136134
137135recordTerm :: IParser Source.Expr
138136recordTerm =
139− addLocation $
140− brackets (choice [ misc, record ])
141− where
142− record =
143− E.Record <$> commaSep field
144−
145− field =
146− do label <- rLabel
147− patterns <- spacePrefix Pattern.term
148− padded equals
149− body <- expr
150− return (label, makeFunction patterns body)
151−
152− misc =
153− do nextParser <- try miscStarter
154− whitespace
155− nextParser
156−
157− miscStarter =
158− do start <- getMyPosition
159− record <- addLocation (E.rawVar <$> rLabel)
137+ addLocation $ brackets $ choice $
138+ [ do starter <- try (addLocation rLabel)
160139 whitespace
161140 choice
162− [ string "-" >> return (afterMinus start record)
163− , string "|" >> return (afterBar record)
141+ [ update starter
142+ , literal starter
164143 ]
144+ , return (E.Record [])
145+ ]
146+ where
147+ update (A.A ann starter) =
148+ do try (string "|")
149+ whitespace
150+ fields <- commaSep1 field
151+ return (E.Update (A.A ann (E.rawVar starter)) fields)
165152
166− afterMinus start record =
167− do field <- rLabel
168− end <- getMyPosition
169− let subRecord' = E.Remove record field
170− maybe <- optionMaybe (try (whitespace >> string "|"))
171− case maybe of
172− Nothing ->
173− return subRecord'
174− Just _ ->
175− do whitespace
176− field <- rLabel
177− padded equals
178− E.Insert (A.at start end subRecord') field <$> expr
179−
180− afterBar record =
181− do field <- rLabel
153+ literal (A.A _ starter) =
154+ do try equals
155+ whitespace
156+ value <- expr
182157 whitespace
183158 choice
184− [ do equals
159+ [ do try comma
185160 whitespace
186− E.Insert record field <$> expr
187− , do leftArrow
188− whitespace
189− value <- expr
190− updates <- spaceyPrefixBy comma update
191− return (E.Modify record ((field,value) : updates))
161+ fields <- commaSep field
162+ return (E.Record ((starter, value) : fields))
163+ , return (E.Record [(starter, value)])
192164 ]
193165
194− update =
195− do lbl <- rLabel
196− padded leftArrow
197− (,) lbl <$> expr
166+ field =
167+ do key <- rLabel
168+ padded equals
169+ value <- expr
170+ return (key, value)
198171
199172
200173term :: IParser Source.Expr
@@ -222,8 +195,7 @@ appExpr =
222195
223196expr :: IParser Source.Expr
224197expr =
225− addLocation (choice [ letExpr, caseExpr ])
226− <|> ifExpr
198+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
227199 <|> lambdaExpr
228200 <|> binaryExpr
229201 <?> "an expression"
@@ -234,45 +206,31 @@ binaryExpr =
234206 Binop.binops appExpr lastExpr anyOp
235207 where
236208 lastExpr =
237− addLocation (choice [ letExpr, caseExpr ])
238− <|> ifExpr
209+ addLocation (choice [ letExpr, caseExpr, ifExpr ])
239210 <|> lambdaExpr
240211 <?> "an expression"
241212
242213
243−ifExpr :: IParser Source.Expr
214+ifExpr :: IParser Source.Expr'
244215ifExpr =
245− do start <- getMyPosition
246− try (reserved "if")
247− whitespace
248− makeExpr <- normal <|> multiIf
249− end <- getMyPosition
250− return (makeExpr (R.Region start end))
251− where
252− normal =
253− do condition <- expr
254− padded (reserved "then")
255− thenBranch <- expr
256− whitespace <?> "an 'else' branch"
257− reserved "else" <?> "an 'else' branch"
258− whitespace
259− elseBranch <- expr
260− return $ \region -> A.A region (E.MultiIf [(condition, thenBranch)] elseBranch)
216+ ifHelp []
261217
262− multiIf =
263− do branches <- spaceSep1 multiBranch
264− return $ \region -> A.A region (E.MultiIf branches (crash region))
265218
266− crash region =
267− A.A region (E.Crash (Crash.IncompleteMultiIf region))
268−
269− multiBranch =
270− do string "|"
271− whitespace
272− condition <- expr
273− padded rightArrow
274− branch <- expr
275− return (condition, branch)
219+ifHelp :: [(Source.Expr, Source.Expr)] -> IParser Source.Expr'
220+ifHelp branches =
221+ do try (reserved "if")
222+ whitespace
223+ condition <- expr
224+ padded (reserved "then")
225+ thenBranch <- expr
226+ whitespace <?> "an 'else' branch"
227+ reserved "else" <?> "an 'else' branch"
228+ whitespace
229+ let newBranches = (condition, thenBranch) : branches
230+ choice
231+ [ ifHelp newBranches
232+ , E.If (reverse newBranches) <$> expr
233+ ]
276234
277235
278236lambdaExpr :: IParser Source.Expr