1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
48import Prelude hiding (Char, String)
49import qualified Data.Char as Char
50import qualified Data.Name as Name
51import Data.Word (Word16)
52import Numeric (showHex)
54import qualified Elm.ModuleName as ModuleName
55import Parse.Primitives (Row, Col)
56import Parse.Symbol (BadOperator(..))
57import qualified Reporting.Annotation as A
58import qualified Reporting.Doc as D
59import qualified Reporting.Report as Report
60import qualified Reporting.Render.Code as Code
68 = ModuleNameUnspecified ModuleName.Raw
69 | ModuleNameMismatch ModuleName.Raw (A.Located ModuleName.Raw)
70 | UnexpectedPort A.Region
80 = ModuleSpace Space Row Col
81 | ModuleBadEnd Row Col
83 | ModuleProblem Row Col
85 | ModuleExposing Exposing Row Col
87 | PortModuleProblem Row Col
88 | PortModuleName Row Col
89 | PortModuleExposing Exposing Row Col
99 | ImportExposing Row Col
100 | ImportExposingList Exposing Row Col
101 | ImportEnd Row Col -- different based on col=1 or if greater
103 | ImportIndentName Row Col
104 | ImportIndentAlias Row Col
105 | ImportIndentExposingList Row Col
109 | Declarations Decl Row Col
113 = ExposingSpace Space Row Col
114 | ExposingStart Row Col
115 | ExposingValue Row Col
116 | ExposingOperator Row Col
117 | ExposingOperatorReserved BadOperator Row Col
118 | ExposingOperatorRightParen Row Col
119 | ExposingTypePrivacy Row Col
120 | ExposingEnd Row Col
122 | ExposingIndentEnd Row Col
123 | ExposingIndentValue Row Col
132 | DeclSpace Space Row Col
135 | DeclType DeclType Row Col
136 | DeclDef Name.Name DeclDef Row Col
138 | DeclFreshLineAfterDocComment Row Col
142 = DeclDefSpace Space Row Col
143 | DeclDefEquals Row Col
144 | DeclDefType Type Row Col
145 | DeclDefArg Pattern Row Col
146 | DeclDefBody Expr Row Col
147 | DeclDefNameRepeat Row Col
148 | DeclDefNameMatch Name.Name Row Col
150 | DeclDefIndentType Row Col
151 | DeclDefIndentEquals Row Col
152 | DeclDefIndentBody Row Col
156 = PortSpace Space Row Col
159 | PortType Type Row Col
160 | PortIndentName Row Col
161 | PortIndentColon Row Col
162 | PortIndentType Row Col
170 = DT_Space Space Row Col
172 | DT_Alias TypeAlias Row Col
173 | DT_Union CustomType Row Col
175 | DT_IndentName Row Col
179 = AliasSpace Space Row Col
181 | AliasEquals Row Col
182 | AliasBody Type Row Col
184 | AliasIndentEquals Row Col
185 | AliasIndentBody Row Col
189 = CT_Space Space Row Col
194 | CT_VariantArg Type Row Col
196 | CT_IndentEquals Row Col
197 | CT_IndentBar Row Col
198 | CT_IndentAfterBar Row Col
199 | CT_IndentAfterEquals Row Col
211 | Record Record Row Col
212 | Tuple Tuple Row Col
217 | OperatorRight Name.Name Row Col
218 | OperatorReserved BadOperator Row Col
222 | String String Row Col
223 | Number Number Row Col
224 | Space Space Row Col
225 | EndlessShader Row Col
226 | ShaderProblem [Char.Char] Row Col
227 | IndentOperatorRight Name.Name Row Col
233 | RecordField Row Col
234 | RecordEquals Row Col
235 | RecordExpr Expr Row Col
236 | RecordSpace Space Row Col
238 | RecordIndentOpen Row Col
239 | RecordIndentEnd Row Col
240 | RecordIndentField Row Col
241 | RecordIndentEquals Row Col
242 | RecordIndentExpr Row Col
246 = TupleExpr Expr Row Col
247 | TupleSpace Space Row Col
249 | TupleOperatorClose Row Col
250 | TupleOperatorReserved BadOperator Row Col
252 | TupleIndentExpr1 Row Col
253 | TupleIndentExprN Row Col
254 | TupleIndentEnd Row Col
258 = ListSpace Space Row Col
260 | ListExpr Expr Row Col
263 | ListIndentOpen Row Col
264 | ListIndentEnd Row Col
265 | ListIndentExpr Row Col
269 = FuncSpace Space Row Col
270 | FuncArg Pattern Row Col
271 | FuncBody Expr Row Col
274 | FuncIndentArg Row Col
275 | FuncIndentArrow Row Col
276 | FuncIndentBody Row Col
280 = CaseSpace Space Row Col
282 | CasePattern Pattern Row Col
284 | CaseExpr Expr Row Col
285 | CaseBranch Expr Row Col
287 | CaseIndentOf Row Col
288 | CaseIndentExpr Row Col
289 | CaseIndentPattern Row Col
290 | CaseIndentArrow Row Col
291 | CaseIndentBranch Row Col
292 | CasePatternAlignment Word16 Row Col
296 = IfSpace Space Row Col
299 | IfElseBranchStart Row Col
301 | IfCondition Expr Row Col
302 | IfThenBranch Expr Row Col
303 | IfElseBranch Expr Row Col
305 | IfIndentCondition Row Col
306 | IfIndentThen Row Col
307 | IfIndentThenBranch Row Col
308 | IfIndentElseBranch Row Col
309 | IfIndentElse Row Col
313 = LetSpace Space Row Col
315 | LetDefAlignment Word16 Row Col
317 | LetDef Name.Name Def Row Col
318 | LetDestruct Destruct Row Col
319 | LetBody Expr Row Col
320 | LetIndentDef Row Col
321 | LetIndentIn Row Col
322 | LetIndentBody Row Col
326 = DefSpace Space Row Col
327 | DefType Type Row Col
328 | DefNameRepeat Row Col
329 | DefNameMatch Name.Name Row Col
330 | DefArg Pattern Row Col
332 | DefBody Expr Row Col
333 | DefIndentEquals Row Col
334 | DefIndentType Row Col
335 | DefIndentBody Row Col
336 | DefAlignment Word16 Row Col
340 = DestructSpace Space Row Col
341 | DestructPattern Pattern Row Col
342 | DestructEquals Row Col
343 | DestructBody Expr Row Col
344 | DestructIndentEquals Row Col
345 | DestructIndentBody Row Col
353 = PRecord PRecord Row Col
354 | PTuple PTuple Row Col
355 | PList PList Row Col
359 | PString String Row Col
360 | PNumber Number Row Col
361 | PFloat Word16 Row Col
363 | PWildcardNotVar Name.Name Int Row Col
364 | PSpace Space Row Col
366 | PIndentStart Row Col
367 | PIndentAlias Row Col
371 = PRecordOpen Row Col
373 | PRecordField Row Col
374 | PRecordSpace Space Row Col
376 | PRecordIndentOpen Row Col
377 | PRecordIndentEnd Row Col
378 | PRecordIndentField Row Col
384 | PTupleExpr Pattern Row Col
385 | PTupleSpace Space Row Col
387 | PTupleIndentEnd Row Col
388 | PTupleIndentExpr1 Row Col
389 | PTupleIndentExprN Row Col
395 | PListExpr Pattern Row Col
396 | PListSpace Space Row Col
398 | PListIndentOpen Row Col
399 | PListIndentEnd Row Col
400 | PListIndentExpr Row Col
408 = TRecord TRecord Row Col
409 | TTuple TTuple Row Col
412 | TSpace Space Row Col
414 | TIndentStart Row Col
418 = TRecordOpen Row Col
421 | TRecordField Row Col
422 | TRecordColon Row Col
423 | TRecordType Type Row Col
425 | TRecordSpace Space Row Col
427 | TRecordIndentOpen Row Col
428 | TRecordIndentField Row Col
429 | TRecordIndentColon Row Col
430 | TRecordIndentType Row Col
431 | TRecordIndentEnd Row Col
437 | TTupleType Type Row Col
438 | TTupleSpace Space Row Col
440 | TTupleIndentType1 Row Col
441 | TTupleIndentTypeN Row Col
442 | TTupleIndentEnd Row Col
452 | CharNotString Word16
456 = StringEndless_Single
457 | StringEndless_Multi
458 | StringEscape Escape
463 | BadUnicodeFormat Word16
464 | BadUnicodeCode Word16
465 | BadUnicodeLength Word16 Int Int
472 | NumberNoLeadingZero
481 | EndlessMultiComment
488toReport :: Code.Source -> Error -> Report.Report
491 ModuleNameUnspecified name ->
493 region = toRegion 1 1
495 Report.Report "MODULE NAME MISSING" region [] $
498 "I need the module name to be declared at the top of this file, like this:"
499 , D.indent 4 $ D.fillSep $
500 [ D.cyan "module", D.fromName name, D.cyan "exposing", "(..)" ]
502 "Try adding that as the first line of your file!"
504 "It is best to replace (..) with an explicit list of types and\
505 \ functions you want to expose. When you know a value is only used\
506 \ within this module, you can refactor without worrying about uses\
507 \ elsewhere. Limiting exposed values can also speed up compilation\
508 \ because I can skip a bunch of work if I see that the exposed API\
512 ModuleNameMismatch expectedName (A.At region actualName) ->
513 Report.Report "MODULE NAME MISMATCH" region [ModuleName.toChars expectedName] $
514 Code.toSnippet source region Nothing
516 "It looks like this module name is out of sync:"
520 "I need it to match the file path, so I was expecting to see `"
521 ++ ModuleName.toChars expectedName
522 ++ "` here. Make the following change, and you should be all set!"
524 D.dullyellow (D.fromName actualName) <> " -> " <> D.green (D.fromName expectedName)
526 "I require that module names correspond to file paths. This makes it much\
527 \ easier to explore unfamiliar codebases! So if you want to keep the current\
528 \ module name, try renaming the file instead."
532 UnexpectedPort region ->
533 Report.Report "UNEXPECTED PORTS" region [] $
534 Code.toSnippet source region Nothing
537 "You are declaring ports in a normal module."
541 ["Switch","this","to","say",D.cyan "port module","instead,"
542 ,"marking","that","this","module","contains","port","declarations."
545 "Ports are not a traditional FFI for calling JS functions directly. They need a different mindset! Read"
547 "to learn the syntax and how to use it effectively."
552 Report.Report "NO PORTS" region [] $
553 Code.toSnippet source region Nothing
556 "This module does not declare any ports, but it says it will:"
559 ["Switch","this","to",D.cyan "module"
560 ,"and","you","should","be","all","set!"
565 toParseErrorReport source modul
568toParseErrorReport :: Code.Source -> Module -> Report.Report
569toParseErrorReport source modul =
571 ModuleSpace space row col ->
572 toSpaceReport source space row col
574 ModuleBadEnd row col ->
575 case Code.whatIsNext source row col of
576 Code.Keyword keyword ->
578 region = toKeywordRegion row col keyword
580 Report.Report "RESERVED WORD" region [] $
581 Code.toSnippet source region Nothing
584 "I got stuck on this reserved word:"
587 "The name `" ++ keyword ++ "` is reserved, so try using a different name?"
592 region = toKeywordRegion row col op
594 Report.Report "UNEXPECTED SYMBOL" region [] $
595 Code.toSnippet source region Nothing
598 "I ran into an unexpected symbol:"
601 "I was not expecting to see a " ++ op ++ " here. Try deleting it? Maybe\
602 \ I can give a better hint from there?"
605 Code.Close term bracket ->
607 region = toRegion row col
609 Report.Report ("UNEXPECTED " ++ map Char.toUpper term) region [] $
610 Code.toSnippet source region Nothing
613 "I ran into an unexpected " ++ term ++ ":"
616 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
619 Code.Other maybeChar ->
621 region = toRegion row col
625 Report.Report "UNEXPECTED SEMICOLON" region [] $
626 Code.toSnippet source region Nothing
629 "I got stuck on this semicolon:"
632 [ D.reflow $ "Try removing it?"
634 "Some languages require semicolons at the end of each statement. These are\
635 \ often called C-like languages, and they usually share a lot of language design\
636 \ choices. (E.g. side-effects, for loops, etc.) Elm manages effects with commands\
637 \ and subscriptions instead, so there is no special syntax for \"statements\" and\
638 \ therefore no need to use semicolons to separate them. I think this will make\
639 \ more sense as you work through <https://guide.elm-lang.org> though!"
644 Report.Report "UNEXPECTED COMMA" region [] $
645 Code.toSnippet source region Nothing
648 "I got stuck on this comma:"
652 "I do not think I am parsing a list or tuple right now. Try deleting the comma?"
654 "If this is supposed to be part of a list, the problem may be a bit earlier.\
655 \ Perhaps the opening [ is missing? Or perhaps some value in the list has an extra\
656 \ closing ] that is making me think the list ended earlier? The same kinds of\
657 \ things could be going wrong if this is supposed to be a tuple."
662 Report.Report "UNEXPECTED CHARACTER" region [] $
663 Code.toSnippet source region Nothing
666 "I got stuck on this character:"
670 "It is not used for anything in Elm syntax. It is used for multi-line strings in\
671 \ some languages though, so if you want a string that spans multiple lines, you\
672 \ can use Elm's multi-line string syntax like this:"
674 [ D.fillSep [D.green "markdown","=",D.dullyellow "\"\"\""]
676 , D.dullyellow "# Multi-line Strings"
678 , D.dullyellow "- start with triple double quotes"
679 , D.dullyellow "- write whatever you want"
680 , D.dullyellow "- no need to escape newlines or double quotes"
681 , D.dullyellow "- end with triple double quotes"
683 , D.dullyellow "\"\"\""
686 "Otherwise I do not know what is going on! Try removing the character?"
691 Report.Report "UNEXPECTED SYMBOL" region [] $
692 Code.toSnippet source region Nothing
695 "I got stuck on this dollar sign:"
698 "It is not used for anything in Elm syntax. Are you coming from a language where\
699 \ dollar signs can be used in variable names? If so, try a name that (1) starts\
700 \ with a letter and (2) only contains letters, numbers, and underscores."
703 Just c | elem c ['#','@','!','%','~'] ->
704 Report.Report "UNEXPECTED SYMBOL" region [] $
705 Code.toSnippet source region Nothing
708 "I got stuck on this symbol:"
711 "It is not used for anything in Elm syntax. Try removing it?"
715 Report.Report "SYNTAX PROBLEM" region [] $
716 Code.toSnippet source region Nothing
722 "Whatever I am running into is confusing me a lot! Normally I can give fairly\
723 \ specific hints, but something is really tripping me up this time."
726 ModuleProblem row col ->
728 region = toRegion row col
730 Report.Report "UNFINISHED MODULE DECLARATION" region [] $
731 Code.toSnippet source region Nothing
734 "I am parsing an `module` declaration, but I got stuck here:"
738 "Here are some examples of valid `module` declarations:"
739 , D.indent 4 $ D.vcat $
740 [ D.fillSep [D.cyan "module","Main",D.cyan "exposing","(..)"]
741 , D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(Dict, empty, get)"]
744 "I generally recommend using an explicit exposing list. I can skip compiling a bunch\
745 \ of files when the public interface of a module stays the same, so exposing fewer\
746 \ values can help improve compile times!"
750 ModuleName row col ->
752 region = toRegion row col
754 Report.Report "EXPECTING MODULE NAME" region [] $
755 Code.toSnippet source region Nothing
758 "I was parsing an `module` declaration until I got stuck here:"
762 "I was expecting to see the module name next, like in these examples:"
763 , D.indent 4 $ D.vcat $
764 [ D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(..)"]
765 , D.fillSep [D.cyan "module","Maybe",D.cyan "exposing","(..)"]
766 , D.fillSep [D.cyan "module","Html.Attributes",D.cyan "exposing","(..)"]
767 , D.fillSep [D.cyan "module","Json.Decode",D.cyan "exposing","(..)"]
770 "Notice that the module names all start with capital letters. That is required!"
774 ModuleExposing exposing row col ->
775 toExposingReport source exposing row col
777 PortModuleProblem row col ->
779 region = toRegion row col
781 Report.Report "UNFINISHED PORT MODULE DECLARATION" region [] $
782 Code.toSnippet source region Nothing
785 "I am parsing an `port module` declaration, but I got stuck here:"
789 "Here are some examples of valid `port module` declarations:"
790 , D.indent 4 $ D.vcat $
791 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
792 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
794 , D.link "Note" "Read" "ports" "for more help."
798 PortModuleName row col ->
800 region = toRegion row col
802 Report.Report "EXPECTING MODULE NAME" region [] $
803 Code.toSnippet source region Nothing
806 "I was parsing an `module` declaration until I got stuck here:"
810 "I was expecting to see the module name next, like in these examples:"
811 , D.indent 4 $ D.vcat $
812 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
813 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
816 "Notice that the module names start with capital letters. That is required!"
820 PortModuleExposing exposing row col ->
821 toExposingReport source exposing row col
825 region = toRegion row col
827 Report.Report "BAD MODULE DECLARATION" region [] $
828 Code.toSnippet source region Nothing
831 "I cannot parse this module declaration:"
834 "This type of module is reserved for the @elm organization. It is used to\
835 \ define certain effects, avoiding building them into the compiler."
840 region = toRegion row col
842 toBadFirstLineReport keyword =
843 Report.Report "TOO MUCH INDENTATION" region [] $
844 Code.toSnippet source region Nothing
847 "This `" ++ keyword ++ "` should not have any spaces before it:"
850 "Delete the spaces before `" ++ keyword ++ "` until there are none left!"
854 case Code.whatIsNext source row col of
855 Code.Keyword "module" -> toBadFirstLineReport "module"
856 Code.Keyword "import" -> toBadFirstLineReport "import"
857 Code.Keyword "type" -> toBadFirstLineReport "type"
858 Code.Keyword "port" -> toBadFirstLineReport "port"
860 Report.Report "SYNTAX PROBLEM" region [] $
861 Code.toSnippet source region Nothing
868 "I am not sure what is going on, but I recommend starting an Elm\
869 \ file with the following lines:"
871 [ D.fillSep [D.cyan "import","Html"]
873 , D.fillSep [D.green "main","="]
874 , D.indent 2 $ D.fillSep [D.cyan "Html" <> ".text",D.dullyellow "\"Hello!\""]
877 "You should be able to copy those lines directly into your file. Check out the\
878 \ examples at <https://elm-lang.org/examples> for more help getting started!"
880 "This can also happen when something is indented too much!"
884 ImportStart row col ->
885 toImportReport source row col
887 ImportName row col ->
889 region = toRegion row col
891 Report.Report "EXPECTING IMPORT NAME" region [] $
892 Code.toSnippet source region Nothing
895 "I was parsing an `import` until I got stuck here:"
899 "I was expecting to see a module name next, like in these examples:"
900 , D.indent 4 $ D.vcat $
901 [ D.fillSep [D.cyan "import","Dict"]
902 , D.fillSep [D.cyan "import","Maybe"]
903 , D.fillSep [D.cyan "import","Html.Attributes"]
904 , D.fillSep [D.cyan "import","Json.Decode"]
907 "Notice that the module names all start with capital letters. That is required!"
912 toImportReport source row col
914 ImportAlias row col ->
916 region = toRegion row col
918 Report.Report "EXPECTING IMPORT ALIAS" region [] $
919 Code.toSnippet source region Nothing
922 "I was parsing an `import` until I got stuck here:"
926 "I was expecting to see an alias next, like in these examples:"
927 , D.indent 4 $ D.vcat $
928 [ D.fillSep [D.cyan "import","Html.Attributes",D.cyan "as","Attr"]
929 , D.fillSep [D.cyan "import","WebGL.Texture",D.cyan "as","Texture"]
930 , D.fillSep [D.cyan "import","Json.Decode",D.cyan "as","D"]
933 "Notice that the alias always starts with a capital letter. That is required!"
937 ImportExposing row col ->
938 toImportReport source row col
940 ImportExposingList exposing row col ->
941 toExposingReport source exposing row col
944 toImportReport source row col
946 ImportIndentName row col ->
947 toImportReport source row col
949 ImportIndentAlias row col ->
950 toImportReport source row col
952 ImportIndentExposingList row col ->
954 region = toRegion row col
956 Report.Report "UNFINISHED IMPORT" region [] $
957 Code.toSnippet source region Nothing
960 "I was parsing an `import` until I got stuck here:"
964 "I was expecting to see the list of exposed values next. For example, here\
965 \ are two ways to expose values from the `Html` module:"
966 , D.indent 4 $ D.vcat $
967 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
968 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
971 "I generally recommend the second style. It is more explicit, making it\
972 \ much easier to figure out where values are coming from in large projects!"
978 region = toRegion row col
980 Report.Report "BAD INFIX" region [] $
981 Code.toSnippet source region Nothing
984 "Something went wrong in this infix operator declaration:"
987 "This feature is used by the @elm organization to define the\
988 \ languages built-in operators."
991 Declarations decl _ _ ->
992 toDeclarationsReport source decl
999toImportReport :: Code.Source -> Row -> Col -> Report.Report
1000toImportReport source row col =
1002 region = toRegion row col
1004 Report.Report "UNFINISHED IMPORT" region [] $
1005 Code.toSnippet source region Nothing
1008 "I am partway through parsing an import, but I got stuck here:"
1012 "Here are some examples of valid `import` declarations:"
1013 , D.indent 4 $ D.vcat $
1014 [ D.fillSep [D.cyan "import","Html"]
1015 , D.fillSep [D.cyan "import","Html",D.cyan "as","H"]
1016 , D.fillSep [D.cyan "import","Html",D.cyan "as","H",D.cyan "exposing","(..)"]
1017 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
1020 "You are probably trying to import a different module, but try to make it look like one of these examples!"
1029toExposingReport :: Code.Source -> Exposing -> Row -> Col -> Report.Report
1030toExposingReport source exposing startRow startCol =
1032 ExposingSpace space row col ->
1033 toSpaceReport source space row col
1035 ExposingStart row col ->
1037 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1038 region = toRegion row col
1040 Report.Report "PROBLEM IN EXPOSING" region [] $
1041 Code.toSnippet source surroundings (Just region)
1044 "I want to parse exposed values, but I am getting stuck here:"
1048 ["Exposed","values","are","always","surrounded","by","parentheses."
1049 ,"So","try","adding","a",D.green "(","here?"
1051 , D.toSimpleNote "Here are some valid examples of `exposing` for reference:"
1052 , D.indent 4 $ D.vcat $
1053 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
1054 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
1057 "If you are getting tripped up, you can just expose everything for now. It should\
1058 \ get easier to make an explicit exposing list as you see more examples in the wild."
1062 ExposingValue row col ->
1063 case Code.whatIsNext source row col of
1064 Code.Keyword keyword ->
1066 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1067 region = toKeywordRegion row col keyword
1069 Report.Report "RESERVED WORD" region [] $
1070 Code.toSnippet source surroundings (Just region)
1073 "I got stuck on this reserved word:"
1076 "It looks like you are trying to expose `" ++ keyword ++ "` but that is a reserved word. Is there a typo?"
1081 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1082 region = toKeywordRegion row col op
1084 Report.Report "UNEXPECTED SYMBOL" region [] $
1085 Code.toSnippet source surroundings (Just region)
1088 "I got stuck on this symbol:"
1092 "If you are trying to expose an operator, add parentheses around it like this:"
1093 , D.indent 4 $ D.dullyellow (D.fromChars op) <> " -> " <> D.green ("(" <> D.fromChars op <> ")")
1099 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1100 region = toRegion row col
1102 Report.Report "PROBLEM IN EXPOSING" region [] $
1103 Code.toSnippet source surroundings (Just region)
1106 "I got stuck while parsing these exposed values:"
1110 "I do not have an exact recommendation, so here are some valid examples\
1111 \ of `exposing` for reference:"
1112 , D.indent 4 $ D.vcat $
1113 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
1114 , D.fillSep [D.cyan "import","Basics",D.cyan "exposing","(Int, Float, Bool(..), (+), not, sqrt)"]
1117 "These examples show how to expose types, variants, operators, and functions. Everything\
1118 \ should be some permutation of these examples, just with different names."
1122 ExposingOperator row col ->
1124 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1125 region = toRegion row col
1127 Report.Report "PROBLEM IN EXPOSING" region [] $
1128 Code.toSnippet source surroundings (Just region)
1131 "I just saw an open parenthesis, so I was expecting an operator next:"
1134 ["It","is","possible","to","expose","operators,","so","I","was","expecting"
1135 ,"to","see","something","like",D.dullyellow "(+)","or",D.dullyellow "(|=)"
1136 ,"or",D.dullyellow "(||)","after","I","saw","that","open","parenthesis."
1140 ExposingOperatorReserved op row col ->
1142 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1143 region = toRegion row col
1145 Report.Report "RESERVED SYMBOL" region [] $
1146 Code.toSnippet source surroundings (Just region)
1149 "I cannot expose this as an operator:"
1152 BadDot -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1153 BadPipe -> D.fillSep ["Maybe","you","want",D.dullyellow "(||)","instead?"]
1154 BadArrow -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1155 BadEquals -> D.fillSep ["Maybe","you","want",D.dullyellow "(==)","instead?"]
1156 BadHasType -> D.fillSep ["Maybe","you","want",D.dullyellow "(::)","instead?"]
1159 ExposingOperatorRightParen row col ->
1161 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1162 region = toRegion row col
1164 Report.Report "PROBLEM IN EXPOSING" region [] $
1165 Code.toSnippet source surroundings (Just region)
1168 "It looks like you are exposing an operator, but I got stuck here:"
1171 ["I","was","expecting","to","see","the","closing","parenthesis","immediately"
1172 ,"after","the","operator.","Try","adding","a",D.green ")","right","here?"
1176 ExposingEnd row col ->
1178 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1179 region = toRegion row col
1181 Report.Report "UNFINISHED EXPOSING" region [] $
1182 Code.toSnippet source surroundings (Just region)
1185 "I was partway through parsing exposed values, but I got stuck here:"
1188 "Maybe there is a comma missing before this?"
1191 ExposingTypePrivacy row col ->
1193 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1194 region = toRegion row col
1196 Report.Report "PROBLEM EXPOSING CUSTOM TYPE VARIANTS" region [] $
1197 Code.toSnippet source surroundings (Just region)
1200 "It looks like you are trying to expose the variants of a custom type:"
1204 ["You","need","to","write","something","like"
1205 ,D.dullyellow "Status(..)","or",D.dullyellow "Entity(..)"
1206 ,"though.","It","is","all","or","nothing,","otherwise","`case`"
1207 ,"expressions","could","miss","a","variant","and","crash!"
1210 "It is often best to keep the variants hidden! If someone pattern matches on\
1211 \ the variants, it is a MAJOR change if any new variants are added. Suddenly\
1212 \ their `case` expressions do not cover all variants! So if you do not need\
1213 \ people to pattern match, keep the variants hidden and expose functions to\
1214 \ construct values of this type. This way you can add new variants as a MINOR change!"
1218 ExposingIndentEnd row col ->
1220 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1221 region = toRegion row col
1223 Report.Report "UNFINISHED EXPOSING" region [] $
1224 Code.toSnippet source surroundings (Just region)
1227 "I was partway through parsing exposed values, but I got stuck here:"
1231 ["I","was","expecting","a","closing","parenthesis."
1232 ,"Try","adding","a",D.green ")","right","here?"
1235 "I can get confused when there is not enough indentation, so if you already\
1236 \ have a closing parenthesis, it probably just needs some spaces in front of it."
1240 ExposingIndentValue row col ->
1242 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1243 region = toRegion row col
1245 Report.Report "UNFINISHED EXPOSING" region [] $
1246 Code.toSnippet source surroundings (Just region)
1249 "I was partway through parsing exposed values, but I got stuck here:"
1252 "I was expecting another value to expose."
1260toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
1261toSpaceReport source space row col =
1265 region = toRegion row col
1267 Report.Report "NO TABS" region [] $
1268 Code.toSnippet source region Nothing
1271 "I ran into a tab, but tabs are not allowed in Elm files."
1274 "Replace the tab with spaces."
1277 EndlessMultiComment ->
1279 region = toWiderRegion row col 2
1281 Report.Report "ENDLESS COMMENT" region [] $
1282 Code.toSnippet source region Nothing
1285 "I cannot find the end of this multi-line comment:"
1288 [ D.reflow "Add a -} somewhere after this to end the comment."
1290 "Multi-line comments can be nested in Elm, so {- {- -} -} is a comment\
1291 \ that happens to contain another comment. Like parentheses and curly braces,\
1292 \ the start and end markers must always be balanced. Maybe that is the problem?"
1301toRegion :: Row -> Col -> A.Region
1304 pos = A.Position row col
1309toWiderRegion :: Row -> Col -> Word16 -> A.Region
1310toWiderRegion row col extra =
1312 (A.Position row col)
1313 (A.Position row (col + extra))
1316toKeywordRegion :: Row -> Col -> [Char.Char] -> A.Region
1317toKeywordRegion row col keyword =
1319 (A.Position row col)
1320 (A.Position row (col + fromIntegral (length keyword)))
1323toDeclarationsReport :: Code.Source -> Decl -> Report.Report
1324toDeclarationsReport source decl =
1326 DeclStart row col ->
1327 case Code.whatIsNext source row col of
1328 Code.Close term bracket ->
1330 region = toRegion row col
1332 Report.Report ("STRAY " ++ map Char.toUpper term) region [] $
1333 Code.toSnippet source region Nothing
1336 "I was not expecting to see a " ++ term ++ " here:"
1338 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
1341 Code.Keyword keyword ->
1343 region = toKeywordRegion row col keyword
1345 Report.Report "RESERVED WORD" region [] $
1346 Code.toSnippet source region Nothing
1349 "I was not expecting to run into the `" ++ keyword ++ "` keyword here:"
1354 "It is reserved for declaring imports at the top of your module. If you want\
1355 \ another import, try moving it up top with the other imports. If you want to\
1356 \ define a value or function, try changing the name to something else!"
1361 "It is reserved for writing `case` expressions. Try using a different name?"
1363 "If you are trying to write a `case` expression, it needs to be part of a\
1364 \ definition. So you could write something like this instead:"
1366 [ D.indent 0 $ D.fillSep [D.green "getWidth","maybeWidth","="]
1367 , D.indent 2 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
1368 , D.indent 4 $ D.fillSep [D.blue "Just","width","->"]
1369 , D.indent 6 $ D.fillSep ["width","+",D.dullyellow "200"]
1371 , D.indent 4 $ D.fillSep [D.blue "Nothing","->"]
1372 , D.indent 6 $ D.fillSep [D.dullyellow "400"]
1375 "This defines a `getWidth` function that we can use elsewhere in our program."
1381 "It is reserved for writing `if` expressions. Try using a different name?"
1383 "If you are trying to write an `if` expression, it needs to be part of a\
1384 \ definition. So you could write something like this instead:"
1386 [ D.indent 0 $ D.fillSep [D.green "reviewPowerLevel","powerLevel","="]
1387 , D.indent 2 $ D.fillSep $
1388 [D.cyan "if","powerLevel",">",D.dullyellow "9000"
1389 ,D.cyan "then",D.dullyellow "\"OVER 9000!!!\""
1390 ,D.cyan "else",D.dullyellow "\"not bad\""
1394 "This defines a `reviewPowerLevel` function that we can use elsewhere in our program."
1399 "It is a reserved word. Try changing the name to something else?"
1404 region = toRegion row col
1406 Report.Report "SYNTAX PROBLEM" region [] $
1407 Code.toSnippet source region Nothing
1410 "I was expecting to see a new declaration here:"
1414 "I am not sure what is going wrong exactly, but here are a couple valid\
1415 \ declarations for reference:"
1417 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1418 , D.fillSep [D.green "add","x","y","="]
1419 , D.fillSep [" ","x","+","y"]
1421 , D.fillSep [D.cyan "type",D.green "User","=","Anonymous","|","LoggedIn String"]
1423 "Note that all declarations start with lower-case letters in Elm. Capitalization\
1424 \ makes a difference, so be conscious of that if you are working from examples!"
1428 DeclSpace space row col ->
1429 toSpaceReport source space row col
1431 Port port_ row col ->
1432 toPortReport source port_ row col
1434 DeclType declType row col ->
1435 toDeclTypeReport source declType row col
1437 DeclDef name declDef row col ->
1438 toDeclDefReport source name declDef row col
1440 DeclFreshLineAfterDocComment row col ->
1442 region = toRegion row col
1444 Report.Report "EXPECTING DECLARATION" region [] $
1445 Code.toSnippet source region Nothing
1448 "I just saw a doc comment, but then I got stuck here:"
1451 "I was expecting to see the corresponding declaration next, starting on a fresh\
1452 \ line with no indentation."
1460toPortReport :: Code.Source -> Port -> Row -> Col -> Report.Report
1461toPortReport source port_ startRow startCol =
1463 PortSpace space row col ->
1464 toSpaceReport source space row col
1467 case Code.whatIsNext source row col of
1468 Code.Keyword keyword ->
1470 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1471 region = toKeywordRegion row col keyword
1473 Report.Report "RESERVED WORD" region [] $
1474 Code.toSnippet source surroundings (Just region)
1477 "I cannot handle ports with names like this:"
1480 "You are trying to make a port named `" ++ keyword
1481 ++ "` but that is a reserved word. Try using some other name?"
1486 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1487 region = toRegion row col
1489 Report.Report "PORT PROBLEM" region [] $
1490 Code.toSnippet source surroundings (Just region)
1493 "I just saw the start of a `port` declaration, but then I got stuck here:"
1497 ["I","was","expecting","to","see","a","name","like"
1498 ,D.dullyellow "send","or",D.dullyellow "receive","next."
1499 ,"Something","that","starts","with","a","lower-case","letter."
1505 PortColon row col ->
1507 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1508 region = toRegion row col
1510 Report.Report "PORT PROBLEM" region [] $
1511 Code.toSnippet source surroundings (Just region)
1514 "I just saw the start of a `port` declaration, but then I got stuck here:"
1518 "I was expecting to see a colon next. And then a type that tells me\
1519 \ what type of values are going to flow through."
1524 PortType tipe row col ->
1525 toTypeReport source TC_Port tipe row col
1527 PortIndentName row col ->
1529 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1530 region = toRegion row col
1532 Report.Report "UNFINISHED PORT" region [] $
1533 Code.toSnippet source surroundings (Just region)
1536 "I just saw the start of a `port` declaration, but then I got stuck here:"
1540 ["I","was","expecting","to","see","a","name","like"
1541 ,D.dullyellow "send","or",D.dullyellow "receive","next."
1542 ,"Something","that","starts","with","a","lower-case","letter."
1548 PortIndentColon row col ->
1550 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1551 region = toRegion row col
1553 Report.Report "UNFINISHED PORT" region [] $
1554 Code.toSnippet source surroundings (Just region)
1557 "I just saw the start of a `port` declaration, but then I got stuck here:"
1561 "I was expecting to see a colon next. And then a type that tells me\
1562 \ what type of values are going to flow through."
1567 PortIndentType row col ->
1569 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1570 region = toRegion row col
1572 Report.Report "UNFINISHED PORT" region [] $
1573 Code.toSnippet source surroundings (Just region)
1576 "I just saw the start of a `port` declaration, but then I got stuck here:"
1580 "I was expecting to see a type next. Here are examples of outgoing and\
1581 \ incoming ports for reference:"
1583 [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1584 , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1587 "The first line defines a `send` port so you can send strings out to JavaScript.\
1588 \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1589 \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1590 \ get receive messages when new WebSocket messages come in or when an entry in\
1591 \ IndexedDB changes for some external reason."
1600 "Here are some example `port` declarations for reference:"
1602 [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1603 , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1606 "The first line defines a `send` port so you can send strings out to JavaScript.\
1607 \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1608 \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1609 \ get receive messages when new WebSocket messages come in or when the IndexedDB\
1610 \ is changed for some external reason."
1618toDeclTypeReport :: Code.Source -> DeclType -> Row -> Col -> Report.Report
1619toDeclTypeReport source declType startRow startCol =
1621 DT_Space space row col ->
1622 toSpaceReport source space row col
1626 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1627 region = toRegion row col
1629 Report.Report "EXPECTING TYPE NAME" region [] $
1630 Code.toSnippet source surroundings (Just region)
1633 "I think I am parsing a type declaration, but I got stuck here:"
1637 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1638 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1644 DT_Alias typeAlias row col ->
1645 toTypeAliasReport source typeAlias row col
1647 DT_Union customType row col ->
1648 toCustomTypeReport source customType row col
1650 DT_IndentName row col ->
1652 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1653 region = toRegion row col
1655 Report.Report "EXPECTING TYPE NAME" region [] $
1656 Code.toSnippet source surroundings (Just region)
1659 "I think I am parsing a type declaration, but I got stuck here:"
1663 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1664 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1671toTypeAliasReport :: Code.Source -> TypeAlias -> Row -> Col -> Report.Report
1672toTypeAliasReport source typeAlias startRow startCol =
1674 AliasSpace space row col ->
1675 toSpaceReport source space row col
1677 AliasName row col ->
1679 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1680 region = toRegion row col
1682 Report.Report "EXPECTING TYPE ALIAS NAME" region [] $
1683 Code.toSnippet source surroundings (Just region)
1686 "I am partway through parsing a type alias, but I got stuck here:"
1690 ["I","was","expecting","a","name","like",D.dullyellow "Person","or",D.dullyellow "Point"
1691 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1697 AliasEquals row col ->
1698 case Code.whatIsNext source row col of
1699 Code.Keyword keyword ->
1701 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1702 region = toKeywordRegion row col keyword
1704 Report.Report "RESERVED WORD" region [] $
1705 Code.toSnippet source surroundings (Just region)
1708 "I ran into a reserved word unexpectedly while parsing this type alias:"
1712 "It looks like you are trying use `" ++ keyword
1713 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1720 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1721 region = toRegion row col
1723 Report.Report "PROBLEM IN TYPE ALIAS" region [] $
1724 Code.toSnippet source surroundings (Just region)
1727 "I am partway through parsing a type alias, but I got stuck here:"
1731 "I was expecting to see a type variable or an equals sign next."
1736 AliasBody tipe row col ->
1737 toTypeReport source TC_TypeAlias tipe row col
1739 AliasIndentEquals row col ->
1741 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1742 region = toRegion row col
1744 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1745 Code.toSnippet source surroundings (Just region)
1748 "I am partway through parsing a type alias, but I got stuck here:"
1752 "I was expecting to see a type variable or an equals sign next."
1757 AliasIndentBody row col ->
1759 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1760 region = toRegion row col
1762 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1763 Code.toSnippet source surroundings (Just region)
1766 "I am partway through parsing a type alias, but I got stuck here:"
1770 ["I","was","expecting","to","see","a","type","next.","Something","as","simple"
1771 ,"as",D.dullyellow "Int","or",D.dullyellow "Float","would","work!"
1778typeAliasNote :: D.Doc
1782 "Here is an example of a valid `type alias` for reference:"
1784 [ D.indent 0 $ D.fillSep [D.cyan "type",D.cyan "alias",D.green "Person","="]
1785 , D.indent 2 $ D.vcat $
1793 "This would let us use `Person` as a shorthand for that record type. Using this\
1794 \ shorthand makes type annotations much easier to read, and makes changing code\
1795 \ easier if you decide later that there is more to a person than age and height!"
1799toCustomTypeReport :: Code.Source -> CustomType -> Row -> Col -> Report.Report
1800toCustomTypeReport source customType startRow startCol =
1802 CT_Space space row col ->
1803 toSpaceReport source space row col
1807 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1808 region = toRegion row col
1810 Report.Report "EXPECTING TYPE NAME" region [] $
1811 Code.toSnippet source surroundings (Just region)
1814 "I think I am parsing a type declaration, but I got stuck here:"
1818 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1819 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1825 CT_Equals row col ->
1826 case Code.whatIsNext source row col of
1827 Code.Keyword keyword ->
1829 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1830 region = toKeywordRegion row col keyword
1832 Report.Report "RESERVED WORD" region [] $
1833 Code.toSnippet source surroundings (Just region)
1836 "I ran into a reserved word unexpectedly while parsing this custom type:"
1840 "It looks like you are trying use `" ++ keyword
1841 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1848 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1849 region = toRegion row col
1851 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1852 Code.toSnippet source surroundings (Just region)
1855 "I am partway through parsing a custom type, but I got stuck here:"
1859 "I was expecting to see a type variable or an equals sign next."
1866 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1867 region = toRegion row col
1869 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1870 Code.toSnippet source surroundings (Just region)
1873 "I am partway through parsing a custom type, but I got stuck here:"
1877 "I was expecting to see a vertical bar like | next."
1882 CT_Variant row col ->
1884 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1885 region = toRegion row col
1887 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1888 Code.toSnippet source surroundings (Just region)
1891 "I am partway through parsing a custom type, but I got stuck here:"
1895 ["I","was","expecting","to","see","a","variant","name","next."
1896 ,"Something","like",D.dullyellow "Success","or",D.dullyellow "Sandwich" <> "."
1897 ,"Any","name","that","starts","with","a","capital","letter","really!"
1903 CT_VariantArg tipe row col ->
1904 toTypeReport source TC_CustomType tipe row col
1906 CT_IndentEquals row col ->
1908 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1909 region = toRegion row col
1911 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1912 Code.toSnippet source surroundings (Just region)
1915 "I am partway through parsing a custom type, but I got stuck here:"
1919 "I was expecting to see a type variable or an equals sign next."
1924 CT_IndentBar row col ->
1926 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1927 region = toRegion row col
1929 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1930 Code.toSnippet source surroundings (Just region)
1933 "I am partway through parsing a custom type, but I got stuck here:"
1937 "I was expecting to see a vertical bar like | next."
1942 CT_IndentAfterBar row col ->
1944 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1945 region = toRegion row col
1947 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1948 Code.toSnippet source surroundings (Just region)
1951 "I am partway through parsing a custom type, but I got stuck here:"
1955 "I just saw a vertical bar, so I was expecting to see another variant defined next."
1960 CT_IndentAfterEquals row col ->
1962 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1963 region = toRegion row col
1965 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1966 Code.toSnippet source surroundings (Just region)
1969 "I am partway through parsing a custom type, but I got stuck here:"
1973 "I just saw an equals sign, so I was expecting to see the first variant defined next."
1979customTypeNote :: D.Doc
1983 "Here is an example of a valid `type` declaration for reference:"
1985 [ D.indent 0 $ D.fillSep [D.cyan "type",D.green "Status"]
1986 , D.indent 2 $ D.fillSep ["=","Failure"]
1987 , D.indent 2 $ D.fillSep ["|","Waiting"]
1988 , D.indent 2 $ D.fillSep ["|","Success","String"]
1991 "This defines a new `Status` type with three variants. This could be useful if\
1992 \ we are waiting for an HTTP request. Maybe we start with `Waiting` and then\
1993 \ switch to `Failure` or `Success \"message from server\"` depending on how\
1994 \ things go. Notice that the Success variant has some associated data, allowing\
1995 \ us to store a String if the request goes well!"
2003toDeclDefReport :: Code.Source -> Name.Name -> DeclDef -> Row -> Col -> Report.Report
2004toDeclDefReport source name declDef startRow startCol =
2006 DeclDefSpace space row col ->
2007 toSpaceReport source space row col
2009 DeclDefEquals row col ->
2010 case Code.whatIsNext source row col of
2011 Code.Keyword keyword ->
2013 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2014 region = toKeywordRegion row col keyword
2016 Report.Report "RESERVED WORD" region [] $
2017 Code.toSnippet source surroundings (Just region)
2020 "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
2023 "This is a reserved word! Try using some other name?"
2026 Code.Operator "->" ->
2028 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2029 region = toWiderRegion row col 2
2031 Report.Report "MISSING COLON?" region [] $
2032 Code.toSnippet source surroundings (Just region)
2035 "I was not expecting to see an arrow here:"
2039 ["This","usually","means","a",D.green ":","is","missing","a","bit","earlier","in"
2040 ,"a","type","annotation.","It","could","be","something","else","though,","so"
2041 ,"here","is","a","valid","definition","for","reference:"
2044 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2045 , D.fillSep [D.green "add","x","y","="]
2046 , D.fillSep [" ","x","+","y"]
2049 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
2055 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2056 region = toKeywordRegion row col op
2058 Report.Report "UNEXPECTED SYMBOL" region [] $
2059 Code.toSnippet source surroundings (Just region)
2062 "I was not expecting to see this symbol here:"
2066 "I am not sure what is going wrong exactly, so here is a valid\
2067 \ definition (with an optional type annotation) for reference:"
2069 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2070 , D.fillSep [D.green "add","x","y","="]
2071 , D.fillSep [" ","x","+","y"]
2074 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
2080 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2081 region = toRegion row col
2083 Report.Report "PROBLEM IN DEFINITION" region [] $
2084 Code.toSnippet source surroundings (Just region)
2087 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
2091 "I am not sure what is going wrong exactly, so here is a valid\
2092 \ definition (with an optional type annotation) for reference:"
2094 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2095 , D.fillSep [D.green "add","x","y","="]
2096 , D.fillSep [" ","x","+","y"]
2099 "Try to use that format!"
2103 DeclDefType tipe row col ->
2104 toTypeReport source (TC_Annotation name) tipe row col
2106 DeclDefArg pattern row col ->
2107 toPatternReport source PArg pattern row col
2109 DeclDefBody expr row col ->
2110 toExprReport source (InDef name startRow startCol) expr row col
2112 DeclDefNameRepeat row col ->
2114 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2115 region = toRegion row col
2117 Report.Report "EXPECTING DEFINITION" region [] $
2118 Code.toSnippet source surroundings (Just region)
2121 "I just saw the type annotation for `" ++ Name.toChars name
2122 ++ "` so I was expecting to see its definition here:"
2126 "Type annotations always appear directly above the relevant\
2127 \ definition, without anything else in between. (Not even doc comments!)"
2132 DeclDefNameMatch defName row col ->
2134 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2135 region = toRegion row col
2137 Report.Report "NAME MISMATCH" region [] $
2138 Code.toSnippet source surroundings (Just region)
2141 "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
2145 "These names do not match! Is there a typo?"
2146 , D.indent 4 $ D.fillSep $
2147 [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
2151 DeclDefIndentType row col ->
2153 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2154 region = toRegion row col
2156 Report.Report "UNFINISHED DEFINITION" region [] $
2157 Code.toSnippet source surroundings (Just region)
2160 "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
2164 "I just saw a colon, so I am expecting to see a type next."
2169 DeclDefIndentEquals row col ->
2171 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2172 region = toRegion row col
2174 Report.Report "UNFINISHED DEFINITION" region [] $
2175 Code.toSnippet source surroundings (Just region)
2178 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
2182 "I was expecting to see an argument or an equals sign next."
2187 DeclDefIndentBody row col ->
2189 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2190 region = toRegion row col
2192 Report.Report "UNFINISHED DEFINITION" region [] $
2193 Code.toSnippet source surroundings (Just region)
2196 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
2200 "I was expecting to see an expression next. What is it equal to?"
2210 "Here is a valid definition (with a type annotation) for reference:"
2212 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2213 , D.fillSep [D.green "add","x","y","="]
2214 , D.fillSep [" ","x","+","y"]
2217 "The top line (called a \"type annotation\") is optional. You can leave it off\
2218 \ if you want. As you get more comfortable with Elm and as your project grows,\
2219 \ it becomes more and more valuable to add them though! They work great as\
2220 \ compiler-verified documentation, and they often improve error messages!"
2229 = InNode Node Row Col Context
2230 | InDef Name.Name Row Col
2231 | InDestruct Row Col
2247getDefName :: Context -> Maybe Name.Name
2250 InDestruct _ _ -> Nothing
2251 InDef name _ _ -> Just name
2252 InNode _ _ _ c -> getDefName c
2255isWithin :: Node -> Context -> Bool
2256isWithin desiredNode context =
2258 InDestruct _ _ -> False
2259 InDef _ _ _ -> False
2260 InNode actualNode _ _ _ -> desiredNode == actualNode
2267toExprReport :: Code.Source -> Context -> Expr -> Row -> Col -> Report.Report
2268toExprReport source context expr startRow startCol =
2271 toLetReport source context let_ row col
2273 Case case_ row col ->
2274 toCaseReport source context case_ row col
2277 toIfReport source context if_ row col
2279 List list row col ->
2280 toListReport source context list row col
2282 Record record row col ->
2283 toRecordReport source context record row col
2285 Tuple tuple row col ->
2286 toTupleReport source context tuple row col
2288 Func func row col ->
2289 toFuncReport source context func row col
2292 let region = toRegion row col in
2293 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
2294 Code.toSnippet source region Nothing
2297 "I was expecting to see a record accessor here:"
2300 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
2301 ,"that","accesses","a","value","from","a","record."
2306 let region = toRegion row col in
2307 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
2308 Code.toSnippet source region Nothing
2311 "I am trying to parse a record accessor here:"
2316 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
2317 ,"that","accesses","a","value","from","a","record."
2321 "Record field names must start with a lower case letter!"
2325 OperatorRight op row col ->
2326 let region = toRegion row col in
2327 Report.Report "MISSING EXPRESSION" region [] $
2328 Code.toSnippet source region Nothing
2331 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
2335 ["You","can","just","put","anything","for","now,","like"
2336 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2337 ,"Once","there","is","something","there,","I","can","probably"
2338 ,"give","a","more","specific","hint!"
2341 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
2342 \ Or if I run into operators in unexpected spots. Point is, there are a\
2343 \ couple ways I can get confused and give sort of weird advice!"
2347 OperatorReserved operator row col ->
2348 toOperatorReport source context operator row col
2352 (contextRow, contextCol, aThing) =
2354 InDestruct r c -> (r, c, "a definition")
2355 InDef name r c -> (r, c, "the `" ++ Name.toChars name ++ "` definition")
2356 InNode NRecord r c _ -> (r, c, "a record")
2357 InNode NParens r c _ -> (r, c, "some parentheses")
2358 InNode NList r c _ -> (r, c, "a list")
2359 InNode NFunc r c _ -> (r, c, "an anonymous function")
2360 InNode NCond r c _ -> (r, c, "an `if` expression")
2361 InNode NThen r c _ -> (r, c, "an `if` expression")
2362 InNode NElse r c _ -> (r, c, "an `if` expression")
2363 InNode NCase r c _ -> (r, c, "a `case` expression")
2364 InNode NBranch r c _ -> (r, c, "a `case` expression")
2366 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
2367 region = toRegion row col
2369 Report.Report "MISSING EXPRESSION" region [] $
2370 Code.toSnippet source surroundings (Just region)
2373 "I am partway through parsing " ++ aThing ++ ", but I got stuck here:"
2377 ["I","was","expecting","to","see","an","expression","like"
2378 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2379 ,"Once","there","is","something","there,","I","can","probably"
2380 ,"give","a","more","specific","hint!"
2383 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
2384 \ Or if I run into operators in unexpected spots. Point is, there are a\
2385 \ couple ways I can get confused and give sort of weird advice!"
2389 Char char row col ->
2390 toCharReport source char row col
2392 String string row col ->
2393 toStringReport source string row col
2395 Number number row col ->
2396 toNumberReport source number row col
2398 Space space row col ->
2399 toSpaceReport source space row col
2401 EndlessShader row col ->
2403 region = toWiderRegion row col 6
2405 Report.Report "ENDLESS SHADER" region [] $
2406 Code.toSnippet source region Nothing
2408 D.reflow "I cannot find the end of this shader:"
2410 D.reflow "Add a |] somewhere after this to end the shader."
2413 ShaderProblem problem row col ->
2415 region = toRegion row col
2417 Report.Report "SHADER PROBLEM" region [] $
2418 Code.toSnippet source region Nothing
2421 "I ran into a problem while parsing this GLSL block."
2425 "I use a 3rd party GLSL parser for now, and I did my best to extract their error message:"
2426 , D.indent 4 $ D.vcat $
2427 map D.fromChars (filter (/="") (lines problem))
2431 IndentOperatorRight op row col ->
2433 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2434 region = toRegion row col
2436 Report.Report "MISSING EXPRESSION" region [] $
2437 Code.toSnippet source surroundings (Just region)
2440 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
2445 ["You","can","just","put","anything","for","now,","like"
2446 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2447 ,"Once","there","is","something","there,","I","can","probably"
2448 ,"give","a","more","specific","hint!"
2452 "I may be getting confused by your indentation? The easiest way to make sure\
2453 \ this is not an indentation problem is to put the expression on the right of\
2454 \ the " ++ Name.toChars op ++ " operator on the same line."
2463toCharReport :: Code.Source -> Char -> Row -> Col -> Report.Report
2464toCharReport source char row col =
2468 region = toRegion row col
2470 Report.Report "MISSING SINGLE QUOTE" region [] $
2471 Code.toSnippet source region Nothing
2474 "I thought I was parsing a character, but I got to the end of\
2475 \ the line without seeing the closing single quote:"
2478 "Add a closing single quote here!"
2481 CharEscape escape ->
2482 toEscapeReport source escape row col
2484 CharNotString width ->
2486 region = toWiderRegion row col width
2488 Report.Report "NEEDS DOUBLE QUOTES" region [] $
2489 Code.toSnippet source region Nothing
2491 "The following string uses single quotes:"
2494 [ "Please switch to double quotes instead:"
2496 D.dullyellow "'this'" <> " => " <> D.green "\"this\""
2498 "Elm uses double quotes for strings like \"hello\", whereas it uses single\
2499 \ quotes for individual characters like 'a' and 'ø'. This distinction helps with\
2500 \ code like (String.any (\\c -> c == 'X') \"90210\") where you are inspecting\
2501 \ individual characters."
2510toStringReport :: Code.Source -> String -> Row -> Col -> Report.Report
2511toStringReport source string row col =
2513 StringEndless_Single ->
2515 region = toRegion row col
2517 Report.Report "ENDLESS STRING" region [] $
2518 Code.toSnippet source region Nothing
2521 "I got to the end of the line without seeing the closing double quote:"
2525 ["Strings","look","like",D.green "\"this\"","with","double"
2526 ,"quotes","on","each","end.","Is","the","closing","double"
2527 ,"quote","missing","in","your","code?"
2530 "For a string that spans multiple lines, you can use the multi-line string\
2531 \ syntax like this:"
2533 [ D.fillSep [D.green "markdown","=",D.dullyellow "\"\"\""]
2535 , D.dullyellow "# Multi-line Strings"
2537 , D.dullyellow "- start with triple double quotes"
2538 , D.dullyellow "- write whatever you want"
2539 , D.dullyellow "- no need to escape newlines or double quotes"
2540 , D.dullyellow "- end with triple double quotes"
2542 , D.dullyellow "\"\"\""
2547 StringEndless_Multi ->
2549 region = toWiderRegion row col 3
2551 Report.Report "ENDLESS STRING" region [] $
2552 Code.toSnippet source region Nothing
2555 "I cannot find the end of this multi-line string:"
2558 [ D.reflow "Add a \"\"\" somewhere after this to end the string."
2560 "Here is a valid multi-line string for reference:"
2562 [ D.fillSep [D.green "markdown","=",D.dullyellow "\"\"\""]
2564 , D.dullyellow "# Multi-line Strings"
2566 , D.dullyellow "- start with triple double quotes"
2567 , D.dullyellow "- write whatever you want"
2568 , D.dullyellow "- no need to escape newlines or double quotes"
2569 , D.dullyellow "- end with triple double quotes"
2571 , D.dullyellow "\"\"\""
2576 StringEscape escape ->
2577 toEscapeReport source escape row col
2584toEscapeReport :: Code.Source -> Escape -> Row -> Col -> Report.Report
2585toEscapeReport source escape row col =
2589 region = toWiderRegion row col 2
2591 Report.Report "UNKNOWN ESCAPE" region [] $
2592 Code.toSnippet source region Nothing
2595 "Backslashes always start escaped characters, but I do not recognize this one:"
2599 "Valid escape characters include:"
2600 , D.dullyellow $ D.indent 4 $ D.vcat $
2610 "Do you want one of those instead? Maybe you need \\\\ to escape a backslash?"
2612 "The last style lets encode ANY character by its Unicode code\
2613 \ point. That means \\u{0009} and \\t are the same. You can use\
2614 \ that style for anything not covered by the other six escapes!"
2618 BadUnicodeFormat width ->
2620 region = toWiderRegion row col width
2622 Report.Report "BAD UNICODE ESCAPE" region [] $
2623 Code.toSnippet source region Nothing
2626 "I ran into an invalid Unicode escape:"
2630 "Here are some examples of valid Unicode escapes:"
2631 , D.dullyellow $ D.indent 4 $ D.vcat $
2638 "Notice that the code point is always surrounded by curly braces.\
2639 \ Maybe you are missing the opening or closing curly brace?"
2642 BadUnicodeCode width ->
2644 region = toWiderRegion row col width
2646 Report.Report "BAD UNICODE ESCAPE" region [] $
2647 Code.toSnippet source region Nothing
2650 "This is not a valid code point:"
2653 "The valid code points are between 0 and 10FFFF inclusive."
2656 BadUnicodeLength width numDigits badCode ->
2658 region = toWiderRegion row col width
2660 Report.Report "BAD UNICODE ESCAPE" region [] $
2661 Code.toSnippet source region Nothing $
2662 if numDigits < 4 then
2665 "Every code point needs at least four digits:"
2668 goodCode = replicate (4 - numDigits) '0' ++ map Char.toUpper (showHex badCode "")
2669 suggestion = "\\u{" <> D.fromChars goodCode <> "}"
2671 D.fillSep ["Try",D.green suggestion,"instead?"]
2677 "This code point has too many digits:"
2680 ["Valid","code","points","are","between"
2681 ,D.green "\\u{0000}","and",D.green "\\u{10FFFF}" <> ","
2682 ,"so","try","trimming","any","leading","zeros","until"
2683 ,"you","have","between","four","and","six","digits."
2692toNumberReport :: Code.Source -> Number -> Row -> Col -> Report.Report
2693toNumberReport source number row col =
2695 region = toRegion row col
2699 Report.Report "WEIRD NUMBER" region [] $
2700 Code.toSnippet source region Nothing
2703 "I thought I was reading a number, but I ran into some weird stuff here:"
2707 "I recognize numbers in the following formats:"
2708 , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
2710 "So is there a way to write it like one of those?"
2715 Report.Report "WEIRD NUMBER" region [] $
2716 Code.toSnippet source region Nothing
2719 "Numbers cannot end with a dot like this:"
2722 ["Switching","to",D.green (D.fromChars (show int))
2723 ,"or",D.green (D.fromChars (show int ++ ".0"))
2724 ,"will","work","though!"
2729 Report.Report "WEIRD HEXIDECIMAL" region [] $
2730 Code.toSnippet source region Nothing
2733 "I thought I was reading a hexidecimal number until I got here:"
2737 "Valid hexidecimal digits include 0123456789abcdefABCDEF, so I can\
2738 \ only recognize things like this:"
2739 , D.indent 4 $ D.vcat [ "0x2B", "0x002B", "0x00ffb3" ]
2743 NumberNoLeadingZero ->
2744 Report.Report "LEADING ZEROS" region [] $
2745 Code.toSnippet source region Nothing
2748 "I do not accept numbers with leading zeros:"
2752 "Just delete the leading zeros and it should work!"
2754 "Some languages let you to specify octal numbers by adding a leading zero.\
2755 \ So in C, writing 0111 is the same as writing 73. Some people are used to\
2756 \ that, but others probably want it to equal 111. Either path is going to\
2757 \ surprise people from certain backgrounds, so Elm tries to avoid this whole\
2767toOperatorReport :: Code.Source -> Context -> BadOperator -> Row -> Col -> Report.Report
2768toOperatorReport source context operator row col =
2772 region = toRegion row col
2774 Report.Report "UNEXPECTED SYMBOL" region [] $
2775 Code.toSnippet source region Nothing
2777 "I was not expecting this dot:"
2780 "Dots are for record access and decimal points, so\
2781 \ they cannot float around on their own. Maybe\
2782 \ there is some extra whitespace?"
2787 region = toRegion row col
2789 Report.Report "UNEXPECTED SYMBOL" region [] $
2790 Code.toSnippet source region Nothing
2793 "I was not expecting this vertical bar:"
2796 "Vertical bars should only appear in custom type declarations. Maybe you want || instead?"
2801 region = toWiderRegion row col 2
2803 Report.Report "UNEXPECTED ARROW" region [] $
2804 Code.toSnippet source region Nothing $
2805 if isWithin NCase context then
2808 "I am parsing a `case` expression right now, but this arrow is confusing me:"
2811 [ D.reflow "Maybe the `of` keyword is missing on a previous line?"
2816 else if isWithin NBranch context then
2819 "I am parsing a `case` expression right now, but this arrow is confusing me:"
2823 "It makes sense to see arrows around here, so I suspect it is something earlier. Maybe this pattern is indented a bit farther than the previous patterns?"
2824 , noteForCaseIndentError
2831 "I was not expecting an arrow here:"
2834 "Arrows should only appear in `case` expressions and anonymous functions. Maybe\
2835 \ you want > or >= instead?"
2840 region = toRegion row col
2842 Report.Report "UNEXPECTED EQUALS" region [] $
2843 Code.toSnippet source region Nothing
2846 "I was not expecting to see this equals sign:"
2850 D.reflow "Maybe you want == instead? To check if two values are equal?"
2853 if isWithin NRecord context then
2854 "Records look like { x = 3, y = 4 } with the equals sign right\
2855 \ after the field name. So maybe you forgot a comma?"
2857 case getDefName context of
2859 "I may be getting confused by your indentation. I need all definitions to be indented\
2860 \ exactly the same amount, so if this is meant to be a new definition, it may have too\
2861 \ many spaces in front of it."
2864 "I may be getting confused by your indentation. I think I am still parsing the `"
2865 ++ Name.toChars name ++ "` definition. Is this supposed to be part of a definition\
2866 \ after that? If so, the problem may be a bit before the equals sign. I need all\
2867 \ definitions to be indented exactly the same amount, so the problem may be that\
2868 \ this new definition has too many spaces in front of it."
2874 region = toRegion row col
2876 Report.Report "UNEXPECTED SYMBOL" region [] $
2877 Code.toSnippet source region Nothing $
2880 "I was not expecting to run into the \"has type\" symbol here:"
2882 case getDefName context of
2885 ["Maybe","you","want",D.green "::","instead?"
2886 ,"To","put","something","on","the","front","of","a","list?"
2893 ["Maybe","you","want",D.green "::","instead?"
2894 ,"To","put","something","on","the","front","of","a","list?"
2897 "The single colon is reserved for type annotations and record types, but I think\
2898 \ I am parsing the definition of `" ++ Name.toChars name ++ "` right now."
2901 "I may be getting confused by your indentation. Is this supposed to be part of\
2902 \ a type annotation AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2903 \ the problem may be a bit before the \"has type\" symbol. I need all definitions to\
2904 \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2905 \ this new definition is indented a bit too much."
2914toLetReport :: Code.Source -> Context -> Let -> Row -> Col -> Report.Report
2915toLetReport source context let_ startRow startCol =
2917 LetSpace space row col ->
2918 toSpaceReport source space row col
2922 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2923 region = toRegion row col
2925 Report.Report "LET PROBLEM" region [] $
2926 Code.toSnippet source surroundings (Just region)
2929 "I was partway through parsing a `let` expression, but I got stuck here:"
2933 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2934 ,"keyword","next.","Is","there","a","typo?"
2937 "This can also happen if you are trying to define another value within the `let` but\
2938 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2939 \ spaces before it. They should line up exactly!"
2943 LetDefAlignment _ row col ->
2945 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2946 region = toRegion row col
2948 Report.Report "LET PROBLEM" region [] $
2949 Code.toSnippet source surroundings (Just region)
2952 "I was partway through parsing a `let` expression, but I got stuck here:"
2956 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2957 ,"keyword","next.","Is","there","a","typo?"
2960 "This can also happen if you are trying to define another value within the `let` but\
2961 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2962 \ spaces before it. They should line up exactly!"
2966 LetDefName row col ->
2967 case Code.whatIsNext source row col of
2968 Code.Keyword keyword ->
2970 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2971 region = toKeywordRegion row col keyword
2973 Report.Report "RESERVED WORD" region [] $
2974 Code.toSnippet source surroundings (Just region)
2977 "I was partway through parsing a `let` expression, but I got stuck here:"
2980 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but\
2981 \ it is a reserved word! Try using a different name instead."
2985 toUnfinishLetReport source row col startRow startCol $
2987 "I was expecting the name of a definition next."
2989 LetDef name def row col ->
2990 toLetDefReport source name def row col
2992 LetDestruct destruct row col ->
2993 toLetDestructReport source destruct row col
2995 LetBody expr row col ->
2996 toExprReport source context expr row col
2998 LetIndentDef row col ->
2999 toUnfinishLetReport source row col startRow startCol $
3001 "I was expecting a value to be defined here."
3003 LetIndentIn row col ->
3004 toUnfinishLetReport source row col startRow startCol $
3006 ["I","was","expecting","to","see","the",D.cyan "in","keyword","next."
3007 ,"Or","maybe","more","of","that","expression?"
3010 LetIndentBody row col ->
3011 toUnfinishLetReport source row col startRow startCol $
3013 "I was expecting an expression next. Tell me what should happen with the value you just defined!"
3016toUnfinishLetReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
3017toUnfinishLetReport source row col startRow startCol message =
3019 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3020 region = toRegion row col
3022 Report.Report "UNFINISHED LET" region [] $
3023 Code.toSnippet source surroundings (Just region)
3026 "I was partway through parsing a `let` expression, but I got stuck here:"
3031 "Here is an example with a valid `let` expression for reference:"
3033 [ D.indent 0 $ D.fillSep [D.green "viewPerson","person","="]
3034 , D.indent 2 $ D.fillSep [D.cyan "let"]
3035 , D.indent 4 $ D.fillSep ["fullName","="]
3036 , D.indent 6 $ D.fillSep ["person.firstName","++",D.dullyellow "\" \"","++","person.lastName"]
3037 , D.indent 2 $ D.fillSep [D.cyan "in"]
3038 , D.indent 2 $ D.fillSep ["div","[]","[","text","fullName","]"]
3041 "Here we defined a `viewPerson` function that turns a person into some HTML. We use\
3042 \ a `let` expression to define the `fullName` we want to show. Notice the indentation! The\
3043 \ `fullName` is indented more than the `let` keyword, and the actual value of `fullName` is\
3044 \ indented a bit more than that. That is important!"
3049toLetDefReport :: Code.Source -> Name.Name -> Def -> Row -> Col -> Report.Report
3050toLetDefReport source name def startRow startCol =
3052 DefSpace space row col ->
3053 toSpaceReport source space row col
3055 DefType tipe row col ->
3056 toTypeReport source (TC_Annotation name) tipe row col
3058 DefNameRepeat row col ->
3060 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3061 region = toRegion row col
3063 Report.Report "EXPECTING DEFINITION" region [] $
3064 Code.toSnippet source surroundings (Just region)
3067 "I just saw the type annotation for `" ++ Name.toChars name
3068 ++ "` so I was expecting to see its definition here:"
3072 "Type annotations always appear directly above the relevant\
3073 \ definition, without anything else in between."
3078 DefNameMatch defName row col ->
3080 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3081 region = toRegion row col
3083 Report.Report "NAME MISMATCH" region [] $
3084 Code.toSnippet source surroundings (Just region)
3087 "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
3091 "These names do not match! Is there a typo?"
3092 , D.indent 4 $ D.fillSep $
3093 [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
3097 DefArg pattern row col ->
3098 toPatternReport source PArg pattern row col
3100 DefEquals row col ->
3101 case Code.whatIsNext source row col of
3102 Code.Keyword keyword ->
3104 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3105 region = toKeywordRegion row col keyword
3107 Report.Report "RESERVED WORD" region [] $
3108 Code.toSnippet source surroundings (Just region)
3111 "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
3114 "This is a reserved word! Try using some other name?"
3117 Code.Operator "->" ->
3119 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3120 region = toWiderRegion row col 2
3122 Report.Report "MISSING COLON?" region [] $
3123 Code.toSnippet source surroundings (Just region)
3126 "I was not expecting to see an arrow here:"
3130 ["This","usually","means","a",D.green ":","is","missing","a","bit","earlier","in"
3131 ,"a","type","annotation.","It","could","be","something","else","though,","so"
3132 ,"here","is","a","valid","definition","for","reference:"
3135 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3136 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3137 , D.indent 6 $ D.fillSep ["x","+","y"]
3140 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
3146 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3147 region = toKeywordRegion row col op
3149 Report.Report "UNEXPECTED SYMBOL" region [] $
3150 Code.toSnippet source surroundings (Just region)
3153 "I was not expecting to see this symbol here:"
3157 "I am not sure what is going wrong exactly, so here is a valid\
3158 \ definition (with an optional type annotation) for reference:"
3160 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3161 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3162 , D.indent 6 $ D.fillSep ["x","+","y"]
3165 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
3171 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3172 region = toRegion row col
3174 Report.Report "PROBLEM IN DEFINITION" region [] $
3175 Code.toSnippet source surroundings (Just region)
3178 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3182 "I am not sure what is going wrong exactly, so here is a valid\
3183 \ definition (with an optional type annotation) for reference:"
3185 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3186 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3187 , D.indent 4 $ D.fillSep ["x","+","y"]
3190 "Try to use that format!"
3194 DefBody expr row col ->
3195 toExprReport source (InDef name startRow startCol) expr row col
3197 DefIndentEquals row col ->
3199 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3200 region = toRegion row col
3202 Report.Report "UNFINISHED DEFINITION" region [] $
3203 Code.toSnippet source surroundings (Just region)
3206 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3210 "I was expecting to see an argument or an equals sign next."
3215 DefIndentType row col ->
3217 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3218 region = toRegion row col
3220 Report.Report "UNFINISHED DEFINITION" region [] $
3221 Code.toSnippet source surroundings (Just region)
3224 "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
3228 "I just saw a colon, so I am expecting to see a type next."
3233 DefIndentBody row col ->
3235 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3236 region = toRegion row col
3238 Report.Report "UNFINISHED DEFINITION" region [] $
3239 Code.toSnippet source surroundings (Just region)
3242 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3246 "I was expecting to see an expression next. What is it equal to?"
3251 DefAlignment indent row col ->
3253 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3254 region = toRegion row col
3255 offset = indent - col
3257 Report.Report "PROBLEM IN DEFINITION" region [] $
3258 Code.toSnippet source surroundings (Just region)
3261 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3264 "I just saw a type annotation indented " ++ show indent ++ " spaces, so I was\
3265 \ expecting to see the corresponding definition next with the exact same amount\
3266 \ of indentation. It looks like this line needs "
3267 ++ show offset ++ " more " ++ (if offset == 1 then "space" else "spaces") ++ "?"
3276 "Here is a valid definition (with a type annotation) for reference:"
3278 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3279 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3280 , D.indent 6 $ D.fillSep ["x","+","y"]
3283 "The top line (called a \"type annotation\") is optional. You can leave it off\
3284 \ if you want. As you get more comfortable with Elm and as your project grows,\
3285 \ it becomes more and more valuable to add them though! They work great as\
3286 \ compiler-verified documentation, and they often improve error messages!"
3290toLetDestructReport :: Code.Source -> Destruct -> Row -> Col -> Report.Report
3291toLetDestructReport source destruct startRow startCol =
3293 DestructSpace space row col ->
3294 toSpaceReport source space row col
3296 DestructPattern pattern row col ->
3297 toPatternReport source PLet pattern row col
3299 DestructEquals row col ->
3301 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3302 region = toRegion row col
3304 Report.Report "PROBLEM IN DEFINITION" region [] $
3305 Code.toSnippet source surroundings (Just region)
3308 "I got stuck trying to parse this definition:"
3310 case Code.whatIsNext source row col of
3311 Code.Operator ":" ->
3314 "I was expecting to see an equals sign next, followed by an expression\
3315 \ telling me what to compute."
3317 "It looks like you may be trying to write a type annotation? It is not\
3318 \ possible to add type annotations on destructuring definitions like this.\
3319 \ You can assign a name to the overall structure, put a type annotation on\
3320 \ that, and then destructure separately though."
3325 "I was expecting to see an equals sign next, followed by an expression\
3326 \ telling me what to compute."
3329 DestructBody expr row col ->
3330 toExprReport source (InDestruct startRow startCol) expr row col
3332 DestructIndentEquals row col ->
3334 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3335 region = toRegion row col
3337 Report.Report "UNFINISHED DEFINITION" region [] $
3338 Code.toSnippet source surroundings (Just region)
3341 "I got stuck trying to parse this definition:"
3344 "I was expecting to see an equals sign next, followed by an expression\
3345 \ telling me what to compute."
3348 DestructIndentBody row col ->
3350 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3351 region = toRegion row col
3353 Report.Report "UNFINISHED DEFINITION" region [] $
3354 Code.toSnippet source surroundings (Just region)
3357 "I got stuck while parsing this definition:"
3360 "I was expecting to see an expression next. What is it equal to?"
3368toCaseReport :: Code.Source -> Context -> Case -> Row -> Col -> Report.Report
3369toCaseReport source context case_ startRow startCol =
3371 CaseSpace space row col ->
3372 toSpaceReport source space row col
3375 toUnfinishCaseReport source row col startRow startCol $
3376 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
3378 CasePattern pattern row col ->
3379 toPatternReport source PCase pattern row col
3381 CaseArrow row col ->
3382 case Code.whatIsNext source row col of
3383 Code.Keyword keyword ->
3385 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3386 region = toKeywordRegion row col keyword
3388 Report.Report "RESERVED WORD" region [] $
3389 Code.toSnippet source surroundings (Just region)
3392 "I am partway through parsing a `case` expression, but I got stuck here:"
3395 "It looks like you are trying to use `" ++ keyword ++ "` in one of your\
3396 \ patterns, but it is a reserved word. Try using a different name?"
3399 Code.Operator ":" ->
3401 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3402 region = toRegion row col
3404 Report.Report "UNEXPECTED OPERATOR" region [] $
3405 Code.toSnippet source surroundings (Just region)
3408 "I am partway through parsing a `case` expression, but I got stuck here:"
3411 ["I","am","seeing",D.dullyellow ":","but","maybe","you","want",D.green "::","instead?"
3412 ,"For","pattern","matching","on","lists?"
3416 Code.Operator "=" ->
3418 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3419 region = toRegion row col
3421 Report.Report "UNEXPECTED OPERATOR" region [] $
3422 Code.toSnippet source surroundings (Just region)
3425 "I am partway through parsing a `case` expression, but I got stuck here:"
3428 ["I","am","seeing",D.dullyellow "=","but","maybe","you","want",D.green "->","instead?"
3434 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3435 region = toRegion row col
3437 Report.Report "MISSING ARROW" region [] $
3438 Code.toSnippet source surroundings (Just region)
3441 "I am partway through parsing a `case` expression, but I got stuck here:"
3444 [ D.reflow "I was expecting to see an arrow next."
3445 , noteForCaseIndentError
3449 CaseExpr expr row col ->
3450 toExprReport source (InNode NCase startRow startCol context) expr row col
3452 CaseBranch expr row col ->
3453 toExprReport source (InNode NBranch startRow startCol context) expr row col
3455 CaseIndentOf row col ->
3456 toUnfinishCaseReport source row col startRow startCol $
3457 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
3459 CaseIndentExpr row col ->
3460 toUnfinishCaseReport source row col startRow startCol $
3461 D.reflow "I was expecting to see a expression next."
3463 CaseIndentPattern row col ->
3464 toUnfinishCaseReport source row col startRow startCol $
3465 D.reflow "I was expecting to see a pattern next."
3467 CaseIndentArrow row col ->
3468 toUnfinishCaseReport source row col startRow startCol $
3470 ["I","just","saw","a","pattern,","so","I","was","expecting"
3471 ,"to","see","a",D.dullyellow "->","next."
3474 CaseIndentBranch row col ->
3475 toUnfinishCaseReport source row col startRow startCol $
3477 "I was expecting to see an expression next. What should I do when\
3478 \ I run into this particular pattern?"
3480 CasePatternAlignment indent row col ->
3481 toUnfinishCaseReport source row col startRow startCol $
3483 "I suspect this is a pattern that is not indented far enough? (" ++ show indent ++ " spaces)"
3486toUnfinishCaseReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
3487toUnfinishCaseReport source row col startRow startCol message =
3489 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3490 region = toRegion row col
3492 Report.Report "UNFINISHED CASE" region [] $
3493 Code.toSnippet source surroundings (Just region)
3496 "I was partway through parsing a `case` expression, but I got stuck here:"
3505noteForCaseError :: D.Doc
3509 "Here is an example of a valid `case` expression for reference."
3511 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3512 , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
3513 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3515 , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
3516 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3519 "Notice the indentation. Each pattern is aligned, and each branch is indented\
3520 \ a bit more than the corresponding pattern. That is important!"
3524noteForCaseIndentError :: D.Doc
3525noteForCaseIndentError =
3528 "Sometimes I get confused by indentation, so try to make your `case` look\
3529 \ something like this:"
3531 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3532 , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
3533 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3535 , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
3536 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3539 "Notice the indentation! Patterns are aligned with each other. Same indentation.\
3540 \ The expressions after each arrow are all indented a bit more than the patterns.\
3541 \ That is important!"
3549toIfReport :: Code.Source -> Context -> If -> Row -> Col -> Report.Report
3550toIfReport source context if_ startRow startCol =
3552 IfSpace space row col ->
3553 toSpaceReport source space row col
3557 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3558 region = toRegion row col
3560 Report.Report "UNFINISHED IF" region [] $
3561 Code.toSnippet source surroundings (Just region)
3564 "I was expecting to see more of this `if` expression, but I got stuck here:"
3567 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
3573 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3574 region = toRegion row col
3576 Report.Report "UNFINISHED IF" region [] $
3577 Code.toSnippet source surroundings (Just region)
3580 "I was expecting to see more of this `if` expression, but I got stuck here:"
3583 ["I","was","expecting","to","see","the",D.cyan "else","keyword","next."
3587 IfElseBranchStart row col ->
3589 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3590 region = toRegion row col
3592 Report.Report "UNFINISHED IF" region [] $
3593 Code.toSnippet source surroundings (Just region)
3596 "I just saw the start of an `else` branch, but then I got stuck here:"
3599 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3602 IfCondition expr row col ->
3603 toExprReport source (InNode NCond startRow startCol context) expr row col
3605 IfThenBranch expr row col ->
3606 toExprReport source (InNode NThen startRow startCol context) expr row col
3608 IfElseBranch expr row col ->
3609 toExprReport source (InNode NElse startRow startCol context) expr row col
3611 IfIndentCondition row col ->
3613 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3614 region = toRegion row col
3616 Report.Report "UNFINISHED IF" region [] $
3617 Code.toSnippet source surroundings (Just region)
3620 "I was expecting to see more of this `if` expression, but I got stuck here:"
3624 ["I","was","expecting","to","see","an","expression","like",D.dullyellow "x < 0"
3625 ,"that","evaluates","to","True","or","False."
3628 "I can be confused by indentation. Maybe something is not indented enough?"
3632 IfIndentThen row col ->
3634 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3635 region = toRegion row col
3637 Report.Report "UNFINISHED IF" region [] $
3638 Code.toSnippet source surroundings (Just region)
3641 "I was expecting to see more of this `if` expression, but I got stuck here:"
3645 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
3648 "I can be confused by indentation. Maybe something is not indented enough?"
3652 IfIndentThenBranch row col ->
3654 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3655 region = toRegion row col
3657 Report.Report "UNFINISHED IF" region [] $
3658 Code.toSnippet source surroundings (Just region)
3661 "I got stuck after the start of this `then` branch:"
3665 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3667 "I can be confused by indentation, so if the `then` branch is already\
3668 \ present, it may not be indented enough for me to recognize it."
3672 IfIndentElseBranch row col ->
3674 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3675 region = toRegion row col
3677 Report.Report "UNFINISHED IF" region [] $
3678 Code.toSnippet source surroundings (Just region)
3681 "I got stuck after the start of this `else` branch:"
3685 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3687 "I can be confused by indentation, so if the `else` branch is already\
3688 \ present, it may not be indented enough for me to recognize it."
3692 IfIndentElse row col ->
3694 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3695 region = toRegion row col
3697 Report.Report "UNFINISHED IF" region [] $
3698 Code.toSnippet source surroundings (Just region)
3701 "I was expecting to see an `else` branch after this:"
3705 "I know what to do when the condition is `True`, but not when it is `False`."
3707 ["Add","an",D.cyan "else","branch","to","handle","that","scenario!"]
3716toRecordReport :: Code.Source -> Context -> Record -> Row -> Col -> Report.Report
3717toRecordReport source context record startRow startCol =
3719 RecordOpen row col ->
3720 case Code.whatIsNext source row col of
3721 Code.Keyword keyword ->
3723 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3724 region = toKeywordRegion row col keyword
3726 Report.Report "RESERVED WORD" region [] $
3727 Code.toSnippet source surroundings (Just region)
3730 "I just started parsing a record, but I got stuck on this field name:"
3733 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3734 \ that is a reserved word. Try using a different name!"
3739 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3740 region = toRegion row col
3742 Report.Report "UNFINISHED RECORD" region [] $
3743 Code.toSnippet source surroundings (Just region)
3746 "I just started parsing a record, but I got stuck here:"
3749 ["Records","look","like",D.dullyellow "{ x = 3, y = 4 },"
3750 ,"so","I","was","expecting","to","see","a","field","name","next."
3754 RecordEnd row col ->
3756 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3757 region = toRegion row col
3759 Report.Report "UNFINISHED RECORD" region [] $
3760 Code.toSnippet source surroundings (Just region)
3763 "I am partway through parsing a record, but I got stuck here:"
3767 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
3768 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
3771 "When I get stuck like this, it usually means that there is a missing parenthesis\
3772 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3776 RecordField row col ->
3777 case Code.whatIsNext source row col of
3778 Code.Keyword keyword ->
3780 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3781 region = toKeywordRegion row col keyword
3783 Report.Report "RESERVED WORD" region [] $
3784 Code.toSnippet source surroundings (Just region)
3787 "I am partway through parsing a record, but I got stuck on this field name:"
3790 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3791 \ that is a reserved word. Try using a different name!"
3796 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3797 region = toRegion row col
3799 Report.Report "UNFINISHED RECORD" region [] $
3800 Code.toSnippet source surroundings (Just region)
3803 "I am partway through parsing a record, but I got stuck after that last comma:"
3805 addNoteForRecordError $
3807 "Trailing commas are not allowed in records, so I was expecting to see another\
3808 \ record field defined next. If you are done defining the record, the fix may\
3809 \ be to delete that last comma?"
3812 RecordEquals row col ->
3814 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3815 region = toRegion row col
3817 Report.Report "UNFINISHED RECORD" region [] $
3818 Code.toSnippet source surroundings (Just region)
3821 "I am partway through parsing a record, but I got stuck here:"
3823 addNoteForRecordError $
3825 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
3826 ,"an","equals","sign","next.","So","try","putting","an",D.green "=","sign","here?"
3830 RecordExpr expr row col ->
3831 toExprReport source (InNode NRecord startRow startCol context) expr row col
3833 RecordSpace space row col ->
3834 toSpaceReport source space row col
3836 RecordIndentOpen row col ->
3838 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3839 region = toRegion row col
3841 Report.Report "UNFINISHED RECORD" region [] $
3842 Code.toSnippet source surroundings (Just region)
3845 "I just saw the opening curly brace of a record, but then I got stuck here:"
3847 addNoteForRecordIndentError $
3849 ["I","am","expecting","a","record","like",D.dullyellow "{ x = 3, y = 4 }","here."
3850 ,"Try","defining","some","fields","of","your","own?"
3854 RecordIndentEnd row col ->
3856 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3857 region = toRegion row col
3859 Report.Report "UNFINISHED RECORD" region [] $
3860 Code.toSnippet source surroundings (Just region)
3863 "I was expecting to see a closing curly brace next:"
3865 addNoteForRecordIndentError $
3867 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
3871 RecordIndentField row col ->
3873 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3874 region = toRegion row col
3876 Report.Report "UNFINISHED RECORD" region [] $
3877 Code.toSnippet source surroundings (Just region)
3880 "I am partway through parsing a record, but I got stuck after that last comma:"
3882 addNoteForRecordError $
3884 "Trailing commas are not allowed in records, so the fix may be to\
3885 \ delete that last comma? Or maybe you were in the middle of defining\
3886 \ an additional field?"
3889 RecordIndentEquals row col ->
3891 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3892 region = toRegion row col
3894 Report.Report "UNFINISHED RECORD" region [] $
3895 Code.toSnippet source surroundings (Just region)
3898 "I am partway through parsing a record. I just saw a record\
3899 \ field, so I was expecting to see an equals sign next:"
3901 addNoteForRecordIndentError $
3903 ["Try","putting","an",D.green "=","followed","by","an","expression?"
3907 RecordIndentExpr row col ->
3909 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3910 region = toRegion row col
3912 Report.Report "UNFINISHED RECORD" region [] $
3913 Code.toSnippet source surroundings (Just region)
3916 "I am partway through parsing a record, and I was expecting to run into an expression next:"
3918 addNoteForRecordIndentError $
3920 ["Try","putting","something","like"
3921 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
3926addNoteForRecordError :: D.Doc -> D.Doc
3927addNoteForRecordError normalRecommendation =
3929 normalRecommendation
3932 "If you are trying to define a record across multiple lines, I recommend using this format:"
3933 , D.indent 4 $ D.vcat $
3934 [ "{ name = \"Alice\""
3940 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3941 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3942 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3946addNoteForRecordIndentError :: D.Doc -> D.Doc
3947addNoteForRecordIndentError normalRecommendation =
3949 normalRecommendation
3952 "I may be confused by indentation. For example, if you are trying to define\
3953 \ a record across multiple lines, I recommend using this format:"
3954 , D.indent 4 $ D.vcat $
3955 [ "{ name = \"Alice\""
3961 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3962 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3963 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3971toTupleReport :: Code.Source -> Context -> Tuple -> Row -> Col -> Report.Report
3972toTupleReport source context tuple startRow startCol =
3974 TupleExpr expr row col ->
3975 toExprReport source (InNode NParens startRow startCol context) expr row col
3977 TupleSpace space row col ->
3978 toSpaceReport source space row col
3982 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3983 region = toRegion row col
3985 Report.Report "UNFINISHED PARENTHESES" region [] $
3986 Code.toSnippet source surroundings (Just region)
3989 "I was expecting to see a closing parentheses next, but I got stuck here:"
3992 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
3994 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
3995 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
3996 \ or missing brackets) that is confusing me."
4000 TupleOperatorClose row col ->
4002 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4003 region = toRegion row col
4005 Report.Report "UNFINISHED OPERATOR FUNCTION" region [] $
4006 Code.toSnippet source surroundings (Just region)
4008 D.reflow "I was expecting a closing parenthesis here:"
4011 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
4013 "I think I am parsing an operator function right now, so I am expecting to see\
4014 \ something like (+) or (&&) where an operator is surrounded by parentheses with\
4019 TupleOperatorReserved operator row col ->
4021 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4022 region = toRegion row col
4024 Report.Report "UNEXPECTED SYMBOL" region [] $
4025 Code.toSnippet source surroundings (Just region)
4028 "I ran into an unexpected symbol here:"
4032 BadDot -> ["Maybe","you","wanted","a","record","accessor","like",D.dullyellow ".x","or",D.dullyellow ".name","instead?"]
4033 BadPipe -> ["Try",D.dullyellow "(||)","instead?","To","turn","boolean","OR","into","a","function?"]
4034 BadArrow -> ["Maybe","you","wanted",D.dullyellow "(>)","or",D.dullyellow "(>=)","instead?"]
4035 BadEquals -> ["Try",D.dullyellow "(==)","instead?","To","make","a","function","that","checks","equality?"]
4036 BadHasType -> ["Try",D.dullyellow "(::)","instead?","To","add","values","to","the","front","of","lists?"]
4039 TupleIndentExpr1 row col ->
4041 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4042 region = toRegion row col
4044 Report.Report "UNFINISHED PARENTHESES" region [] $
4045 Code.toSnippet source surroundings (Just region)
4048 "I just saw an open parenthesis, so I was expecting to see an expression next."
4052 ["Something","like",D.dullyellow "(4 + 5)","or"
4053 ,D.dullyellow "(String.reverse \"desserts\")" <> "."
4054 ,"Anything","where","you","are","putting","parentheses","around","normal","expressions."
4057 "I can get confused by indentation in cases like this, so\
4058 \ maybe you have an expression but it is not indented enough?"
4062 TupleIndentExprN row col ->
4064 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4065 region = toRegion row col
4067 Report.Report "UNFINISHED TUPLE" region [] $
4068 Code.toSnippet source surroundings (Just region)
4071 "I think I am in the middle of parsing a tuple. I just saw a comma, so I was expecting to see an expression next."
4075 ["A","tuple","looks","like",D.dullyellow "(3,4)","or"
4076 ,D.dullyellow "(\"Tom\",42)" <> ","
4077 ,"so","I","think","there","is","an","expression","missing","here?"
4080 "I can get confused by indentation in cases like this, so\
4081 \ maybe you have an expression but it is not indented enough?"
4085 TupleIndentEnd row col ->
4087 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4088 region = toRegion row col
4090 Report.Report "UNFINISHED PARENTHESES" region [] $
4091 Code.toSnippet source surroundings (Just region)
4094 "I was expecting to see a closing parenthesis next:"
4097 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
4099 "I can get confused by indentation in cases like this, so\
4100 \ maybe you have a closing parenthesis but it is not indented enough?"
4105toListReport :: Code.Source -> Context -> List -> Row -> Col -> Report.Report
4106toListReport source context list startRow startCol =
4108 ListSpace space row col ->
4109 toSpaceReport source space row col
4113 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4114 region = toRegion row col
4116 Report.Report "UNFINISHED LIST" region [] $
4117 Code.toSnippet source surroundings (Just region)
4120 "I am partway through parsing a list, but I got stuck here:"
4124 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
4125 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
4128 "When I get stuck like this, it usually means that there is a missing parenthesis\
4129 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
4133 ListExpr expr row col ->
4137 surroundings = A.Region (A.Position startRow startCol) (A.Position r c)
4138 region = toRegion r c
4140 Report.Report "UNFINISHED LIST" region [] $
4141 Code.toSnippet source surroundings (Just region)
4144 "I was expecting to see another list entry after that last comma:"
4148 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
4150 "I recommend using the following format for lists that span multiple lines:"
4151 , D.indent 4 $ D.vcat $
4158 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4159 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4160 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4165 toExprReport source (InNode NList startRow startCol context) expr row col
4169 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4170 region = toRegion row col
4172 Report.Report "UNFINISHED LIST" region [] $
4173 Code.toSnippet source surroundings (Just region)
4176 "I am partway through parsing a list, but I got stuck here:"
4180 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
4181 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
4184 "When I get stuck like this, it usually means that there is a missing parenthesis\
4185 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
4189 ListIndentOpen row col ->
4191 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4192 region = toRegion row col
4194 Report.Report "UNFINISHED LIST" region [] $
4195 Code.toSnippet source surroundings (Just region)
4198 "I cannot find the end of this list:"
4202 ["You","could","change","it","to","something","like"
4203 ,D.dullyellow "[3,4,5]"
4205 ,D.dullyellow "[]" <> "."
4206 ,"Anything","where","there","is","an","open","and","close","square","brace,"
4207 ,"and","where","the","elements","of","the","list","are","separated","by","commas."
4210 "I may be confused by indentation. For example, if you are trying to define\
4211 \ a list across multiple lines, I recommend using this format:"
4212 , D.indent 4 $ D.vcat $
4219 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4220 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4221 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4225 ListIndentEnd row col ->
4227 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4228 region = toRegion row col
4230 Report.Report "UNFINISHED LIST" region [] $
4231 Code.toSnippet source surroundings (Just region)
4234 "I cannot find the end of this list:"
4238 ["You","can","just","add","a","closing",D.dullyellow "]"
4239 ,"right","here,","and","I","will","be","all","set!"
4242 "I may be confused by indentation. For example, if you are trying to define\
4243 \ a list across multiple lines, I recommend using this format:"
4244 , D.indent 4 $ D.vcat $
4251 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4252 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4253 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4257 ListIndentExpr row col ->
4259 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4260 region = toRegion row col
4262 Report.Report "UNFINISHED LIST" region [] $
4263 Code.toSnippet source surroundings (Just region)
4266 "I was expecting to see another list entry after this comma:"
4270 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
4272 "I recommend using the following format for lists that span multiple lines:"
4273 , D.indent 4 $ D.vcat $
4280 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4281 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4282 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4287toFuncReport :: Code.Source -> Context -> Func -> Row -> Col -> Report.Report
4288toFuncReport source context func startRow startCol =
4290 FuncSpace space row col ->
4291 toSpaceReport source space row col
4293 FuncArg pattern row col ->
4294 toPatternReport source PArg pattern row col
4296 FuncBody expr row col ->
4297 toExprReport source (InNode NFunc startRow startCol context) expr row col
4299 FuncArrow row col ->
4300 case Code.whatIsNext source row col of
4301 Code.Keyword keyword ->
4303 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4304 region = toKeywordRegion row col keyword
4306 Report.Report "RESERVED WORD" region [] $
4307 Code.toSnippet source surroundings (Just region)
4310 "I was parsing an anonymous function, but I got stuck here:"
4313 "It looks like you are trying to use `" ++ keyword ++ "` as an argument, but\
4314 \ it is a reserved word in this language. Try using a different argument name!"
4319 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4320 region = toRegion row col
4322 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4323 Code.toSnippet source surroundings (Just region)
4326 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
4329 ["The","syntax","for","anonymous","functions","is"
4330 ,D.dullyellow "(\\x -> x + 1)"
4331 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
4335 FuncIndentArg row col ->
4337 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4338 region = toRegion row col
4340 Report.Report "MISSING ARGUMENT" region [] $
4341 Code.toSnippet source surroundings (Just region)
4344 "I just saw the beginning of an anonymous function, so I was expecting to see an argument next:"
4348 ["Something","like",D.dullyellow"x","or",D.dullyellow "name" <> "."
4349 ,"Anything","that","starts","with","a","lower","case","letter!"
4352 "The syntax for anonymous functions is (\\x -> x + 1) where the backslash\
4353 \ is meant to look a bit like a lambda if you squint. This visual pun seemed\
4354 \ like a better idea at the time!"
4358 FuncIndentArrow row col ->
4360 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4361 region = toRegion row col
4363 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4364 Code.toSnippet source surroundings (Just region)
4367 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
4371 ["The","syntax","for","anonymous","functions","is"
4372 ,D.dullyellow "(\\x -> x + 1)"
4373 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
4376 "It is possible that I am confused about indetation! I generally recommend\
4377 \ switching to named functions if the definition cannot fit inline nicely, so\
4378 \ either (1) try to fit the whole anonymous function on one line or (2) break\
4379 \ the whole thing out into a named function. Things tend to be clearer that way!"
4383 FuncIndentBody row col ->
4385 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4386 region = toRegion row col
4388 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4389 Code.toSnippet source surroundings (Just region)
4392 "I was expecting to see the body of your anonymous function next:"
4396 ["The","syntax","for","anonymous","functions","is"
4397 ,D.dullyellow "(\\x -> x + 1)"
4398 ,"so","I","am","missing","all","the","stuff","after","the","arrow!"
4401 "It is possible that I am confused about indetation! I generally recommend\
4402 \ switching to named functions if the definition cannot fit inline nicely, so\
4403 \ either (1) try to fit the whole anonymous function on one line or (2) break\
4404 \ the whole thing out into a named function. Things tend to be clearer that way!"
4419toPatternReport :: Code.Source -> PContext -> Pattern -> Row -> Col -> Report.Report
4420toPatternReport source context pattern startRow startCol =
4422 PRecord record row col ->
4423 toPRecordReport source record row col
4425 PTuple tuple row col ->
4426 toPTupleReport source context tuple row col
4428 PList list row col ->
4429 toPListReport source context list row col
4432 case Code.whatIsNext source row col of
4433 Code.Keyword keyword ->
4435 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4436 region = toKeywordRegion row col keyword
4439 PArg -> "as an argument"
4440 PCase -> "in this pattern"
4441 PLet -> "in this pattern"
4443 Report.Report "RESERVED WORD" region [] $
4444 Code.toSnippet source surroundings (Just region)
4447 "It looks like you are trying to use `" ++ keyword ++ "` " ++ inThisThing ++ ":"
4450 "This is a reserved word! Try using some other name?"
4453 Code.Operator "-" ->
4455 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4456 region = toRegion row col
4458 Report.Report "UNEXPECTED SYMBOL" region [] $
4459 Code.toSnippet source surroundings (Just region)
4462 "I ran into a minus sign unexpectedly in this pattern:"
4465 "It is not possible to pattern match on negative numbers at this\
4466 \ time. Try using an `if` expression for that sort of thing for now."
4471 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4472 region = toRegion row col
4474 Report.Report "PROBLEM IN PATTERN" region [] $
4475 Code.toSnippet source surroundings (Just region)
4478 "I wanted to parse a pattern next, but I got stuck here:"
4481 ["I","am","not","sure","why","I","am","getting","stuck","exactly."
4482 ,"I","just","know","that","I","want","a","pattern","next."
4483 ,"Something","as","simple","as"
4484 ,D.dullyellow "maybeHeight","or",D.dullyellow "result"
4489 PChar char row col ->
4490 toCharReport source char row col
4492 PString string row col ->
4493 toStringReport source string row col
4495 PNumber number row col ->
4496 toNumberReport source number row col
4498 PFloat width row col ->
4500 region = toWiderRegion row col width
4502 Report.Report "UNEXPECTED PATTERN" region [] $
4503 Code.toSnippet source region Nothing
4506 "I cannot pattern match with floating point numbers:"
4509 ["Equality","on","floats","can","be","unreliable,","so","you","usually","want"
4510 ,"to","check","that","they","are","nearby","with","some","sort","of"
4511 ,D.dullyellow "(abs (actual - expected) < 0.001)","check."
4517 region = toRegion row col
4519 Report.Report "UNFINISHED PATTERN" region [] $
4520 Code.toSnippet source region Nothing $
4523 "I was expecting to see a variable name after the `as` keyword:"
4527 ["The","`as`","keyword","lets","you","write","patterns","like"
4528 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
4529 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
4530 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
4531 ,"thing","with",D.dullyellow "point" <> "."
4534 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
4535 \ people just want to use `as` as a variable name though. Try using a different name\
4540 PWildcardNotVar name width row col ->
4542 region = toWiderRegion row col (fromIntegral width)
4544 case dropWhile (=='_') (Name.toChars name) of
4545 [] -> [D.dullyellow "x","or",D.dullyellow "age"]
4546 c:cs -> [D.dullyellow (D.fromChars (Char.toLower c : cs))]
4548 Report.Report "UNEXPECTED NAME" region [] $
4549 Code.toSnippet source region Nothing $
4552 "Variable names cannot start with underscores like this:"
4555 ["You","can","either","have","an","underscore","like",D.dullyellow "_","to"
4556 ,"ignore","the","value,","or","you","can","have","a","name","like"
4557 ] ++ examples ++ ["to","use","the","matched","value." ]
4560 PSpace space row col ->
4561 toSpaceReport source space row col
4563 PIndentStart row col ->
4565 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4566 region = toRegion row col
4568 Report.Report "UNFINISHED PATTERN" region [] $
4569 Code.toSnippet source surroundings (Just region)
4572 "I wanted to parse a pattern next, but I got stuck here:"
4576 ["I","am","not","sure","why","I","am","getting","stuck","exactly."
4577 ,"I","just","know","that","I","want","a","pattern","next."
4578 ,"Something","as","simple","as"
4579 ,D.dullyellow "maybeHeight","or",D.dullyellow "result"
4583 "I can get confused by indentation. If you think there is a pattern next, maybe\
4584 \ it needs to be indented a bit more?"
4588 PIndentAlias row col ->
4590 region = toRegion row col
4592 Report.Report "UNFINISHED PATTERN" region [] $
4593 Code.toSnippet source region Nothing $
4596 "I was expecting to see a variable name after the `as` keyword:"
4600 ["The","`as`","keyword","lets","you","write","patterns","like"
4601 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
4602 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
4603 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
4604 ,"thing","with",D.dullyellow "point."
4607 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
4608 \ people just want to use `as` as a variable name though. Try using a different name\
4614toPRecordReport :: Code.Source -> PRecord -> Row -> Col -> Report.Report
4615toPRecordReport source record startRow startCol =
4617 PRecordOpen row col ->
4618 toUnfinishRecordPatternReport source row col startRow startCol $
4619 D.reflow "I was expecting to see a field name next."
4621 PRecordEnd row col ->
4622 toUnfinishRecordPatternReport source row col startRow startCol $
4624 ["I","was","expecting","to","see","a","closing","curly","brace","next."
4625 ,"Try","adding","a",D.dullyellow "}","here?"
4628 PRecordField row col ->
4629 case Code.whatIsNext source row col of
4630 Code.Keyword keyword ->
4632 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4633 region = toKeywordRegion row col keyword
4635 Report.Report "RESERVED WORD" region [] $
4636 Code.toSnippet source surroundings (Just region)
4639 "I was not expecting to see `" ++ keyword ++ "` as a record field name:"
4642 "This is a reserved word, not available for variable names. Try another name!"
4646 toUnfinishRecordPatternReport source row col startRow startCol $
4647 D.reflow "I was expecting to see a field name next."
4649 PRecordSpace space row col ->
4650 toSpaceReport source space row col
4652 PRecordIndentOpen row col ->
4653 toUnfinishRecordPatternReport source row col startRow startCol $
4654 D.reflow "I was expecting to see a field name next."
4656 PRecordIndentEnd row col ->
4657 toUnfinishRecordPatternReport source row col startRow startCol $
4659 ["I","was","expecting","to","see","a","closing","curly","brace","next."
4660 ,"Try","adding","a",D.dullyellow "}","here?"
4663 PRecordIndentField row col ->
4664 toUnfinishRecordPatternReport source row col startRow startCol $
4665 D.reflow "I was expecting to see a field name next."
4668toUnfinishRecordPatternReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
4669toUnfinishRecordPatternReport source row col startRow startCol message =
4671 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4672 region = toRegion row col
4674 Report.Report "UNFINISHED RECORD PATTERN" region [] $
4675 Code.toSnippet source surroundings (Just region)
4678 "I was partway through parsing a record pattern, but I got stuck here:"
4683 ["A","record","pattern","looks","like",D.dullyellow "{x,y}","or",D.dullyellow "{name,age}"
4684 ,"where","you","list","the","field","names","you","want","to","access."
4691toPTupleReport :: Code.Source -> PContext -> PTuple -> Row -> Col -> Report.Report
4692toPTupleReport source context tuple startRow startCol =
4694 PTupleOpen row col ->
4695 case Code.whatIsNext source row col of
4696 Code.Keyword keyword ->
4698 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4699 region = toKeywordRegion row col keyword
4701 Report.Report "RESERVED WORD" region [] $
4702 Code.toSnippet source surroundings (Just region)
4705 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name:"
4708 "This is a reserved word! Try using some other name?"
4713 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4714 region = toRegion row col
4716 Report.Report "UNFINISHED PARENTHESES" region [] $
4717 Code.toSnippet source surroundings (Just region)
4720 "I just saw an open parenthesis, but I got stuck here:"
4723 ["I","was","expecting","to","see","a","pattern","next."
4724 ,"Maybe","it","will","end","up","being","something"
4725 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
4729 PTupleEnd row col ->
4730 case Code.whatIsNext source row col of
4731 Code.Keyword keyword ->
4733 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4734 region = toKeywordRegion row col keyword
4736 Report.Report "RESERVED WORD" region [] $
4737 Code.toSnippet source surroundings (Just region) $
4740 "I ran into a reserved word in this pattern:"
4743 "The `" ++ keyword ++ "` keyword is reserved. Try using a different name instead!"
4748 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4749 region = toKeywordRegion row col op
4751 Report.Report "UNEXPECTED SYMBOL" region [] $
4752 Code.toSnippet source surroundings (Just region) $
4755 "I ran into the " ++ op ++ " symbol unexpectedly in this pattern:"
4758 "Only the :: symbol that works in patterns. It is useful if you\
4759 \ are pattern matching on lists, trying to get the first element\
4760 \ off the front. Did you want that instead?"
4763 Code.Close term bracket ->
4765 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4766 region = toRegion row col
4768 Report.Report ("STRAY " ++ map Char.toUpper term) region [] $
4769 Code.toSnippet source surroundings (Just region) $
4772 "I ran into a an unexpected " ++ term ++ " in this pattern:"
4775 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
4780 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4781 region = toRegion row col
4783 Report.Report "UNFINISHED PARENTHESES" region [] $
4784 Code.toSnippet source surroundings (Just region) $
4787 "I was partway through parsing a pattern, but I got stuck here:"
4790 ["I","was","expecting","a","closing","parenthesis","next,","so"
4791 ,"try","adding","a",D.dullyellow ")","to","see","if","that","helps?"
4795 PTupleExpr pattern row col ->
4796 toPatternReport source context pattern row col
4798 PTupleSpace space row col ->
4799 toSpaceReport source space row col
4801 PTupleIndentEnd row col ->
4803 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4804 region = toRegion row col
4806 Report.Report "UNFINISHED PARENTHESES" region [] $
4807 Code.toSnippet source surroundings (Just region) $
4810 "I was expecting a closing parenthesis next:"
4813 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
4815 "I can get confused by indentation in cases like this, so\
4816 \ maybe you have a closing parenthesis but it is not indented enough?"
4820 PTupleIndentExpr1 row col ->
4822 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4823 region = toRegion row col
4825 Report.Report "UNFINISHED PARENTHESES" region [] $
4826 Code.toSnippet source surroundings (Just region) $
4829 "I just saw an open parenthesis, but then I got stuck here:"
4832 ["I","was","expecting","to","see","a","pattern","next."
4833 ,"Maybe","it","will","end","up","being","something"
4834 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
4838 PTupleIndentExprN row col ->
4840 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4841 region = toRegion row col
4843 Report.Report "UNFINISHED TUPLE PATTERN" region [] $
4844 Code.toSnippet source surroundings (Just region) $
4847 "I am partway through parsing a tuple pattern, but I got stuck here:"
4851 ["I","was","expecting","to","see","a","pattern","next."
4852 ,"I","am","expecting","the","final","result","to","be","something"
4853 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "."
4856 "I can get confused by indentation in cases like this, so the problem\
4857 \ may be that the next part is not indented enough?"
4862toPListReport :: Code.Source -> PContext -> PList -> Row -> Col -> Report.Report
4863toPListReport source context list startRow startCol =
4865 PListOpen row col ->
4866 case Code.whatIsNext source row col of
4867 Code.Keyword keyword ->
4869 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4870 region = toKeywordRegion row col keyword
4872 Report.Report "RESERVED WORD" region [] $
4873 Code.toSnippet source surroundings (Just region)
4876 "It looks like you are trying to use `" ++ keyword ++ "` to name an element of a list:"
4879 "This is a reserved word though! Try using some other name?"
4884 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4885 region = toRegion row col
4887 Report.Report "UNFINISHED LIST PATTERN" region [] $
4888 Code.toSnippet source surroundings (Just region) $
4891 "I just saw an open square bracket, but then I got stuck here:"
4893 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4898 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4899 region = toRegion row col
4901 Report.Report "UNFINISHED LIST PATTERN" region [] $
4902 Code.toSnippet source surroundings (Just region) $
4905 "I was expecting a closing square bracket to end this list pattern:"
4907 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4910 PListExpr pattern row col ->
4911 toPatternReport source context pattern row col
4913 PListSpace space row col ->
4914 toSpaceReport source space row col
4916 PListIndentOpen row col ->
4918 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4919 region = toRegion row col
4921 Report.Report "UNFINISHED LIST PATTERN" region [] $
4922 Code.toSnippet source surroundings (Just region) $
4925 "I just saw an open square bracket, but then I got stuck here:"
4928 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4930 "I can get confused by indentation in cases like this, so\
4931 \ maybe there is something next, but it is not indented enough?"
4935 PListIndentEnd row col ->
4937 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4938 region = toRegion row col
4940 Report.Report "UNFINISHED LIST PATTERN" region [] $
4941 Code.toSnippet source surroundings (Just region) $
4944 "I was expecting a closing square bracket to end this list pattern:"
4947 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4949 "I can get confused by indentation in cases like this, so\
4950 \ maybe you have a closing square bracket but it is not indented enough?"
4954 PListIndentExpr row col ->
4956 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4957 region = toRegion row col
4959 Report.Report "UNFINISHED LIST PATTERN" region [] $
4960 Code.toSnippet source surroundings (Just region) $
4963 "I am partway through parsing a list pattern, but I got stuck here:"
4967 "I was expecting to see another pattern next. Maybe a variable name."
4969 "I can get confused by indentation in cases like this, so\
4970 \ maybe there is more to this pattern but it is not indented enough?"
4980 = TC_Annotation Name.Name
4986toTypeReport :: Code.Source -> TContext -> Type -> Row -> Col -> Report.Report
4987toTypeReport source context tipe startRow startCol =
4989 TRecord record row col ->
4990 toTRecordReport source context record row col
4992 TTuple tuple row col ->
4993 toTTupleReport source context tuple row col
4996 case Code.whatIsNext source row col of
4997 Code.Keyword keyword ->
4999 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5000 region = toKeywordRegion row col keyword
5002 Report.Report "RESERVED WORD" region [] $
5003 Code.toSnippet source surroundings (Just region)
5006 "I was expecting to see a type next, but I got stuck on this reserved word:"
5009 "It looks like you are trying to use `" ++ keyword ++ "` as a type variable, but \
5010 \ it is a reserved word. Try using a different name!"
5015 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5016 region = toRegion row col
5020 TC_Annotation _ -> "type annotation"
5021 TC_CustomType -> "custom type"
5022 TC_TypeAlias -> "type alias"
5027 TC_Annotation name -> "the `" ++ Name.toChars name ++ "` type annotation"
5028 TC_CustomType -> "a custom type"
5029 TC_TypeAlias -> "a type alias"
5032 Report.Report ("PROBLEM IN " ++ map Char.toUpper thing) region [] $
5033 Code.toSnippet source surroundings (Just region)
5036 "I was partway through parsing " ++ something ++ ", but I got stuck here:"
5039 ["I","was","expecting","to","see","a","type","next."
5040 ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
5044 TSpace space row col ->
5045 toSpaceReport source space row col
5047 TIndentStart row col ->
5049 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5050 region = toRegion row col
5054 TC_Annotation _ -> "type annotation"
5055 TC_CustomType -> "custom type"
5056 TC_TypeAlias -> "type alias"
5059 Report.Report ("UNFINISHED " ++ map Char.toUpper thing) region [] $
5060 Code.toSnippet source surroundings (Just region)
5063 "I was partway through parsing a " ++ thing ++ ", but I got stuck here:"
5067 ["I","was","expecting","to","see","a","type","next."
5068 ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
5071 "I can get confused by indentation. If you think there is already a type\
5072 \ next, maybe it is not indented enough?"
5077toTRecordReport :: Code.Source -> TContext -> TRecord -> Row -> Col -> Report.Report
5078toTRecordReport source context record startRow startCol =
5080 TRecordOpen row col ->
5081 case Code.whatIsNext source row col of
5082 Code.Keyword keyword ->
5084 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5085 region = toKeywordRegion row col keyword
5087 Report.Report "RESERVED WORD" region [] $
5088 Code.toSnippet source surroundings (Just region)
5091 "I just started parsing a record type, but I got stuck on this field name:"
5094 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
5095 \ that is a reserved word. Try using a different name!"
5100 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5101 region = toRegion row col
5103 Report.Report "UNFINISHED RECORD TYPE" region [] $
5104 Code.toSnippet source surroundings (Just region)
5107 "I just started parsing a record type, but I got stuck here:"
5110 ["Record","types","look","like",D.dullyellow "{ name : String, age : Int },"
5111 ,"so","I","was","expecting","to","see","a","field","name","next."
5115 TRecordEnd row col ->
5117 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5118 region = toRegion row col
5120 Report.Report "UNFINISHED RECORD TYPE" region [] $
5121 Code.toSnippet source surroundings (Just region)
5124 "I am partway through parsing a record type, but I got stuck here:"
5128 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
5129 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
5132 "When I get stuck like this, it usually means that there is a missing parenthesis\
5133 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
5137 TRecordField row col ->
5138 case Code.whatIsNext source row col of
5139 Code.Keyword keyword ->
5141 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5142 region = toKeywordRegion row col keyword
5144 Report.Report "RESERVED WORD" region [] $
5145 Code.toSnippet source surroundings (Just region)
5148 "I am partway through parsing a record type, but I got stuck on this field name:"
5151 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
5152 \ that is a reserved word. Try using a different name!"
5157 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5158 region = toRegion row col
5160 Report.Report "UNFINISHED RECORD TYPE" region [] $
5161 Code.toSnippet source surroundings (Just region)
5164 "I am partway through parsing a record type, but I got stuck after that last comma:"
5166 addNoteForRecordTypeError $
5168 "Trailing commas are not allowed in records, so I was expecting to see another\
5169 \ record field defined next. If you are done defining the record, the fix may\
5170 \ be to delete that last comma?"
5173 TRecordColon row col ->
5175 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5176 region = toRegion row col
5178 Report.Report "UNFINISHED RECORD TYPE" region [] $
5179 Code.toSnippet source surroundings (Just region)
5182 "I am partway through parsing a record type, but I got stuck here:"
5184 addNoteForRecordTypeError $
5186 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
5187 ,"a","colon","next.","So","try","putting","an",D.green ":","sign","here?"
5191 TRecordType tipe row col ->
5192 toTypeReport source context tipe row col
5194 TRecordSpace space row col ->
5195 toSpaceReport source space row col
5197 TRecordIndentOpen row col ->
5199 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5200 region = toRegion row col
5202 Report.Report "UNFINISHED RECORD TYPE" region [] $
5203 Code.toSnippet source surroundings (Just region)
5206 "I just saw the opening curly brace of a record type, but then I got stuck here:"
5208 addNoteForRecordTypeIndentError $
5210 ["I","am","expecting","a","record","like",D.dullyellow "{ name : String, age : Int }","here."
5211 ,"Try","defining","some","fields","of","your","own?"
5215 TRecordIndentEnd row col ->
5217 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5218 region = toRegion row col
5220 Report.Report "UNFINISHED RECORD TYPE" region [] $
5221 Code.toSnippet source surroundings (Just region)
5224 "I was expecting to see a closing curly brace next:"
5226 addNoteForRecordTypeIndentError $
5228 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
5232 TRecordIndentField row col ->
5234 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5235 region = toRegion row col
5237 Report.Report "UNFINISHED RECORD TYPE" region [] $
5238 Code.toSnippet source surroundings (Just region)
5241 "I am partway through parsing a record type, but I got stuck after that last comma:"
5243 addNoteForRecordTypeIndentError $
5245 "Trailing commas are not allowed in record types, so the fix may be to\
5246 \ delete that last comma? Or maybe you were in the middle of defining\
5247 \ an additional field?"
5250 TRecordIndentColon row col ->
5252 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5253 region = toRegion row col
5255 Report.Report "UNFINISHED RECORD TYPE" region [] $
5256 Code.toSnippet source surroundings (Just region)
5259 "I am partway through parsing a record type. I just saw a record\
5260 \ field, so I was expecting to see a colon next:"
5262 addNoteForRecordTypeIndentError $
5264 ["Try","putting","an",D.green ":","followed","by","a","type?"
5268 TRecordIndentType row col ->
5270 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5271 region = toRegion row col
5273 Report.Report "UNFINISHED RECORD TYPE" region [] $
5274 Code.toSnippet source surroundings (Just region)
5277 "I am partway through parsing a record type, and I was expecting to run into a type next:"
5279 addNoteForRecordTypeIndentError $
5281 ["Try","putting","something","like"
5282 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
5287addNoteForRecordTypeError :: D.Doc -> D.Doc
5288addNoteForRecordTypeError normalRecommendation =
5290 normalRecommendation
5293 "If you are trying to define a record type across multiple lines, I recommend using this format:"
5294 , D.indent 4 $ D.vcat $
5297 , ", height : Float"
5301 "Notice that each line starts with some indentation. Usually two or four spaces. I\
5302 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
5303 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
5307addNoteForRecordTypeIndentError :: D.Doc -> D.Doc
5308addNoteForRecordTypeIndentError normalRecommendation =
5310 normalRecommendation
5313 "I may be confused by indentation. For example, if you are trying to define\
5314 \ a record type across multiple lines, I recommend using this format:"
5315 , D.indent 4 $ D.vcat $
5318 , ", height : Float"
5322 "Notice that each line starts with some indentation. Usually two or four spaces. I\
5323 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
5324 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
5328toTTupleReport :: Code.Source -> TContext -> TTuple -> Row -> Col -> Report.Report
5329toTTupleReport source context tuple startRow startCol =
5331 TTupleOpen row col ->
5332 case Code.whatIsNext source row col of
5333 Code.Keyword keyword ->
5335 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5336 region = toKeywordRegion row col keyword
5338 Report.Report "RESERVED WORD" region [] $
5339 Code.toSnippet source surroundings (Just region)
5342 "I ran into a reserved word unexpectedly:"
5345 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but \
5346 \ it is a reserved word. Try using a different name!"
5351 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5352 region = toRegion row col
5354 Report.Report "UNFINISHED PARENTHESES" region [] $
5355 Code.toSnippet source surroundings (Just region)
5358 "I just saw an open parenthesis, so I was expecting to see a type next."
5361 ["Something","like",D.dullyellow "(Maybe Int)","or"
5362 ,D.dullyellow "(List Person)" <> "."
5363 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
5367 TTupleEnd row col ->
5369 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5370 region = toRegion row col
5372 Report.Report "UNFINISHED PARENTHESES" region [] $
5373 Code.toSnippet source surroundings (Just region)
5376 "I was expecting to see a closing parenthesis next, but I got stuck here:"
5379 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
5381 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
5382 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
5383 \ or missing brackets) that is confusing me."
5387 TTupleType tipe row col ->
5388 toTypeReport source context tipe row col
5390 TTupleSpace space row col ->
5391 toSpaceReport source space row col
5393 TTupleIndentType1 row col ->
5395 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5396 region = toRegion row col
5398 Report.Report "UNFINISHED PARENTHESES" region [] $
5399 Code.toSnippet source surroundings (Just region)
5402 "I just saw an open parenthesis, so I was expecting to see a type next."
5406 ["Something","like",D.dullyellow "(Maybe Int)","or"
5407 ,D.dullyellow "(List Person)" <> "."
5408 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
5411 "I can get confused by indentation in cases like this, so\
5412 \ maybe you have a type but it is not indented enough?"
5416 TTupleIndentTypeN row col ->
5418 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5419 region = toRegion row col
5421 Report.Report "UNFINISHED TUPLE TYPE" region [] $
5422 Code.toSnippet source surroundings (Just region)
5425 "I think I am in the middle of parsing a tuple type. I just saw a comma, so I was expecting to see a type next."
5429 ["A","tuple","type","looks","like",D.dullyellow "(Float,Float)","or"
5430 ,D.dullyellow "(String,Int)" <> ","
5431 ,"so","I","think","there","is","a","type","missing","here?"
5434 "I can get confused by indentation in cases like this, so\
5435 \ maybe you have an expression but it is not indented enough?"
5439 TTupleIndentEnd row col ->
5441 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5442 region = toRegion row col
5444 Report.Report "UNFINISHED PARENTHESES" region [] $
5445 Code.toSnippet source surroundings (Just region)
5448 "I was expecting to see a closing parenthesis next:"
5451 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
5453 "I can get confused by indentation in cases like this, so\
5454 \ maybe you have a closing parenthesis but it is not indented enough?"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list