@@ -325,8 +325,8 @@ data Let
326 = DefSpace Space Row Col
326 = DefSpace Space Row Col
327 | DefType Type Row Col
327 | DefType Type Row Col
328− | DefNameRepeat Name.Name Row Col
328+ | DefNameRepeat Row Col
329− | DefNameMatch Name.Name Name.Name Row Col
329+ | DefNameMatch Name.Name Row Col
330 | DefArg Pattern Row Col
330 | DefArg Pattern Row Col
331 | DefEquals Row Col
331 | DefEquals Row Col
332 | DefBody Expr Row Col
332 | DefBody Expr Row Col
@@ -2005,7 +2005,7 @@ toDeclDefReport source name declDef startRow startCol =
2006 [ D.reflow $
2006 [ D.reflow $
2007 "Type annotations always appear directly above the relevant\
2007 "Type annotations always appear directly above the relevant\
2008− \ definition, without anything else (even doc comments) in between."
2008+ \ definition, without anything else in between. (Not even doc comments!)"
2009 , declDefNote
2009 , declDefNote
@@ -2109,6 +2109,7 @@ declDefNote =
2109data Context
2109data Context
2110 = InNode Node Row Col Context
2110 = InNode Node Row Col Context
2111 | InDef Name.Name Row Col
2111 | InDef Name.Name Row Col
2112+ | InDestruct Row Col
2114data Node
2115data Node
@@ -2124,16 +2125,18 @@ data Node
2124 deriving (Eq)
2125 deriving (Eq)
2127−getDefName :: Context -> Name.Name
2128+getDefName :: Context -> Maybe Name.Name
2128getDefName context =
2129getDefName context =
2129 case context of
2130 case context of
2131+ InDestruct _ _ -> Nothing
2130− InDef name _ _ -> name
2132+ InDef name _ _ -> Just name
2131 InNode _ _ _ c -> getDefName c
2133 InNode _ _ _ c -> getDefName c
2134isWithin :: Node -> Context -> Bool
2136isWithin :: Node -> Context -> Bool
2135isWithin desiredNode context =
2137isWithin desiredNode context =
2136 case context of
2138 case context of
2139+ InDestruct _ _ -> False
2137 InDef _ _ _ -> False
2140 InDef _ _ _ -> False
2138 InNode actualNode _ _ _ -> desiredNode == actualNode
2141 InNode actualNode _ _ _ -> desiredNode == actualNode
@@ -2229,6 +2232,7 @@ toExprReport source context expr startRow startCol =
2230 (contextRow, contextCol, aThing) =
2233 (contextRow, contextCol, aThing) =
2231 case context of
2234 case context of
2235+ InDestruct r c -> (r, c, "a definition")
2232 InDef name r c -> (r, c, "the `" ++ Name.toChars name ++ "` definition")
2236 InDef name r c -> (r, c, "the `" ++ Name.toChars name ++ "` definition")
2233 InNode NRecord r c _ -> (r, c, "a record")
2237 InNode NRecord r c _ -> (r, c, "a record")
2234 InNode NParens r c _ -> (r, c, "some parentheses")
2238 InNode NParens r c _ -> (r, c, "some parentheses")
@@ -2688,7 +2692,6 @@ toOperatorReport source context operator row col =
2688 BadEquals ->
2692 BadEquals ->
2690 region = toRegion row col
2694 region = toRegion row col
2691− name = getDefName context
2693 Report.Report "UNEXPECTED EQUALS" region [] $
2696 Report.Report "UNEXPECTED EQUALS" region [] $
2694 Code.toSnippet source region Nothing
2697 Code.toSnippet source region Nothing
@@ -2705,18 +2708,24 @@ toOperatorReport source context operator row col =
2705 "Records look like { x = 3, y = 4 } with the equals sign right\
2708 "Records look like { x = 3, y = 4 } with the equals sign right\
2706 \ after the field name. So maybe you forgot a comma?"
2709 \ after the field name. So maybe you forgot a comma?"
2711+ case getDefName context of
2708− "I may be getting confused by your indentation. Is this supposed to be part of\
2713+ "I may be getting confused by your indentation. I need all definitions to be indented\
2709− \ a definition AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2714+ \ exactly the same amount, so if this is meant to be a new definition, it may have too\
2715+ \ many spaces in front of it."
2718+ "I may be getting confused by your indentation. I think I am still parsing the `"
2719+ ++ Name.toChars name ++ "` definition. Is this supposed to be part of a definition\
2710− \ the problem may be a bit before the equals sign. I need all definitions to\
2720+ \ after that? If so, the problem may be a bit before the equals sign. I need all\
2711− \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2721+ \ definitions to be indented exactly the same amount, so the problem may be that\
2712− \ this new definition is indented a bit too much."
2722+ \ this new definition has too many spaces in front of it."
2716 BadHasType ->
2726 BadHasType ->
2718 region = toRegion row col
2728 region = toRegion row col
2719− name = getDefName context
2721 Report.Report "UNEXPECTED SYMBOL" region [] $
2730 Report.Report "UNEXPECTED SYMBOL" region [] $
2722 Code.toSnippet source region Nothing $
2731 Code.toSnippet source region Nothing $
@@ -2724,23 +2733,31 @@ toOperatorReport source context operator row col =
2724 D.reflow $
2733 D.reflow $
2725 "I was not expecting to run into the \"has type\" symbol here:"
2734 "I was not expecting to run into the \"has type\" symbol here:"
2727− D.stack
2736+ case getDefName context of
2729 D.fillSep
2738 D.fillSep
2730 ["Maybe","you","want",D.green "::","instead?"
2739 ["Maybe","you","want",D.green "::","instead?"
2731 ,"To","put","something","on","the","front","of","a","list?"
2740 ,"To","put","something","on","the","front","of","a","list?"
2747+ ["Maybe","you","want",D.green "::","instead?"
2748+ ,"To","put","something","on","the","front","of","a","list?"
2733− , D.toSimpleNote $
2750+ , D.toSimpleNote $
2734− "The single colon is reserved for type annotations and record types, but I think\
2751+ "The single colon is reserved for type annotations and record types, but I think\
2735− \ I am parsing the definition of `" ++ Name.toChars name ++ "` right now."
2752+ \ I am parsing the definition of `" ++ Name.toChars name ++ "` right now."
2737− D.toSimpleNote $
2754+ D.toSimpleNote $
2738− "I may be getting confused by your indentation. Is this supposed to be part of\
2755+ "I may be getting confused by your indentation. Is this supposed to be part of\
2739− \ a type annotation AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2756+ \ a type annotation AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2740− \ the problem may be a bit before the \"has type\" symbol. I need all definitions to\
2757+ \ the problem may be a bit before the \"has type\" symbol. I need all definitions to\
2741− \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2758+ \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2742− \ this new definition is indented a bit too much."
2759+ \ this new definition is indented a bit too much."
@@ -2824,10 +2841,10 @@ toLetReport source context let_ startRow startCol =
2824 "I was expecting the name of a definition next."
2841 "I was expecting the name of a definition next."
2826 LetDef name def row col ->
2843 LetDef name def row col ->
2827− error "TODO LetDef" name def row col
2844+ toLetDefReport source name def row col
2829 LetDestruct destruct row col ->
2846 LetDestruct destruct row col ->
2830− error "TODO LetDestruct" destruct row col
2847+ toLetDestructReport source destruct row col
2832 LetBody expr row col ->
2849 LetBody expr row col ->
2833 toExprReport source context expr row col
2850 toExprReport source context expr row col
@@ -2883,6 +2900,294 @@ toUnfinishLetReport source row col startRow startCol message =
2903+toLetDefReport :: Code.Source -> Name.Name -> Def -> Row -> Col -> Report.Report
2904+toLetDefReport source name def startRow startCol =
2906+ DefSpace space row col ->
2907+ toSpaceReport source space row col
2909+ DefType tipe row col ->
2910+ toTypeReport source (TC_Annotation name) tipe row col
2912+ DefNameRepeat row col ->
2914+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2915+ region = toRegion row col
2917+ Report.Report "EXPECTING DEFINITION" region [] $
2918+ Code.toSnippet source surroundings (Just region)
2921+ "I just saw the type annotation for `" ++ Name.toChars name
2922+ ++ "` so I was expecting to see its definition here:"
2926+ "Type annotations always appear directly above the relevant\
2927+ \ definition, without anything else in between."
2932+ DefNameMatch defName row col ->
2934+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2935+ region = toRegion row col
2937+ Report.Report "NAME MISMATCH" region [] $
2938+ Code.toSnippet source surroundings (Just region)
2941+ "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
2945+ "These names do not match! Is there a typo?"
2946+ , D.indent 4 $ D.fillSep $
2947+ [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
2951+ DefArg pattern row col ->
2952+ toPatternReport source PArg pattern row col
2954+ DefEquals row col ->
2955+ case Code.whatIsNext source row col of
2956+ Code.Keyword keyword ->
2958+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2959+ region = toKeywordRegion row col keyword
2961+ Report.Report "RESERVED WORD" region [] $
2962+ Code.toSnippet source surroundings (Just region)
2965+ "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
2968+ "This is a reserved word! Try using some other name?"
2971+ Code.Operator op ->
2973+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2974+ region = toKeywordRegion row col op
2976+ Report.Report "UNEXPECTED SYMBOL" region [] $
2977+ Code.toSnippet source surroundings (Just region)
2980+ "I was not expecting to see this symbol here:"
2984+ "I am not sure what is going wrong exactly, so here is a valid\
2985+ \ definition (with an optional type annotation) for reference:"
2987+ [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2988+ , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
2989+ , D.indent 6 $ D.fillSep ["x","+","y"]
2992+ "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
2998+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2999+ region = toRegion row col
3001+ Report.Report "PROBLEM IN DEFINITION" region [] $
3002+ Code.toSnippet source surroundings (Just region)
3005+ "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3009+ "I am not sure what is going wrong exactly, so here is a valid\
3010+ \ definition (with an optional type annotation) for reference:"
3012+ [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3013+ , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3014+ , D.indent 4 $ D.fillSep ["x","+","y"]
3017+ "Try to use that format!"
3021+ DefBody expr row col ->
3022+ toExprReport source (InDef name startRow startCol) expr row col
3024+ DefIndentEquals row col ->
3026+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3027+ region = toRegion row col
3029+ Report.Report "UNFINISHED DEFINITION" region [] $
3030+ Code.toSnippet source surroundings (Just region)
3033+ "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3037+ "I was expecting to see an argument or an equals sign next."
3042+ DefIndentType row col ->
3044+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3045+ region = toRegion row col
3047+ Report.Report "UNFINISHED DEFINITION" region [] $
3048+ Code.toSnippet source surroundings (Just region)
3051+ "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
3055+ "I just saw a colon, so I am expecting to see a type next."
3060+ DefIndentBody row col ->
3062+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3063+ region = toRegion row col
3065+ Report.Report "UNFINISHED DEFINITION" region [] $
3066+ Code.toSnippet source surroundings (Just region)
3069+ "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3073+ "I was expecting to see an expression next. What is it equal to?"
3078+ DefAlignment indent row col ->
3080+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3081+ region = toRegion row col
3082+ offset = indent - col
3084+ Report.Report "PROBLEM IN DEFINITION" region [] $
3085+ Code.toSnippet source surroundings (Just region)
3088+ "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3091+ "I just saw a type annotation indented " ++ show indent ++ " spaces, so I was\
3092+ \ expecting to see the corresponding definition next with the exact same amount\
3093+ \ of indentation. It looks like this line needs "
3094+ ++ show offset ++ " more " ++ (if offset == 1 then "space" else "spaces") ++ "?"
3103+ "Here is a valid definition (with a type annotation) for reference:"
3105+ [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3106+ , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3107+ , D.indent 6 $ D.fillSep ["x","+","y"]
3110+ "The top line (called a \"type annotation\") is optional. You can leave it off\
3111+ \ if you want. As you get more comfortable with Elm and as your project grows,\
3112+ \ it becomes more and more valuable to add them though! They work great as\
3113+ \ compiler-verified documentation, and they often improve error messages!"
3117+toLetDestructReport :: Code.Source -> Destruct -> Row -> Col -> Report.Report
3118+toLetDestructReport source destruct startRow startCol =
3120+ DestructSpace space row col ->
3121+ toSpaceReport source space row col
3123+ DestructPattern pattern row col ->
3124+ toPatternReport source PLet pattern row col
3126+ DestructEquals row col ->
3128+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3129+ region = toRegion row col
3131+ Report.Report "PROBLEM IN DEFINITION" region [] $
3132+ Code.toSnippet source surroundings (Just region)
3135+ "I got stuck trying to parse this definition:"
3137+ case Code.whatIsNext source row col of
3138+ Code.Operator ":" ->
3141+ "I was expecting to see an equals sign next, followed by an expression\
3142+ \ telling me what to compute."
3144+ "It looks like you may be trying to write a type annotation? It is not\
3145+ \ possible to add type annotations on destructuring definitions like this.\
3146+ \ You can assign a name to the overall structure, put a type annotation on\
3147+ \ that, and then destructure separately though."
3152+ "I was expecting to see an equals sign next, followed by an expression\
3153+ \ telling me what to compute."
3156+ DestructBody expr row col ->
3157+ toExprReport source (InDestruct startRow startCol) expr row col
3159+ DestructIndentEquals row col ->
3161+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3162+ region = toRegion row col
3164+ Report.Report "UNFINISHED DEFINITION" region [] $
3165+ Code.toSnippet source surroundings (Just region)
3168+ "I got stuck trying to parse this definition:"
3171+ "I was expecting to see an equals sign next, followed by an expression\
3172+ \ telling me what to compute."
3175+ DestructIndentBody row col ->
3177+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3178+ region = toRegion row col
3180+ Report.Report "UNFINISHED DEFINITION" region [] $
3181+ Code.toSnippet source surroundings (Just region)
3184+ "I got stuck while parsing this definition:"
3187+ "I was expecting to see an expression next. What is it equal to?"
@@ -3928,6 +4233,7 @@ toFuncReport source context func startRow startCol =
3928data PContext
4233data PContext
3933toPatternReport :: Code.Source -> PContext -> Pattern -> Row -> Col -> Report.Report
4239toPatternReport :: Code.Source -> PContext -> Pattern -> Row -> Col -> Report.Report
@@ -3948,16 +4254,17 @@ toPatternReport source context pattern startRow startCol =
3949 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4255 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3950 region = toKeywordRegion row col keyword
4256 region = toKeywordRegion row col keyword
3951− aThing =
4257+ inThisThing =
3952 case context of
4258 case context of
3953− PArg -> "an argument"
4259+ PArg -> "as an argument"
4260+ PCase -> "in this pattern"
3954− PCase -> "a pattern"
4261+ PLet -> "in this pattern"
3956 Report.Report "RESERVED WORD" region [] $
4263 Report.Report "RESERVED WORD" region [] $
3957 Code.toSnippet source surroundings (Just region)
4264 Code.toSnippet source surroundings (Just region)
3959 D.reflow $
4266 D.reflow $
3960− "It looks like you are trying to use `" ++ keyword ++ "` as " ++ aThing ++ ":"
4267+ "It looks like you are trying to use `" ++ keyword ++ "` " ++ inThisThing ++ ":"
3962 D.reflow $
4269 D.reflow $
3963 "This is a reserved word! Try using some other name?"
4270 "This is a reserved word! Try using some other name?"