@@ -7,19 +7,18 @@ module Parse.Expression
9import qualified Data.Name as Name
9import qualified Data.Name as Name
10−import Data.Word (Word16)
12import qualified AST.Source as Src
11import qualified AST.Source as Src
13import qualified Parse.Keyword as Keyword
12import qualified Parse.Keyword as Keyword
14import qualified Parse.Number as Number
13import qualified Parse.Number as Number
15import qualified Parse.Pattern as Pattern
14import qualified Parse.Pattern as Pattern
16import qualified Parse.Shader as Shader
15import qualified Parse.Shader as Shader
16+import qualified Parse.Space as Space
17import qualified Parse.Symbol as Symbol
17import qualified Parse.Symbol as Symbol
18import qualified Parse.Type as Type
18import qualified Parse.Type as Type
19import qualified Parse.Utf8 as Utf8
19import qualified Parse.Utf8 as Utf8
20import qualified Parse.Variable as Var
20import qualified Parse.Variable as Var
22−import Parse.Primitives hiding (Parser, State)
21+import Parse.Primitives hiding (State)
23import qualified Parse.Primitives as P
22import qualified Parse.Primitives as P
24import qualified Reporting.Annotation as A
23import qualified Reporting.Annotation as A
25import qualified Reporting.Error.Syntax as E
24import qualified Reporting.Error.Syntax as E
@@ -29,10 +28,10 @@ import qualified Reporting.Error.Syntax as E
32−term :: Parser Src.Expr
31+term :: Parser E.Expr Src.Expr
34 do start <- getPosition
33 do start <- getPosition
35− oneOf E.XXX
34+ oneOf E.Start
36 [ variable start >>= accessible start
35 [ variable start >>= accessible start
37 , string start
36 , string start
38 , number start
37 , number start
@@ -45,90 +44,80 @@ term =
48−string :: A.Position -> Parser Src.Expr
47+string :: A.Position -> Parser E.Expr Src.Expr
49string start =
48string start =
50− do str <- Utf8.string
49+ do str <- Utf8.string E.Start E.String
52− return (A.at start end (Src.Str str))
50+ addEnd start (Src.Str str)
55−character :: A.Position -> Parser Src.Expr
53+character :: A.Position -> Parser E.Expr Src.Expr
56character start =
54character start =
57− do chr <- Utf8.character
55+ do chr <- Utf8.character E.Start E.Char
59− return (A.at start end (Src.Chr chr))
56+ addEnd start (Src.Chr chr)
62−number :: A.Position -> Parser Src.Expr
59+number :: A.Position -> Parser E.Expr Src.Expr
63number start =
60number start =
64− do nmbr <- Number.number
61+ do nmbr <- Number.number E.Start E.Number
65− end <- getPosition
62+ addEnd start $
66− return $ A.at start end $
67 case nmbr of
63 case nmbr of
68− Number.Int int ->
64+ Number.Int int -> Src.Int int
69− Src.Int int
65+ Number.Float float -> Src.Float float
71− Number.Float float ->
75−accessor :: A.Position -> Parser Src.Expr
68+accessor :: A.Position -> Parser E.Expr Src.Expr
76accessor start =
69accessor start =
77− do word1 0x2E {-.-} E.XXX
70+ do word1 0x2E {-.-} E.Dot
78− field <- Var.lower
71+ field <- Var.lower E.Access
80− return (A.at start end (Src.Accessor field))
72+ addEnd start (Src.Accessor field)
83−variable :: A.Position -> Parser Src.Expr
75+variable :: A.Position -> Parser E.Expr Src.Expr
84variable start =
76variable start =
85− do var <- Var.foreignAlpha
77+ do var <- Var.foreignAlpha E.Start
87− return (A.at start end var)
78+ addEnd start var
90−accessible :: A.Position -> Src.Expr -> Parser Src.Expr
81+accessible :: A.Position -> Src.Expr -> Parser E.Expr Src.Expr
91accessible start expr =
82accessible start expr =
92− oneOf E.XXX
83+ oneOfWithFallback
93− [ do word1 0x2E {-.-} E.XXX
84+ [ do word1 0x2E {-.-} E.Dot
94 pos <- getPosition
85 pos <- getPosition
95− field <- Var.lower
86+ field <- Var.lower E.Access
96 end <- getPosition
87 end <- getPosition
97− let newExpr = A.at start end (Src.Access expr (A.at pos end field))
89+ A.at start end (Src.Access expr (A.at pos end field))
98− accessible start newExpr
107−list :: A.Position -> Parser Src.Expr
98+list :: A.Position -> Parser E.Expr Src.Expr
108list start =
99list start =
109− inContext E.ExprList (word1 0x5B {-[-} E.XXX) $
100+ inContext E.List (word1 0x5B {-[-} E.Start) $
110− do spaces
101+ do Space.chompAndCheckIndent E.ListSpace E.ListIndentOpen
111− oneOf E.XXX
102+ oneOf E.ListOpen
112− [ do (entry, _, pos) <- expression
103+ [ do (entry, end) <- specialize E.ListExpr expression
113− checkSpace pos
104+ Space.checkIndent end E.ListIndentEnd
114− listHelp start [entry]
105+ chompListEnd start [entry]
115− , do word1 0x5D {-]-} E.XXX
106+ , do word1 0x5D {-]-} E.ListOpen
117− return (A.at start end (Src.List []))
107+ addEnd start (Src.List [])
121−listHelp :: A.Position -> [Src.Expr] -> Parser Src.Expr
111+chompListEnd :: A.Position -> [Src.Expr] -> Parser E.List Src.Expr
122−listHelp start entries =
112+chompListEnd start entries =
123− oneOf E.XXX
113+ oneOf E.ListEnd
124− [ do word1 0x2C {-,-} E.XXX
114+ [ do word1 0x2C {-,-} E.ListEnd
125− spaces
115+ Space.chompAndCheckIndent E.ListSpace E.ListIndentExpr
126− (entry, _, pos) <- expression
116+ (entry, end) <- specialize E.ListExpr expression
127− checkSpace pos
117+ Space.checkIndent end E.ListIndentEnd
128− listHelp start (entry:entries)
118+ chompListEnd start (entry:entries)
129− , do word1 0x5D {-]-} E.XXX
119+ , do word1 0x5D {-]-} E.ListEnd
131− return (A.at start end (Src.List (reverse entries)))
120+ addEnd start (Src.List (reverse entries))
@@ -136,52 +125,55 @@ listHelp start entries =
139−tuple :: A.Position -> Parser Src.Expr
128+tuple :: A.Position -> Parser E.Expr Src.Expr
140−tuple start =
129+tuple start@(A.Position row col) =
141− inContext E.ExprTuple (word1 0x28 {-(-} E.XXX) $
130+ inContext E.Tuple (word1 0x28 {-(-} E.Start) $
142− do pos <- getPosition
144− oneOf E.XXX
131+ oneOf E.TupleOpen
145− [ do noSpace pos spos
132+ [
146− word2 0x2D 0x29 {--)-} E.XXX
133+ do op <- Symbol.operator E.TupleOpen E.TupleOperatorReserved
147− end <- getPosition
134+ if op == "-"
148− return $ A.at start end (Src.Op "-")
135+ then
149−
136+ oneOf E.TupleOperatorClose
150− , do checkSpace spos
137+ [
151− (entry, _, spos2) <- expression
153− tupleHelp start entry []
155− , do noSpace pos spos
157− word1 0x29 {-)-} E.XXX
138+ do word1 0x29 {-)-} E.TupleOperatorClose
158− end <- getPosition
139+ addEnd start (Src.Op op)
159− return $ A.at start end (Src.Op op)
140+ ,
160−
141+ specialize E.TupleExpr $
161− , do noSpace pos spos
142+ do negatedExpr@(A.At (A.Region _ end) _) <- term
144+ let exprStart = A.Position row (col + 2)
145+ let expr = A.at exprStart end (Src.Negate negatedExpr)
146+ fst <$> chompExprEnd exprStart (State [] expr [] end)
162− word1 0x29 {-)-} E.XXX
149+ do word1 0x29 {-)-} E.TupleOperatorClose
163− end <- getPosition
150+ addEnd start (Src.Op op)
152+ do word1 0x29 {-)-} E.TupleOpen
164− return (A.at start end Src.Unit)
153+ addEnd start Src.Unit
155+ do Space.chompAndCheckIndent E.TupleSpace E.TupleIndentExpr
156+ (entry, end) <- specialize E.TupleExpr expression
157+ Space.checkIndent end E.TupleIndentEnd
158+ chompTupleEnd start entry []
169−tupleHelp :: A.Position -> Src.Expr -> [Src.Expr] -> Parser Src.Expr
162+chompTupleEnd :: A.Position -> Src.Expr -> [Src.Expr] -> Parser E.Tuple Src.Expr
170−tupleHelp start firstExpr revExprs =
163+chompTupleEnd start firstExpr revExprs =
171− oneOf E.XXX
164+ oneOf E.TupleEnd
172− [ do word1 0x2C {-,-} E.XXX
165+ [ do word1 0x2C {-,-} E.TupleEnd
173− spaces
166+ Space.chompAndCheckIndent E.TupleSpace E.TupleIndentExpr
174− (entry, _, pos) <- expression
167+ (entry, end) <- specialize E.TupleExpr expression
175− checkSpace pos
168+ Space.checkIndent end E.TupleIndentEnd
176− tupleHelp start firstExpr (entry : revExprs)
169+ chompTupleEnd start firstExpr (entry : revExprs)
177− , do word1 0x29 {-)-} E.XXX
170+ , do word1 0x29 {-)-} E.TupleEnd
179 case reverse revExprs of
171 case reverse revExprs of
181 return firstExpr
173 return firstExpr
183− secondExpr : others ->
175+ secondExpr : otherExprs ->
184− return (A.at start end (Src.Tuple firstExpr secondExpr others))
176+ addEnd start (Src.Tuple firstExpr secondExpr otherExprs)
@@ -189,30 +181,27 @@ tupleHelp start firstExpr revExprs =
189-- RECORDS
181-- RECORDS
192−record :: A.Position -> Parser Src.Expr
184+record :: A.Position -> Parser E.Expr Src.Expr
193record start =
185record start =
194− inContext E.ExprRecord (word1 0x7B {- { -} E.XXX) $
186+ inContext E.Record (word1 0x7B {- { -} E.Start) $
195− do spaces
187+ do Space.chompAndCheckIndent E.RecordSpace E.RecordIndentOpen
196− oneOf E.XXX
188+ oneOf E.RecordOpen
197− [ do word1 0x7D {-}-} E.XXX
189+ [ do word1 0x7D {-}-} E.RecordOpen
199− return (A.at start end (Src.Record []))
190+ addEnd start (Src.Record [])
200− , do starter <- addLocation Var.lower
191+ , do starter <- addLocation (Var.lower E.RecordField)
201− spaces
192+ Space.chompAndCheckIndent E.RecordSpace E.RecordIndentEquals
202− oneOf E.XXX
193+ oneOf E.RecordEquals
203− [ do word1 0x7C {-|-} E.XXX
194+ [ do word1 0x7C {-|-} E.RecordEquals
204− spaces
195+ Space.chompAndCheckIndent E.RecordSpace E.RecordIndentField
205 firstField <- chompField
196 firstField <- chompField
206 fields <- chompFields [firstField]
197 fields <- chompFields [firstField]
208− return (A.at start end (Src.Update starter fields))
198+ addEnd start (Src.Update starter fields)
209− , do word1 0x3D {-=-} E.XXX
199+ , do word1 0x3D {-=-} E.RecordEquals
210− spaces
200+ Space.chompAndCheckIndent E.RecordSpace E.RecordIndentExpr
211− (value, _, nextPos) <- expression
201+ (value, end) <- specialize E.RecordExpr expression
212− checkSpace nextPos
202+ Space.checkIndent end E.RecordIndentEnd
213 fields <- chompFields [(starter, value)]
203 fields <- chompFields [(starter, value)]
215− return (A.at start end (Src.Record fields))
204+ addEnd start (Src.Record fields)
@@ -220,26 +209,26 @@ record start =
220type Field = ( A.Located Name.Name, Src.Expr )
209type Field = ( A.Located Name.Name, Src.Expr )
223−chompFields :: [Field] -> Parser [Field]
212+chompFields :: [Field] -> Parser E.Record [Field]
224chompFields fields =
213chompFields fields =
225− oneOf E.XXX
214+ oneOf E.RecordEnd
226− [ do word1 0x2C {-,-} E.XXX
215+ [ do word1 0x2C {-,-} E.RecordEnd
227− spaces
216+ Space.chompAndCheckIndent E.RecordSpace E.RecordField
228 f <- chompField
217 f <- chompField
229 chompFields (f : fields)
218 chompFields (f : fields)
230− , do word1 0x7D {-}-} E.XXX
219+ , do word1 0x7D {-}-} E.RecordEnd
231 return (reverse fields)
220 return (reverse fields)
235−chompField :: Parser Field
224+chompField :: Parser E.Record Field
236chompField =
225chompField =
237− do key <- addLocation Var.lower
226+ do key <- addLocation (Var.lower E.RecordField)
238− spaces
227+ Space.chompAndCheckIndent E.RecordSpace E.RecordIndentEquals
239− word1 0x3D {-=-} E.XXX
228+ word1 0x3D {-=-} E.RecordEquals
240− spaces
229+ Space.chompAndCheckIndent E.RecordSpace E.RecordIndentExpr
241− (value, _, pos) <- expression
230+ (value, end) <- specialize E.RecordExpr expression
242− checkSpace pos
231+ Space.checkIndent end E.RecordIndentEnd
243 return (key, value)
232 return (key, value)
@@ -247,22 +236,18 @@ chompField =
247-- EXPRESSIONS
236-- EXPRESSIONS
250−type ExprParser =
239+expression :: Space.Parser E.Expr Src.Expr
254−expression :: ExprParser
255expression =
240expression =
256 do start <- getPosition
241 do start <- getPosition
257− oneOf E.XXX
242+ oneOf E.Start
258 [ let_ start
243 [ let_ start
259 , if_ start
244 , if_ start
260 , case_ start
245 , case_ start
261 , function start
246 , function start
262 , do expr <- possiblyNegativeTerm start
247 , do expr <- possiblyNegativeTerm start
263 end <- getPosition
248 end <- getPosition
264− space <- whitespace
249+ Space.chomp E.Space
265− exprHelp start (State [] expr [] end space)
250+ chompExprEnd start (State [] expr [] end)
@@ -272,77 +257,76 @@ data State =
272 , _expr :: !Src.Expr
257 , _expr :: !Src.Expr
273 , _args :: ![Src.Expr]
258 , _args :: ![Src.Expr]
274 , _end :: !A.Position
259 , _end :: !A.Position
279−exprHelp :: A.Position -> State -> ExprParser
263+chompExprEnd :: A.Position -> State -> Space.Parser E.Expr Src.Expr
280−exprHelp start (State ops expr args end pos) =
264+chompExprEnd start (State ops expr args end) =
281− oneOf E.XXX
265+ oneOfWithFallback
282 [ -- argument
266 [ -- argument
283− do checkSpace pos
267+ do Space.checkIndent end E.IndentMoreExpr
284 arg <- term
268 arg <- term
285 newEnd <- getPosition
269 newEnd <- getPosition
286− newPos <- whitespace
270+ Space.chomp E.Space
287− exprHelp start (State ops expr (arg:args) newEnd newPos)
271+ chompExprEnd start (State ops expr (arg:args) newEnd)
289− , -- infix operator
273+ , -- operator
290− do checkSpace pos
274+ do Space.checkIndent end E.IndentMoreExpr
291− opStart <- getPosition
275+ op@(A.At (A.Region opStart opEnd) opName) <- addLocation (Symbol.operator E.Start E.OperatorReserved)
292− opName <- Symbol.binop
276+ Space.chompAndCheckIndent E.Space E.IndentOperatorRight
293− opEnd <- getPosition
294− let op = A.at opStart opEnd opName
297 newStart <- getPosition
277 newStart <- getPosition
298 if "-" == opName && end /= opStart && opEnd == newStart
278 if "-" == opName && end /= opStart && opEnd == newStart
300 -- negative terms
280 -- negative terms
301− do rawTerm <- term
281+ do negatedExpr <- term
302 newEnd <- getPosition
282 newEnd <- getPosition
303− newPos <- whitespace
283+ Space.chomp E.Space
304− let arg = A.at opStart newEnd (Src.Negate rawTerm)
284+ let arg = A.at opStart newEnd (Src.Negate negatedExpr)
305− exprHelp start (State ops expr (arg:args) newEnd newPos)
285+ chompExprEnd start (State ops expr (arg:args) newEnd)
307− oneOf E.XXX
287+ oneOf E.OperatorRight
308 [ -- term
288 [ -- term
309 do newExpr <- possiblyNegativeTerm newStart
289 do newExpr <- possiblyNegativeTerm newStart
310 newEnd <- getPosition
290 newEnd <- getPosition
311− newPos <- whitespace
291+ Space.chomp E.Space
312 let newOps = (toCall expr args, op) : ops
292 let newOps = (toCall expr args, op) : ops
313− exprHelp start (State newOps newExpr [] newEnd newPos)
293+ chompExprEnd start (State newOps newExpr [] newEnd)
315 , -- final term
295 , -- final term
316− do (newLast, newEnd, newPos) <-
296+ do (newLast, newEnd) <-
317− oneOf E.XXX
297+ oneOf E.OperatorRight
318 [ let_ newStart
298 [ let_ newStart
319 , case_ newStart
299 , case_ newStart
320 , if_ newStart
300 , if_ newStart
321 , function newStart
301 , function newStart
323 let newOps = (toCall expr args, op) : ops
303 let newOps = (toCall expr args, op) : ops
324− let finalExpr = A.at start newEnd (Src.Binops (reverse newOps) newLast)
304+ let finalExpr = Src.Binops (reverse newOps) newLast
325− return ( finalExpr, newEnd, newPos )
305+ return ( A.at start newEnd finalExpr, newEnd )
328− , -- done
309+ -- done
329− let finalExpr = toCall expr args in
310+ (
330 case ops of
311 case ops of
332− return ( finalExpr, end, pos )
313+ ( toCall expr args
335− return ( A.at start end (Src.Binops (reverse ops) finalExpr), end, pos )
318+ ( A.at start end (Src.Binops (reverse ops) (toCall expr args))
339−possiblyNegativeTerm :: A.Position -> Parser Src.Expr
324+possiblyNegativeTerm :: A.Position -> Parser E.Expr Src.Expr
340possiblyNegativeTerm start =
325possiblyNegativeTerm start =
341− oneOf E.XXX
326+ oneOf E.Start
342− [ do word1 0x2D {---} E.XXX
327+ [ do word1 0x2D {---} E.Start
343 expr <- term
328 expr <- term
345− return (A.at start end (Src.Negate expr))
329+ addEnd start (Src.Negate expr)
@@ -361,30 +345,32 @@ toCall func revArgs =
361-- IF EXPRESSION
345-- IF EXPRESSION
364−if_ :: A.Position -> ExprParser
348+if_ :: A.Position -> Space.Parser E.Expr Src.Expr
365if_ start =
349if_ start =
366− inContext E.ExprIf Keyword.if_ $
350+ inContext E.If (Keyword.if_ E.Start) $
367− ifHelp start []
351+ chompIfEnd start []
370−ifHelp :: A.Position -> [(Src.Expr, Src.Expr)] -> ExprParser
354+chompIfEnd :: A.Position -> [(Src.Expr, Src.Expr)] -> Space.Parser E.If Src.Expr
371−ifHelp start branches =
355+chompIfEnd start branches =
372− do spaces
356+ do Space.chompAndCheckIndent E.IfSpace E.IfIndentCondition
373− (condition, _, condPos) <- expression
357+ (condition, condEnd) <- specialize E.IfCondition expression
374− checkSpace condPos
358+ Space.checkIndent condEnd E.IfIndentThen
375− Keyword.then_
359+ Keyword.then_ E.IfThen
376− spaces
360+ Space.chompAndCheckIndent E.IfSpace E.IfIndentThenBranch
377− (thenBranch, _, thenPos) <- expression
361+ (thenBranch, thenEnd) <- specialize E.IfThenBranch expression
378− checkSpace thenPos
362+ Space.checkIndent thenEnd E.IfIndentElse
379− Keyword.else_
363+ Keyword.else_ E.IfElse
380− spaces
364+ Space.chompAndCheckIndent E.IfSpace E.IfIndentElseBranch
381 let newBranches = (condition, thenBranch) : branches
365 let newBranches = (condition, thenBranch) : branches
382− oneOf E.XXX
366+ oneOf E.IfElseBranchStart
383− [ do Keyword.if_
368+ do Keyword.if_ E.IfElseBranchStart
384− ifHelp start newBranches
369+ chompIfEnd start newBranches
385− , do (elseBranch, elseEnd, elseSpace) <- expression
371+ do (elseBranch, elseEnd) <- specialize E.IfElseBranch expression
386− let ifExpr = A.at start elseEnd (Src.If (reverse newBranches) elseBranch)
372+ let ifExpr = Src.If (reverse newBranches) elseBranch
387− return ( ifExpr, elseEnd, elseSpace )
373+ return ( A.at start elseEnd ifExpr, elseEnd )
@@ -392,27 +378,27 @@ ifHelp start branches =
392-- LAMBDA EXPRESSION
378-- LAMBDA EXPRESSION
395−function :: A.Position -> ExprParser
381+function :: A.Position -> Space.Parser E.Expr Src.Expr
396function start =
382function start =
397− inContext E.ExprFunc (word1 0x5C {-\-} E.XXX) $
383+ inContext E.Func (word1 0x5C {-\-} E.Start) $
398− do spaces
384+ do Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArg
399− arg <- Pattern.term
385+ arg <- specialize E.FuncArg Pattern.term
400− spaces
386+ Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArrow
401− revArgs <- gatherArgs [arg]
387+ revArgs <- chompArgs [arg]
402− spaces
388+ Space.chompAndCheckIndent E.FuncSpace E.FuncIndentBody
403− (body, end, space) <- expression
389+ (body, end) <- specialize E.FuncBody expression
404− let func = A.at start end (Src.Lambda (reverse revArgs) body)
390+ let funcExpr = Src.Lambda (reverse revArgs) body
405− return ( func, end, space )
391+ return (A.at start end funcExpr, end)
408−gatherArgs :: [Src.Pattern] -> Parser [Src.Pattern]
394+chompArgs :: [Src.Pattern] -> Parser E.Func [Src.Pattern]
409−gatherArgs args =
395+chompArgs revArgs =
410− oneOf E.XXX
396+ oneOf E.FuncArrow
411− [ do arg <- Pattern.term
397+ [ do arg <- specialize E.FuncArg Pattern.term
412− spaces
398+ Space.chompAndCheckIndent E.FuncSpace E.FuncIndentArrow
413− gatherArgs (arg:args)
399+ chompArgs (arg:revArgs)
414− , do word2 0x2D 0x3E {-->-} E.XXX
400+ , do word2 0x2D 0x3E {-->-} E.FuncArrow
415− return args
401+ return revArgs
@@ -420,90 +406,84 @@ gatherArgs args =
420-- CASE EXPRESSIONS
406-- CASE EXPRESSIONS
423−case_ :: A.Position -> ExprParser
409+case_ :: A.Position -> Space.Parser E.Expr Src.Expr
424case_ start =
410case_ start =
425− inContext E.ExprCase Keyword.case_ $
411+ inContext E.Case (Keyword.case_ E.Start) $
426− do spaces
412+ do Space.chompAndCheckIndent E.CaseSpace E.CaseIndentExpr
427− (switcher, _, switcherPos) <- expression
413+ (expr, exprEnd) <- specialize E.CaseExpr expression
428− checkSpace switcherPos
414+ Space.checkIndent exprEnd E.CaseIndentOf
429− Keyword.of_
415+ Keyword.of_ E.CaseOf
430− spaces
416+ Space.chompAndCheckIndent E.CaseSpace E.CaseIndentPattern
431− oldIndent <- getIndent
417+ withIndent $
434− (firstBranch, firstEnd, firstPos) <- branchHelp
418+ do (firstBranch, firstEnd) <- chompBranch
435− (branches, end, pos) <- caseHelp [firstBranch] firstEnd firstPos
419+ (branches, end) <- chompCaseEnd [firstBranch] firstEnd
438− ( A.at start end (Src.Case switcher branches)
421+ ( A.at start end (Src.Case expr branches)
444−branchHelp :: SParser (Src.Pattern, Src.Expr)
426+chompBranch :: Space.Parser E.Case (Src.Pattern, Src.Expr)
445−branchHelp =
427+chompBranch =
446− do (pattern, patternPos) <- Pattern.expression
428+ do (pattern, patternEnd) <- specialize E.CasePattern Pattern.expression
447− checkSpace patternPos
429+ Space.checkIndent patternEnd E.CaseIndentArrow
448− word2 0x2D 0x3E {-->-} E.XXX
430+ word2 0x2D 0x3E {-->-} E.CaseArrow
449− spaces
431+ Space.chompAndCheckIndent E.CaseSpace E.CaseIndentBranch
450− (branchExpr, end, pos) <- expression
432+ (branchExpr, end) <- specialize E.CaseBranch expression
451− return ( (pattern, branchExpr), end, pos )
433+ return ( (pattern, branchExpr), end )
454−caseHelp :: [(Src.Pattern, Src.Expr)] -> A.Position -> SPos -> SParser [(Src.Pattern, Src.Expr)]
436+chompCaseEnd :: [(Src.Pattern, Src.Expr)] -> A.Position -> Space.Parser E.Case [(Src.Pattern, Src.Expr)]
455−caseHelp branches end pos =
437+chompCaseEnd branches end =
456− oneOf E.XXX
438+ oneOfWithFallback
457− [ do checkAligned pos
439+ [ do Space.checkAligned E.CasePatternAlignment
458− (branch, newEnd, newPos) <- branchHelp
440+ (branch, newEnd) <- chompBranch
459− caseHelp (branch:branches) newEnd newPos
441+ chompCaseEnd (branch:branches) newEnd
460− , return ( reverse branches, end, pos )
443+ (reverse branches, end)
465-- LET EXPRESSION
447-- LET EXPRESSION
468−let_ :: A.Position -> ExprParser
450+let_ :: A.Position -> Space.Parser E.Expr Src.Expr
469let_ start =
451let_ start =
470− do oldIndent <- getIndent
472− Keyword.let_
452+ inContext E.Let (Keyword.let_ E.Start) $
473− pushContext start E.ExprLet
453+ do (defs, defsEnd) <-
474− setIndent letIndent
454+ withBacksetIndent 3 $
475− spaces
455+ do Space.chompAndCheckIndent E.LetSpace E.LetIndentDef
476− defIndent <- getCol
456+ withIndent $
477− setIndent defIndent
457+ do (def, end) <- chompLetDef
478− (def, end, space) <- letDef
458+ chompLetDefs [def] end
479− letHelp start oldIndent [def] end space
459+
480−
460+ Space.checkIndent defsEnd E.LetIndentIn
482−letHelp :: A.Position -> Word16 -> [A.Located Src.Def] -> A.Position -> SPos -> ExprParser
483−letHelp start oldIndent revDefs end pos =
485− [ do checkAligned pos
486− (def, newEnd, newPos) <- letDef
487− letHelp start oldIndent (def:revDefs) newEnd newPos
489− , do setIndent oldIndent
491− Keyword.in_
461+ Keyword.in_ E.LetIn
492− popContext ()
462+ Space.chompAndCheckIndent E.LetSpace E.LetIndentBody
494− (body, newEnd, newPos) <- expression
463+ (body, end) <- specialize E.LetBody expression
495− let letExpr = A.at start end (Src.Let (reverse revDefs) body)
465+ ( A.at start end (Src.Let defs body)
496− return ( letExpr, newEnd, newPos )
466+ , end
470+chompLetDefs :: [A.Located Src.Def] -> A.Position -> Space.Parser E.Let [A.Located Src.Def]
471+chompLetDefs revDefs end =
473+ [ do Space.checkAligned E.LetDefAlignment
474+ (def, newEnd) <- chompLetDef
475+ chompLetDefs (def:revDefs) newEnd
477+ (reverse revDefs, end)
501-- LET DEFINITIONS
481-- LET DEFINITIONS
504−letDef :: SParser (A.Located Src.Def)
484+chompLetDef :: Space.Parser E.Let (A.Located Src.Def)
505−letDef =
485+chompLetDef =
506− oneOf E.XXX
486+ oneOf E.LetDefName
507 [ definition
487 [ definition
508 , destructure
488 , destructure
@@ -513,61 +493,57 @@ letDef =
513-- DEFINITION
493-- DEFINITION
516−definition :: SParser (A.Located Src.Def)
496+definition :: Space.Parser E.Let (A.Located Src.Def)
517definition =
497definition =
518− do start <- getPosition
498+ do aname@(A.At (A.Region start _) name) <- addLocation (Var.lower E.LetDefName)
519− name <- Var.lower
499+ specialize (E.LetDef name) $
520− end <- getPosition
500+ do Space.chompAndCheckIndent E.DefSpace E.DefIndentEquals
522− oneOf E.XXX
501+ oneOf E.DefEquals
524− do word1 0x3A {-:-} E.XXX
503+ do word1 0x3A {-:-} E.DefEquals
525− pushContext start (E.Annotation name)
504+ Space.chompAndCheckIndent E.DefSpace E.DefIndentType
527− (tipe, _, space) <- Type.expression
505+ (tipe, _) <- specialize E.DefType Type.expression
529− checkAligned space
506+ Space.checkAligned E.DefAlignment
530− defName <- matchingName name
507+ defName <- chompMatchingName name
531− let (A.At (A.Region defStart _) _) = defName
508+ Space.chompAndCheckIndent E.DefSpace E.DefIndentEquals
532− pushContext defStart (E.Definition name)
534− def <- definitionHelp start defName (Just tipe) []
509+ chompDefArgsAndBody start defName (Just tipe) []
537− do pushContext start (E.Definition name)
511+ chompDefArgsAndBody start aname Nothing []
538− def <- definitionHelp start (A.at start end name) Nothing []
543−definitionHelp :: A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> SParser (A.Located Src.Def)
515+chompDefArgsAndBody :: A.Position -> A.Located Name.Name -> Maybe Src.Type -> [Src.Pattern] -> Space.Parser E.Def (A.Located Src.Def)
544−definitionHelp start name tipe revArgs =
516+chompDefArgsAndBody start name tipe revArgs =
545− oneOf E.XXX
517+ oneOf E.DefEquals
546− [ do arg <- Pattern.term
518+ [ do arg <- specialize E.DefArg Pattern.term
547− spaces
519+ Space.chompAndCheckIndent E.DefSpace E.DefIndentEquals
548− definitionHelp start name tipe (arg : revArgs)
520+ chompDefArgsAndBody start name tipe (arg : revArgs)
549− , do word1 0x3D {-=-} E.XXX
521+ , do word1 0x3D {-=-} E.DefEquals
550− spaces
522+ Space.chompAndCheckIndent E.DefSpace E.DefIndentBody
551− (body, end, space) <- expression
523+ (body, end) <- specialize E.DefBody expression
552− let def = A.at start end (Src.Define name (reverse revArgs) body tipe)
525+ ( A.at start end (Src.Define name (reverse revArgs) body tipe)
553− return ( def, end, space )
526+ , end
557−matchingName :: Name.Name -> Parser (A.Located Name.Name)
531+chompMatchingName :: Name.Name -> Parser E.Def (A.Located Name.Name)
558−matchingName expectedName =
532+chompMatchingName expectedName =
559− let (P.Parser parserL) = Var.lower in
534+ (P.Parser parserL) = Var.lower (E.DefNameRepeat expectedName)
560− P.Parser $ \state@(P.State _ _ _ sr sc ctx) cok eok cerr eerr ->
536+ P.Parser $ \state@(P.State _ _ _ sr sc) cok eok cerr eerr ->
562− cokL name newState@(P.State _ _ _ er ec _) =
538+ cokL name newState@(P.State _ _ _ er ec) =
563 if expectedName == name
539 if expectedName == name
564 then cok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState
540 then cok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState
565− else cerr sr sc ctx (E.MatchingName expectedName)
541+ else cerr sr sc (E.DefNameMatch expectedName name)
567− eokL name newState@(P.State _ _ _ er ec _) =
543+ eokL name newState@(P.State _ _ _ er ec) =
568 if expectedName == name
544 if expectedName == name
569 then eok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState
545 then eok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState
570− else eerr sr sc ctx (E.MatchingName expectedName)
546+ else eerr sr sc (E.DefNameMatch expectedName name)
572 parserL state cokL eokL cerr eerr
548 parserL state cokL eokL cerr eerr
@@ -577,12 +553,13 @@ matchingName expectedName =
577-- DESTRUCTURE
553-- DESTRUCTURE
580−destructure :: SParser (A.Located Src.Def)
556+destructure :: Space.Parser E.Let (A.Located Src.Def)
581destructure =
557destructure =
558+ specialize E.LetDestruct $
582 do start <- getPosition
559 do start <- getPosition
583− pattern <- Pattern.term
560+ pattern <- specialize E.DestructPattern Pattern.term
584− spaces
561+ Space.chompAndCheckIndent E.DestructSpace E.DestructIndentEquals
585− word1 0x3D {-=-} E.XXX
562+ word1 0x3D {-=-} E.DestructEquals
586− spaces
563+ Space.chompAndCheckIndent E.DestructSpace E.DestructIndentBody
587− (expr, end, space) <- expression
564+ (expr, end) <- specialize E.DestructBody expression
588− return ( A.at start end (Src.Destruct pattern expr), end, space )
565+ return ( A.at start end (Src.Destruct pattern expr), end )