1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
47import Prelude hiding (Char, String)
48import qualified Data.Char as Char
49import qualified Data.Name as Name
50import Data.Word (Word16)
51import Numeric (showHex)
53import qualified Elm.ModuleName as ModuleName
54import Parse.Primitives (Row, Col)
55import Parse.Symbol (BadOperator(..))
56import qualified Reporting.Annotation as A
57import qualified Reporting.Doc as D
58import qualified Reporting.Report as Report
59import qualified Reporting.Render.Code as Code
67 = ModuleNameUnspecified ModuleName.Raw
68 | ModuleNameMismatch ModuleName.Raw (A.Located ModuleName.Raw)
69 | UnexpectedPort A.Region
79 = ModuleSpace Space Row Col
80 | ModuleEndOfFile Row Col
82 | ModuleProblem Row Col
84 | ModuleExposing Exposing Row Col
86 | PortModuleProblem Row Col
87 | PortModuleName Row Col
88 | PortModuleExposing Exposing Row Col
98 | ImportExposing Row Col
99 | ImportExposingList Exposing Row Col
100 | ImportEnd Row Col -- different based on col=1 or if greater
102 | ImportIndentName Row Col
103 | ImportIndentAlias Row Col
104 | ImportIndentExposingList Row Col
108 | Declarations Decl Row Col
112 = ExposingSpace Space Row Col
113 | ExposingStart Row Col
114 | ExposingValue Row Col
115 | ExposingOperator Row Col
116 | ExposingOperatorReserved BadOperator Row Col
117 | ExposingOperatorRightParen Row Col
118 | ExposingTypePrivacy Row Col
119 | ExposingEnd Row Col
121 | ExposingIndentEnd Row Col
122 | ExposingIndentValue Row Col
131 | DeclSpace Space Row Col
134 | DeclType DeclType Row Col
135 | DeclDef Name.Name DeclDef Row Col
137 | DeclFreshLineStart 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
228 | IndentMoreExpr Row Col
234 | RecordField Row Col
235 | RecordEquals Row Col
236 | RecordExpr Expr Row Col
237 | RecordSpace Space Row Col
239 | RecordIndentOpen Row Col
240 | RecordIndentEnd Row Col
241 | RecordIndentField Row Col
242 | RecordIndentEquals Row Col
243 | RecordIndentExpr Row Col
247 = TupleExpr Expr Row Col
248 | TupleSpace Space Row Col
250 | TupleOperatorClose Row Col
251 | TupleOperatorReserved BadOperator Row Col
253 | TupleIndentExpr1 Row Col
254 | TupleIndentExprN Row Col
255 | TupleIndentEnd Row Col
259 = ListSpace Space Row Col
261 | ListExpr Expr Row Col
264 | ListIndentOpen Row Col
265 | ListIndentEnd Row Col
266 | ListIndentExpr Row Col
270 = FuncSpace Space Row Col
271 | FuncArg Pattern Row Col
272 | FuncBody Expr Row Col
275 | FuncIndentArg Row Col
276 | FuncIndentArrow Row Col
277 | FuncIndentBody Row Col
281 = CaseSpace Space Row Col
283 | CasePattern Pattern Row Col
285 | CaseExpr Expr Row Col
286 | CaseBranch Expr Row Col
288 | CaseIndentOf Row Col
289 | CaseIndentExpr Row Col
290 | CaseIndentPattern Row Col
291 | CaseIndentArrow Row Col
292 | CaseIndentBranch Row Col
293 | CasePatternAlignment Word16 Row Col
297 = IfSpace Space Row Col
300 | IfElseBranchStart Row Col
302 | IfCondition Expr Row Col
303 | IfThenBranch Expr Row Col
304 | IfElseBranch Expr Row Col
306 | IfIndentCondition Row Col
307 | IfIndentThen Row Col
308 | IfIndentThenBranch Row Col
309 | IfIndentElseBranch Row Col
310 | IfIndentElse Row Col
314 = LetSpace Space Row Col
316 | LetDefAlignment Word16 Row Col
318 | LetDef Name.Name Def Row Col
319 | LetDestruct Destruct Row Col
320 | LetBody Expr Row Col
321 | LetIndentDef Row Col
322 | LetIndentIn Row Col
323 | LetIndentBody Row Col
327 = DefSpace Space Row Col
328 | DefType Type Row Col
329 | DefNameRepeat Name.Name Row Col
330 | DefNameMatch Name.Name Name.Name Row Col
331 | DefArg Pattern Row Col
333 | DefBody Expr Row Col
334 | DefIndentEquals Row Col
335 | DefIndentType Row Col
336 | DefIndentBody Row Col
337 | DefAlignment Word16 Row Col
341 = DestructSpace Space Row Col
342 | DestructPattern Pattern Row Col
343 | DestructEquals Row Col
344 | DestructBody Expr Row Col
345 | DestructIndentEquals Row Col
346 | DestructIndentBody Row Col
354 = PRecord PRecord Row Col
355 | PTuple PTuple Row Col
356 | PList PList Row Col
360 | PString String Row Col
361 | PNumber Number Row Col
362 | PFloat Word16 Row Col
364 | PWildcardNotVar Name.Name Int Row Col
365 | PSpace Space Row Col
367 | PIndentStart Row Col
368 | PIndentAlias Row Col
372 = PRecordOpen Row Col
374 | PRecordField Row Col
375 | PRecordSpace Space Row Col
377 | PRecordIndentOpen Row Col
378 | PRecordIndentEnd Row Col
379 | PRecordIndentField Row Col
385 | PTupleExpr Pattern Row Col
386 | PTupleSpace Space Row Col
388 | PTupleIndentEnd Row Col
389 | PTupleIndentExpr1 Row Col
390 | PTupleIndentExprN Row Col
396 | PListExpr Pattern Row Col
397 | PListSpace Space Row Col
399 | PListIndentOpen Row Col
400 | PListIndentEnd Row Col
401 | PListIndentExpr Row Col
409 = TRecord TRecord Row Col
410 | TTuple TTuple Row Col
414 | TSpace Space Row Col
416 | TIndentStart Row Col
420 = TRecordOpen Row Col
423 | TRecordField Row Col
424 | TRecordColon Row Col
425 | TRecordType Type Row Col
427 | TRecordSpace Space Row Col
429 | TRecordIndentOpen Row Col
430 | TRecordIndentField Row Col
431 | TRecordIndentColon Row Col
432 | TRecordIndentType Row Col
433 | TRecordIndentEnd Row Col
439 | TTupleType Type Row Col
440 | TTupleSpace Space Row Col
442 | TTupleIndentType1 Row Col
443 | TTupleIndentTypeN Row Col
444 | TTupleIndentEnd Row Col
454 | CharNotString Word16
458 = StringEndless_Single
459 | StringEndless_Multi
460 | StringEscape Escape
465 | BadUnicodeFormat Word16
466 | BadUnicodeCode Word16
467 | BadUnicodeLength Word16 Int Int
474 | NumberNoLeadingZero
483 | EndlessMultiComment
490toReport :: Code.Source -> Error -> Report.Report
493 ModuleNameUnspecified name ->
495 region = toRegion 1 1
497 Report.Report "MODULE NAME MISSING" region [] $
500 "I need the module name to be declared at the top of this file, like this:"
501 , D.indent 4 $ D.fillSep $
502 [ D.cyan "module", D.fromName name, D.cyan "exposing", "(..)" ]
504 "Try adding that as the first line of your file!"
506 "It is best to replace (..) with an explicit list of types and\
507 \ functions you want to expose. When you know a value is only used\
508 \ within this module, you can refactor without worrying about uses\
509 \ elsewhere. Limiting exposed values can also speed up compilation\
510 \ because I can skip a bunch of work if I see that the exposed API\
514 ModuleNameMismatch expectedName (A.At region actualName) ->
515 Report.Report "MODULE NAME MISMATCH" region [ModuleName.toChars expectedName] $
516 Code.toSnippet source region Nothing
518 "It looks like this module name is out of sync:"
522 "I need it to match the file path, so I was expecting it to see `"
523 ++ ModuleName.toChars expectedName
524 ++ "` here. Make the following change, and you should be all set!"
526 D.dullyellow (D.fromName actualName) <> " -> " <> D.green (D.fromName expectedName)
528 "I require that module names correspond to file paths. This makes it much\
529 \ easier to explore unfamiliar codebases! So if you want to keep the current\
530 \ module name, try renaming the file instead."
534 UnexpectedPort region ->
535 Report.Report "UNEXPECTED PORTS" region [] $
536 Code.toSnippet source region Nothing
539 "You are declaring ports in a normal module."
543 ["Switch","this","to","say",D.cyan "port module","instead,"
544 ,"marking","that","this","module","contains","port","declarations."
547 "Ports are not a traditional FFI for calling JS functions directly. They need a different mindset! Read"
549 "to learn the syntax and how to use it effectively."
554 Report.Report "NO PORTS" region [] $
555 Code.toSnippet source region Nothing
558 "This module does not declare any ports, but it says it will:"
561 ["Switch","this","to",D.cyan "module"
562 ,"and","you","should","be","all","set!"
567 toParseErrorReport source modul
570toParseErrorReport :: Code.Source -> Module -> Report.Report
571toParseErrorReport source modul =
573 ModuleSpace space row col ->
574 toSpaceReport source space row col
576 ModuleEndOfFile row col ->
577 case Code.whatIsNext source row col of
578 Code.Keyword keyword ->
580 region = toKeywordRegion row col keyword
582 Report.Report "RESERVED WORD" region [] $
583 Code.toSnippet source region Nothing
586 "I got stuck on this reserved word:"
589 "The name `" ++ keyword ++ "` is reserved, so try using a different name?"
594 region = toKeywordRegion row col op
596 Report.Report "UNEXPECTED SYMBOL" region [] $
597 Code.toSnippet source region Nothing
600 "I ran into an unexpected symbol:"
603 "I was not expecting to see a " ++ op ++ " here. Try deleting it? Maybe\
604 \ I can give a better hint from there?"
607 Code.Close term bracket ->
609 region = toRegion row col
611 Report.Report ("UNEXPECTED " ++ map Char.toUpper term) region [] $
612 Code.toSnippet source region Nothing
615 "I ran into an unexpected " ++ term ++ ":"
618 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
623 region = toRegion row col
625 Report.Report "SYNTAX PROBLEM" region [] $
626 Code.toSnippet source region Nothing
632 "Whatever I am running into is confusing me a lot! Normally I can give fairly\
633 \ specific hints, but something is really tripping me up this time."
636 ModuleProblem row col ->
638 region = toRegion row col
640 Report.Report "UNFINISHED MODULE DECLARATION" region [] $
641 Code.toSnippet source region Nothing
644 "I am parsing an `module` declaration, but I got stuck here:"
648 "Here are some examples of valid `module` declarations:"
649 , D.indent 4 $ D.vcat $
650 [ D.fillSep [D.cyan "module","Main",D.cyan "exposing","(..)"]
651 , D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(Dict, empty, get)"]
654 "I generally recommend using an explicit exposing list. I can skip compiling a bunch\
655 \ of files when the public interface of a module stays the same, so exposing fewer\
656 \ values can help improve compile times!"
660 ModuleName row col ->
662 region = toRegion row col
664 Report.Report "EXPECTING MODULE NAME" region [] $
665 Code.toSnippet source region Nothing
668 "I was parsing an `module` declaration until I got stuck here:"
672 "I was expecting to see the module name next, like in these examples:"
673 , D.indent 4 $ D.vcat $
674 [ D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(..)"]
675 , D.fillSep [D.cyan "module","Maybe",D.cyan "exposing","(..)"]
676 , D.fillSep [D.cyan "module","Html.Attributes",D.cyan "exposing","(..)"]
677 , D.fillSep [D.cyan "module","Json.Decode",D.cyan "exposing","(..)"]
680 "Notice that the module names all start with capital letters. That is required!"
684 ModuleExposing exposing row col ->
685 toExposingReport source exposing row col
687 PortModuleProblem row col ->
689 region = toRegion row col
691 Report.Report "UNFINISHED PORT MODULE DECLARATION" region [] $
692 Code.toSnippet source region Nothing
695 "I am parsing an `port module` declaration, but I got stuck here:"
699 "Here are some examples of valid `port module` declarations:"
700 , D.indent 4 $ D.vcat $
701 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
702 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
704 , D.link "Note" "Read" "ports" "for more help."
708 PortModuleName row col ->
710 region = toRegion row col
712 Report.Report "EXPECTING MODULE NAME" region [] $
713 Code.toSnippet source region Nothing
716 "I was parsing an `module` declaration until I got stuck here:"
720 "I was expecting to see the module name next, like in these examples:"
721 , D.indent 4 $ D.vcat $
722 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
723 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
726 "Notice that the module names start with capital letters. That is required!"
730 PortModuleExposing exposing row col ->
731 toExposingReport source exposing row col
735 region = toRegion row col
737 Report.Report "BAD MODULE DECLARATION" region [] $
738 Code.toSnippet source region Nothing
741 "I cannot parse this module declaration:"
744 "This type of module is reserved for the @elm organization. It is used to\
745 \ define certain effects, avoiding building them into the compiler."
750 region = toRegion row col
752 toBadFirstLineReport keyword =
753 Report.Report "TOO MUCH INDENTATION" region [] $
754 Code.toSnippet source region Nothing
757 "This `" ++ keyword ++ "` should not have any spaces before it:"
760 "Delete the spaces before `" ++ keyword ++ "` until there are none left!"
764 case Code.whatIsNext source row col of
765 Code.Keyword "module" -> toBadFirstLineReport "module"
766 Code.Keyword "import" -> toBadFirstLineReport "import"
767 Code.Keyword "type" -> toBadFirstLineReport "type"
768 Code.Keyword "port" -> toBadFirstLineReport "port"
770 Report.Report "SYNTAX PROBLEM" region [] $
771 Code.toSnippet source region Nothing
778 "I am not sure what is going on, but I recommend starting an Elm\
779 \ file with the following lines:"
781 [ D.fillSep [D.cyan "import","Html"]
783 , D.fillSep [D.green "main","="]
784 , D.indent 2 $ D.fillSep [D.cyan "Html" <> ".text",D.dullyellow "\"Hello!\""]
787 "You should be able to copy those lines directly into your file. Check out\
788 \ some of the examples on https://elm-lang.org for more help getting started!"
790 "This can also happen when something is indented too much!"
794 ImportStart row col ->
795 toImportReport source row col
797 ImportName row col ->
799 region = toRegion row col
801 Report.Report "EXPECTING IMPORT NAME" region [] $
802 Code.toSnippet source region Nothing
805 "I was parsing an `import` until I got stuck here:"
809 "I was expecting to see a module name next, like in these examples:"
810 , D.indent 4 $ D.vcat $
811 [ D.fillSep [D.cyan "import","Dict"]
812 , D.fillSep [D.cyan "import","Maybe"]
813 , D.fillSep [D.cyan "import","Html.Attributes"]
814 , D.fillSep [D.cyan "import","Json.Decode"]
817 "Notice that the module names all start with capital letters. That is required!"
822 toImportReport source row col
824 ImportAlias row col ->
826 region = toRegion row col
828 Report.Report "EXECTING IMPORT ALIAS" region [] $
829 Code.toSnippet source region Nothing
832 "I was parsing an `import` until I got stuck here:"
836 "I was expecting to see an alias next, like in these examples:"
837 , D.indent 4 $ D.vcat $
838 [ D.fillSep [D.cyan "import","Html.Attributes",D.cyan "as","Attr"]
839 , D.fillSep [D.cyan "import","WebGL.Texture",D.cyan "as","Texture"]
840 , D.fillSep [D.cyan "import","Json.Decode",D.cyan "as","D"]
843 "Notice that the alias always starts with a capital letter. That is required!"
847 ImportExposing row col ->
848 toImportReport source row col
850 ImportExposingList exposing row col ->
851 toExposingReport source exposing row col
854 toImportReport source row col
856 ImportIndentName row col ->
857 toImportReport source row col
859 ImportIndentAlias row col ->
860 toImportReport source row col
862 ImportIndentExposingList row col ->
864 region = toRegion row col
866 Report.Report "UNFINISHED IMPORT" region [] $
867 Code.toSnippet source region Nothing
870 "I was parsing an `import` until I got stuck here:"
874 "I was expecting to see the list of exposed values next. For example, here\
875 \ are two ways to expose values from the `Html` module:"
876 , D.indent 4 $ D.vcat $
877 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
878 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
881 "I generally recommend the second style. It is more explicit, making it\
882 \ much easier to figure out where values are coming from in large projects!"
888 region = toRegion row col
890 Report.Report "BAD INFIX" region [] $
891 Code.toSnippet source region Nothing
894 "Something went wrong in this infix operator declaration:"
897 "This feature is used by the @elm organization to define the\
898 \ languages built-in operators."
901 Declarations decl _ _ ->
902 toDeclarationsReport source decl
909toImportReport :: Code.Source -> Row -> Col -> Report.Report
910toImportReport source row col =
912 region = toRegion row col
914 Report.Report "UNFINISHED IMPORT" region [] $
915 Code.toSnippet source region Nothing
918 "I am partway through parsing an import, but I got stuck here:"
922 "Here are some examples of valid `import` declarations:"
923 , D.indent 4 $ D.vcat $
924 [ D.fillSep [D.cyan "import","Html"]
925 , D.fillSep [D.cyan "import","Html",D.cyan "as","H"]
926 , D.fillSep [D.cyan "import","Html",D.cyan "as","H",D.cyan "exposing","(..)"]
927 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
930 "You are probably trying to import a different module, but try to make it look like one of these examples!"
939toExposingReport :: Code.Source -> Exposing -> Row -> Col -> Report.Report
940toExposingReport source exposing startRow startCol =
942 ExposingSpace space row col ->
943 toSpaceReport source space row col
945 ExposingStart row col ->
947 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
948 region = toRegion row col
950 Report.Report "PROBLEM IN EXPOSING" region [] $
951 Code.toSnippet source surroundings (Just region)
954 "I want to parse exposed values, but I am getting stuck here:"
958 ["Exposed","values","are","always","surrounded","by","parentheses."
959 ,"So","try","adding","a",D.green "(","here?"
961 , D.toSimpleNote "Here are some valid examples of `exposing` for reference:"
962 , D.indent 4 $ D.vcat $
963 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
964 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
967 "If you are getting tripped up, you can just expose everything for now. It should\
968 \ get easier to make an explicit exposing list as you see more examples in the wild."
972 ExposingValue row col ->
973 case Code.whatIsNext source row col of
974 Code.Keyword keyword ->
976 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
977 region = toKeywordRegion row col keyword
979 Report.Report "RESERVED WORD" region [] $
980 Code.toSnippet source surroundings (Just region)
983 "I got stuck on this reserved word:"
986 "It looks like you are trying to expose `" ++ keyword ++ "` but that is a reserved word. Is there a typo?"
991 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
992 region = toKeywordRegion row col op
994 Report.Report "UNEXPECTED SYMBOL" region [] $
995 Code.toSnippet source surroundings (Just region)
998 "I got stuck on this symbol:"
1002 "If you are trying to expose an operator, add parentheses around it like this:"
1003 , D.indent 4 $ D.dullyellow (D.fromChars op) <> " -> " <> D.green ("(" <> D.fromChars op <> ")")
1009 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1010 region = toRegion row col
1012 Report.Report "PROBLEM IN EXPOSING" region [] $
1013 Code.toSnippet source surroundings (Just region)
1016 "I got stuck while parsing these exposed values:"
1020 "I do not have an exact recommendation, so here are some valid examples\
1021 \ of `exposing` for reference:"
1022 , D.indent 4 $ D.vcat $
1023 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
1024 , D.fillSep [D.cyan "import","Basics",D.cyan "exposing","(Int, Float, Bool(..), (+), not, sqrt)"]
1027 "These examples show how to expose types, variants, operators, and functions. Everything\
1028 \ should be some permutation of these examples, just with different names."
1032 ExposingOperator row col ->
1034 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1035 region = toRegion row col
1037 Report.Report "PROBLEM IN EXPOSING" region [] $
1038 Code.toSnippet source surroundings (Just region)
1041 "I just saw an open parenthesis, so I was expecting an operator next:"
1044 ["It","is","possible","to","expose","operators,","so","I","was","expecting"
1045 ,"to","see","something","like",D.dullyellow "(+)","or",D.dullyellow "(|=)"
1046 ,"or",D.dullyellow "(||)","after","I","saw","that","open","parenthesis."
1050 ExposingOperatorReserved op row col ->
1052 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1053 region = toRegion row col
1055 Report.Report "RESERVED SYMBOL" region [] $
1056 Code.toSnippet source surroundings (Just region)
1059 "I cannot expose this as an operator:"
1062 BadDot -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1063 BadPipe -> D.fillSep ["Maybe","you","want",D.dullyellow "(||)","instead?"]
1064 BadArrow -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1065 BadEquals -> D.fillSep ["Maybe","you","want",D.dullyellow "(==)","instead?"]
1066 BadHasType -> D.fillSep ["Maybe","you","want",D.dullyellow "(::)","instead?"]
1069 ExposingOperatorRightParen row col ->
1071 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1072 region = toRegion row col
1074 Report.Report "PROBLEM IN EXPOSING" region [] $
1075 Code.toSnippet source surroundings (Just region)
1078 "It looks like you are exposing an operator, but I got stuck here:"
1081 ["I","was","expecting","to","see","the","closing","parenthesis","immediately"
1082 ,"after","the","operator.","Try","adding","a",D.green ")","right","here?"
1086 ExposingEnd row col ->
1088 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1089 region = toRegion row col
1091 Report.Report "UNFINISHED EXPOSING" region [] $
1092 Code.toSnippet source surroundings (Just region)
1095 "I was partway through parsing exposed values, but I got stuck here:"
1098 "Maybe there is a comma missing before this?"
1101 ExposingTypePrivacy row col ->
1103 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1104 region = toRegion row col
1106 Report.Report "PROBLEM EXPOSING CUSTOM TYPE VARIANTS" region [] $
1107 Code.toSnippet source surroundings (Just region)
1110 "It looks like you are trying to expose the variants of a custom type:"
1114 ["You","need","to","write","something","like"
1115 ,D.dullyellow "Status(..)","or",D.dullyellow "Entity(..)"
1116 ,"though.","It","is","all","or","nothing,","otherwise","`case`"
1117 ,"expressions","could","miss","a","variant","and","crash!"
1120 "It is often best to keep the variants hidden! If someone pattern matches on\
1121 \ the variants, it is a MAJOR change if any new variants are added. Suddenly\
1122 \ their `case` expressions do not cover all variants! So if you do not need\
1123 \ people to pattern match, keep the variants hidden and expose functions to\
1124 \ construct values of this type. This way you can add new variants as a MINOR change!"
1128 ExposingIndentEnd row col ->
1130 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1131 region = toRegion row col
1133 Report.Report "UNFINISHED EXPOSING" region [] $
1134 Code.toSnippet source surroundings (Just region)
1137 "I was partway through parsing exposed values, but I got stuck here:"
1141 ["I","was","expecting","a","closing","parenthesis."
1142 ,"Try","adding","a",D.green ")","right","here?"
1145 "I can get confused when there is not enough indentation, so if you already\
1146 \ have a closing parenthesis, it probably just needs some spaces in front of it."
1150 ExposingIndentValue row col ->
1152 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1153 region = toRegion row col
1155 Report.Report "UNFINISHED EXPOSING" region [] $
1156 Code.toSnippet source surroundings (Just region)
1159 "I was partway through parsing exposed values, but I got stuck here:"
1162 "I was expecting another value to expose."
1170toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
1171toSpaceReport source space row col =
1175 region = toRegion row col
1177 Report.Report "NO TABS" region [] $
1178 Code.toSnippet source region Nothing
1181 "I ran into a tab, but tabs are not allowed in Elm files."
1184 "Replace the tab with spaces."
1187 EndlessMultiComment ->
1189 region = toWiderRegion row col 2
1191 Report.Report "ENDLESS COMMENT" region [] $
1192 Code.toSnippet source region Nothing
1195 "I cannot find the end of this multi-line comment:"
1198 [ D.reflow "Add a -} somewhere after this to end the comment."
1200 "Multi-line comments can be nested in Elm, so {- {- -} -} is a comment\
1201 \ that happens to contain another comment. Like parentheses and curly braces,\
1202 \ the start and end markers must always be balanced. Maybe that is the problem?"
1211toRegion :: Row -> Col -> A.Region
1214 pos = A.Position row col
1219toWiderRegion :: Row -> Col -> Word16 -> A.Region
1220toWiderRegion row col extra =
1222 (A.Position row col)
1223 (A.Position row (col + extra))
1226toKeywordRegion :: Row -> Col -> [Char.Char] -> A.Region
1227toKeywordRegion row col keyword =
1229 (A.Position row col)
1230 (A.Position row (col + fromIntegral (length keyword)))
1233toDeclarationsReport :: Code.Source -> Decl -> Report.Report
1234toDeclarationsReport source decl =
1236 DeclStart row col ->
1237 error "TODO DeclStart" row col
1239 DeclSpace space row col ->
1240 toSpaceReport source space row col
1242 Port port_ row col ->
1243 error "TODO Port" port_ row col
1245 DeclType declType row col ->
1246 toDeclTypeReport source declType row col
1248 DeclDef name declDef row col ->
1249 toDeclDefReport source name declDef row col
1251 DeclFreshLineStart row col ->
1252 error "TODO DeclFreshLineStart" row col
1254 DeclFreshLineAfterDocComment row col ->
1255 error "TODO DeclFreshLineAfterDocComment" row col
1262toDeclTypeReport :: Code.Source -> DeclType -> Row -> Col -> Report.Report
1263toDeclTypeReport source declType startRow startCol =
1265 DT_Space space row col ->
1266 toSpaceReport source space row col
1270 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1271 region = toRegion row col
1273 Report.Report "EXPECTING TYPE NAME" region [] $
1274 Code.toSnippet source surroundings (Just region)
1277 "I think I am parsing a type declaration, but I got stuck here:"
1281 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1282 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1288 DT_Alias typeAlias row col ->
1289 toTypeAliasReport source typeAlias row col
1291 DT_Union customType row col ->
1292 toCustomTypeReport source customType row col
1294 DT_IndentName row col ->
1296 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1297 region = toRegion row col
1299 Report.Report "EXPECTING TYPE NAME" region [] $
1300 Code.toSnippet source surroundings (Just region)
1303 "I think I am parsing a type declaration, but I got stuck here:"
1307 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1308 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1315toTypeAliasReport :: Code.Source -> TypeAlias -> Row -> Col -> Report.Report
1316toTypeAliasReport source typeAlias startRow startCol =
1318 AliasSpace space row col ->
1319 toSpaceReport source space row col
1321 AliasName row col ->
1323 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1324 region = toRegion row col
1326 Report.Report "EXPECTING TYPE ALIAS NAME" region [] $
1327 Code.toSnippet source surroundings (Just region)
1330 "I am partway through parsing a type alias, but I got stuck here:"
1334 ["I","was","expecting","a","name","like",D.dullyellow "Person","or",D.dullyellow "Point"
1335 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1341 AliasEquals row col ->
1342 case Code.whatIsNext source row col of
1343 Code.Keyword keyword ->
1345 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1346 region = toKeywordRegion row col keyword
1348 Report.Report "RESERVED WORD" region [] $
1349 Code.toSnippet source surroundings (Just region)
1352 "I ran into a reserved word unexpectedly while parsing this type alias:"
1356 "It looks like you are trying use `" ++ keyword
1357 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1364 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1365 region = toRegion row col
1367 Report.Report "PROBLEM IN TYPE ALIAS" region [] $
1368 Code.toSnippet source surroundings (Just region)
1371 "I am partway through parsing a type alias, but I got stuck here:"
1375 "I was expecting to see a type variable or an an equals sign next."
1380 AliasBody tipe row col ->
1381 toTypeReport source tipe row col
1383 AliasIndentEquals row col ->
1385 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1386 region = toRegion row col
1388 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1389 Code.toSnippet source surroundings (Just region)
1392 "I am partway through parsing a type alias, but I got stuck here:"
1396 "I was expecting to see a type variable or an an equals sign next."
1401 AliasIndentBody row col ->
1403 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1404 region = toRegion row col
1406 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1407 Code.toSnippet source surroundings (Just region)
1410 "I am partway through parsing a type alias, but I got stuck here:"
1414 ["I","was","expecting","to","see","a","type","next.","Something","as","simple"
1415 ,"as",D.dullyellow "Int","or",D.dullyellow "Float","would","work!"
1422typeAliasNote :: D.Doc
1426 "Here is an example of a valid `type alias` for reference:"
1428 [ D.indent 0 $ D.fillSep [D.cyan "type",D.cyan "alias",D.green "Person","="]
1429 , D.indent 2 $ D.vcat $
1437 "This would let us use `Person` as a shorthand for that record type. Using this\
1438 \ shorthand makes type annotations much easier to read, and makes changing code\
1439 \ easier if you decide later that there is more to a person than age and height!"
1443toCustomTypeReport :: Code.Source -> CustomType -> Row -> Col -> Report.Report
1444toCustomTypeReport source customType startRow startCol =
1446 CT_Space space row col ->
1447 toSpaceReport source space row col
1451 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1452 region = toRegion row col
1454 Report.Report "EXPECTING TYPE NAME" region [] $
1455 Code.toSnippet source surroundings (Just region)
1458 "I think I am parsing a type declaration, but I got stuck here:"
1462 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1463 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1469 CT_Equals row col ->
1470 case Code.whatIsNext source row col of
1471 Code.Keyword keyword ->
1473 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1474 region = toKeywordRegion row col keyword
1476 Report.Report "RESERVED WORD" region [] $
1477 Code.toSnippet source surroundings (Just region)
1480 "I ran into a reserved word unexpectedly while parsing this custom type:"
1484 "It looks like you are trying use `" ++ keyword
1485 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1492 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1493 region = toRegion row col
1495 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1496 Code.toSnippet source surroundings (Just region)
1499 "I am partway through parsing a custom type, but I got stuck here:"
1503 "I was expecting to see a type variable or an an equals sign next."
1510 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1511 region = toRegion row col
1513 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1514 Code.toSnippet source surroundings (Just region)
1517 "I am partway through parsing a custom type, but I got stuck here:"
1521 "I was expecting to see a vertical bar like | next."
1526 CT_Variant row col ->
1528 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1529 region = toRegion row col
1531 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1532 Code.toSnippet source surroundings (Just region)
1535 "I am partway through parsing a custom type, but I got stuck here:"
1539 ["I","was","expecting","to","see","a","variant","name","next."
1540 ,"Something","like",D.dullyellow "Success","or",D.dullyellow "Sandwich" <> "."
1541 ,"Any","name","that","starts","with","a","capital","letter","really!"
1547 CT_VariantArg tipe row col ->
1548 toTypeReport source tipe row col
1550 CT_IndentEquals row col ->
1552 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1553 region = toRegion row col
1555 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1556 Code.toSnippet source surroundings (Just region)
1559 "I am partway through parsing a custom type, but I got stuck here:"
1563 "I was expecting to see a type variable or an an equals sign next."
1568 CT_IndentBar row col ->
1570 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1571 region = toRegion row col
1573 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1574 Code.toSnippet source surroundings (Just region)
1577 "I am partway through parsing a custom type, but I got stuck here:"
1581 "I was expecting to see a vertical bar like | next."
1586 CT_IndentAfterBar row col ->
1588 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1589 region = toRegion row col
1591 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1592 Code.toSnippet source surroundings (Just region)
1595 "I am partway through parsing a custom type, but I got stuck here:"
1599 "I just saw a vertical bar, so I was expecting to see another variant defined next."
1604 CT_IndentAfterEquals row col ->
1606 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1607 region = toRegion row col
1609 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1610 Code.toSnippet source surroundings (Just region)
1613 "I am partway through parsing a custom type, but I got stuck here:"
1617 "I just saw an equals sign, so I was expecting to see the first variant defined next."
1623customTypeNote :: D.Doc
1627 "Here is an example of a valid `type` declaration for reference:"
1629 [ D.indent 0 $ D.fillSep [D.cyan "type",D.green "Status"]
1630 , D.indent 2 $ D.fillSep ["=","Failure"]
1631 , D.indent 2 $ D.fillSep ["|","Waiting"]
1632 , D.indent 2 $ D.fillSep ["|","Success","String"]
1635 "This defines a new `Status` type with three variants. This could be useful if\
1636 \ we are waiting for an HTTP request. Maybe we start with `Waiting` and then\
1637 \ switch to `Failure` or `Success \"message from server\"` depending on how\
1638 \ things go. Notice that the Success variant has some associated data, allowing\
1639 \ us to store a String if the request goes well!"
1647toDeclDefReport :: Code.Source -> Name.Name -> DeclDef -> Row -> Col -> Report.Report
1648toDeclDefReport source name declDef startRow startCol =
1650 DeclDefSpace space row col ->
1651 toSpaceReport source space row col
1653 DeclDefEquals row col ->
1654 case Code.whatIsNext source row col of
1655 Code.Keyword keyword ->
1657 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1658 region = toKeywordRegion row col keyword
1660 Report.Report "RESERVED WORD" region [] $
1661 Code.toSnippet source surroundings (Just region)
1664 "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
1667 "This is a reserved word! Try using some other name?"
1672 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1673 region = toKeywordRegion row col op
1675 Report.Report "UNEXPECTED SYMBOL" region [] $
1676 Code.toSnippet source surroundings (Just region)
1679 "I was not expecting to see this symbol here:"
1683 "I am not sure what is going wrong exactly, so here is a valid\
1684 \ definition (with an optional type annotation) for reference:"
1686 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1687 , D.fillSep [D.green "add","x","y","="]
1688 , D.fillSep [" ","x","+","y"]
1691 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
1697 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1698 region = toRegion row col
1700 Report.Report "PROBLEM IN DEFINITION" region [] $
1701 Code.toSnippet source surroundings (Just region)
1704 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
1708 "I am not sure what is going wrong exactly, so here is a valid\
1709 \ definition (with an optional type annotation) for reference:"
1711 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1712 , D.fillSep [D.green "add","x","y","="]
1713 , D.fillSep [" ","x","+","y"]
1716 "Try to use that format!"
1720 DeclDefType tipe row col ->
1721 toTypeReport source tipe row col
1723 DeclDefArg pattern row col ->
1724 toPatternReport source PArg pattern row col
1726 DeclDefBody expr row col ->
1727 toExprReport source (InDef name startRow startCol) expr row col
1729 DeclDefNameRepeat row col ->
1731 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1732 region = toRegion row col
1734 Report.Report "EXPECTING DEFINITION" region [] $
1735 Code.toSnippet source surroundings (Just region)
1738 "I just saw the type annotation for `" ++ Name.toChars name
1739 ++ "` so I was expecting to see its definition here:"
1743 "Type annotations always appear directly above the relevant\
1744 \ definition, without anything else (even doc comments) in between."
1749 DeclDefNameMatch defName row col ->
1751 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1752 region = toRegion row col
1754 Report.Report "NAME MISMATCH" region [] $
1755 Code.toSnippet source surroundings (Just region)
1758 "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
1762 "These names do not match! Is there a typo?"
1763 , D.indent 4 $ D.fillSep $
1764 [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
1768 DeclDefIndentType row col ->
1770 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1771 region = toRegion row col
1773 Report.Report "UNFINISHED DEFINITION" region [] $
1774 Code.toSnippet source surroundings (Just region)
1777 "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
1781 "I just saw a colon, so I am expecting to see a type next."
1786 DeclDefIndentEquals row col ->
1788 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1789 region = toRegion row col
1791 Report.Report "UNFINISHED DEFINITION" region [] $
1792 Code.toSnippet source surroundings (Just region)
1795 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
1799 "I was expecting to see an argument or an equals sign next."
1804 DeclDefIndentBody row col ->
1806 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1807 region = toRegion row col
1809 Report.Report "UNFINISHED DEFINITION" region [] $
1810 Code.toSnippet source surroundings (Just region)
1813 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
1817 "I was expecting to see an expression next. What is it equal to?"
1827 "Here is a valid definition (with a type annotation) for reference:"
1829 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1830 , D.fillSep [D.green "add","x","y","="]
1831 , D.fillSep [" ","x","+","y"]
1834 "The top line (called a \"type annotation\") is optional. You can leave it off\
1835 \ if you want. As you get more comfortable with Elm and as your project grows,\
1836 \ it becomes more and more valuable to add them though! They work great as\
1837 \ compiler-verified documentation, and they often improve error messages!"
1846 = InNode Node Row Col Context
1847 | InDef Name.Name Row Col
1863getDefName :: Context -> Name.Name
1866 InDef name _ _ -> name
1867 InNode _ _ _ c -> getDefName c
1870isWithin :: Node -> Context -> Bool
1871isWithin desiredNode context =
1873 InDef _ _ _ -> False
1874 InNode actualNode _ _ _ -> desiredNode == actualNode
1881toExprReport :: Code.Source -> Context -> Expr -> Row -> Col -> Report.Report
1882toExprReport source context expr startRow startCol =
1885 toLetReport source context let_ row col
1887 Case case_ row col ->
1888 toCaseReport source context case_ row col
1891 toIfReport source context if_ row col
1893 List list row col ->
1894 toListReport source context list row col
1896 Record record row col ->
1897 toRecordReport source context record row col
1899 Tuple tuple row col ->
1900 toTupleReport source context tuple row col
1902 Func func row col ->
1903 toFuncReport source context func row col
1906 let region = toRegion row col in
1907 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
1908 Code.toSnippet source region Nothing
1911 "I was expecting to see a record accessor here:"
1914 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
1915 ,"that","accesses","a","value","from","a","record."
1920 let region = toRegion row col in
1921 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
1922 Code.toSnippet source region Nothing
1925 "I am trying to parse a record accessor here:"
1930 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
1931 ,"that","accesses","a","value","from","a","record."
1935 "Record field names must start with a lower case letter!"
1939 OperatorRight op row col ->
1940 let region = toRegion row col in
1941 Report.Report "MISSING EXPRESSION" region [] $
1942 Code.toSnippet source region Nothing
1945 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
1949 ["You","can","just","put","anything","for","now,","like"
1950 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
1951 ,"Once","there","is","something","there,","I","can","probably"
1952 ,"give","a","more","specific","hint!"
1955 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
1956 \ Or if I run into operators in unexpected spots. Point is, there are a\
1957 \ couple ways I can get confused and give sort of weird advice!"
1961 OperatorReserved operator row col ->
1962 toOperatorReport source context operator row col
1966 (contextRow, contextCol, aThing) =
1968 InDef name r c -> (r, c, "the `" ++ Name.toChars name ++ "` definition")
1969 InNode NRecord r c _ -> (r, c, "a record")
1970 InNode NParens r c _ -> (r, c, "some parentheses")
1971 InNode NList r c _ -> (r, c, "a list")
1972 InNode NFunc r c _ -> (r, c, "an anonymous function")
1973 InNode NCond r c _ -> (r, c, "an `if` expression")
1974 InNode NThen r c _ -> (r, c, "an `if` expression")
1975 InNode NElse r c _ -> (r, c, "an `if` expression")
1976 InNode NCase r c _ -> (r, c, "a `case` expression")
1977 InNode NBranch r c _ -> (r, c, "a `case` expression")
1978 -- TODO check if this is actually the best way to get error messages here
1980 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
1981 region = toRegion row col
1983 Report.Report "MISSING EXPRESSION" region [] $
1984 Code.toSnippet source surroundings (Just region)
1987 "I am partway through parsing " ++ aThing ++ ", but I got stuck here:"
1991 ["I","was","expecting","to","see","an","expression","like"
1992 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
1993 ,"Once","there","is","something","there,","I","can","probably"
1994 ,"give","a","more","specific","hint!"
1997 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
1998 \ Or if I run into operators in unexpected spots. Point is, there are a\
1999 \ couple ways I can get confused and give sort of weird advice!"
2003 Char char row col ->
2004 toCharReport source char row col
2006 String string row col ->
2007 toStringReport source string row col
2009 Number number row col ->
2010 toNumberReport source number row col
2012 Space space row col ->
2013 toSpaceReport source space row col
2015 EndlessShader row col ->
2017 region = toWiderRegion row col 6
2019 Report.Report "ENDLESS SHADER" region [] $
2020 Code.toSnippet source region Nothing
2022 D.reflow "I cannot find the end of this shader:"
2024 D.reflow "Add a |] somewhere after this to end the shader."
2027 ShaderProblem problem row col ->
2029 region = toRegion row col
2031 Report.Report "SHADER PROBLEM" region [] $
2032 Code.toSnippet source region Nothing
2035 "I ran into a problem while parsing this GLSL block."
2039 "I use a 3rd party GLSL parser for now, and I did my best to extract their error message:"
2040 , D.indent 4 $ D.vcat $
2041 map D.fromChars (filter (/="") (lines problem))
2045 IndentOperatorRight op row col ->
2047 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2048 region = toRegion row col
2050 Report.Report "MISSING EXPRESSION" region [] $
2051 Code.toSnippet source surroundings (Just region)
2054 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
2059 ["You","can","just","put","anything","for","now,","like"
2060 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2061 ,"Once","there","is","something","there,","I","can","probably"
2062 ,"give","a","more","specific","hint!"
2066 "I may be getting confused by your indentation? The easiest way to make sure\
2067 \ this is not an indentation problem is to put the expression on the right of\
2068 \ the " ++ Name.toChars op ++ " operator on the same line."
2072 IndentMoreExpr _ _ ->
2073 error "TODO IndentMoreExpr"
2080toCharReport :: Code.Source -> Char -> Row -> Col -> Report.Report
2081toCharReport source char row col =
2085 region = toRegion row col
2087 Report.Report "MISSING SINGLE QUOTE" region [] $
2088 Code.toSnippet source region Nothing
2091 "I thought I was parsing a character, but I got to the end of\
2092 \ the line without seeing the closing single quote:"
2095 "Add a closing single quote here!"
2098 CharEscape escape ->
2099 toEscapeReport source escape row col
2101 CharNotString width ->
2103 region = toWiderRegion row col width
2105 Report.Report "NEEDS DOUBLE QUOTES" region [] $
2106 Code.toSnippet source region Nothing
2108 "The following string uses single quotes:"
2111 [ "Please switch to double quotes instead:"
2113 D.dullyellow "'this'" <> " => " <> D.green "\"this\""
2115 "Elm uses double quotes for strings like \"hello\", whereas it uses single\
2116 \ quotes for individual characters like 'a' and 'ø'. This distinction helps with\
2117 \ code like (String.any (\\c -> c == 'X') \"90210\") where you are inspecting\
2118 \ individual characters."
2127toStringReport :: Code.Source -> String -> Row -> Col -> Report.Report
2128toStringReport source string row col =
2130 StringEndless_Single ->
2132 region = toRegion row col
2134 Report.Report "ENDLESS STRING" region [] $
2135 Code.toSnippet source region Nothing
2138 "I got to the end of the line without seeing the closing double quote:"
2142 ["Strings","look","like",D.green "\"this\"","with","double"
2143 ,"quotes","on","each","end.","Is","the","closing","double"
2144 ,"quote","missing","in","your","code?"
2146 , D.link "Note" "Read" "TODO" "if you want a string that spans multiple lines."
2150 StringEndless_Multi ->
2152 region = toWiderRegion row col 3
2154 Report.Report "ENDLESS STRING" region [] $
2155 Code.toSnippet source region Nothing
2158 "I cannot find the end of this multi-line string:"
2161 [ D.reflow "Add a \"\"\" somewhere after this to end the string."
2162 , D.link "Hint" "Read" "TODO" "for more information on strings and multi-line strings."
2166 StringEscape escape ->
2167 toEscapeReport source escape row col
2174toEscapeReport :: Code.Source -> Escape -> Row -> Col -> Report.Report
2175toEscapeReport source escape row col =
2179 region = toWiderRegion row col 2
2181 Report.Report "UNKNOWN ESCAPE" region [] $
2182 Code.toSnippet source region Nothing
2185 "Backslashes always start escaped characters, but I do not recognize this one:"
2189 "Valid escape characters include:"
2190 , D.dullyellow $ D.indent 4 $ D.vcat $
2200 "Do you want one of those instead? Maybe you need \\\\ to escape a backslash?"
2202 "The last style lets encode ANY character by its Unicode code\
2203 \ point. That means \\u{0009} and \\t are the same. You can use\
2204 \ that style for anything not covered by the other six escapes!"
2208 BadUnicodeFormat width ->
2210 region = toWiderRegion row col width
2212 Report.Report "BAD UNICODE ESCAPE" region [] $
2213 Code.toSnippet source region Nothing
2216 "I ran into an invalid Unicode escape:"
2220 "Here are some examples of valid Unicode escapes:"
2221 , D.dullyellow $ D.indent 4 $ D.vcat $
2228 "Notice that the code point is always surrounded by curly braces.\
2229 \ Maybe you are missing the opening or closing curly brace?"
2232 BadUnicodeCode width ->
2234 region = toWiderRegion row col width
2236 Report.Report "BAD UNICODE ESCAPE" region [] $
2237 Code.toSnippet source region Nothing
2240 "This is not a valid code point:"
2243 "The valid code points are between 0 and 10FFFF inclusive."
2246 BadUnicodeLength width numDigits badCode ->
2248 region = toWiderRegion row col width
2250 Report.Report "BAD UNICODE ESCAPE" region [] $
2251 Code.toSnippet source region Nothing $
2252 if numDigits < 4 then
2255 "Every code point needs at least four digits:"
2258 goodCode = replicate (4 - numDigits) '0' ++ map Char.toUpper (showHex badCode "")
2259 suggestion = "\\u{" <> D.fromChars goodCode <> "}"
2261 D.fillSep ["Try",D.green suggestion,"instead?"]
2267 "This code point has too many digits:"
2270 ["Valid","code","points","are","between"
2271 ,D.green "\\u{0000}","and",D.green "\\u{10FFFF}" <> ","
2272 ,"so","try","trimming","any","leading","zeros","until"
2273 ,"you","have","between","four","and","six","digits."
2282toNumberReport :: Code.Source -> Number -> Row -> Col -> Report.Report
2283toNumberReport source number row col =
2285 region = toRegion row col
2289 Report.Report "WEIRD NUMBER" region [] $
2290 Code.toSnippet source region Nothing
2293 "I thought I was reading a number, but I ran into some weird stuff here:"
2297 "I recognize numbers in the following formats:"
2298 , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
2300 "So is there a way to write it like one of those?"
2305 Report.Report "WEIRD NUMBER" region [] $
2306 Code.toSnippet source region Nothing
2309 "Numbers cannot end with a dot like this:"
2312 ["Switching","to",D.green (D.fromChars (show int))
2313 ,"or",D.green (D.fromChars (show int ++ ".0"))
2314 ,"will","work","though!"
2319 Report.Report "WEIRD HEXIDECIMAL" region [] $
2320 Code.toSnippet source region Nothing
2323 "I thought I was reading a hexidecimal number until I got here:"
2327 "Valid hexidecimal digits include 0123456789abcdefABCDEF, so I can\
2328 \ only recognize things like this:"
2329 , D.indent 4 $ D.vcat [ "0x2B", "0x002B", "0x00ffb3" ]
2333 NumberNoLeadingZero ->
2334 Report.Report "LEADING ZEROS" region [] $
2335 Code.toSnippet source region Nothing
2338 "I do not accept numbers with leading zeros:"
2342 "Just delete the leading zeros and it should work!"
2344 "Some languages let you to specify octal numbers by adding a leading zero.\
2345 \ So in C, writing 0111 is the same as writing 73. Some people are used to\
2346 \ that, but others probably want it to equal 111. Either path is going to\
2347 \ surprise people from certain backgrounds, so Elm tries to avoid this whole\
2357toOperatorReport :: Code.Source -> Context -> BadOperator -> Row -> Col -> Report.Report
2358toOperatorReport source context operator row col =
2362 region = toRegion row col
2364 Report.Report "UNEXPECTED SYMBOL" region [] $
2365 Code.toSnippet source region Nothing
2367 "I was not expecting this dot:"
2370 "Dots are for record access and decimal points, so\
2371 \ they cannot float around on their own. Maybe\
2372 \ there is some extra whitespace?"
2377 region = toRegion row col
2379 Report.Report "UNEXPECTED SYMBOL" region [] $
2380 Code.toSnippet source region Nothing
2383 "I was not expecting this vertical bar:"
2386 "Vertical bars should only appear in custom type declarations. Maybe you want || instead?"
2391 region = toWiderRegion row col 2
2393 Report.Report "UNEXPECTED ARROW" region [] $
2394 Code.toSnippet source region Nothing $
2395 if isWithin NBranch context then
2398 "I was not expecting to see an arrow here:"
2403 "I think I am parsing a `case` expression right now, so it makes sense to\
2404 \ see arrows around here. This one is confusing me though!"
2407 "I may be getting confused by the indentation. Every pattern in a `case` must be\
2408 \ exactly aligned (with exactly the same amount of indentation) so maybe this arrow\
2409 \ appears after the next `case` pattern (correctly!) but the pattern itself is\
2410 \ indented too far?"
2417 "I was not expecting an arrow here:"
2420 "Arrows should only appear in `case` expressions and anonymous functions. Maybe\
2421 \ you want > or >= instead?"
2426 region = toRegion row col
2427 name = getDefName context
2429 Report.Report "UNEXPECTED EQUALS" region [] $
2430 Code.toSnippet source region Nothing
2433 "I was not expecting to see this equals sign:"
2437 D.reflow "Maybe you want == instead? To check if two values are equal?"
2440 if isWithin NRecord context then
2441 "Records look like { x = 3, y = 4 } with the equals sign right\
2442 \ after the field name. So maybe you forgot a comma?"
2444 "I may be getting confused by your indentation. Is this supposed to be part of\
2445 \ a definition AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2446 \ the problem may be a bit before the equals sign. I need all definitions to\
2447 \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2448 \ this new definition is indented a bit too much."
2454 region = toRegion row col
2455 name = getDefName context
2457 Report.Report "UNEXPECTED SYMBOL" region [] $
2458 Code.toSnippet source region Nothing $
2461 "I was not expecting to run into the \"has type\" symbol here:"
2466 ["Maybe","you","want",D.green "::","instead?"
2467 ,"To","put","something","on","the","front","of","a","list?"
2470 "The single colon is reserved for type annotations and record types, but I think\
2471 \ I am parsing the definition of `" ++ Name.toChars name ++ "` right now."
2474 "I may be getting confused by your indentation. Is this supposed to be part of\
2475 \ a type annotation AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2476 \ the problem may be a bit before the \"has type\" symbol. I need all definitions to\
2477 \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2478 \ this new definition is indented a bit too much."
2487toLetReport :: Code.Source -> Context -> Let -> Row -> Col -> Report.Report
2488toLetReport source context let_ startRow startCol =
2490 LetSpace space row col ->
2491 toSpaceReport source space row col
2495 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2496 region = toRegion row col
2498 Report.Report "LET PROBLEM" region [] $
2499 Code.toSnippet source surroundings (Just region)
2502 "I was partway through parsing a `let` expression, but I got stuck here:"
2506 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2507 ,"keyword","next.","Is","there","a","typo?"
2510 "This can also happen if you are trying to define another value within the `let` but\
2511 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2512 \ spaces before it. They should line up exactly!"
2516 LetDefAlignment _ row col ->
2518 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2519 region = toRegion row col
2521 Report.Report "LET PROBLEM" region [] $
2522 Code.toSnippet source surroundings (Just region)
2525 "I was partway through parsing a `let` expression, but I got stuck here:"
2529 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2530 ,"keyword","next.","Is","there","a","typo?"
2533 "This can also happen if you are trying to define another value within the `let` but\
2534 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2535 \ spaces before it. The should line up exactly!"
2539 LetDefName row col ->
2540 case Code.whatIsNext source row col of
2541 Code.Keyword keyword ->
2543 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2544 region = toKeywordRegion row col keyword
2546 Report.Report "RESERVED WORD" region [] $
2547 Code.toSnippet source surroundings (Just region)
2550 "I was partway through parsing a `let` expression, but I got stuck here:"
2553 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but\
2554 \ it is a reserved word! Try using a different name instead."
2558 toUnfinishLetReport source row col startRow startCol $
2560 "I was expecting the name of a definition next."
2562 LetDef name def row col ->
2563 error "TODO LetDef" name def row col
2565 LetDestruct destruct row col ->
2566 error "TODO LetDestruct" destruct row col
2568 LetBody expr row col ->
2569 toExprReport source context expr row col
2571 LetIndentDef row col ->
2572 toUnfinishLetReport source row col startRow startCol $
2574 "I was expecting a value to be defined here."
2576 LetIndentIn row col ->
2577 toUnfinishLetReport source row col startRow startCol $
2579 ["I","was","expecting","to","see","the",D.cyan "in","keyword","next."
2580 ,"Or","maybe","more","of","that","expression?"
2583 LetIndentBody row col ->
2584 toUnfinishLetReport source row col startRow startCol $
2586 "I was expecting an expression next. Tell me what should happen with the value you just defined!"
2589toUnfinishLetReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
2590toUnfinishLetReport source row col startRow startCol message =
2592 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2593 region = toRegion row col
2595 Report.Report "UNFINISHED LET" region [] $
2596 Code.toSnippet source surroundings (Just region)
2599 "I was partway through parsing a `let` expression, but I got stuck here:"
2604 "Here is an example with a valid `let` expression for reference:"
2606 [ D.indent 0 $ D.fillSep [D.green "viewPerson","person","="]
2607 , D.indent 2 $ D.fillSep [D.cyan "let"]
2608 , D.indent 4 $ D.fillSep ["fullName","="]
2609 , D.indent 6 $ D.fillSep ["person.firstName","++",D.dullyellow "\" \"","++","person.lastName"]
2610 , D.indent 2 $ D.fillSep [D.cyan "in"]
2611 , D.indent 2 $ D.fillSep ["div","[]","[","text","fullName","]"]
2614 "Here we defined a `viewPerson` function that turns a person into some HTML. We use\
2615 \ a `let` expression to define the `fullName` we want to show. Notice the indentation! The\
2616 \ `fullName` is indented more than the `let` keyword, and the actual value of `fullName` is\
2617 \ indented a bit more than that. That is important!"
2626toCaseReport :: Code.Source -> Context -> Case -> Row -> Col -> Report.Report
2627toCaseReport source context case_ startRow startCol =
2629 CaseSpace space row col ->
2630 toSpaceReport source space row col
2633 toUnfinishCaseReport source row col startRow startCol $
2634 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
2636 CasePattern pattern row col ->
2637 toPatternReport source PCase pattern row col
2639 CaseArrow row col ->
2640 case Code.whatIsNext source row col of
2641 Code.Keyword keyword ->
2643 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2644 region = toKeywordRegion row col keyword
2646 Report.Report "RESERVED WORD" region [] $
2647 Code.toSnippet source surroundings (Just region)
2650 "I am partway through parsing a `case` expression, but I got stuck here:"
2653 "It looks like you are trying to use `" ++ keyword ++ "` in one of your\
2654 \ patterns, but it is a reserved word. Try using a different name?"
2657 Code.Operator ":" ->
2659 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2660 region = toRegion row col
2662 Report.Report "UNEXPECTED OPERATOR" region [] $
2663 Code.toSnippet source surroundings (Just region)
2666 "I am partway through parsing a `case` expression, but I got stuck here:"
2669 ["I","am","seeing",D.dullyellow ":","but","maybe","you","want",D.green "::","instead?"
2670 ,"For","pattern","matching","on","lists?"
2674 Code.Operator "=" ->
2676 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2677 region = toRegion row col
2679 Report.Report "UNEXPECTED OPERATOR" region [] $
2680 Code.toSnippet source surroundings (Just region)
2683 "I am partway through parsing a `case` expression, but I got stuck here:"
2686 ["I","am","seeing",D.dullyellow "=","but","maybe","you","want",D.green "->","instead?"
2692 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2693 region = toRegion row col
2695 Report.Report "MISSING ARROW" region [] $
2696 Code.toSnippet source surroundings (Just region)
2699 "I am partway through parsing a `case` expression, but I got stuck here:"
2701 addNoteForCaseIndentError $
2703 "I was expecting to see an arrow next."
2706 CaseExpr expr row col ->
2707 toExprReport source (InNode NCase startRow startCol context) expr row col
2709 CaseBranch expr row col ->
2710 toExprReport source (InNode NBranch startRow startCol context) expr row col
2712 CaseIndentOf row col ->
2713 toUnfinishCaseReport source row col startRow startCol $
2714 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
2716 CaseIndentExpr row col ->
2717 toUnfinishCaseReport source row col startRow startCol $
2718 D.reflow "I was expecting to see a expression next."
2720 CaseIndentPattern row col ->
2721 toUnfinishCaseReport source row col startRow startCol $
2722 D.reflow "I was expecting to see a pattern next."
2724 CaseIndentArrow row col ->
2725 toUnfinishCaseReport source row col startRow startCol $
2727 ["I","just","saw","a","pattern,","so","I","was","expecting"
2728 ,"to","see","a",D.dullyellow "->","next."
2731 CaseIndentBranch row col ->
2732 toUnfinishCaseReport source row col startRow startCol $
2734 "I was expecting to see an expression next. What should I do when\
2735 \ I run into this particular pattern?"
2737 CasePatternAlignment indent row col ->
2738 toUnfinishCaseReport source row col startRow startCol $
2740 "I suspect this is a pattern that is not indented far enough? (" ++ show indent ++ " spaces)"
2743toUnfinishCaseReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
2744toUnfinishCaseReport source row col startRow startCol message =
2746 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2747 region = toRegion row col
2749 Report.Report "UNFINISHED CASE" region [] $
2750 Code.toSnippet source surroundings (Just region)
2753 "I was partway through parsing a `case` expression, but I got stuck here:"
2758 "Here is an example of a valid `case` expression for reference."
2760 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
2761 , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
2762 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
2764 , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
2765 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
2768 "Notice the indentation. Each pattern is aligned, and each branch is indented\
2769 \ a bit more than the corresponding pattern. That is important!"
2774addNoteForCaseIndentError :: D.Doc -> D.Doc
2775addNoteForCaseIndentError message =
2780 "Sometimes I get confused by indentation, so try to make your `case` look\
2781 \ something like this:"
2783 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
2784 , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
2785 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
2787 , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
2788 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
2791 "Notice the indentation! Each pattern is aligned, and each branch is indented\
2792 \ a bit more than the corresponding pattern. That is important!"
2800toIfReport :: Code.Source -> Context -> If -> Row -> Col -> Report.Report
2801toIfReport source context if_ startRow startCol =
2803 IfSpace space row col ->
2804 toSpaceReport source space row col
2808 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2809 region = toRegion row col
2811 Report.Report "UNFINISHED IF" region [] $
2812 Code.toSnippet source surroundings (Just region)
2815 "I was expecting to see more of this `if` expression, but I got stuck here:"
2818 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
2824 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2825 region = toRegion row col
2827 Report.Report "UNFINISHED IF" region [] $
2828 Code.toSnippet source surroundings (Just region)
2831 "I was expecting to see more of this `if` expression, but I got stuck here:"
2834 ["I","was","expecting","to","see","the",D.cyan "else","keyword","next."
2838 IfElseBranchStart row col ->
2840 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2841 region = toRegion row col
2843 Report.Report "UNFINISHED IF" region [] $
2844 Code.toSnippet source surroundings (Just region)
2847 "I just saw the start of an `else` branch, but then I got stuck here:"
2850 "I was expecting to see an expression next. Maybe it is not filled in yet?"
2853 IfCondition expr row col ->
2854 toExprReport source (InNode NCond startRow startCol context) expr row col
2856 IfThenBranch expr row col ->
2857 toExprReport source (InNode NThen startRow startCol context) expr row col
2859 IfElseBranch expr row col ->
2860 toExprReport source (InNode NElse startRow startCol context) expr row col
2862 IfIndentCondition row col ->
2864 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2865 region = toRegion row col
2867 Report.Report "UNFINISHED IF" region [] $
2868 Code.toSnippet source surroundings (Just region)
2871 "I was expecting to see more of this `if` expression, but I got stuck here:"
2875 ["I","was","expecting","to","see","an","expression","like",D.dullyellow "x < 0"
2876 ,"that","evaluates","to","True","or","False."
2879 "I can be confused by indentation. Maybe something is not indented enough?"
2883 IfIndentThen row col ->
2885 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2886 region = toRegion row col
2888 Report.Report "UNFINISHED IF" region [] $
2889 Code.toSnippet source surroundings (Just region)
2892 "I was expecting to see more of this `if` expression, but I got stuck here:"
2896 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
2899 "I can be confused by indentation. Maybe something is not indented enough?"
2903 IfIndentThenBranch row col ->
2905 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2906 region = toRegion row col
2908 Report.Report "UNFINISHED IF" region [] $
2909 Code.toSnippet source surroundings (Just region)
2912 "I got stuck after the start of this `then` branch:"
2916 "I was expecting to see an expression next. Maybe it is not filled in yet?"
2918 "I can be confused by indentation, so if the `then` branch is already\
2919 \ present, it may not be indented enough for me to recognize it."
2923 IfIndentElseBranch row col ->
2925 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2926 region = toRegion row col
2928 Report.Report "UNFINISHED IF" region [] $
2929 Code.toSnippet source surroundings (Just region)
2932 "I got stuck after the start of this `else` branch:"
2936 "I was expecting to see an expression next. Maybe it is not filled in yet?"
2938 "I can be confused by indentation, so if the `else` branch is already\
2939 \ present, it may not be indented enough for me to recognize it."
2943 IfIndentElse row col ->
2945 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2946 region = toRegion row col
2948 Report.Report "UNFINISHED IF" region [] $
2949 Code.toSnippet source surroundings (Just region)
2952 "I was expecting to see an `else` branch after this:"
2956 "I know what to do when the condition is `True`, but not when it is `False`."
2958 ["Add","an",D.cyan "else","branch","to","handle","that","scenario!"]
2967toRecordReport :: Code.Source -> Context -> Record -> Row -> Col -> Report.Report
2968toRecordReport source context record startRow startCol =
2970 RecordOpen row col ->
2971 case Code.whatIsNext source row col of
2972 Code.Keyword keyword ->
2974 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2975 region = toKeywordRegion row col keyword
2977 Report.Report "RESERVED WORD" region [] $
2978 Code.toSnippet source surroundings (Just region)
2981 "I just started parsing a record, but I got stuck on this field name:"
2984 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
2985 \ that is a reserved word. Try using a different name!"
2990 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2991 region = toRegion row col
2993 Report.Report "UNFINISHED RECORD" region [] $
2994 Code.toSnippet source surroundings (Just region)
2997 "I just started parsing a record, but I got stuck here:"
3000 ["Records","look","like",D.dullyellow "{ x = 3, y = 4 },"
3001 ,"so","I","was","expecting","to","see","a","field","name","next."
3005 RecordEnd row col ->
3007 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3008 region = toRegion row col
3010 Report.Report "UNFINISHED RECORD" region [] $
3011 Code.toSnippet source surroundings (Just region)
3014 "I am partway through parsing a record, but I got stuck here:"
3018 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
3019 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
3022 "When I get stuck like this, it usually means that there is a missing parenthesis\
3023 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3027 RecordField row col ->
3028 case Code.whatIsNext source row col of
3029 Code.Keyword keyword ->
3031 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3032 region = toKeywordRegion row col keyword
3034 Report.Report "RESERVED WORD" region [] $
3035 Code.toSnippet source surroundings (Just region)
3038 "I am partway through parsing a record, but I got stuck on this field name:"
3041 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3042 \ that is a reserved word. Try using a different name!"
3047 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3048 region = toRegion row col
3050 Report.Report "UNFINISHED RECORD" region [] $
3051 Code.toSnippet source surroundings (Just region)
3054 "I am partway through parsing a record, but I got stuck after that last comma:"
3056 addNoteForRecordError $
3058 "Trailing commas are not allowed in records, so I was expecting to see another\
3059 \ record field defined next. If you are done defining the record, the fix may\
3060 \ be to delete that last comma?"
3063 RecordEquals row col ->
3065 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3066 region = toRegion row col
3068 Report.Report "UNFINISHED RECORD" region [] $
3069 Code.toSnippet source surroundings (Just region)
3072 "I am partway through parsing a record, but I got stuck here:"
3074 addNoteForRecordError $
3076 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
3077 ,"an","equals","sign","next.","So","try","putting","an",D.green "=","sign","here?"
3081 RecordExpr expr row col ->
3082 toExprReport source (InNode NRecord startRow startCol context) expr row col
3084 RecordSpace space row col ->
3085 toSpaceReport source space row col
3087 RecordIndentOpen row col ->
3089 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3090 region = toRegion row col
3092 Report.Report "UNFINISHED RECORD" region [] $
3093 Code.toSnippet source surroundings (Just region)
3096 "I just saw the opening curly brace of a record, but then I got stuck here:"
3098 addNoteForRecordIndentError $
3100 ["I","am","expecting","a","record","like",D.dullyellow "{ x = 3, y = 4 }","here."
3101 ,"Try","defining","some","fields","of","your","own?"
3105 RecordIndentEnd row col ->
3107 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3108 region = toRegion row col
3110 Report.Report "UNFINISHED RECORD" region [] $
3111 Code.toSnippet source surroundings (Just region)
3114 "I was expecting to see a closing curly brace next:"
3116 addNoteForRecordIndentError $
3118 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
3122 RecordIndentField row col ->
3124 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3125 region = toRegion row col
3127 Report.Report "UNFINISHED RECORD" region [] $
3128 Code.toSnippet source surroundings (Just region)
3131 "I am partway through parsing a record, but I got stuck after that last comma:"
3133 addNoteForRecordError $
3135 "Trailing commas are not allowed in records, so the fix may be to\
3136 \ delete that last comma? Or maybe you were in the middle of defining\
3137 \ an additional field?"
3140 RecordIndentEquals row col ->
3142 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3143 region = toRegion row col
3145 Report.Report "UNFINISHED RECORD" region [] $
3146 Code.toSnippet source surroundings (Just region)
3149 "I am partway through parsing a record. I just saw a record\
3150 \ field, so I was expecting to see an equals sign next:"
3152 addNoteForRecordIndentError $
3154 ["Try","putting","an",D.green "=","followed","by","an","expression?"
3158 RecordIndentExpr row col ->
3160 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3161 region = toRegion row col
3163 Report.Report "UNFINISHED RECORD" region [] $
3164 Code.toSnippet source surroundings (Just region)
3167 "I am partway through parsing a record, and I was expecting to run into an expression next:"
3169 addNoteForRecordIndentError $
3171 ["Try","putting","something,","like"
3172 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
3177addNoteForRecordError :: D.Doc -> D.Doc
3178addNoteForRecordError normalRecommendation =
3180 normalRecommendation
3183 "If you are trying to define a record across multiple lines, I recommend using this format:"
3184 , D.indent 4 $ D.vcat $
3185 [ "{ name = \"Alice\""
3191 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3192 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3193 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3197addNoteForRecordIndentError :: D.Doc -> D.Doc
3198addNoteForRecordIndentError normalRecommendation =
3200 normalRecommendation
3203 "I may be confused by indentation. For example, if you are trying to define\
3204 \ a record across multiple lines, I recommend using this format:"
3205 , D.indent 4 $ D.vcat $
3206 [ "{ name = \"Alice\""
3212 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3213 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3214 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3222toTupleReport :: Code.Source -> Context -> Tuple -> Row -> Col -> Report.Report
3223toTupleReport source context tuple startRow startCol =
3225 TupleExpr expr row col ->
3226 toExprReport source (InNode NParens startRow startCol context) expr row col
3228 TupleSpace space row col ->
3229 toSpaceReport source space row col
3233 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3234 region = toRegion row col
3236 Report.Report "UNFINISHED PARENTHESES" region [] $
3237 Code.toSnippet source surroundings (Just region)
3240 "I was expecting to see a closing parentheses next, but I got stuck here:"
3243 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
3245 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
3246 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
3247 \ or missing brackets) that is confusing me."
3251 TupleOperatorClose row col ->
3253 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3254 region = toRegion row col
3256 Report.Report "UNFINISHED OPERATOR FUNCTION" region [] $
3257 Code.toSnippet source surroundings (Just region)
3259 D.reflow "I was expecting a closing parenthesis here:"
3262 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
3264 "I think I am parsing an operator function right now, so I am expecting to see\
3265 \ something like (+) or (&&) where an operator is surrounded by parentheses with\
3270 TupleOperatorReserved operator row col ->
3272 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3273 region = toRegion row col
3275 Report.Report "UNEXPECTED SYMBOL" region [] $
3276 Code.toSnippet source surroundings (Just region)
3279 "I ran into an unexpected symbol here:"
3283 BadDot -> ["Maybe","you","wanted","a","record","accessor","like",D.dullyellow ".x","or",D.dullyellow ".name","instead?"]
3284 BadPipe -> ["Try",D.dullyellow "(||)","instead?","To","turn","boolean","OR","into","a","function?"]
3285 BadArrow -> ["Maybe","you","wanted",D.dullyellow "(>)","or",D.dullyellow "(>=)","instead?"]
3286 BadEquals -> ["Try",D.dullyellow "(==)","instead?","To","make","a","function","that","checks","equality?"]
3287 BadHasType -> ["Try",D.dullyellow "(::)","instead?","To","add","values","to","the","front","of","lists?"]
3290 TupleIndentExpr1 row col ->
3292 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3293 region = toRegion row col
3295 Report.Report "UNFINISHED PARENTHESES" region [] $
3296 Code.toSnippet source surroundings (Just region)
3299 "I just saw an open parenthesis, so I was expecting to see an expression next."
3303 ["Something","like",D.dullyellow "(4 + 5)","or"
3304 ,D.dullyellow "(String.reverse \"desserts\")" <> "."
3305 ,"Anything","where","you","are","putting","parentheses","around","normal","expressions."
3308 "I can get confused by indentation in cases like this, so\
3309 \ maybe you have an expression but it is not indented enough?"
3313 TupleIndentExprN row col ->
3315 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3316 region = toRegion row col
3318 Report.Report "UNFINISHED TUPLE" region [] $
3319 Code.toSnippet source surroundings (Just region)
3322 "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."
3326 ["A","tuple","looks","like",D.dullyellow "(3,4)","or"
3327 ,D.dullyellow "(\"Tom\",42)" <> ","
3328 ,"so","I","think","there","is","an","expression","missing","here?"
3331 "I can get confused by indentation in cases like this, so\
3332 \ maybe you have an expression but it is not indented enough?"
3336 TupleIndentEnd row col ->
3338 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3339 region = toRegion row col
3341 Report.Report "UNFINISHED PARENTHESES" region [] $
3342 Code.toSnippet source surroundings (Just region)
3345 "I was expecting to see a closing parenthesis next:"
3348 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
3350 "I can get confused by indentation in cases like this, so\
3351 \ maybe you have a closing parenthesis but it is not indented enough?"
3356toListReport :: Code.Source -> Context -> List -> Row -> Col -> Report.Report
3357toListReport source context list startRow startCol =
3359 ListSpace space row col ->
3360 toSpaceReport source space row col
3364 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3365 region = toRegion row col
3367 Report.Report "UNFINISHED LIST" region [] $
3368 Code.toSnippet source surroundings (Just region)
3371 "I am partway through parsing a list, but I got stuck here:"
3375 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
3376 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
3379 "When I get stuck like this, it usually means that there is a missing parenthesis\
3380 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3384 ListExpr expr row col ->
3388 surroundings = A.Region (A.Position startRow startCol) (A.Position r c)
3389 region = toRegion r c
3391 Report.Report "UNFINISHED LIST" region [] $
3392 Code.toSnippet source surroundings (Just region)
3395 "I was expecting to see another list entry after that last comma:"
3399 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
3401 "I recommend using the following format for lists that span multiple lines:"
3402 , D.indent 4 $ D.vcat $
3409 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
3410 \ This style can be jarring for people coming from C-like syntax, but folks generally\
3411 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3416 toExprReport source (InNode NList startRow startCol context) expr row col
3420 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3421 region = toRegion row col
3423 Report.Report "UNFINISHED LIST" region [] $
3424 Code.toSnippet source surroundings (Just region)
3427 "I am partway through parsing a list, but I got stuck here:"
3431 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
3432 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
3435 "When I get stuck like this, it usually means that there is a missing parenthesis\
3436 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3440 ListIndentOpen row col ->
3442 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3443 region = toRegion row col
3445 Report.Report "UNFINISHED LIST" region [] $
3446 Code.toSnippet source surroundings (Just region)
3449 "I cannot find the end of this list:"
3453 ["You","could","change","it","to","something","like"
3454 ,D.dullyellow "[3,4,5]"
3456 ,D.dullyellow "[]" <> "."
3457 ,"Anything","where","there","is","an","open","and","close","square","brace,"
3458 ,"and","where","the","elements","of","the","list","are","separated","by","commas."
3461 "I may be confused by indentation. For example, if you are trying to define\
3462 \ a list across multiple lines, I recommend using this format:"
3463 , D.indent 4 $ D.vcat $
3470 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
3471 \ This style can be jarring for people coming from C-like syntax, but folks generally\
3472 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3476 ListIndentEnd row col ->
3478 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3479 region = toRegion row col
3481 Report.Report "UNFINISHED LIST" region [] $
3482 Code.toSnippet source surroundings (Just region)
3485 "I cannot find the end of this list:"
3489 ["You","can","just","add","a","closing",D.dullyellow "]"
3490 ,"right","here,","and","I","will","be","all","set!"
3493 "I may be confused by indentation. For example, if you are trying to define\
3494 \ a list across multiple lines, I recommend using this format:"
3495 , D.indent 4 $ D.vcat $
3502 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
3503 \ This style can be jarring for people coming from C-like syntax, but folks generally\
3504 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3508 ListIndentExpr row col ->
3510 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3511 region = toRegion row col
3513 Report.Report "UNFINISHED LIST" region [] $
3514 Code.toSnippet source surroundings (Just region)
3517 "I was expecting to see another list entry after this comma:"
3521 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
3523 "I recommend using the following format for lists that span multiple lines:"
3524 , D.indent 4 $ D.vcat $
3531 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
3532 \ This style can be jarring for people coming from C-like syntax, but folks generally\
3533 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3538toFuncReport :: Code.Source -> Context -> Func -> Row -> Col -> Report.Report
3539toFuncReport source context func startRow startCol =
3541 FuncSpace space row col ->
3542 toSpaceReport source space row col
3544 FuncArg pattern row col ->
3545 toPatternReport source PArg pattern row col
3547 FuncBody expr row col ->
3548 toExprReport source (InNode NFunc startRow startCol context) expr row col
3550 FuncArrow row col ->
3551 case Code.whatIsNext source row col of
3552 Code.Keyword keyword ->
3554 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3555 region = toKeywordRegion row col keyword
3557 Report.Report "RESERVED WORD" region [] $
3558 Code.toSnippet source surroundings (Just region)
3561 "I was parsing an anonymous function, but I got stuck here:"
3564 "It looks like you are trying to use `" ++ keyword ++ "` as an argument, but\
3565 \ it is a reserved word in this language. Try using a different argument name!"
3570 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3571 region = toRegion row col
3573 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
3574 Code.toSnippet source surroundings (Just region)
3577 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
3580 ["The","syntax","for","anonymous","functions","is"
3581 ,D.dullyellow "(\\x -> x + 1)"
3582 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
3586 FuncIndentArg row col ->
3588 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3589 region = toRegion row col
3591 Report.Report "MISSING ARGUMENT" region [] $
3592 Code.toSnippet source surroundings (Just region)
3595 "I just saw the beginning of an anonymous function, so I was expecting to see an argument next:"
3599 ["Something","like",D.dullyellow"x","or",D.dullyellow "name" <> "."
3600 ,"Anything","that","starts","with","a","lower","case","letter!"
3603 "The syntax for anonymous functions is (\\x -> x + 1) where the backslash\
3604 \ is meant to look a bit like a lambda if you squint. This visual pun seemed\
3605 \ like a better idea at the time!"
3609 FuncIndentArrow row col ->
3611 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3612 region = toRegion row col
3614 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
3615 Code.toSnippet source surroundings (Just region)
3618 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
3622 ["The","syntax","for","anonymous","functions","is"
3623 ,D.dullyellow "(\\x -> x + 1)"
3624 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
3627 "It is possible that I am confused about indetation! I generally recommend\
3628 \ switching to named functions if the definition cannot fit inline nicely, so\
3629 \ either (1) try to fit the whole anonymous function on one line or (2) break\
3630 \ the whole thing out into a named function. Things tend to be clearer that way!"
3634 FuncIndentBody row col ->
3636 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3637 region = toRegion row col
3639 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
3640 Code.toSnippet source surroundings (Just region)
3643 "I was expecting to see the body of your anonymous function next:"
3647 ["The","syntax","for","anonymous","functions","is"
3648 ,D.dullyellow "(\\x -> x + 1)"
3649 ,"so","I","am","missing","all","the","stuff","after","the","arrow!"
3652 "It is possible that I am confused about indetation! I generally recommend\
3653 \ switching to named functions if the definition cannot fit inline nicely, so\
3654 \ either (1) try to fit the whole anonymous function on one line or (2) break\
3655 \ the whole thing out into a named function. Things tend to be clearer that way!"
3669toPatternReport :: Code.Source -> PContext -> Pattern -> Row -> Col -> Report.Report
3670toPatternReport source context pattern startRow startCol =
3672 PRecord record row col ->
3673 toPRecordReport source record row col
3675 PTuple tuple row col ->
3676 toPTupleReport source context tuple row col
3678 PList list row col ->
3679 toPListReport source context list row col
3682 case Code.whatIsNext source row col of
3683 Code.Keyword keyword ->
3685 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3686 region = toKeywordRegion row col keyword
3689 PArg -> "an argument"
3690 PCase -> "a pattern"
3692 Report.Report "RESERVED WORD" region [] $
3693 Code.toSnippet source surroundings (Just region)
3696 "It looks like you are trying to use `" ++ keyword ++ "` as " ++ aThing ++ ":"
3699 "This is a reserved word! Try using some other name?"
3705 PChar char row col ->
3706 toCharReport source char row col
3708 PString string row col ->
3709 toStringReport source string row col
3711 PNumber number row col ->
3712 toNumberReport source number row col
3714 PFloat width row col ->
3716 region = toWiderRegion row col width
3718 Report.Report "UNEXPECTED PATTERN" region [] $
3719 Code.toSnippet source region Nothing
3722 "I cannot pattern match with floating point numbers:"
3725 ["Equality","on","floats","can","be","unreliable,","so","you","usually","want"
3726 ,"to","check","that","they","are","nearby","with","some","sort","of"
3727 ,D.dullyellow "(abs (actual - expected) < 0.001)","check."
3733 region = toRegion row col
3735 Report.Report "UNFINISHED PATTERN" region [] $
3736 Code.toSnippet source region Nothing $
3739 "I was expecting to see a variable name after the `as` keyword:"
3743 ["The","`as`","keyword","lets","you","write","patterns","like"
3744 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
3745 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
3746 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
3747 ,"thing","with",D.dullyellow "point."
3750 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
3751 \ people just want to use `as` as a variable name though. Try using a different name\
3756 PWildcardNotVar name width row col ->
3758 region = toWiderRegion row col (fromIntegral width)
3760 case dropWhile (=='_') (Name.toChars name) of
3761 [] -> [D.dullyellow "x","or",D.dullyellow "age"]
3762 c:cs -> [D.dullyellow (D.fromChars (Char.toLower c : cs))]
3764 Report.Report "UNEXPECTED NAME" region [] $
3765 Code.toSnippet source region Nothing $
3768 "Variable names cannot start with underscores like this:"
3771 ["You","can","either","have","an","underscore","like",D.dullyellow "_","to"
3772 ,"ignore","the","value,","or","you","can","have","a","name","like"
3773 ] ++ examples ++ ["to","use","the","matched","value." ]
3776 PSpace space row col ->
3777 toSpaceReport source space row col
3779 PIndentStart row col ->
3780 error "TODO PIndentStart" row col
3782 PIndentAlias row col ->
3784 region = toRegion row col
3786 Report.Report "UNFINISHED PATTERN" region [] $
3787 Code.toSnippet source region Nothing $
3790 "I was expecting to see a variable name after the `as` keyword:"
3794 ["The","`as`","keyword","lets","you","write","patterns","like"
3795 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
3796 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
3797 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
3798 ,"thing","with",D.dullyellow "point."
3801 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
3802 \ people just want to use `as` as a variable name though. Try using a different name\
3808toPRecordReport :: Code.Source -> PRecord -> Row -> Col -> Report.Report
3809toPRecordReport source record startRow startCol =
3811 PRecordOpen row col ->
3812 toUnfinishRecordPatternReport source row col startRow startCol $
3813 D.reflow "I was expecting to see a field name next."
3815 PRecordEnd row col ->
3816 toUnfinishRecordPatternReport source row col startRow startCol $
3818 ["I","was","expecting","to","see","a","closing","curly","brace","next."
3819 ,"Try","adding","a",D.dullyellow "}","here?"
3822 PRecordField row col ->
3823 case Code.whatIsNext source row col of
3824 Code.Keyword keyword ->
3826 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3827 region = toKeywordRegion row col keyword
3829 Report.Report "RESERVED WORD" region [] $
3830 Code.toSnippet source surroundings (Just region)
3833 "I was not expecting to see `" ++ keyword ++ "` as a record field name:"
3836 "This is a reserved word, not available for variable names. Try another name!"
3840 toUnfinishRecordPatternReport source row col startRow startCol $
3841 D.reflow "I was expecting to see a field name next."
3843 PRecordSpace space row col ->
3844 toSpaceReport source space row col
3846 PRecordIndentOpen row col ->
3847 toUnfinishRecordPatternReport source row col startRow startCol $
3848 D.reflow "I was expecting to see a field name next."
3850 PRecordIndentEnd row col ->
3851 toUnfinishRecordPatternReport source row col startRow startCol $
3853 ["I","was","expecting","to","see","a","closing","curly","brace","next."
3854 ,"Try","adding","a",D.dullyellow "}","here?"
3857 PRecordIndentField row col ->
3858 toUnfinishRecordPatternReport source row col startRow startCol $
3859 D.reflow "I was expecting to see a field name next."
3862toUnfinishRecordPatternReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
3863toUnfinishRecordPatternReport source row col startRow startCol message =
3865 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3866 region = toRegion row col
3868 Report.Report "UNFINISHED RECORD PATTERN" region [] $
3869 Code.toSnippet source surroundings (Just region)
3872 "I was partway through parsing a record pattern, but I got stuck here:"
3877 ["A","record","pattern","looks","like",D.dullyellow "{x,y}","or",D.dullyellow "{name,age}"
3878 ,"where","you","list","the","field","names","you","want","to","access."
3885toPTupleReport :: Code.Source -> PContext -> PTuple -> Row -> Col -> Report.Report
3886toPTupleReport source context tuple startRow startCol =
3888 PTupleOpen row col ->
3889 case Code.whatIsNext source row col of
3890 Code.Keyword keyword ->
3892 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3893 region = toKeywordRegion row col keyword
3895 Report.Report "RESERVED WORD" region [] $
3896 Code.toSnippet source surroundings (Just region)
3899 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name:"
3902 "This is a reserved word! Try using some other name?"
3907 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3908 region = toRegion row col
3910 Report.Report "UNFINISHED PARENTHESES" region [] $
3911 Code.toSnippet source surroundings (Just region)
3914 "I just saw an open parenthesis, but I got stuck here:"
3917 ["I","was","expecting","so","see","a","pattern","next."
3918 ,"Maybe","it","will","end","up","being","something"
3919 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
3923 PTupleEnd row col ->
3925 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3926 region = toRegion row col
3928 Report.Report "UNFINISHED PARENTHESES" region [] $
3929 Code.toSnippet source surroundings (Just region) $
3932 "I was expecting a closing parenthesis next:"
3934 D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
3937 PTupleExpr pattern row col ->
3938 toPatternReport source context pattern row col
3940 PTupleSpace space row col ->
3941 toSpaceReport source space row col
3943 PTupleIndentEnd row col ->
3945 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3946 region = toRegion row col
3948 Report.Report "UNFINISHED PARENTHESES" region [] $
3949 Code.toSnippet source surroundings (Just region) $
3952 "I was expecting a closing parenthesis next:"
3955 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
3957 "I can get confused by indentation in cases like this, so\
3958 \ maybe you have a closing parenthesis but it is not indented enough?"
3962 PTupleIndentExpr1 row col ->
3964 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3965 region = toRegion row col
3967 Report.Report "UNFINISHED PARENTHESES" region [] $
3968 Code.toSnippet source surroundings (Just region) $
3971 "I just saw an open parenthesis, but then I got stuck here:"
3974 ["I","was","expecting","so","see","a","pattern","next."
3975 ,"Maybe","it","will","end","up","being","something"
3976 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
3980 PTupleIndentExprN row col ->
3982 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3983 region = toRegion row col
3985 Report.Report "UNFINISHED TUPLE PATTERN" region [] $
3986 Code.toSnippet source surroundings (Just region) $
3989 "I am partway through parsing a tuple pattern, but I got stuck here:"
3993 ["I","was","expecting","so","see","a","pattern","next."
3994 ,"I","am","expecting","the","final","result","to","be","something"
3995 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "."
3998 "I can get confused by indentation in cases like this, so the problem\
3999 \ may be that the next part is not indented enough?"
4004toPListReport :: Code.Source -> PContext -> PList -> Row -> Col -> Report.Report
4005toPListReport source context list startRow startCol =
4007 PListOpen row col ->
4008 case Code.whatIsNext source row col of
4009 Code.Keyword keyword ->
4011 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4012 region = toKeywordRegion row col keyword
4014 Report.Report "RESERVED WORD" region [] $
4015 Code.toSnippet source surroundings (Just region)
4018 "It looks like you are trying to use `" ++ keyword ++ "` to name an element of a list:"
4021 "This is a reserved word though! Try using some other name?"
4026 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4027 region = toRegion row col
4029 Report.Report "UNFINISHED LIST PATTERN" region [] $
4030 Code.toSnippet source surroundings (Just region) $
4033 "I just saw an open square bracket, but then I got stuck here:"
4035 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4040 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4041 region = toRegion row col
4043 Report.Report "UNFINISHED LIST PATTERN" region [] $
4044 Code.toSnippet source surroundings (Just region) $
4047 "I was expecting a closing square bracket to end this list pattern:"
4049 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4052 PListExpr pattern row col ->
4053 toPatternReport source context pattern row col
4055 PListSpace space row col ->
4056 toSpaceReport source space row col
4058 PListIndentOpen row col ->
4060 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4061 region = toRegion row col
4063 Report.Report "UNFINISHED LIST PATTERN" region [] $
4064 Code.toSnippet source surroundings (Just region) $
4067 "I just saw an open square bracket, but then I got stuck here:"
4070 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4072 "I can get confused by indentation in cases like this, so\
4073 \ maybe there is something next, but it is not indented enough?"
4077 PListIndentEnd row col ->
4079 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4080 region = toRegion row col
4082 Report.Report "UNFINISHED LIST PATTERN" region [] $
4083 Code.toSnippet source surroundings (Just region) $
4086 "I was expecting a closing square bracket to end this list pattern:"
4089 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4091 "I can get confused by indentation in cases like this, so\
4092 \ maybe you have a closing square bracket but it is not indented enough?"
4096 PListIndentExpr row col ->
4098 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4099 region = toRegion row col
4101 Report.Report "UNFINISHED LIST PATTERN" region [] $
4102 Code.toSnippet source surroundings (Just region) $
4105 "I am partway through parsing a list pattern, but I got stuck here:"
4109 "I was expecting to see another pattern next. Maybe a variable name."
4111 "I can get confused by indentation in cases like this, so\
4112 \ maybe there is more to this pattern but it is not indented enough?"
4121-- TODO add context? say something special if you are in type annotation vs custom type vs type alias?
4123toTypeReport :: Code.Source -> Type -> Row -> Col -> Report.Report
4124toTypeReport source tipe startRow startCol =
4126 TRecord record row col ->
4127 toTRecordReport source record row col
4129 TTuple tuple row col ->
4130 toTTupleReport source tuple row col
4133 error "TODO TArrow" row col
4136 error "TODO TStart" row col startRow startCol
4138 TSpace space row col ->
4139 toSpaceReport source space row col
4141 TIndentStart row col ->
4142 error "TODO TIndentStart" row col
4146toTRecordReport :: Code.Source -> TRecord -> Row -> Col -> Report.Report
4147toTRecordReport source record startRow startCol =
4149 TRecordOpen row col ->
4150 case Code.whatIsNext source row col of
4151 Code.Keyword keyword ->
4153 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4154 region = toKeywordRegion row col keyword
4156 Report.Report "RESERVED WORD" region [] $
4157 Code.toSnippet source surroundings (Just region)
4160 "I just started parsing a record type, but I got stuck on this field name:"
4163 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
4164 \ that is a reserved word. Try using a different name!"
4169 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4170 region = toRegion row col
4172 Report.Report "UNFINISHED RECORD TYPE" region [] $
4173 Code.toSnippet source surroundings (Just region)
4176 "I just started parsing a record type, but I got stuck here:"
4179 ["Record","types","look","like",D.dullyellow "{ name : String, age : Int },"
4180 ,"so","I","was","expecting","to","see","a","field","name","next."
4184 TRecordEnd row col ->
4186 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4187 region = toRegion row col
4189 Report.Report "UNFINISHED RECORD TYPE" region [] $
4190 Code.toSnippet source surroundings (Just region)
4193 "I am partway through parsing a record type, but I got stuck here:"
4197 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
4198 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
4201 "When I get stuck like this, it usually means that there is a missing parenthesis\
4202 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
4206 TRecordField row col ->
4207 case Code.whatIsNext source row col of
4208 Code.Keyword keyword ->
4210 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4211 region = toKeywordRegion row col keyword
4213 Report.Report "RESERVED WORD" region [] $
4214 Code.toSnippet source surroundings (Just region)
4217 "I am partway through parsing a record type, but I got stuck on this field name:"
4220 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
4221 \ that is a reserved word. Try using a different name!"
4226 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4227 region = toRegion row col
4229 Report.Report "UNFINISHED RECORD TYPE" region [] $
4230 Code.toSnippet source surroundings (Just region)
4233 "I am partway through parsing a record type, but I got stuck after that last comma:"
4235 addNoteForRecordTypeError $
4237 "Trailing commas are not allowed in records, so I was expecting to see another\
4238 \ record field defined next. If you are done defining the record, the fix may\
4239 \ be to delete that last comma?"
4242 TRecordColon row col ->
4244 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4245 region = toRegion row col
4247 Report.Report "UNFINISHED RECORD TYPE" region [] $
4248 Code.toSnippet source surroundings (Just region)
4251 "I am partway through parsing a record type, but I got stuck here:"
4253 addNoteForRecordTypeError $
4255 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
4256 ,"a","colon","next.","So","try","putting","an",D.green ":","sign","here?"
4260 TRecordType tipe row col ->
4261 toTypeReport source tipe row col
4263 TRecordSpace space row col ->
4264 toSpaceReport source space row col
4266 TRecordIndentOpen row col ->
4268 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4269 region = toRegion row col
4271 Report.Report "UNFINISHED RECORD TYPE" region [] $
4272 Code.toSnippet source surroundings (Just region)
4275 "I just saw the opening curly brace of a record type, but then I got stuck here:"
4277 addNoteForRecordTypeIndentError $
4279 ["I","am","expecting","a","record","like",D.dullyellow "{ name : String, age : Int }","here."
4280 ,"Try","defining","some","fields","of","your","own?"
4284 TRecordIndentEnd row col ->
4286 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4287 region = toRegion row col
4289 Report.Report "UNFINISHED RECORD TYPE" region [] $
4290 Code.toSnippet source surroundings (Just region)
4293 "I was expecting to see a closing curly brace next:"
4295 addNoteForRecordTypeIndentError $
4297 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
4301 TRecordIndentField row col ->
4303 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4304 region = toRegion row col
4306 Report.Report "UNFINISHED RECORD TYPE" region [] $
4307 Code.toSnippet source surroundings (Just region)
4310 "I am partway through parsing a record type, but I got stuck after that last comma:"
4312 addNoteForRecordTypeIndentError $
4314 "Trailing commas are not allowed in record types, so the fix may be to\
4315 \ delete that last comma? Or maybe you were in the middle of defining\
4316 \ an additional field?"
4319 TRecordIndentColon row col ->
4321 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4322 region = toRegion row col
4324 Report.Report "UNFINISHED RECORD TYPE" region [] $
4325 Code.toSnippet source surroundings (Just region)
4328 "I am partway through parsing a record type. I just saw a record\
4329 \ field, so I was expecting to see a colon next:"
4331 addNoteForRecordTypeIndentError $
4333 ["Try","putting","an",D.green ":","followed","by","a","type?"
4337 TRecordIndentType row col ->
4339 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4340 region = toRegion row col
4342 Report.Report "UNFINISHED RECORD TYPE" region [] $
4343 Code.toSnippet source surroundings (Just region)
4346 "I am partway through parsing a record type, and I was expecting to run into a type next:"
4348 addNoteForRecordTypeIndentError $
4350 ["Try","putting","something,","like"
4351 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4356addNoteForRecordTypeError :: D.Doc -> D.Doc
4357addNoteForRecordTypeError normalRecommendation =
4359 normalRecommendation
4362 "If you are trying to define a record type across multiple lines, I recommend using this format:"
4363 , D.indent 4 $ D.vcat $
4366 , ", height : Float"
4370 "Notice that each line starts with some indentation. Usually two or four spaces. I\
4371 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
4372 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4376addNoteForRecordTypeIndentError :: D.Doc -> D.Doc
4377addNoteForRecordTypeIndentError normalRecommendation =
4379 normalRecommendation
4382 "I may be confused by indentation. For example, if you are trying to define\
4383 \ a record type across multiple lines, I recommend using this format:"
4384 , D.indent 4 $ D.vcat $
4387 , ", height : Float"
4391 "Notice that each line starts with some indentation. Usually two or four spaces. I\
4392 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
4393 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4397toTTupleReport :: Code.Source -> TTuple -> Row -> Col -> Report.Report
4398toTTupleReport source tuple startRow startCol =
4400 TTupleOpen row col ->
4401 case Code.whatIsNext source row col of
4402 Code.Keyword keyword ->
4404 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4405 region = toKeywordRegion row col keyword
4407 Report.Report "RESERVED WORD" region [] $
4408 Code.toSnippet source surroundings (Just region)
4411 "I ran into a reserved word unexpectedly:"
4414 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but \
4415 \ it is a reserved word. Try using a different name!"
4420 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4421 region = toRegion row col
4423 Report.Report "UNFINISHED PARENTHESES" region [] $
4424 Code.toSnippet source surroundings (Just region)
4427 "I just saw an open parenthesis, so I was expecting to see a type next."
4430 ["Something","like",D.dullyellow "(Maybe Int)","or"
4431 ,D.dullyellow "(List Person)" <> "."
4432 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
4436 TTupleEnd row col ->
4438 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4439 region = toRegion row col
4441 Report.Report "UNFINISHED PARENTHESES" region [] $
4442 Code.toSnippet source surroundings (Just region)
4445 "I was expecting to see a closing parentheses next, but I got stuck here:"
4448 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
4450 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
4451 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
4452 \ or missing brackets) that is confusing me."
4456 TTupleType tipe row col ->
4457 toTypeReport source tipe row col
4459 TTupleSpace space row col ->
4460 toSpaceReport source space row col
4462 TTupleIndentType1 row col ->
4464 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4465 region = toRegion row col
4467 Report.Report "UNFINISHED PARENTHESES" region [] $
4468 Code.toSnippet source surroundings (Just region)
4471 "I just saw an open parenthesis, so I was expecting to see a type next."
4475 ["Something","like",D.dullyellow "(Maybe Int)","or"
4476 ,D.dullyellow "(List Person)" <> "."
4477 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
4480 "I can get confused by indentation in cases like this, so\
4481 \ maybe you have a type but it is not indented enough?"
4485 TTupleIndentTypeN row col ->
4487 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4488 region = toRegion row col
4490 Report.Report "UNFINISHED TUPLE TYPE" region [] $
4491 Code.toSnippet source surroundings (Just region)
4494 "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."
4498 ["A","tuple","type","looks","like",D.dullyellow "(Float,Float)","or"
4499 ,D.dullyellow "(String,Int)" <> ","
4500 ,"so","I","think","there","is","a","type","missing","here?"
4503 "I can get confused by indentation in cases like this, so\
4504 \ maybe you have an expression but it is not indented enough?"
4508 TTupleIndentEnd row col ->
4510 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4511 region = toRegion row col
4513 Report.Report "UNFINISHED PARENTHESES" region [] $
4514 Code.toSnippet source surroundings (Just region)
4517 "I was expecting to see a closing parenthesis next:"
4520 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
4522 "I can get confused by indentation in cases like this, so\
4523 \ 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