on 5e73421 help sign in
5309 lines 190 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
4 ( Error(..)
5 , toReport
6 --
7 , Module(..)
8 , Exposing(..)
9 --
10 , Decl(..)
11 , DeclType(..)
12 , TypeAlias(..)
13 , CustomType(..)
14 , DeclDef(..)
15 , Port(..)
16 --
17 , Expr(..)
18 , Record(..)
19 , Tuple(..)
20 , List(..)
21 , Func(..)
22 , Case(..)
23 , If(..)
24 , Let(..)
25 , Def(..)
26 , Destruct(..)
27 --
28 , Pattern(..)
29 , PRecord(..)
30 , PTuple(..)
31 , PList(..)
32 --
33 , Type(..)
34 , TRecord(..)
35 , TTuple(..)
36 --
37 , Char(..)
38 , String(..)
39 , Escape(..)
40 , Number(..)
41 --
42 , Space(..)
43 , toSpaceReport
44 )
45 where
48import Prelude hiding (Char, String)
49import qualified Data.Char as Char
50import qualified Data.Name as Name
51import Data.Word (Word16)
52import Numeric (showHex)
54import qualified Elm.ModuleName as ModuleName
55import Parse.Primitives (Row, Col)
56import Parse.Symbol (BadOperator(..))
57import qualified Reporting.Annotation as A
58import qualified Reporting.Doc as D
59import qualified Reporting.Report as Report
60import qualified Reporting.Render.Code as Code
64-- ALL SYNTAX ERRORS
67data Error
68 = ModuleNameUnspecified ModuleName.Raw
69 | ModuleNameMismatch ModuleName.Raw (A.Located ModuleName.Raw)
70 | UnexpectedPort A.Region
71 | NoPorts A.Region
72 | ParseError Module
76-- MODULE
79data Module
80 = ModuleSpace Space Row Col
81 | ModuleBadEnd Row Col
82 --
83 | ModuleProblem Row Col
84 | ModuleName Row Col
85 | ModuleExposing Exposing Row Col
86 --
87 | PortModuleProblem Row Col
88 | PortModuleName Row Col
89 | PortModuleExposing Exposing Row Col
90 --
91 | Effect Row Col
92 --
93 | FreshLine Row Col
94 --
95 | ImportStart Row Col
96 | ImportName Row Col
97 | ImportAs Row Col
98 | ImportAlias Row Col
99 | ImportExposing Row Col
100 | ImportExposingList Exposing Row Col
101 | ImportEnd Row Col -- different based on col=1 or if greater
102 --
103 | ImportIndentName Row Col
104 | ImportIndentAlias Row Col
105 | ImportIndentExposingList Row Col
106 --
107 | Infix Row Col
108 --
109 | Declarations Decl Row Col
112data Exposing
113 = ExposingSpace Space Row Col
114 | ExposingStart Row Col
115 | ExposingValue Row Col
116 | ExposingOperator Row Col
117 | ExposingOperatorReserved BadOperator Row Col
118 | ExposingOperatorRightParen Row Col
119 | ExposingTypePrivacy Row Col
120 | ExposingEnd Row Col
121 --
122 | ExposingIndentEnd Row Col
123 | ExposingIndentValue Row Col
127-- DECLARATIONS
130data Decl
131 = DeclStart Row Col
132 | DeclSpace Space Row Col
133 --
134 | Port Port Row Col
135 | DeclType DeclType Row Col
136 | DeclDef Name.Name DeclDef Row Col
137 --
138 | DeclFreshLineAfterDocComment Row Col
141data DeclDef
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
149 --
150 | DeclDefIndentType Row Col
151 | DeclDefIndentEquals Row Col
152 | DeclDefIndentBody Row Col
155data Port
156 = PortSpace Space Row Col
157 | PortName Row Col
158 | PortColon Row Col
159 | PortType Type Row Col
160 | PortIndentName Row Col
161 | PortIndentColon Row Col
162 | PortIndentType Row Col
166-- TYPE DECLARATIONS
169data DeclType
170 = DT_Space Space Row Col
171 | DT_Name Row Col
172 | DT_Alias TypeAlias Row Col
173 | DT_Union CustomType Row Col
174 --
175 | DT_IndentName Row Col
178data TypeAlias
179 = AliasSpace Space Row Col
180 | AliasName Row Col
181 | AliasEquals Row Col
182 | AliasBody Type Row Col
183 --
184 | AliasIndentEquals Row Col
185 | AliasIndentBody Row Col
188data CustomType
189 = CT_Space Space Row Col
190 | CT_Name Row Col
191 | CT_Equals Row Col
192 | CT_Bar Row Col
193 | CT_Variant Row Col
194 | CT_VariantArg Type Row Col
195 --
196 | CT_IndentEquals Row Col
197 | CT_IndentBar Row Col
198 | CT_IndentAfterBar Row Col
199 | CT_IndentAfterEquals Row Col
203-- EXPRESSIONS
206data Expr
207 = Let Let Row Col
208 | Case Case Row Col
209 | If If Row Col
210 | List List Row Col
211 | Record Record Row Col
212 | Tuple Tuple Row Col
213 | Func Func Row Col
214 --
215 | Dot Row Col
216 | Access Row Col
217 | OperatorRight Name.Name Row Col
218 | OperatorReserved BadOperator Row Col
219 --
220 | Start Row Col
221 | Char Char 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
230data Record
231 = RecordOpen Row Col
232 | RecordEnd Row Col
233 | RecordField Row Col
234 | RecordEquals Row Col
235 | RecordExpr Expr Row Col
236 | RecordSpace Space Row Col
237 --
238 | RecordIndentOpen Row Col
239 | RecordIndentEnd Row Col
240 | RecordIndentField Row Col
241 | RecordIndentEquals Row Col
242 | RecordIndentExpr Row Col
245data Tuple
246 = TupleExpr Expr Row Col
247 | TupleSpace Space Row Col
248 | TupleEnd Row Col
249 | TupleOperatorClose Row Col
250 | TupleOperatorReserved BadOperator Row Col
251 --
252 | TupleIndentExpr1 Row Col
253 | TupleIndentExprN Row Col
254 | TupleIndentEnd Row Col
257data List
258 = ListSpace Space Row Col
259 | ListOpen Row Col
260 | ListExpr Expr Row Col
261 | ListEnd Row Col
262 --
263 | ListIndentOpen Row Col
264 | ListIndentEnd Row Col
265 | ListIndentExpr Row Col
268data Func
269 = FuncSpace Space Row Col
270 | FuncArg Pattern Row Col
271 | FuncBody Expr Row Col
272 | FuncArrow Row Col
273 --
274 | FuncIndentArg Row Col
275 | FuncIndentArrow Row Col
276 | FuncIndentBody Row Col
279data Case
280 = CaseSpace Space Row Col
281 | CaseOf Row Col
282 | CasePattern Pattern Row Col
283 | CaseArrow Row Col
284 | CaseExpr Expr Row Col
285 | CaseBranch Expr Row Col
286 --
287 | CaseIndentOf Row Col
288 | CaseIndentExpr Row Col
289 | CaseIndentPattern Row Col
290 | CaseIndentArrow Row Col
291 | CaseIndentBranch Row Col
292 | CasePatternAlignment Word16 Row Col
295data If
296 = IfSpace Space Row Col
297 | IfThen Row Col
298 | IfElse Row Col
299 | IfElseBranchStart Row Col
300 --
301 | IfCondition Expr Row Col
302 | IfThenBranch Expr Row Col
303 | IfElseBranch Expr Row Col
304 --
305 | IfIndentCondition Row Col
306 | IfIndentThen Row Col
307 | IfIndentThenBranch Row Col
308 | IfIndentElseBranch Row Col
309 | IfIndentElse Row Col
312data Let
313 = LetSpace Space Row Col
314 | LetIn Row Col
315 | LetDefAlignment Word16 Row Col
316 | LetDefName Row Col
317 | LetDef Name.Name Def Row Col
318 | LetDestruct Destruct Row Col
319 | LetBody Expr Row Col
320 | LetIndentDef Row Col
321 | LetIndentIn Row Col
322 | LetIndentBody Row Col
325data Def
326 = DefSpace Space Row Col
327 | DefType Type Row Col
328 | DefNameRepeat Row Col
329 | DefNameMatch Name.Name Row Col
330 | DefArg Pattern Row Col
331 | DefEquals Row Col
332 | DefBody Expr Row Col
333 | DefIndentEquals Row Col
334 | DefIndentType Row Col
335 | DefIndentBody Row Col
336 | DefAlignment Word16 Row Col
339data Destruct
340 = DestructSpace Space Row Col
341 | DestructPattern Pattern Row Col
342 | DestructEquals Row Col
343 | DestructBody Expr Row Col
344 | DestructIndentEquals Row Col
345 | DestructIndentBody Row Col
349-- PATTERNS
352data Pattern
353 = PRecord PRecord Row Col
354 | PTuple PTuple Row Col
355 | PList PList Row Col
356 --
357 | PStart Row Col
358 | PChar Char Row Col
359 | PString String Row Col
360 | PNumber Number Row Col
361 | PFloat Word16 Row Col
362 | PAlias Row Col
363 | PWildcardNotVar Name.Name Int Row Col
364 | PSpace Space Row Col
365 --
366 | PIndentStart Row Col
367 | PIndentAlias Row Col
370data PRecord
371 = PRecordOpen Row Col
372 | PRecordEnd Row Col
373 | PRecordField Row Col
374 | PRecordSpace Space Row Col
375 --
376 | PRecordIndentOpen Row Col
377 | PRecordIndentEnd Row Col
378 | PRecordIndentField Row Col
381data PTuple
382 = PTupleOpen Row Col
383 | PTupleEnd Row Col
384 | PTupleExpr Pattern Row Col
385 | PTupleSpace Space Row Col
386 --
387 | PTupleIndentEnd Row Col
388 | PTupleIndentExpr1 Row Col
389 | PTupleIndentExprN Row Col
392data PList
393 = PListOpen Row Col
394 | PListEnd Row Col
395 | PListExpr Pattern Row Col
396 | PListSpace Space Row Col
397 --
398 | PListIndentOpen Row Col
399 | PListIndentEnd Row Col
400 | PListIndentExpr Row Col
404-- TYPES
407data Type
408 = TRecord TRecord Row Col
409 | TTuple TTuple Row Col
410 --
411 | TStart Row Col
412 | TSpace Space Row Col
413 --
414 | TIndentStart Row Col
417data TRecord
418 = TRecordOpen Row Col
419 | TRecordEnd Row Col
420 --
421 | TRecordField Row Col
422 | TRecordColon Row Col
423 | TRecordType Type Row Col
424 --
425 | TRecordSpace Space Row Col
426 --
427 | TRecordIndentOpen Row Col
428 | TRecordIndentField Row Col
429 | TRecordIndentColon Row Col
430 | TRecordIndentType Row Col
431 | TRecordIndentEnd Row Col
434data TTuple
435 = TTupleOpen Row Col
436 | TTupleEnd Row Col
437 | TTupleType Type Row Col
438 | TTupleSpace Space Row Col
439 --
440 | TTupleIndentType1 Row Col
441 | TTupleIndentTypeN Row Col
442 | TTupleIndentEnd Row Col
446-- LITERALS
449data Char
450 = CharEndless
451 | CharEscape Escape
452 | CharNotString Word16
455data String
456 = StringEndless_Single
457 | StringEndless_Multi
458 | StringEscape Escape
461data Escape
462 = EscapeUnknown
463 | BadUnicodeFormat Word16
464 | BadUnicodeCode Word16
465 | BadUnicodeLength Word16 Int Int
468data Number
469 = NumberEnd
470 | NumberDot Int
471 | NumberHexDigit
472 | NumberNoLeadingZero
476-- MISC
479data Space
480 = HasTab
481 | EndlessMultiComment
485-- TO REPORT
488toReport :: Code.Source -> Error -> Report.Report
489toReport source err =
490 case err of
491 ModuleNameUnspecified name ->
492 let
493 region = toRegion 1 1
494 in
495 Report.Report "MODULE NAME MISSING" region [] $
496 D.stack
497 [ D.reflow $
498 "I need the module name to be declared at the top of this file, like this:"
499 , D.indent 4 $ D.fillSep $
500 [ D.cyan "module", D.fromName name, D.cyan "exposing", "(..)" ]
501 , D.reflow $
502 "Try adding that as the first line of your file!"
503 , D.toSimpleNote $
504 "It is best to replace (..) with an explicit list of types and\
505 \ functions you want to expose. When you know a value is only used\
506 \ within this module, you can refactor without worrying about uses\
507 \ elsewhere. Limiting exposed values can also speed up compilation\
508 \ because I can skip a bunch of work if I see that the exposed API\
509 \ has not changed."
510 ]
512 ModuleNameMismatch expectedName (A.At region actualName) ->
513 Report.Report "MODULE NAME MISMATCH" region [ModuleName.toChars expectedName] $
514 Code.toSnippet source region Nothing
515 (
516 "It looks like this module name is out of sync:"
517 ,
518 D.stack
519 [ D.reflow $
520 "I need it to match the file path, so I was expecting it to see `"
521 ++ ModuleName.toChars expectedName
522 ++ "` here. Make the following change, and you should be all set!"
523 , D.indent 4 $
524 D.dullyellow (D.fromName actualName) <> " -> " <> D.green (D.fromName expectedName)
525 , D.toSimpleNote $
526 "I require that module names correspond to file paths. This makes it much\
527 \ easier to explore unfamiliar codebases! So if you want to keep the current\
528 \ module name, try renaming the file instead."
529 ]
530 )
532 UnexpectedPort region ->
533 Report.Report "UNEXPECTED PORTS" region [] $
534 Code.toSnippet source region Nothing
535 (
536 D.reflow $
537 "You are declaring ports in a normal module."
538 ,
539 D.stack
540 [ D.fillSep
541 ["Switch","this","to","say",D.cyan "port module","instead,"
542 ,"marking","that","this","module","contains","port","declarations."
543 ]
544 , D.link "Note"
545 "Ports are not a traditional FFI for calling JS functions directly. They need a different mindset! Read"
546 "ports"
547 "to learn the syntax and how to use it effectively."
548 ]
549 )
551 NoPorts region ->
552 Report.Report "NO PORTS" region [] $
553 Code.toSnippet source region Nothing
554 (
555 D.reflow $
556 "This module does not declare any ports, but it says it will:"
557 ,
558 D.fillSep
559 ["Switch","this","to",D.cyan "module"
560 ,"and","you","should","be","all","set!"
561 ]
562 )
564 ParseError modul ->
565 toParseErrorReport source modul
568toParseErrorReport :: Code.Source -> Module -> Report.Report
569toParseErrorReport source modul =
570 case modul of
571 ModuleSpace space row col ->
572 toSpaceReport source space row col
574 ModuleBadEnd row col ->
575 case Code.whatIsNext source row col of
576 Code.Keyword keyword ->
577 let
578 region = toKeywordRegion row col keyword
579 in
580 Report.Report "RESERVED WORD" region [] $
581 Code.toSnippet source region Nothing
582 (
583 D.reflow $
584 "I got stuck on this reserved word:"
585 ,
586 D.reflow $
587 "The name `" ++ keyword ++ "` is reserved, so try using a different name?"
588 )
590 Code.Operator op ->
591 let
592 region = toKeywordRegion row col op
593 in
594 Report.Report "UNEXPECTED SYMBOL" region [] $
595 Code.toSnippet source region Nothing
596 (
597 D.reflow $
598 "I ran into an unexpected symbol:"
599 ,
600 D.reflow $
601 "I was not expecting to see a " ++ op ++ " here. Try deleting it? Maybe\
602 \ I can give a better hint from there?"
603 )
605 Code.Close term bracket ->
606 let
607 region = toRegion row col
608 in
609 Report.Report ("UNEXPECTED " ++ map Char.toUpper term) region [] $
610 Code.toSnippet source region Nothing
611 (
612 D.reflow $
613 "I ran into an unexpected " ++ term ++ ":"
614 ,
615 D.reflow $
616 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
617 )
619 Code.Other ->
620 let
621 region = toRegion row col
622 in
623 Report.Report "SYNTAX PROBLEM" region [] $
624 Code.toSnippet source region Nothing
625 (
626 D.reflow $
627 "I got stuck here:"
628 ,
629 D.reflow $
630 "Whatever I am running into is confusing me a lot! Normally I can give fairly\
631 \ specific hints, but something is really tripping me up this time."
632 )
634 ModuleProblem row col ->
635 let
636 region = toRegion row col
637 in
638 Report.Report "UNFINISHED MODULE DECLARATION" region [] $
639 Code.toSnippet source region Nothing
640 (
641 D.reflow $
642 "I am parsing an `module` declaration, but I got stuck here:"
643 ,
644 D.stack
645 [ D.reflow $
646 "Here are some examples of valid `module` declarations:"
647 , D.indent 4 $ D.vcat $
648 [ D.fillSep [D.cyan "module","Main",D.cyan "exposing","(..)"]
649 , D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(Dict, empty, get)"]
650 ]
651 , D.reflow $
652 "I generally recommend using an explicit exposing list. I can skip compiling a bunch\
653 \ of files when the public interface of a module stays the same, so exposing fewer\
654 \ values can help improve compile times!"
655 ]
656 )
658 ModuleName row col ->
659 let
660 region = toRegion row col
661 in
662 Report.Report "EXPECTING MODULE NAME" region [] $
663 Code.toSnippet source region Nothing
664 (
665 D.reflow $
666 "I was parsing an `module` declaration until I got stuck here:"
667 ,
668 D.stack
669 [ D.reflow $
670 "I was expecting to see the module name next, like in these examples:"
671 , D.indent 4 $ D.vcat $
672 [ D.fillSep [D.cyan "module","Dict",D.cyan "exposing","(..)"]
673 , D.fillSep [D.cyan "module","Maybe",D.cyan "exposing","(..)"]
674 , D.fillSep [D.cyan "module","Html.Attributes",D.cyan "exposing","(..)"]
675 , D.fillSep [D.cyan "module","Json.Decode",D.cyan "exposing","(..)"]
676 ]
677 , D.reflow $
678 "Notice that the module names all start with capital letters. That is required!"
679 ]
680 )
682 ModuleExposing exposing row col ->
683 toExposingReport source exposing row col
685 PortModuleProblem row col ->
686 let
687 region = toRegion row col
688 in
689 Report.Report "UNFINISHED PORT MODULE DECLARATION" region [] $
690 Code.toSnippet source region Nothing
691 (
692 D.reflow $
693 "I am parsing an `port module` declaration, but I got stuck here:"
694 ,
695 D.stack
696 [ D.reflow $
697 "Here are some examples of valid `port module` declarations:"
698 , D.indent 4 $ D.vcat $
699 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
700 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
701 ]
702 , D.link "Note" "Read" "ports" "for more help."
703 ]
704 )
706 PortModuleName row col ->
707 let
708 region = toRegion row col
709 in
710 Report.Report "EXPECTING MODULE NAME" region [] $
711 Code.toSnippet source region Nothing
712 (
713 D.reflow $
714 "I was parsing an `module` declaration until I got stuck here:"
715 ,
716 D.stack
717 [ D.reflow $
718 "I was expecting to see the module name next, like in these examples:"
719 , D.indent 4 $ D.vcat $
720 [ D.fillSep [D.cyan "port",D.cyan "module","WebSockets",D.cyan "exposing","(send, listen, keepAlive)"]
721 , D.fillSep [D.cyan "port",D.cyan "module","Maps",D.cyan "exposing","(Location, goto)"]
722 ]
723 , D.reflow $
724 "Notice that the module names start with capital letters. That is required!"
725 ]
726 )
728 PortModuleExposing exposing row col ->
729 toExposingReport source exposing row col
731 Effect row col ->
732 let
733 region = toRegion row col
734 in
735 Report.Report "BAD MODULE DECLARATION" region [] $
736 Code.toSnippet source region Nothing
737 (
738 D.reflow $
739 "I cannot parse this module declaration:"
740 ,
741 D.reflow $
742 "This type of module is reserved for the @elm organization. It is used to\
743 \ define certain effects, avoiding building them into the compiler."
744 )
746 FreshLine row col ->
747 let
748 region = toRegion row col
750 toBadFirstLineReport keyword =
751 Report.Report "TOO MUCH INDENTATION" region [] $
752 Code.toSnippet source region Nothing
753 (
754 D.reflow $
755 "This `" ++ keyword ++ "` should not have any spaces before it:"
756 ,
757 D.reflow $
758 "Delete the spaces before `" ++ keyword ++ "` until there are none left!"
759 )
761 in
762 case Code.whatIsNext source row col of
763 Code.Keyword "module" -> toBadFirstLineReport "module"
764 Code.Keyword "import" -> toBadFirstLineReport "import"
765 Code.Keyword "type" -> toBadFirstLineReport "type"
766 Code.Keyword "port" -> toBadFirstLineReport "port"
767 _ ->
768 Report.Report "SYNTAX PROBLEM" region [] $
769 Code.toSnippet source region Nothing
770 (
771 D.reflow $
772 "I got stuck here:"
773 ,
774 D.stack
775 [ D.reflow $
776 "I am not sure what is going on, but I recommend starting an Elm\
777 \ file with the following lines:"
778 , D.vcat
779 [ D.fillSep [D.cyan "import","Html"]
780 , ""
781 , D.fillSep [D.green "main","="]
782 , D.indent 2 $ D.fillSep [D.cyan "Html" <> ".text",D.dullyellow "\"Hello!\""]
783 ]
784 , D.reflow $
785 "You should be able to copy those lines directly into your file. Check out\
786 \ some of the examples on https://elm-lang.org for more help getting started!"
787 , D.toSimpleNote $
788 "This can also happen when something is indented too much!"
789 ]
790 )
792 ImportStart row col ->
793 toImportReport source row col
795 ImportName row col ->
796 let
797 region = toRegion row col
798 in
799 Report.Report "EXPECTING IMPORT NAME" region [] $
800 Code.toSnippet source region Nothing
801 (
802 D.reflow $
803 "I was parsing an `import` until I got stuck here:"
804 ,
805 D.stack
806 [ D.reflow $
807 "I was expecting to see a module name next, like in these examples:"
808 , D.indent 4 $ D.vcat $
809 [ D.fillSep [D.cyan "import","Dict"]
810 , D.fillSep [D.cyan "import","Maybe"]
811 , D.fillSep [D.cyan "import","Html.Attributes"]
812 , D.fillSep [D.cyan "import","Json.Decode"]
813 ]
814 , D.reflow $
815 "Notice that the module names all start with capital letters. That is required!"
816 ]
817 )
819 ImportAs row col ->
820 toImportReport source row col
822 ImportAlias row col ->
823 let
824 region = toRegion row col
825 in
826 Report.Report "EXECTING IMPORT ALIAS" region [] $
827 Code.toSnippet source region Nothing
828 (
829 D.reflow $
830 "I was parsing an `import` until I got stuck here:"
831 ,
832 D.stack
833 [ D.reflow $
834 "I was expecting to see an alias next, like in these examples:"
835 , D.indent 4 $ D.vcat $
836 [ D.fillSep [D.cyan "import","Html.Attributes",D.cyan "as","Attr"]
837 , D.fillSep [D.cyan "import","WebGL.Texture",D.cyan "as","Texture"]
838 , D.fillSep [D.cyan "import","Json.Decode",D.cyan "as","D"]
839 ]
840 , D.reflow $
841 "Notice that the alias always starts with a capital letter. That is required!"
842 ]
843 )
845 ImportExposing row col ->
846 toImportReport source row col
848 ImportExposingList exposing row col ->
849 toExposingReport source exposing row col
851 ImportEnd row col ->
852 toImportReport source row col
854 ImportIndentName row col ->
855 toImportReport source row col
857 ImportIndentAlias row col ->
858 toImportReport source row col
860 ImportIndentExposingList row col ->
861 let
862 region = toRegion row col
863 in
864 Report.Report "UNFINISHED IMPORT" region [] $
865 Code.toSnippet source region Nothing
866 (
867 D.reflow $
868 "I was parsing an `import` until I got stuck here:"
869 ,
870 D.stack
871 [ D.reflow $
872 "I was expecting to see the list of exposed values next. For example, here\
873 \ are two ways to expose values from the `Html` module:"
874 , D.indent 4 $ D.vcat $
875 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
876 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
877 ]
878 , D.reflow $
879 "I generally recommend the second style. It is more explicit, making it\
880 \ much easier to figure out where values are coming from in large projects!"
881 ]
882 )
884 Infix row col ->
885 let
886 region = toRegion row col
887 in
888 Report.Report "BAD INFIX" region [] $
889 Code.toSnippet source region Nothing
890 (
891 D.reflow $
892 "Something went wrong in this infix operator declaration:"
893 ,
894 D.reflow $
895 "This feature is used by the @elm organization to define the\
896 \ languages built-in operators."
897 )
899 Declarations decl _ _ ->
900 toDeclarationsReport source decl
904-- IMPORTS
907toImportReport :: Code.Source -> Row -> Col -> Report.Report
908toImportReport source row col =
909 let
910 region = toRegion row col
911 in
912 Report.Report "UNFINISHED IMPORT" region [] $
913 Code.toSnippet source region Nothing
914 (
915 D.reflow $
916 "I am partway through parsing an import, but I got stuck here:"
917 ,
918 D.stack
919 [ D.reflow $
920 "Here are some examples of valid `import` declarations:"
921 , D.indent 4 $ D.vcat $
922 [ D.fillSep [D.cyan "import","Html"]
923 , D.fillSep [D.cyan "import","Html",D.cyan "as","H"]
924 , D.fillSep [D.cyan "import","Html",D.cyan "as","H",D.cyan "exposing","(..)"]
925 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
926 ]
927 , D.reflow $
928 "You are probably trying to import a different module, but try to make it look like one of these examples!"
929 ]
930 )
934-- EXPOSING
937toExposingReport :: Code.Source -> Exposing -> Row -> Col -> Report.Report
938toExposingReport source exposing startRow startCol =
939 case exposing of
940 ExposingSpace space row col ->
941 toSpaceReport source space row col
943 ExposingStart row col ->
944 let
945 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
946 region = toRegion row col
947 in
948 Report.Report "PROBLEM IN EXPOSING" region [] $
949 Code.toSnippet source surroundings (Just region)
950 (
951 D.reflow $
952 "I want to parse exposed values, but I am getting stuck here:"
953 ,
954 D.stack
955 [ D.fillSep $
956 ["Exposed","values","are","always","surrounded","by","parentheses."
957 ,"So","try","adding","a",D.green "(","here?"
958 ]
959 , D.toSimpleNote "Here are some valid examples of `exposing` for reference:"
960 , D.indent 4 $ D.vcat $
961 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
962 , D.fillSep [D.cyan "import","Html",D.cyan "exposing","(Html, div, text)"]
963 ]
964 , D.reflow $
965 "If you are getting tripped up, you can just expose everything for now. It should\
966 \ get easier to make an explicit exposing list as you see more examples in the wild."
967 ]
968 )
970 ExposingValue row col ->
971 case Code.whatIsNext source row col of
972 Code.Keyword keyword ->
973 let
974 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
975 region = toKeywordRegion row col keyword
976 in
977 Report.Report "RESERVED WORD" region [] $
978 Code.toSnippet source surroundings (Just region)
979 (
980 D.reflow $
981 "I got stuck on this reserved word:"
982 ,
983 D.reflow $
984 "It looks like you are trying to expose `" ++ keyword ++ "` but that is a reserved word. Is there a typo?"
985 )
987 Code.Operator op ->
988 let
989 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
990 region = toKeywordRegion row col op
991 in
992 Report.Report "UNEXPECTED SYMBOL" region [] $
993 Code.toSnippet source surroundings (Just region)
994 (
995 D.reflow $
996 "I got stuck on this symbol:"
997 ,
998 D.stack
999 [ D.reflow $
1000 "If you are trying to expose an operator, add parentheses around it like this:"
1001 , D.indent 4 $ D.dullyellow (D.fromChars op) <> " -> " <> D.green ("(" <> D.fromChars op <> ")")
1005 _ ->
1006 let
1007 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1008 region = toRegion row col
1009 in
1010 Report.Report "PROBLEM IN EXPOSING" region [] $
1011 Code.toSnippet source surroundings (Just region)
1013 D.reflow $
1014 "I got stuck while parsing these exposed values:"
1016 D.stack
1017 [ D.reflow $
1018 "I do not have an exact recommendation, so here are some valid examples\
1019 \ of `exposing` for reference:"
1020 , D.indent 4 $ D.vcat $
1021 [ D.fillSep [D.cyan "import","Html",D.cyan "exposing","(..)"]
1022 , D.fillSep [D.cyan "import","Basics",D.cyan "exposing","(Int, Float, Bool(..), (+), not, sqrt)"]
1024 , D.reflow $
1025 "These examples show how to expose types, variants, operators, and functions. Everything\
1026 \ should be some permutation of these examples, just with different names."
1030 ExposingOperator row col ->
1031 let
1032 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1033 region = toRegion row col
1034 in
1035 Report.Report "PROBLEM IN EXPOSING" region [] $
1036 Code.toSnippet source surroundings (Just region)
1038 D.reflow $
1039 "I just saw an open parenthesis, so I was expecting an operator next:"
1041 D.fillSep $
1042 ["It","is","possible","to","expose","operators,","so","I","was","expecting"
1043 ,"to","see","something","like",D.dullyellow "(+)","or",D.dullyellow "(|=)"
1044 ,"or",D.dullyellow "(||)","after","I","saw","that","open","parenthesis."
1048 ExposingOperatorReserved op row col ->
1049 let
1050 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1051 region = toRegion row col
1052 in
1053 Report.Report "RESERVED SYMBOL" region [] $
1054 Code.toSnippet source surroundings (Just region)
1056 D.reflow $
1057 "I cannot expose this as an operator:"
1059 case op of
1060 BadDot -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1061 BadPipe -> D.fillSep ["Maybe","you","want",D.dullyellow "(||)","instead?"]
1062 BadArrow -> D.reflow "Try getting rid of this entry? Maybe I can give you a better hint after that?"
1063 BadEquals -> D.fillSep ["Maybe","you","want",D.dullyellow "(==)","instead?"]
1064 BadHasType -> D.fillSep ["Maybe","you","want",D.dullyellow "(::)","instead?"]
1067 ExposingOperatorRightParen row col ->
1068 let
1069 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1070 region = toRegion row col
1071 in
1072 Report.Report "PROBLEM IN EXPOSING" region [] $
1073 Code.toSnippet source surroundings (Just region)
1075 D.reflow $
1076 "It looks like you are exposing an operator, but I got stuck here:"
1078 D.fillSep $
1079 ["I","was","expecting","to","see","the","closing","parenthesis","immediately"
1080 ,"after","the","operator.","Try","adding","a",D.green ")","right","here?"
1084 ExposingEnd row col ->
1085 let
1086 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1087 region = toRegion row col
1088 in
1089 Report.Report "UNFINISHED EXPOSING" region [] $
1090 Code.toSnippet source surroundings (Just region)
1092 D.reflow $
1093 "I was partway through parsing exposed values, but I got stuck here:"
1095 D.reflow $
1096 "Maybe there is a comma missing before this?"
1099 ExposingTypePrivacy row col ->
1100 let
1101 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1102 region = toRegion row col
1103 in
1104 Report.Report "PROBLEM EXPOSING CUSTOM TYPE VARIANTS" region [] $
1105 Code.toSnippet source surroundings (Just region)
1107 D.reflow $
1108 "It looks like you are trying to expose the variants of a custom type:"
1110 D.stack
1111 [ D.fillSep $
1112 ["You","need","to","write","something","like"
1113 ,D.dullyellow "Status(..)","or",D.dullyellow "Entity(..)"
1114 ,"though.","It","is","all","or","nothing,","otherwise","`case`"
1115 ,"expressions","could","miss","a","variant","and","crash!"
1117 , D.toSimpleNote $
1118 "It is often best to keep the variants hidden! If someone pattern matches on\
1119 \ the variants, it is a MAJOR change if any new variants are added. Suddenly\
1120 \ their `case` expressions do not cover all variants! So if you do not need\
1121 \ people to pattern match, keep the variants hidden and expose functions to\
1122 \ construct values of this type. This way you can add new variants as a MINOR change!"
1126 ExposingIndentEnd row col ->
1127 let
1128 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1129 region = toRegion row col
1130 in
1131 Report.Report "UNFINISHED EXPOSING" region [] $
1132 Code.toSnippet source surroundings (Just region)
1134 D.reflow $
1135 "I was partway through parsing exposed values, but I got stuck here:"
1137 D.stack
1138 [ D.fillSep $
1139 ["I","was","expecting","a","closing","parenthesis."
1140 ,"Try","adding","a",D.green ")","right","here?"
1142 , D.toSimpleNote $
1143 "I can get confused when there is not enough indentation, so if you already\
1144 \ have a closing parenthesis, it probably just needs some spaces in front of it."
1148 ExposingIndentValue row col ->
1149 let
1150 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1151 region = toRegion row col
1152 in
1153 Report.Report "UNFINISHED EXPOSING" region [] $
1154 Code.toSnippet source surroundings (Just region)
1156 D.reflow $
1157 "I was partway through parsing exposed values, but I got stuck here:"
1159 D.reflow $
1160 "I was expecting another value to expose."
1165-- SPACES
1168toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
1169toSpaceReport source space row col =
1170 case space of
1171 HasTab ->
1172 let
1173 region = toRegion row col
1174 in
1175 Report.Report "NO TABS" region [] $
1176 Code.toSnippet source region Nothing
1178 D.reflow $
1179 "I ran into a tab, but tabs are not allowed in Elm files."
1181 D.reflow $
1182 "Replace the tab with spaces."
1185 EndlessMultiComment ->
1186 let
1187 region = toWiderRegion row col 2
1188 in
1189 Report.Report "ENDLESS COMMENT" region [] $
1190 Code.toSnippet source region Nothing
1192 D.reflow $
1193 "I cannot find the end of this multi-line comment:"
1195 D.stack -- "{-"
1196 [ D.reflow "Add a -} somewhere after this to end the comment."
1197 , D.toSimpleHint
1198 "Multi-line comments can be nested in Elm, so {- {- -} -} is a comment\
1199 \ that happens to contain another comment. Like parentheses and curly braces,\
1200 \ the start and end markers must always be balanced. Maybe that is the problem?"
1206-- DECLARATIONS
1209toRegion :: Row -> Col -> A.Region
1210toRegion row col =
1211 let
1212 pos = A.Position row col
1213 in
1214 A.Region pos pos
1217toWiderRegion :: Row -> Col -> Word16 -> A.Region
1218toWiderRegion row col extra =
1219 A.Region
1220 (A.Position row col)
1221 (A.Position row (col + extra))
1224toKeywordRegion :: Row -> Col -> [Char.Char] -> A.Region
1225toKeywordRegion row col keyword =
1226 A.Region
1227 (A.Position row col)
1228 (A.Position row (col + fromIntegral (length keyword)))
1231toDeclarationsReport :: Code.Source -> Decl -> Report.Report
1232toDeclarationsReport source decl =
1233 case decl of
1234 DeclStart row col ->
1235 case Code.whatIsNext source row col of
1236 Code.Close term bracket ->
1237 let
1238 region = toRegion row col
1239 in
1240 Report.Report ("STRAY " ++ map Char.toUpper term) region [] $
1241 Code.toSnippet source region Nothing
1243 D.reflow $
1244 "I was not expecting to see a " ++ term ++ " here:"
1245 , D.reflow $
1246 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
1249 Code.Keyword keyword ->
1250 let
1251 region = toKeywordRegion row col keyword
1252 in
1253 Report.Report "RESERVED WORD" region [] $
1254 Code.toSnippet source region Nothing
1256 D.reflow $
1257 "I was not expecting to run into the `" ++ keyword ++ "` keyword here:"
1259 case keyword of
1260 "import" ->
1261 D.reflow $
1262 "It is reserved for declaring imports at the top of your module. If you want\
1263 \ another import, try moving it up top with the other imports. If you want to\
1264 \ define a value or function, try changing the name to something else!"
1266 "case" ->
1267 D.stack
1268 [ D.reflow $
1269 "It is reserved for writing `case` expressions. Try using a different name?"
1270 , D.toSimpleNote $
1271 "If you are trying to write a `case` expression, it needs to be part of a\
1272 \ definition. So you could write something like this instead:"
1273 , D.vcat $
1274 [ D.indent 0 $ D.fillSep [D.green "getWidth","maybeWidth","="]
1275 , D.indent 2 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
1276 , D.indent 4 $ D.fillSep [D.blue "Just","width","->"]
1277 , D.indent 6 $ D.fillSep ["width","+",D.dullyellow "200"]
1278 , ""
1279 , D.indent 4 $ D.fillSep [D.blue "Nothing","->"]
1280 , D.indent 6 $ D.fillSep [D.dullyellow "400"]
1282 , D.reflow $
1283 "This defines a `getWidth` function that we can use elsewhere in our program."
1286 "if" ->
1287 D.stack
1288 [ D.reflow $
1289 "It is reserved for writing `if` expressions. Try using a different name?"
1290 , D.toSimpleNote $
1291 "If you are trying to write an `if` expression, it needs to be part of a\
1292 \ definition. So you could write something like this instead:"
1293 , D.vcat $
1294 [ D.indent 0 $ D.fillSep [D.green "reviewPowerLevel","powerLevel","="]
1295 , D.indent 2 $ D.fillSep $
1296 [D.cyan "if","powerLevel",">",D.dullyellow "9000"
1297 ,D.cyan "then",D.dullyellow "\"OVER 9000!!!\""
1298 ,D.cyan "else",D.dullyellow "\"not bad\""
1301 , D.reflow $
1302 "This defines a `reviewPowerLevel` function that we can use elsewhere in our program."
1305 _ ->
1306 D.reflow $
1307 "It is a reserved word. Try changing the name to something else?"
1310 _ ->
1311 let
1312 region = toRegion row col
1313 in
1314 Report.Report "SYNTAX PROBLEM" region [] $
1315 Code.toSnippet source region Nothing
1317 D.reflow $
1318 "I was expecting to see a new declaration here:"
1320 D.stack
1321 [ D.reflow $
1322 "I am not sure what is going wrong exactly, but here are a couple valid\
1323 \ declarations for reference:"
1324 , D.vcat
1325 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1326 , D.fillSep [D.green "add","x","y","="]
1327 , D.fillSep [" ","x","+","y"]
1329 , D.fillSep [D.cyan "type",D.green "User","=","Anonymous","|","LoggedIn String"]
1330 , D.reflow $
1331 "Note that all declarations start with lower-case letters in Elm. Capitalization\
1332 \ makes a difference, so be consious of that if you are working from examples!"
1336 DeclSpace space row col ->
1337 toSpaceReport source space row col
1339 Port port_ row col ->
1340 toPortReport source port_ row col
1342 DeclType declType row col ->
1343 toDeclTypeReport source declType row col
1345 DeclDef name declDef row col ->
1346 toDeclDefReport source name declDef row col
1348 DeclFreshLineAfterDocComment row col ->
1349 let
1350 region = toRegion row col
1351 in
1352 Report.Report "EXPECTING DECLARATION" region [] $
1353 Code.toSnippet source region Nothing
1355 D.reflow $
1356 "I just saw a doc comment, but then I got stuck here:"
1358 D.reflow $
1359 "I was expecting to see the corresponding declaration next, starting on a fresh\
1360 \ line with no indentation."
1365-- PORT
1368toPortReport :: Code.Source -> Port -> Row -> Col -> Report.Report
1369toPortReport source port_ startRow startCol =
1370 case port_ of
1371 PortSpace space row col ->
1372 toSpaceReport source space row col
1374 PortName row col ->
1375 case Code.whatIsNext source row col of
1376 Code.Keyword keyword ->
1377 let
1378 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1379 region = toKeywordRegion row col keyword
1380 in
1381 Report.Report "RESERVED WORD" region [] $
1382 Code.toSnippet source surroundings (Just region)
1384 D.reflow $
1385 "I cannot handle ports with names like this:"
1387 D.reflow $
1388 "You are trying to make a port named `" ++ keyword
1389 ++ "` but that is a reserved word. Try using some other name?"
1392 _ ->
1393 let
1394 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1395 region = toRegion row col
1396 in
1397 Report.Report "PORT PROBLEM" region [] $
1398 Code.toSnippet source surroundings (Just region)
1400 D.reflow $
1401 "I just saw the start of a `port` declaration, but then I got stuck here:"
1403 D.stack
1404 [ D.fillSep
1405 ["I","was","expecting","to","see","a","name","like"
1406 ,D.dullyellow "send","or",D.dullyellow "receive","next."
1407 ,"Something","that","starts","with","a","lower-case","letter."
1409 , portNote
1413 PortColon row col ->
1414 let
1415 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1416 region = toRegion row col
1417 in
1418 Report.Report "PORT PROBLEM" region [] $
1419 Code.toSnippet source surroundings (Just region)
1421 D.reflow $
1422 "I just saw the start of a `port` declaration, but then I got stuck here:"
1424 D.stack
1425 [ D.reflow $
1426 "I was expecting to see a colon next. And then a type that tells me\
1427 \ what type of values are going to flow through."
1428 , portNote
1432 PortType tipe row col ->
1433 toTypeReport source TC_Port tipe row col
1435 PortIndentName row col ->
1436 let
1437 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1438 region = toRegion row col
1439 in
1440 Report.Report "UNFINISHED PORT" region [] $
1441 Code.toSnippet source surroundings (Just region)
1443 D.reflow $
1444 "I just saw the start of a `port` declaration, but then I got stuck here:"
1446 D.stack
1447 [ D.fillSep
1448 ["I","was","expecting","to","see","a","name","like"
1449 ,D.dullyellow "send","or",D.dullyellow "receive","next."
1450 ,"Something","that","starts","with","a","lower-case","letter."
1452 , portNote
1456 PortIndentColon row col ->
1457 let
1458 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1459 region = toRegion row col
1460 in
1461 Report.Report "UNFINISHED PORT" region [] $
1462 Code.toSnippet source surroundings (Just region)
1464 D.reflow $
1465 "I just saw the start of a `port` declaration, but then I got stuck here:"
1467 D.stack
1468 [ D.reflow $
1469 "I was expecting to see a colon next. And then a type that tells me\
1470 \ what type of values are going to flow through."
1471 , portNote
1475 PortIndentType row col ->
1476 let
1477 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1478 region = toRegion row col
1479 in
1480 Report.Report "UNFINISHED PORT" region [] $
1481 Code.toSnippet source surroundings (Just region)
1483 D.reflow $
1484 "I just saw the start of a `port` declaration, but then I got stuck here:"
1486 D.stack
1487 [ D.reflow $
1488 "I was expecting to see a type next. Here are examples of outgoing and\
1489 \ incoming ports for reference:"
1490 , D.vcat
1491 [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1492 , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1494 , D.reflow $
1495 "The first line defines a `send` port so you can send strings out to JavaScript.\
1496 \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1497 \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1498 \ get receive messages when new WebSocket messages come in or when an entry in\
1499 \ IndexedDB changes for some external reason."
1504portNote :: D.Doc
1505portNote =
1506 D.stack
1507 [ D.toSimpleNote $
1508 "Here are some example `port` declarations for reference:"
1509 , D.vcat
1510 [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1511 , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1513 , D.reflow $
1514 "The first line defines a `send` port so you can send strings out to JavaScript.\
1515 \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1516 \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1517 \ get receive messages when new WebSocket messages come in or when the IndexedDB\
1518 \ is changed for some external reason."
1523-- DECL TYPE
1526toDeclTypeReport :: Code.Source -> DeclType -> Row -> Col -> Report.Report
1527toDeclTypeReport source declType startRow startCol =
1528 case declType of
1529 DT_Space space row col ->
1530 toSpaceReport source space row col
1532 DT_Name row col ->
1533 let
1534 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1535 region = toRegion row col
1536 in
1537 Report.Report "EXPECTING TYPE NAME" region [] $
1538 Code.toSnippet source surroundings (Just region)
1540 D.reflow $
1541 "I think I am parsing a type declaration, but I got stuck here:"
1543 D.stack
1544 [ D.fillSep $
1545 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1546 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1548 , customTypeNote
1552 DT_Alias typeAlias row col ->
1553 toTypeAliasReport source typeAlias row col
1555 DT_Union customType row col ->
1556 toCustomTypeReport source customType row col
1558 DT_IndentName row col ->
1559 let
1560 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1561 region = toRegion row col
1562 in
1563 Report.Report "EXPECTING TYPE NAME" region [] $
1564 Code.toSnippet source surroundings (Just region)
1566 D.reflow $
1567 "I think I am parsing a type declaration, but I got stuck here:"
1569 D.stack
1570 [ D.fillSep $
1571 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1572 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1574 , customTypeNote
1579toTypeAliasReport :: Code.Source -> TypeAlias -> Row -> Col -> Report.Report
1580toTypeAliasReport source typeAlias startRow startCol =
1581 case typeAlias of
1582 AliasSpace space row col ->
1583 toSpaceReport source space row col
1585 AliasName row col ->
1586 let
1587 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1588 region = toRegion row col
1589 in
1590 Report.Report "EXPECTING TYPE ALIAS NAME" region [] $
1591 Code.toSnippet source surroundings (Just region)
1593 D.reflow $
1594 "I am partway through parsing a type alias, but I got stuck here:"
1596 D.stack
1597 [ D.fillSep $
1598 ["I","was","expecting","a","name","like",D.dullyellow "Person","or",D.dullyellow "Point"
1599 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1601 , typeAliasNote
1605 AliasEquals row col ->
1606 case Code.whatIsNext source row col of
1607 Code.Keyword keyword ->
1608 let
1609 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1610 region = toKeywordRegion row col keyword
1611 in
1612 Report.Report "RESERVED WORD" region [] $
1613 Code.toSnippet source surroundings (Just region)
1615 D.reflow $
1616 "I ran into a reserved word unexpectedly while parsing this type alias:"
1618 D.stack
1619 [ D.reflow $
1620 "It looks like you are trying use `" ++ keyword
1621 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1622 , typeAliasNote
1626 _ ->
1627 let
1628 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1629 region = toRegion row col
1630 in
1631 Report.Report "PROBLEM IN TYPE ALIAS" region [] $
1632 Code.toSnippet source surroundings (Just region)
1634 D.reflow $
1635 "I am partway through parsing a type alias, but I got stuck here:"
1637 D.stack
1638 [ D.reflow $
1639 "I was expecting to see a type variable or an an equals sign next."
1640 , typeAliasNote
1644 AliasBody tipe row col ->
1645 toTypeReport source TC_TypeAlias tipe row col
1647 AliasIndentEquals row col ->
1648 let
1649 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1650 region = toRegion row col
1651 in
1652 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1653 Code.toSnippet source surroundings (Just region)
1655 D.reflow $
1656 "I am partway through parsing a type alias, but I got stuck here:"
1658 D.stack
1659 [ D.reflow $
1660 "I was expecting to see a type variable or an an equals sign next."
1661 , typeAliasNote
1665 AliasIndentBody row col ->
1666 let
1667 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1668 region = toRegion row col
1669 in
1670 Report.Report "UNFINISHED TYPE ALIAS" region [] $
1671 Code.toSnippet source surroundings (Just region)
1673 D.reflow $
1674 "I am partway through parsing a type alias, but I got stuck here:"
1676 D.stack
1677 [ D.fillSep $
1678 ["I","was","expecting","to","see","a","type","next.","Something","as","simple"
1679 ,"as",D.dullyellow "Int","or",D.dullyellow "Float","would","work!"
1681 , typeAliasNote
1686typeAliasNote :: D.Doc
1687typeAliasNote =
1688 D.stack
1689 [ D.toSimpleNote $
1690 "Here is an example of a valid `type alias` for reference:"
1691 , D.vcat $
1692 [ D.indent 0 $ D.fillSep [D.cyan "type",D.cyan "alias",D.green "Person","="]
1693 , D.indent 2 $ D.vcat $
1694 ["{ name : String"
1695 ,", age : Int"
1696 ,", height : Float"
1697 ,"}"
1700 , D.reflow $
1701 "This would let us use `Person` as a shorthand for that record type. Using this\
1702 \ shorthand makes type annotations much easier to read, and makes changing code\
1703 \ easier if you decide later that there is more to a person than age and height!"
1707toCustomTypeReport :: Code.Source -> CustomType -> Row -> Col -> Report.Report
1708toCustomTypeReport source customType startRow startCol =
1709 case customType of
1710 CT_Space space row col ->
1711 toSpaceReport source space row col
1713 CT_Name row col ->
1714 let
1715 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1716 region = toRegion row col
1717 in
1718 Report.Report "EXPECTING TYPE NAME" region [] $
1719 Code.toSnippet source surroundings (Just region)
1721 D.reflow $
1722 "I think I am parsing a type declaration, but I got stuck here:"
1724 D.stack
1725 [ D.fillSep $
1726 ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1727 ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1729 , customTypeNote
1733 CT_Equals row col ->
1734 case Code.whatIsNext source row col of
1735 Code.Keyword keyword ->
1736 let
1737 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1738 region = toKeywordRegion row col keyword
1739 in
1740 Report.Report "RESERVED WORD" region [] $
1741 Code.toSnippet source surroundings (Just region)
1743 D.reflow $
1744 "I ran into a reserved word unexpectedly while parsing this custom type:"
1746 D.stack
1747 [ D.reflow $
1748 "It looks like you are trying use `" ++ keyword
1749 ++ "` as a type variable, but it is a reserved word. Try using a different name?"
1750 , customTypeNote
1754 _ ->
1755 let
1756 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1757 region = toRegion row col
1758 in
1759 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1760 Code.toSnippet source surroundings (Just region)
1762 D.reflow $
1763 "I am partway through parsing a custom type, but I got stuck here:"
1765 D.stack
1766 [ D.reflow $
1767 "I was expecting to see a type variable or an an equals sign next."
1768 , customTypeNote
1772 CT_Bar row col ->
1773 let
1774 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1775 region = toRegion row col
1776 in
1777 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1778 Code.toSnippet source surroundings (Just region)
1780 D.reflow $
1781 "I am partway through parsing a custom type, but I got stuck here:"
1783 D.stack
1784 [ D.reflow $
1785 "I was expecting to see a vertical bar like | next."
1786 , customTypeNote
1790 CT_Variant row col ->
1791 let
1792 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1793 region = toRegion row col
1794 in
1795 Report.Report "PROBLEM IN CUSTOM TYPE" region [] $
1796 Code.toSnippet source surroundings (Just region)
1798 D.reflow $
1799 "I am partway through parsing a custom type, but I got stuck here:"
1801 D.stack
1802 [ D.fillSep $
1803 ["I","was","expecting","to","see","a","variant","name","next."
1804 ,"Something","like",D.dullyellow "Success","or",D.dullyellow "Sandwich" <> "."
1805 ,"Any","name","that","starts","with","a","capital","letter","really!"
1807 , customTypeNote
1811 CT_VariantArg tipe row col ->
1812 toTypeReport source TC_CustomType tipe row col
1814 CT_IndentEquals row col ->
1815 let
1816 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1817 region = toRegion row col
1818 in
1819 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1820 Code.toSnippet source surroundings (Just region)
1822 D.reflow $
1823 "I am partway through parsing a custom type, but I got stuck here:"
1825 D.stack
1826 [ D.reflow $
1827 "I was expecting to see a type variable or an an equals sign next."
1828 , customTypeNote
1832 CT_IndentBar row col ->
1833 let
1834 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1835 region = toRegion row col
1836 in
1837 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1838 Code.toSnippet source surroundings (Just region)
1840 D.reflow $
1841 "I am partway through parsing a custom type, but I got stuck here:"
1843 D.stack
1844 [ D.reflow $
1845 "I was expecting to see a vertical bar like | next."
1846 , customTypeNote
1850 CT_IndentAfterBar row col ->
1851 let
1852 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1853 region = toRegion row col
1854 in
1855 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1856 Code.toSnippet source surroundings (Just region)
1858 D.reflow $
1859 "I am partway through parsing a custom type, but I got stuck here:"
1861 D.stack
1862 [ D.reflow $
1863 "I just saw a vertical bar, so I was expecting to see another variant defined next."
1864 , customTypeNote
1868 CT_IndentAfterEquals row col ->
1869 let
1870 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1871 region = toRegion row col
1872 in
1873 Report.Report "UNFINISHED CUSTOM TYPE" region [] $
1874 Code.toSnippet source surroundings (Just region)
1876 D.reflow $
1877 "I am partway through parsing a custom type, but I got stuck here:"
1879 D.stack
1880 [ D.reflow $
1881 "I just saw an equals sign, so I was expecting to see the first variant defined next."
1882 , customTypeNote
1887customTypeNote :: D.Doc
1888customTypeNote =
1889 D.stack
1890 [ D.toSimpleNote $
1891 "Here is an example of a valid `type` declaration for reference:"
1892 , D.vcat $
1893 [ D.indent 0 $ D.fillSep [D.cyan "type",D.green "Status"]
1894 , D.indent 2 $ D.fillSep ["=","Failure"]
1895 , D.indent 2 $ D.fillSep ["|","Waiting"]
1896 , D.indent 2 $ D.fillSep ["|","Success","String"]
1898 , D.reflow $
1899 "This defines a new `Status` type with three variants. This could be useful if\
1900 \ we are waiting for an HTTP request. Maybe we start with `Waiting` and then\
1901 \ switch to `Failure` or `Success \"message from server\"` depending on how\
1902 \ things go. Notice that the Success variant has some associated data, allowing\
1903 \ us to store a String if the request goes well!"
1908-- DECL DEF
1911toDeclDefReport :: Code.Source -> Name.Name -> DeclDef -> Row -> Col -> Report.Report
1912toDeclDefReport source name declDef startRow startCol =
1913 case declDef of
1914 DeclDefSpace space row col ->
1915 toSpaceReport source space row col
1917 DeclDefEquals row col ->
1918 case Code.whatIsNext source row col of
1919 Code.Keyword keyword ->
1920 let
1921 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1922 region = toKeywordRegion row col keyword
1923 in
1924 Report.Report "RESERVED WORD" region [] $
1925 Code.toSnippet source surroundings (Just region)
1927 D.reflow $
1928 "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
1930 D.reflow $
1931 "This is a reserved word! Try using some other name?"
1934 Code.Operator op ->
1935 let
1936 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1937 region = toKeywordRegion row col op
1938 in
1939 Report.Report "UNEXPECTED SYMBOL" region [] $
1940 Code.toSnippet source surroundings (Just region)
1942 D.reflow $
1943 "I was not expecting to see this symbol here:"
1945 D.stack
1946 [ D.reflow $
1947 "I am not sure what is going wrong exactly, so here is a valid\
1948 \ definition (with an optional type annotation) for reference:"
1949 , D.vcat
1950 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1951 , D.fillSep [D.green "add","x","y","="]
1952 , D.fillSep [" ","x","+","y"]
1954 , D.reflow $
1955 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
1959 _ ->
1960 let
1961 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1962 region = toRegion row col
1963 in
1964 Report.Report "PROBLEM IN DEFINITION" region [] $
1965 Code.toSnippet source surroundings (Just region)
1967 D.reflow $
1968 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
1970 D.stack
1971 [ D.reflow $
1972 "I am not sure what is going wrong exactly, so here is a valid\
1973 \ definition (with an optional type annotation) for reference:"
1974 , D.vcat
1975 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1976 , D.fillSep [D.green "add","x","y","="]
1977 , D.fillSep [" ","x","+","y"]
1979 , D.reflow $
1980 "Try to use that format!"
1984 DeclDefType tipe row col ->
1985 toTypeReport source (TC_Annotation name) tipe row col
1987 DeclDefArg pattern row col ->
1988 toPatternReport source PArg pattern row col
1990 DeclDefBody expr row col ->
1991 toExprReport source (InDef name startRow startCol) expr row col
1993 DeclDefNameRepeat row col ->
1994 let
1995 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1996 region = toRegion row col
1997 in
1998 Report.Report "EXPECTING DEFINITION" region [] $
1999 Code.toSnippet source surroundings (Just region)
2001 D.reflow $
2002 "I just saw the type annotation for `" ++ Name.toChars name
2003 ++ "` so I was expecting to see its definition here:"
2005 D.stack
2006 [ D.reflow $
2007 "Type annotations always appear directly above the relevant\
2008 \ definition, without anything else in between. (Not even doc comments!)"
2009 , declDefNote
2013 DeclDefNameMatch defName row col ->
2014 let
2015 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2016 region = toRegion row col
2017 in
2018 Report.Report "NAME MISMATCH" region [] $
2019 Code.toSnippet source surroundings (Just region)
2021 D.reflow $
2022 "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
2024 D.stack
2025 [ D.reflow $
2026 "These names do not match! Is there a typo?"
2027 , D.indent 4 $ D.fillSep $
2028 [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
2032 DeclDefIndentType row col ->
2033 let
2034 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2035 region = toRegion row col
2036 in
2037 Report.Report "UNFINISHED DEFINITION" region [] $
2038 Code.toSnippet source surroundings (Just region)
2040 D.reflow $
2041 "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
2043 D.stack
2044 [ D.reflow $
2045 "I just saw a colon, so I am expecting to see a type next."
2046 , declDefNote
2050 DeclDefIndentEquals row col ->
2051 let
2052 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2053 region = toRegion row col
2054 in
2055 Report.Report "UNFINISHED DEFINITION" region [] $
2056 Code.toSnippet source surroundings (Just region)
2058 D.reflow $
2059 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
2061 D.stack
2062 [ D.reflow $
2063 "I was expecting to see an argument or an equals sign next."
2064 , declDefNote
2068 DeclDefIndentBody row col ->
2069 let
2070 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2071 region = toRegion row col
2072 in
2073 Report.Report "UNFINISHED DEFINITION" region [] $
2074 Code.toSnippet source surroundings (Just region)
2076 D.reflow $
2077 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
2079 D.stack
2080 [ D.reflow $
2081 "I was expecting to see an expression next. What is it equal to?"
2082 , declDefNote
2087declDefNote :: D.Doc
2088declDefNote =
2089 D.stack
2090 [ D.reflow $
2091 "Here is a valid definition (with a type annotation) for reference:"
2092 , D.vcat
2093 [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
2094 , D.fillSep [D.green "add","x","y","="]
2095 , D.fillSep [" ","x","+","y"]
2097 , D.reflow $
2098 "The top line (called a \"type annotation\") is optional. You can leave it off\
2099 \ if you want. As you get more comfortable with Elm and as your project grows,\
2100 \ it becomes more and more valuable to add them though! They work great as\
2101 \ compiler-verified documentation, and they often improve error messages!"
2106-- CONTEXT
2109data Context
2110 = InNode Node Row Col Context
2111 | InDef Name.Name Row Col
2112 | InDestruct Row Col
2115data Node
2116 = NRecord
2117 | NParens
2118 | NList
2119 | NFunc
2120 | NCond
2121 | NThen
2122 | NElse
2123 | NCase
2124 | NBranch
2125 deriving (Eq)
2128getDefName :: Context -> Maybe Name.Name
2129getDefName context =
2130 case context of
2131 InDestruct _ _ -> Nothing
2132 InDef name _ _ -> Just name
2133 InNode _ _ _ c -> getDefName c
2136isWithin :: Node -> Context -> Bool
2137isWithin desiredNode context =
2138 case context of
2139 InDestruct _ _ -> False
2140 InDef _ _ _ -> False
2141 InNode actualNode _ _ _ -> desiredNode == actualNode
2145-- EXPR REPORTS
2148toExprReport :: Code.Source -> Context -> Expr -> Row -> Col -> Report.Report
2149toExprReport source context expr startRow startCol =
2150 case expr of
2151 Let let_ row col ->
2152 toLetReport source context let_ row col
2154 Case case_ row col ->
2155 toCaseReport source context case_ row col
2157 If if_ row col ->
2158 toIfReport source context if_ row col
2160 List list row col ->
2161 toListReport source context list row col
2163 Record record row col ->
2164 toRecordReport source context record row col
2166 Tuple tuple row col ->
2167 toTupleReport source context tuple row col
2169 Func func row col ->
2170 toFuncReport source context func row col
2172 Dot row col ->
2173 let region = toRegion row col in
2174 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
2175 Code.toSnippet source region Nothing
2177 D.reflow $
2178 "I was expecting to see a record accessor here:"
2180 D.fillSep
2181 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
2182 ,"that","accesses","a","value","from","a","record."
2186 Access row col ->
2187 let region = toRegion row col in
2188 Report.Report "EXPECTING RECORD ACCESSOR" region [] $
2189 Code.toSnippet source region Nothing
2191 D.reflow $
2192 "I am trying to parse a record accessor here:"
2194 D.stack
2196 D.fillSep
2197 ["Something","like",D.dullyellow".name","or",D.dullyellow".price"
2198 ,"that","accesses","a","value","from","a","record."
2201 D.toSimpleNote $
2202 "Record field names must start with a lower case letter!"
2206 OperatorRight op row col ->
2207 let region = toRegion row col in
2208 Report.Report "MISSING EXPRESSION" region [] $
2209 Code.toSnippet source region Nothing
2211 D.reflow $
2212 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
2214 D.stack
2215 [ D.fillSep $
2216 ["You","can","just","put","anything","for","now,","like"
2217 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2218 ,"Once","there","is","something","there,","I","can","probably"
2219 ,"give","a","more","specific","hint!"
2221 , D.toSimpleNote $
2222 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
2223 \ Or if I run into operators in unexpected spots. Point is, there are a\
2224 \ couple ways I can get confused and give sort of weird advice!"
2228 OperatorReserved operator row col ->
2229 toOperatorReport source context operator row col
2231 Start row col ->
2232 let
2233 (contextRow, contextCol, aThing) =
2234 case context of
2235 InDestruct r c -> (r, c, "a definition")
2236 InDef name r c -> (r, c, "the `" ++ Name.toChars name ++ "` definition")
2237 InNode NRecord r c _ -> (r, c, "a record")
2238 InNode NParens r c _ -> (r, c, "some parentheses")
2239 InNode NList r c _ -> (r, c, "a list")
2240 InNode NFunc r c _ -> (r, c, "an anonymous function")
2241 InNode NCond r c _ -> (r, c, "an `if` expression")
2242 InNode NThen r c _ -> (r, c, "an `if` expression")
2243 InNode NElse r c _ -> (r, c, "an `if` expression")
2244 InNode NCase r c _ -> (r, c, "a `case` expression")
2245 InNode NBranch r c _ -> (r, c, "a `case` expression")
2247 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
2248 region = toRegion row col
2249 in
2250 Report.Report "MISSING EXPRESSION" region [] $
2251 Code.toSnippet source surroundings (Just region)
2253 D.reflow $
2254 "I am partway through parsing " ++ aThing ++ ", but I got stuck here:"
2256 D.stack
2257 [ D.fillSep $
2258 ["I","was","expecting","to","see","an","expression","like"
2259 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2260 ,"Once","there","is","something","there,","I","can","probably"
2261 ,"give","a","more","specific","hint!"
2263 , D.toSimpleNote $
2264 "This can also happen if run into reserved words like `let` or `as` unexpectedly.\
2265 \ Or if I run into operators in unexpected spots. Point is, there are a\
2266 \ couple ways I can get confused and give sort of weird advice!"
2270 Char char row col ->
2271 toCharReport source char row col
2273 String string row col ->
2274 toStringReport source string row col
2276 Number number row col ->
2277 toNumberReport source number row col
2279 Space space row col ->
2280 toSpaceReport source space row col
2282 EndlessShader row col ->
2283 let
2284 region = toWiderRegion row col 6
2285 in
2286 Report.Report "ENDLESS SHADER" region [] $
2287 Code.toSnippet source region Nothing
2289 D.reflow "I cannot find the end of this shader:"
2291 D.reflow "Add a |] somewhere after this to end the shader."
2294 ShaderProblem problem row col ->
2295 let
2296 region = toRegion row col
2297 in
2298 Report.Report "SHADER PROBLEM" region [] $
2299 Code.toSnippet source region Nothing
2301 D.reflow $
2302 "I ran into a problem while parsing this GLSL block."
2304 D.stack
2305 [ D.reflow $
2306 "I use a 3rd party GLSL parser for now, and I did my best to extract their error message:"
2307 , D.indent 4 $ D.vcat $
2308 map D.fromChars (filter (/="") (lines problem))
2312 IndentOperatorRight op row col ->
2313 let
2314 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2315 region = toRegion row col
2316 in
2317 Report.Report "MISSING EXPRESSION" region [] $
2318 Code.toSnippet source surroundings (Just region)
2320 D.reflow $
2321 "I was expecting to see an expression after this " ++ Name.toChars op ++ " operator:"
2323 D.stack
2325 D.fillSep $
2326 ["You","can","just","put","anything","for","now,","like"
2327 ,D.dullyellow "42","or",D.dullyellow"\"hello\"" <> "."
2328 ,"Once","there","is","something","there,","I","can","probably"
2329 ,"give","a","more","specific","hint!"
2332 D.toSimpleNote $
2333 "I may be getting confused by your indentation? The easiest way to make sure\
2334 \ this is not an indentation problem is to put the expression on the right of\
2335 \ the " ++ Name.toChars op ++ " operator on the same line."
2341-- CHAR
2344toCharReport :: Code.Source -> Char -> Row -> Col -> Report.Report
2345toCharReport source char row col =
2346 case char of
2347 CharEndless ->
2348 let
2349 region = toRegion row col
2350 in
2351 Report.Report "MISSING SINGLE QUOTE" region [] $
2352 Code.toSnippet source region Nothing
2354 D.reflow $
2355 "I thought I was parsing a character, but I got to the end of\
2356 \ the line without seeing the closing single quote:"
2358 D.reflow $
2359 "Add a closing single quote here!"
2362 CharEscape escape ->
2363 toEscapeReport source escape row col
2365 CharNotString width ->
2366 let
2367 region = toWiderRegion row col width
2368 in
2369 Report.Report "NEEDS DOUBLE QUOTES" region [] $
2370 Code.toSnippet source region Nothing
2372 "The following string uses single quotes:"
2374 D.stack
2375 [ "Please switch to double quotes instead:"
2376 , D.indent 4 $
2377 D.dullyellow "'this'" <> " => " <> D.green "\"this\""
2378 , D.toSimpleNote $
2379 "Elm uses double quotes for strings like \"hello\", whereas it uses single\
2380 \ quotes for individual characters like 'a' and 'ø'. This distinction helps with\
2381 \ code like (String.any (\\c -> c == 'X') \"90210\") where you are inspecting\
2382 \ individual characters."
2388-- STRING
2391toStringReport :: Code.Source -> String -> Row -> Col -> Report.Report
2392toStringReport source string row col =
2393 case string of
2394 StringEndless_Single ->
2395 let
2396 region = toRegion row col
2397 in
2398 Report.Report "ENDLESS STRING" region [] $
2399 Code.toSnippet source region Nothing
2401 D.reflow $
2402 "I got to the end of the line without seeing the closing double quote:"
2404 D.stack
2405 [ D.fillSep $
2406 ["Strings","look","like",D.green "\"this\"","with","double"
2407 ,"quotes","on","each","end.","Is","the","closing","double"
2408 ,"quote","missing","in","your","code?"
2410 , D.toSimpleNote $
2411 "For a string that spans multiple lines, you can use the multi-line string\
2412 \ syntax like this:"
2413 , D.vcat
2414 [ D.fillSep [D.green "markdown","=",D.dullyellow "\"\"\""]
2415 , mempty
2416 , D.dullyellow "# Multi-line Strings"
2417 , mempty
2418 , D.dullyellow "- start with triple double quotes"
2419 , D.dullyellow "- write whatever you want"
2420 , D.dullyellow "- no need to escape newlines or double quotes"
2421 , D.dullyellow "- end with triple double quotes"
2422 , mempty
2423 , D.dullyellow "\"\"\""
2428 StringEndless_Multi ->
2429 let
2430 region = toWiderRegion row col 3
2431 in
2432 Report.Report "ENDLESS STRING" region [] $
2433 Code.toSnippet source region Nothing
2435 D.reflow $
2436 "I cannot find the end of this multi-line string:"
2438 D.stack
2439 [ D.reflow "Add a \"\"\" somewhere after this to end the string."
2440 , D.toSimpleNote $
2441 "Here is a valid multi-line string for reference:"
2442 , D.vcat
2443 [ D.fillSep [D.green "markdown","=",D.dullyellow "\"\"\""]
2444 , mempty
2445 , D.dullyellow "# Multi-line Strings"
2446 , mempty
2447 , D.dullyellow "- start with triple double quotes"
2448 , D.dullyellow "- write whatever you want"
2449 , D.dullyellow "- no need to escape newlines or double quotes"
2450 , D.dullyellow "- end with triple double quotes"
2451 , mempty
2452 , D.dullyellow "\"\"\""
2457 StringEscape escape ->
2458 toEscapeReport source escape row col
2462-- ESCAPES
2465toEscapeReport :: Code.Source -> Escape -> Row -> Col -> Report.Report
2466toEscapeReport source escape row col =
2467 case escape of
2468 EscapeUnknown ->
2469 let
2470 region = toWiderRegion row col 2
2471 in
2472 Report.Report "UNKNOWN ESCAPE" region [] $
2473 Code.toSnippet source region Nothing
2475 D.reflow $
2476 "Backslashes always start escaped characters, but I do not recognize this one:"
2478 D.stack
2479 [ D.reflow $
2480 "Valid escape characters include:"
2481 , D.dullyellow $ D.indent 4 $ D.vcat $
2482 [ "\\n"
2483 , "\\r"
2484 , "\\t"
2485 , "\\\""
2486 , "\\\'"
2487 , "\\\\"
2488 , "\\u{003D}"
2490 , D.reflow $
2491 "Do you want one of those instead? Maybe you need \\\\ to escape a backslash?"
2492 , D.toSimpleNote $
2493 "The last style lets encode ANY character by its Unicode code\
2494 \ point. That means \\u{0009} and \\t are the same. You can use\
2495 \ that style for anything not covered by the other six escapes!"
2499 BadUnicodeFormat width ->
2500 let
2501 region = toWiderRegion row col width
2502 in
2503 Report.Report "BAD UNICODE ESCAPE" region [] $
2504 Code.toSnippet source region Nothing
2506 D.reflow $
2507 "I ran into an invalid Unicode escape:"
2509 D.stack
2510 [ D.reflow $
2511 "Here are some examples of valid Unicode escapes:"
2512 , D.dullyellow $ D.indent 4 $ D.vcat $
2513 [ "\\u{0041}"
2514 , "\\u{03BB}"
2515 , "\\u{6728}"
2516 , "\\u{1F60A}"
2518 , D.reflow $
2519 "Notice that the code point is always surrounded by curly braces.\
2520 \ Maybe you are missing the opening or closing curly brace?"
2523 BadUnicodeCode width ->
2524 let
2525 region = toWiderRegion row col width
2526 in
2527 Report.Report "BAD UNICODE ESCAPE" region [] $
2528 Code.toSnippet source region Nothing
2530 D.reflow $
2531 "This is not a valid code point:"
2533 D.reflow $
2534 "The valid code points are between 0 and 10FFFF inclusive."
2537 BadUnicodeLength width numDigits badCode ->
2538 let
2539 region = toWiderRegion row col width
2540 in
2541 Report.Report "BAD UNICODE ESCAPE" region [] $
2542 Code.toSnippet source region Nothing $
2543 if numDigits < 4 then
2545 D.reflow $
2546 "Every code point needs at least four digits:"
2548 let
2549 goodCode = replicate (4 - numDigits) '0' ++ map Char.toUpper (showHex badCode "")
2550 suggestion = "\\u{" <> D.fromChars goodCode <> "}"
2551 in
2552 D.fillSep ["Try",D.green suggestion,"instead?"]
2555 else
2557 D.reflow $
2558 "This code point has too many digits:"
2560 D.fillSep $
2561 ["Valid","code","points","are","between"
2562 ,D.green "\\u{0000}","and",D.green "\\u{10FFFF}" <> ","
2563 ,"so","try","trimming","any","leading","zeros","until"
2564 ,"you","have","between","four","and","six","digits."
2570-- NUMBERS
2573toNumberReport :: Code.Source -> Number -> Row -> Col -> Report.Report
2574toNumberReport source number row col =
2575 let
2576 region = toRegion row col
2577 in
2578 case number of
2579 NumberEnd ->
2580 Report.Report "WEIRD NUMBER" region [] $
2581 Code.toSnippet source region Nothing
2583 D.reflow $
2584 "I thought I was reading a number, but I ran into some weird stuff here:"
2586 D.stack
2587 [ D.reflow $
2588 "I recognize numbers in the following formats:"
2589 , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
2590 , D.reflow $
2591 "So is there a way to write it like one of those?"
2595 NumberDot int ->
2596 Report.Report "WEIRD NUMBER" region [] $
2597 Code.toSnippet source region Nothing
2599 D.reflow $
2600 "Numbers cannot end with a dot like this:"
2602 D.fillSep
2603 ["Switching","to",D.green (D.fromChars (show int))
2604 ,"or",D.green (D.fromChars (show int ++ ".0"))
2605 ,"will","work","though!"
2609 NumberHexDigit ->
2610 Report.Report "WEIRD HEXIDECIMAL" region [] $
2611 Code.toSnippet source region Nothing
2613 D.reflow $
2614 "I thought I was reading a hexidecimal number until I got here:"
2616 D.stack
2617 [ D.reflow $
2618 "Valid hexidecimal digits include 0123456789abcdefABCDEF, so I can\
2619 \ only recognize things like this:"
2620 , D.indent 4 $ D.vcat [ "0x2B", "0x002B", "0x00ffb3" ]
2624 NumberNoLeadingZero ->
2625 Report.Report "LEADING ZEROS" region [] $
2626 Code.toSnippet source region Nothing
2628 D.reflow $
2629 "I do not accept numbers with leading zeros:"
2631 D.stack
2632 [ D.reflow $
2633 "Just delete the leading zeros and it should work!"
2634 , D.toSimpleNote $
2635 "Some languages let you to specify octal numbers by adding a leading zero.\
2636 \ So in C, writing 0111 is the same as writing 73. Some people are used to\
2637 \ that, but others probably want it to equal 111. Either path is going to\
2638 \ surprise people from certain backgrounds, so Elm tries to avoid this whole\
2639 \ situation."
2645-- OPERATORS
2648toOperatorReport :: Code.Source -> Context -> BadOperator -> Row -> Col -> Report.Report
2649toOperatorReport source context operator row col =
2650 case operator of
2651 BadDot ->
2652 let
2653 region = toRegion row col
2654 in
2655 Report.Report "UNEXPECTED SYMBOL" region [] $
2656 Code.toSnippet source region Nothing
2658 "I was not expecting this dot:"
2660 D.reflow $
2661 "Dots are for record access and decimal points, so\
2662 \ they cannot float around on their own. Maybe\
2663 \ there is some extra whitespace?"
2666 BadPipe ->
2667 let
2668 region = toRegion row col
2669 in
2670 Report.Report "UNEXPECTED SYMBOL" region [] $
2671 Code.toSnippet source region Nothing
2673 D.reflow $
2674 "I was not expecting this vertical bar:"
2676 D.reflow $
2677 "Vertical bars should only appear in custom type declarations. Maybe you want || instead?"
2680 BadArrow ->
2681 let
2682 region = toWiderRegion row col 2
2683 in
2684 Report.Report "UNEXPECTED ARROW" region [] $
2685 Code.toSnippet source region Nothing $
2686 if isWithin NCase context then
2688 D.reflow $
2689 "I am parsing a `case` expression right now, but this arrow is confusing me:"
2691 D.stack
2692 [ D.reflow "Maybe the `of` keyword is missing on a previous line?"
2693 , noteForCaseError
2697 else if isWithin NBranch context then
2699 D.reflow $
2700 "I am parsing a `case` expression right now, but this arrow is confusing me:"
2702 D.stack
2703 [ D.reflow $
2704 "It makes sense to see arrows around here, so I suspect it is something earlier."
2705 , noteForCaseIndentError
2709 else
2711 D.reflow $
2712 "I was not expecting an arrow here:"
2714 D.reflow $
2715 "Arrows should only appear in `case` expressions and anonymous functions. Maybe\
2716 \ you want > or >= instead?"
2719 BadEquals ->
2720 let
2721 region = toRegion row col
2722 in
2723 Report.Report "UNEXPECTED EQUALS" region [] $
2724 Code.toSnippet source region Nothing
2726 D.reflow $
2727 "I was not expecting to see this equals sign:"
2729 D.stack
2731 D.reflow "Maybe you want == instead? To check if two values are equal?"
2733 D.toSimpleNote $
2734 if isWithin NRecord context then
2735 "Records look like { x = 3, y = 4 } with the equals sign right\
2736 \ after the field name. So maybe you forgot a comma?"
2737 else
2738 case getDefName context of
2739 Nothing ->
2740 "I may be getting confused by your indentation. I need all definitions to be indented\
2741 \ exactly the same amount, so if this is meant to be a new definition, it may have too\
2742 \ many spaces in front of it."
2744 Just name ->
2745 "I may be getting confused by your indentation. I think I am still parsing the `"
2746 ++ Name.toChars name ++ "` definition. Is this supposed to be part of a definition\
2747 \ after that? If so, the problem may be a bit before the equals sign. I need all\
2748 \ definitions to be indented exactly the same amount, so the problem may be that\
2749 \ this new definition has too many spaces in front of it."
2753 BadHasType ->
2754 let
2755 region = toRegion row col
2756 in
2757 Report.Report "UNEXPECTED SYMBOL" region [] $
2758 Code.toSnippet source region Nothing $
2760 D.reflow $
2761 "I was not expecting to run into the \"has type\" symbol here:"
2763 case getDefName context of
2764 Nothing ->
2765 D.fillSep
2766 ["Maybe","you","want",D.green "::","instead?"
2767 ,"To","put","something","on","the","front","of","a","list?"
2770 Just name ->
2771 D.stack
2773 D.fillSep
2774 ["Maybe","you","want",D.green "::","instead?"
2775 ,"To","put","something","on","the","front","of","a","list?"
2777 , D.toSimpleNote $
2778 "The single colon is reserved for type annotations and record types, but I think\
2779 \ I am parsing the definition of `" ++ Name.toChars name ++ "` right now."
2781 D.toSimpleNote $
2782 "I may be getting confused by your indentation. Is this supposed to be part of\
2783 \ a type annotation AFTER the `" ++ Name.toChars name ++ "` definition? If so,\
2784 \ the problem may be a bit before the \"has type\" symbol. I need all definitions to\
2785 \ be exactly aligned (with exactly the same indentation) so the problem may be that\
2786 \ this new definition is indented a bit too much."
2792-- CASE
2795toLetReport :: Code.Source -> Context -> Let -> Row -> Col -> Report.Report
2796toLetReport source context let_ startRow startCol =
2797 case let_ of
2798 LetSpace space row col ->
2799 toSpaceReport source space row col
2801 LetIn row col ->
2802 let
2803 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2804 region = toRegion row col
2805 in
2806 Report.Report "LET PROBLEM" region [] $
2807 Code.toSnippet source surroundings (Just region)
2809 D.reflow $
2810 "I was partway through parsing a `let` expression, but I got stuck here:"
2812 D.stack
2813 [ D.fillSep $
2814 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2815 ,"keyword","next.","Is","there","a","typo?"
2817 , D.toSimpleNote $
2818 "This can also happen if you are trying to define another value within the `let` but\
2819 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2820 \ spaces before it. They should line up exactly!"
2824 LetDefAlignment _ row col ->
2825 let
2826 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2827 region = toRegion row col
2828 in
2829 Report.Report "LET PROBLEM" region [] $
2830 Code.toSnippet source surroundings (Just region)
2832 D.reflow $
2833 "I was partway through parsing a `let` expression, but I got stuck here:"
2835 D.stack
2836 [ D.fillSep $
2837 ["Based","on","the","indentation,","I","was","expecting","to","see","the",D.cyan "in"
2838 ,"keyword","next.","Is","there","a","typo?"
2840 , D.toSimpleNote $
2841 "This can also happen if you are trying to define another value within the `let` but\
2842 \ it is not indented enough. Make sure each definition has exactly the same amount of\
2843 \ spaces before it. The should line up exactly!"
2847 LetDefName row col ->
2848 case Code.whatIsNext source row col of
2849 Code.Keyword keyword ->
2850 let
2851 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2852 region = toKeywordRegion row col keyword
2853 in
2854 Report.Report "RESERVED WORD" region [] $
2855 Code.toSnippet source surroundings (Just region)
2857 D.reflow $
2858 "I was partway through parsing a `let` expression, but I got stuck here:"
2860 D.reflow $
2861 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but\
2862 \ it is a reserved word! Try using a different name instead."
2865 _ ->
2866 toUnfinishLetReport source row col startRow startCol $
2867 D.reflow $
2868 "I was expecting the name of a definition next."
2870 LetDef name def row col ->
2871 toLetDefReport source name def row col
2873 LetDestruct destruct row col ->
2874 toLetDestructReport source destruct row col
2876 LetBody expr row col ->
2877 toExprReport source context expr row col
2879 LetIndentDef row col ->
2880 toUnfinishLetReport source row col startRow startCol $
2881 D.reflow $
2882 "I was expecting a value to be defined here."
2884 LetIndentIn row col ->
2885 toUnfinishLetReport source row col startRow startCol $
2886 D.fillSep $
2887 ["I","was","expecting","to","see","the",D.cyan "in","keyword","next."
2888 ,"Or","maybe","more","of","that","expression?"
2891 LetIndentBody row col ->
2892 toUnfinishLetReport source row col startRow startCol $
2893 D.reflow $
2894 "I was expecting an expression next. Tell me what should happen with the value you just defined!"
2897toUnfinishLetReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
2898toUnfinishLetReport source row col startRow startCol message =
2899 let
2900 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2901 region = toRegion row col
2902 in
2903 Report.Report "UNFINISHED LET" region [] $
2904 Code.toSnippet source surroundings (Just region)
2906 D.reflow $
2907 "I was partway through parsing a `let` expression, but I got stuck here:"
2909 D.stack
2910 [ message
2911 , D.toSimpleNote $
2912 "Here is an example with a valid `let` expression for reference:"
2913 , D.vcat $
2914 [ D.indent 0 $ D.fillSep [D.green "viewPerson","person","="]
2915 , D.indent 2 $ D.fillSep [D.cyan "let"]
2916 , D.indent 4 $ D.fillSep ["fullName","="]
2917 , D.indent 6 $ D.fillSep ["person.firstName","++",D.dullyellow "\" \"","++","person.lastName"]
2918 , D.indent 2 $ D.fillSep [D.cyan "in"]
2919 , D.indent 2 $ D.fillSep ["div","[]","[","text","fullName","]"]
2921 , D.reflow $
2922 "Here we defined a `viewPerson` function that turns a person into some HTML. We use\
2923 \ a `let` expression to define the `fullName` we want to show. Notice the indentation! The\
2924 \ `fullName` is indented more than the `let` keyword, and the actual value of `fullName` is\
2925 \ indented a bit more than that. That is important!"
2930toLetDefReport :: Code.Source -> Name.Name -> Def -> Row -> Col -> Report.Report
2931toLetDefReport source name def startRow startCol =
2932 case def of
2933 DefSpace space row col ->
2934 toSpaceReport source space row col
2936 DefType tipe row col ->
2937 toTypeReport source (TC_Annotation name) tipe row col
2939 DefNameRepeat row col ->
2940 let
2941 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2942 region = toRegion row col
2943 in
2944 Report.Report "EXPECTING DEFINITION" region [] $
2945 Code.toSnippet source surroundings (Just region)
2947 D.reflow $
2948 "I just saw the type annotation for `" ++ Name.toChars name
2949 ++ "` so I was expecting to see its definition here:"
2951 D.stack
2952 [ D.reflow $
2953 "Type annotations always appear directly above the relevant\
2954 \ definition, without anything else in between."
2955 , defNote
2959 DefNameMatch defName row col ->
2960 let
2961 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2962 region = toRegion row col
2963 in
2964 Report.Report "NAME MISMATCH" region [] $
2965 Code.toSnippet source surroundings (Just region)
2967 D.reflow $
2968 "I just saw a type annotation for `" ++ Name.toChars name ++ "`, but it is followed by a definition for `" ++ Name.toChars defName ++ "`:"
2970 D.stack
2971 [ D.reflow $
2972 "These names do not match! Is there a typo?"
2973 , D.indent 4 $ D.fillSep $
2974 [D.dullyellow (D.fromName defName),"->",D.green (D.fromName name)]
2978 DefArg pattern row col ->
2979 toPatternReport source PArg pattern row col
2981 DefEquals row col ->
2982 case Code.whatIsNext source row col of
2983 Code.Keyword keyword ->
2984 let
2985 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
2986 region = toKeywordRegion row col keyword
2987 in
2988 Report.Report "RESERVED WORD" region [] $
2989 Code.toSnippet source surroundings (Just region)
2991 D.reflow $
2992 "It looks like you are trying to use `" ++ keyword ++ "` as an argument:"
2994 D.reflow $
2995 "This is a reserved word! Try using some other name?"
2998 Code.Operator op ->
2999 let
3000 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3001 region = toKeywordRegion row col op
3002 in
3003 Report.Report "UNEXPECTED SYMBOL" region [] $
3004 Code.toSnippet source surroundings (Just region)
3006 D.reflow $
3007 "I was not expecting to see this symbol here:"
3009 D.stack
3010 [ D.reflow $
3011 "I am not sure what is going wrong exactly, so here is a valid\
3012 \ definition (with an optional type annotation) for reference:"
3013 , D.vcat
3014 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3015 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3016 , D.indent 6 $ D.fillSep ["x","+","y"]
3018 , D.reflow $
3019 "Try to use that format with your `" ++ Name.toChars name ++ "` definition!"
3023 _ ->
3024 let
3025 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3026 region = toRegion row col
3027 in
3028 Report.Report "PROBLEM IN DEFINITION" region [] $
3029 Code.toSnippet source surroundings (Just region)
3031 D.reflow $
3032 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3034 D.stack
3035 [ D.reflow $
3036 "I am not sure what is going wrong exactly, so here is a valid\
3037 \ definition (with an optional type annotation) for reference:"
3038 , D.vcat
3039 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3040 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3041 , D.indent 4 $ D.fillSep ["x","+","y"]
3043 , D.reflow $
3044 "Try to use that format!"
3048 DefBody expr row col ->
3049 toExprReport source (InDef name startRow startCol) expr row col
3051 DefIndentEquals row col ->
3052 let
3053 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3054 region = toRegion row col
3055 in
3056 Report.Report "UNFINISHED DEFINITION" region [] $
3057 Code.toSnippet source surroundings (Just region)
3059 D.reflow $
3060 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3062 D.stack
3063 [ D.reflow $
3064 "I was expecting to see an argument or an equals sign next."
3065 , defNote
3069 DefIndentType row col ->
3070 let
3071 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3072 region = toRegion row col
3073 in
3074 Report.Report "UNFINISHED DEFINITION" region [] $
3075 Code.toSnippet source surroundings (Just region)
3077 D.reflow $
3078 "I got stuck while parsing the `" ++ Name.toChars name ++ "` type annotation:"
3080 D.stack
3081 [ D.reflow $
3082 "I just saw a colon, so I am expecting to see a type next."
3083 , defNote
3087 DefIndentBody row col ->
3088 let
3089 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3090 region = toRegion row col
3091 in
3092 Report.Report "UNFINISHED DEFINITION" region [] $
3093 Code.toSnippet source surroundings (Just region)
3095 D.reflow $
3096 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3098 D.stack
3099 [ D.reflow $
3100 "I was expecting to see an expression next. What is it equal to?"
3101 , declDefNote
3105 DefAlignment indent row col ->
3106 let
3107 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3108 region = toRegion row col
3109 offset = indent - col
3110 in
3111 Report.Report "PROBLEM IN DEFINITION" region [] $
3112 Code.toSnippet source surroundings (Just region)
3114 D.reflow $
3115 "I got stuck while parsing the `" ++ Name.toChars name ++ "` definition:"
3117 D.reflow $
3118 "I just saw a type annotation indented " ++ show indent ++ " spaces, so I was\
3119 \ expecting to see the corresponding definition next with the exact same amount\
3120 \ of indentation. It looks like this line needs "
3121 ++ show offset ++ " more " ++ (if offset == 1 then "space" else "spaces") ++ "?"
3126defNote :: D.Doc
3127defNote =
3128 D.stack
3129 [ D.reflow $
3130 "Here is a valid definition (with a type annotation) for reference:"
3131 , D.vcat
3132 [ D.indent 4 $ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
3133 , D.indent 4 $ D.fillSep [D.green "add","x","y","="]
3134 , D.indent 6 $ D.fillSep ["x","+","y"]
3136 , D.reflow $
3137 "The top line (called a \"type annotation\") is optional. You can leave it off\
3138 \ if you want. As you get more comfortable with Elm and as your project grows,\
3139 \ it becomes more and more valuable to add them though! They work great as\
3140 \ compiler-verified documentation, and they often improve error messages!"
3144toLetDestructReport :: Code.Source -> Destruct -> Row -> Col -> Report.Report
3145toLetDestructReport source destruct startRow startCol =
3146 case destruct of
3147 DestructSpace space row col ->
3148 toSpaceReport source space row col
3150 DestructPattern pattern row col ->
3151 toPatternReport source PLet pattern row col
3153 DestructEquals row col ->
3154 let
3155 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3156 region = toRegion row col
3157 in
3158 Report.Report "PROBLEM IN DEFINITION" region [] $
3159 Code.toSnippet source surroundings (Just region)
3161 D.reflow $
3162 "I got stuck trying to parse this definition:"
3164 case Code.whatIsNext source row col of
3165 Code.Operator ":" ->
3166 D.stack
3167 [ D.reflow $
3168 "I was expecting to see an equals sign next, followed by an expression\
3169 \ telling me what to compute."
3170 , D.toSimpleNote $
3171 "It looks like you may be trying to write a type annotation? It is not\
3172 \ possible to add type annotations on destructuring definitions like this.\
3173 \ You can assign a name to the overall structure, put a type annotation on\
3174 \ that, and then destructure separately though."
3177 _ ->
3178 D.reflow $
3179 "I was expecting to see an equals sign next, followed by an expression\
3180 \ telling me what to compute."
3183 DestructBody expr row col ->
3184 toExprReport source (InDestruct startRow startCol) expr row col
3186 DestructIndentEquals row col ->
3187 let
3188 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3189 region = toRegion row col
3190 in
3191 Report.Report "UNFINISHED DEFINITION" region [] $
3192 Code.toSnippet source surroundings (Just region)
3194 D.reflow $
3195 "I got stuck trying to parse this definition:"
3197 D.reflow $
3198 "I was expecting to see an equals sign next, followed by an expression\
3199 \ telling me what to compute."
3202 DestructIndentBody row col ->
3203 let
3204 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3205 region = toRegion row col
3206 in
3207 Report.Report "UNFINISHED DEFINITION" region [] $
3208 Code.toSnippet source surroundings (Just region)
3210 D.reflow $
3211 "I got stuck while parsing this definition:"
3213 D.reflow $
3214 "I was expecting to see an expression next. What is it equal to?"
3219-- CASE
3222toCaseReport :: Code.Source -> Context -> Case -> Row -> Col -> Report.Report
3223toCaseReport source context case_ startRow startCol =
3224 case case_ of
3225 CaseSpace space row col ->
3226 toSpaceReport source space row col
3228 CaseOf row col ->
3229 toUnfinishCaseReport source row col startRow startCol $
3230 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
3232 CasePattern pattern row col ->
3233 toPatternReport source PCase pattern row col
3235 CaseArrow row col ->
3236 case Code.whatIsNext source row col of
3237 Code.Keyword keyword ->
3238 let
3239 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3240 region = toKeywordRegion row col keyword
3241 in
3242 Report.Report "RESERVED WORD" region [] $
3243 Code.toSnippet source surroundings (Just region)
3245 D.reflow $
3246 "I am partway through parsing a `case` expression, but I got stuck here:"
3248 D.reflow $
3249 "It looks like you are trying to use `" ++ keyword ++ "` in one of your\
3250 \ patterns, but it is a reserved word. Try using a different name?"
3253 Code.Operator ":" ->
3254 let
3255 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3256 region = toRegion row col
3257 in
3258 Report.Report "UNEXPECTED OPERATOR" region [] $
3259 Code.toSnippet source surroundings (Just region)
3261 D.reflow $
3262 "I am partway through parsing a `case` expression, but I got stuck here:"
3264 D.fillSep $
3265 ["I","am","seeing",D.dullyellow ":","but","maybe","you","want",D.green "::","instead?"
3266 ,"For","pattern","matching","on","lists?"
3270 Code.Operator "=" ->
3271 let
3272 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3273 region = toRegion row col
3274 in
3275 Report.Report "UNEXPECTED OPERATOR" region [] $
3276 Code.toSnippet source surroundings (Just region)
3278 D.reflow $
3279 "I am partway through parsing a `case` expression, but I got stuck here:"
3281 D.fillSep $
3282 ["I","am","seeing",D.dullyellow "=","but","maybe","you","want",D.green "->","instead?"
3286 _ ->
3287 let
3288 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3289 region = toRegion row col
3290 in
3291 Report.Report "MISSING ARROW" region [] $
3292 Code.toSnippet source surroundings (Just region)
3294 D.reflow $
3295 "I am partway through parsing a `case` expression, but I got stuck here:"
3297 D.stack
3298 [ D.reflow "I was expecting to see an arrow next."
3299 , noteForCaseIndentError
3303 CaseExpr expr row col ->
3304 toExprReport source (InNode NCase startRow startCol context) expr row col
3306 CaseBranch expr row col ->
3307 toExprReport source (InNode NBranch startRow startCol context) expr row col
3309 CaseIndentOf row col ->
3310 toUnfinishCaseReport source row col startRow startCol $
3311 D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
3313 CaseIndentExpr row col ->
3314 toUnfinishCaseReport source row col startRow startCol $
3315 D.reflow "I was expecting to see a expression next."
3317 CaseIndentPattern row col ->
3318 toUnfinishCaseReport source row col startRow startCol $
3319 D.reflow "I was expecting to see a pattern next."
3321 CaseIndentArrow row col ->
3322 toUnfinishCaseReport source row col startRow startCol $
3323 D.fillSep
3324 ["I","just","saw","a","pattern,","so","I","was","expecting"
3325 ,"to","see","a",D.dullyellow "->","next."
3328 CaseIndentBranch row col ->
3329 toUnfinishCaseReport source row col startRow startCol $
3330 D.reflow $
3331 "I was expecting to see an expression next. What should I do when\
3332 \ I run into this particular pattern?"
3334 CasePatternAlignment indent row col ->
3335 toUnfinishCaseReport source row col startRow startCol $
3336 D.reflow $
3337 "I suspect this is a pattern that is not indented far enough? (" ++ show indent ++ " spaces)"
3340toUnfinishCaseReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
3341toUnfinishCaseReport source row col startRow startCol message =
3342 let
3343 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3344 region = toRegion row col
3345 in
3346 Report.Report "UNFINISHED CASE" region [] $
3347 Code.toSnippet source surroundings (Just region)
3349 D.reflow $
3350 "I was partway through parsing a `case` expression, but I got stuck here:"
3352 D.stack
3353 [ message
3354 , noteForCaseError
3359noteForCaseError :: D.Doc
3360noteForCaseError =
3361 D.stack
3362 [ D.toSimpleNote $
3363 "Here is an example of a valid `case` expression for reference."
3364 , D.vcat $
3365 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3366 , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
3367 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3368 , ""
3369 , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
3370 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3372 , D.reflow $
3373 "Notice the indentation. Each pattern is aligned, and each branch is indented\
3374 \ a bit more than the corresponding pattern. That is important!"
3378noteForCaseIndentError :: D.Doc
3379noteForCaseIndentError =
3380 D.stack
3381 [ D.toSimpleNote $
3382 "Sometimes I get confused by indentation, so try to make your `case` look\
3383 \ something like this:"
3384 , D.vcat $
3385 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3386 , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
3387 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3388 , ""
3389 , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
3390 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3392 , D.reflow $
3393 "Notice the indentation! Each pattern is aligned, and each branch is indented\
3394 \ a bit more than the corresponding pattern. That is important!"
3399-- IF
3402toIfReport :: Code.Source -> Context -> If -> Row -> Col -> Report.Report
3403toIfReport source context if_ startRow startCol =
3404 case if_ of
3405 IfSpace space row col ->
3406 toSpaceReport source space row col
3408 IfThen row col ->
3409 let
3410 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3411 region = toRegion row col
3412 in
3413 Report.Report "UNFINISHED IF" region [] $
3414 Code.toSnippet source surroundings (Just region)
3416 D.reflow $
3417 "I was expecting to see more of this `if` expression, but I got stuck here:"
3419 D.fillSep $
3420 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
3424 IfElse row col ->
3425 let
3426 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3427 region = toRegion row col
3428 in
3429 Report.Report "UNFINISHED IF" region [] $
3430 Code.toSnippet source surroundings (Just region)
3432 D.reflow $
3433 "I was expecting to see more of this `if` expression, but I got stuck here:"
3435 D.fillSep $
3436 ["I","was","expecting","to","see","the",D.cyan "else","keyword","next."
3440 IfElseBranchStart row col ->
3441 let
3442 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3443 region = toRegion row col
3444 in
3445 Report.Report "UNFINISHED IF" region [] $
3446 Code.toSnippet source surroundings (Just region)
3448 D.reflow $
3449 "I just saw the start of an `else` branch, but then I got stuck here:"
3451 D.reflow $
3452 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3455 IfCondition expr row col ->
3456 toExprReport source (InNode NCond startRow startCol context) expr row col
3458 IfThenBranch expr row col ->
3459 toExprReport source (InNode NThen startRow startCol context) expr row col
3461 IfElseBranch expr row col ->
3462 toExprReport source (InNode NElse startRow startCol context) expr row col
3464 IfIndentCondition row col ->
3465 let
3466 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3467 region = toRegion row col
3468 in
3469 Report.Report "UNFINISHED IF" region [] $
3470 Code.toSnippet source surroundings (Just region)
3472 D.reflow $
3473 "I was expecting to see more of this `if` expression, but I got stuck here:"
3475 D.stack
3476 [ D.fillSep $
3477 ["I","was","expecting","to","see","an","expression","like",D.dullyellow "x < 0"
3478 ,"that","evaluates","to","True","or","False."
3480 , D.toSimpleNote $
3481 "I can be confused by indentation. Maybe something is not indented enough?"
3485 IfIndentThen row col ->
3486 let
3487 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3488 region = toRegion row col
3489 in
3490 Report.Report "UNFINISHED IF" region [] $
3491 Code.toSnippet source surroundings (Just region)
3493 D.reflow $
3494 "I was expecting to see more of this `if` expression, but I got stuck here:"
3496 D.stack
3497 [ D.fillSep $
3498 ["I","was","expecting","to","see","the",D.cyan "then","keyword","next."
3500 , D.toSimpleNote $
3501 "I can be confused by indentation. Maybe something is not indented enough?"
3505 IfIndentThenBranch row col ->
3506 let
3507 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3508 region = toRegion row col
3509 in
3510 Report.Report "UNFINISHED IF" region [] $
3511 Code.toSnippet source surroundings (Just region)
3513 D.reflow $
3514 "I got stuck after the start of this `then` branch:"
3516 D.stack
3517 [ D.reflow $
3518 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3519 , D.toSimpleNote $
3520 "I can be confused by indentation, so if the `then` branch is already\
3521 \ present, it may not be indented enough for me to recognize it."
3525 IfIndentElseBranch row col ->
3526 let
3527 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3528 region = toRegion row col
3529 in
3530 Report.Report "UNFINISHED IF" region [] $
3531 Code.toSnippet source surroundings (Just region)
3533 D.reflow $
3534 "I got stuck after the start of this `else` branch:"
3536 D.stack
3537 [ D.reflow $
3538 "I was expecting to see an expression next. Maybe it is not filled in yet?"
3539 , D.toSimpleNote $
3540 "I can be confused by indentation, so if the `else` branch is already\
3541 \ present, it may not be indented enough for me to recognize it."
3545 IfIndentElse row col ->
3546 let
3547 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3548 region = toRegion row col
3549 in
3550 Report.Report "UNFINISHED IF" region [] $
3551 Code.toSnippet source surroundings (Just region)
3553 D.reflow $
3554 "I was expecting to see an `else` branch after this:"
3556 D.stack
3557 [ D.reflow $
3558 "I know what to do when the condition is `True`, but not when it is `False`."
3559 , D.fillSep
3560 ["Add","an",D.cyan "else","branch","to","handle","that","scenario!"]
3566-- RECORD
3569toRecordReport :: Code.Source -> Context -> Record -> Row -> Col -> Report.Report
3570toRecordReport source context record startRow startCol =
3571 case record of
3572 RecordOpen row col ->
3573 case Code.whatIsNext source row col of
3574 Code.Keyword keyword ->
3575 let
3576 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3577 region = toKeywordRegion row col keyword
3578 in
3579 Report.Report "RESERVED WORD" region [] $
3580 Code.toSnippet source surroundings (Just region)
3582 D.reflow $
3583 "I just started parsing a record, but I got stuck on this field name:"
3585 D.reflow $
3586 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3587 \ that is a reserved word. Try using a different name!"
3590 _ ->
3591 let
3592 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3593 region = toRegion row col
3594 in
3595 Report.Report "UNFINISHED RECORD" region [] $
3596 Code.toSnippet source surroundings (Just region)
3598 D.reflow $
3599 "I just started parsing a record, but I got stuck here:"
3601 D.fillSep
3602 ["Records","look","like",D.dullyellow "{ x = 3, y = 4 },"
3603 ,"so","I","was","expecting","to","see","a","field","name","next."
3607 RecordEnd row col ->
3608 let
3609 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3610 region = toRegion row col
3611 in
3612 Report.Report "UNFINISHED RECORD" region [] $
3613 Code.toSnippet source surroundings (Just region)
3615 D.reflow $
3616 "I am partway through parsing a record, but I got stuck here:"
3618 D.stack
3619 [ D.fillSep
3620 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
3621 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
3623 , D.toSimpleNote $
3624 "When I get stuck like this, it usually means that there is a missing parenthesis\
3625 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3629 RecordField row col ->
3630 case Code.whatIsNext source row col of
3631 Code.Keyword keyword ->
3632 let
3633 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3634 region = toKeywordRegion row col keyword
3635 in
3636 Report.Report "RESERVED WORD" region [] $
3637 Code.toSnippet source surroundings (Just region)
3639 D.reflow $
3640 "I am partway through parsing a record, but I got stuck on this field name:"
3642 D.reflow $
3643 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
3644 \ that is a reserved word. Try using a different name!"
3647 _ ->
3648 let
3649 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3650 region = toRegion row col
3651 in
3652 Report.Report "UNFINISHED RECORD" region [] $
3653 Code.toSnippet source surroundings (Just region)
3655 D.reflow $
3656 "I am partway through parsing a record, but I got stuck after that last comma:"
3658 addNoteForRecordError $
3659 D.reflow $
3660 "Trailing commas are not allowed in records, so I was expecting to see another\
3661 \ record field defined next. If you are done defining the record, the fix may\
3662 \ be to delete that last comma?"
3665 RecordEquals row col ->
3666 let
3667 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3668 region = toRegion row col
3669 in
3670 Report.Report "UNFINISHED RECORD" region [] $
3671 Code.toSnippet source surroundings (Just region)
3673 D.reflow $
3674 "I am partway through parsing a record, but I got stuck here:"
3676 addNoteForRecordError $
3677 D.fillSep $
3678 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
3679 ,"an","equals","sign","next.","So","try","putting","an",D.green "=","sign","here?"
3683 RecordExpr expr row col ->
3684 toExprReport source (InNode NRecord startRow startCol context) expr row col
3686 RecordSpace space row col ->
3687 toSpaceReport source space row col
3689 RecordIndentOpen row col ->
3690 let
3691 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3692 region = toRegion row col
3693 in
3694 Report.Report "UNFINISHED RECORD" region [] $
3695 Code.toSnippet source surroundings (Just region)
3697 D.reflow $
3698 "I just saw the opening curly brace of a record, but then I got stuck here:"
3700 addNoteForRecordIndentError $
3701 D.fillSep $
3702 ["I","am","expecting","a","record","like",D.dullyellow "{ x = 3, y = 4 }","here."
3703 ,"Try","defining","some","fields","of","your","own?"
3707 RecordIndentEnd row col ->
3708 let
3709 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3710 region = toRegion row col
3711 in
3712 Report.Report "UNFINISHED RECORD" region [] $
3713 Code.toSnippet source surroundings (Just region)
3715 D.reflow $
3716 "I was expecting to see a closing curly brace next:"
3718 addNoteForRecordIndentError $
3719 D.fillSep $
3720 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
3724 RecordIndentField row col ->
3725 let
3726 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3727 region = toRegion row col
3728 in
3729 Report.Report "UNFINISHED RECORD" region [] $
3730 Code.toSnippet source surroundings (Just region)
3732 D.reflow $
3733 "I am partway through parsing a record, but I got stuck after that last comma:"
3735 addNoteForRecordError $
3736 D.reflow $
3737 "Trailing commas are not allowed in records, so the fix may be to\
3738 \ delete that last comma? Or maybe you were in the middle of defining\
3739 \ an additional field?"
3742 RecordIndentEquals row col ->
3743 let
3744 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3745 region = toRegion row col
3746 in
3747 Report.Report "UNFINISHED RECORD" region [] $
3748 Code.toSnippet source surroundings (Just region)
3750 D.reflow $
3751 "I am partway through parsing a record. I just saw a record\
3752 \ field, so I was expecting to see an equals sign next:"
3754 addNoteForRecordIndentError $
3755 D.fillSep $
3756 ["Try","putting","an",D.green "=","followed","by","an","expression?"
3760 RecordIndentExpr row col ->
3761 let
3762 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3763 region = toRegion row col
3764 in
3765 Report.Report "UNFINISHED RECORD" region [] $
3766 Code.toSnippet source surroundings (Just region)
3768 D.reflow $
3769 "I am partway through parsing a record, and I was expecting to run into an expression next:"
3771 addNoteForRecordIndentError $
3772 D.fillSep $
3773 ["Try","putting","something","like"
3774 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
3779addNoteForRecordError :: D.Doc -> D.Doc
3780addNoteForRecordError normalRecommendation =
3781 D.stack $
3782 normalRecommendation
3784 [ D.toSimpleNote
3785 "If you are trying to define a record across multiple lines, I recommend using this format:"
3786 , D.indent 4 $ D.vcat $
3787 [ "{ name = \"Alice\""
3788 , ", age = 42"
3789 , ", height = 1.75"
3790 , "}"
3792 , D.reflow $
3793 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3794 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3795 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3799addNoteForRecordIndentError :: D.Doc -> D.Doc
3800addNoteForRecordIndentError normalRecommendation =
3801 D.stack $
3802 normalRecommendation
3804 [ D.toSimpleNote
3805 "I may be confused by indentation. For example, if you are trying to define\
3806 \ a record across multiple lines, I recommend using this format:"
3807 , D.indent 4 $ D.vcat $
3808 [ "{ name = \"Alice\""
3809 , ", age = 42"
3810 , ", height = 1.75"
3811 , "}"
3813 , D.reflow $
3814 "Notice that each line starts with some indentation. Usually two or four spaces. I\
3815 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
3816 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
3821-- TUPLE
3824toTupleReport :: Code.Source -> Context -> Tuple -> Row -> Col -> Report.Report
3825toTupleReport source context tuple startRow startCol =
3826 case tuple of
3827 TupleExpr expr row col ->
3828 toExprReport source (InNode NParens startRow startCol context) expr row col
3830 TupleSpace space row col ->
3831 toSpaceReport source space row col
3833 TupleEnd row col ->
3834 let
3835 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3836 region = toRegion row col
3837 in
3838 Report.Report "UNFINISHED PARENTHESES" region [] $
3839 Code.toSnippet source surroundings (Just region)
3841 D.reflow $
3842 "I was expecting to see a closing parentheses next, but I got stuck here:"
3844 D.stack
3845 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
3846 , D.toSimpleNote $
3847 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
3848 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
3849 \ or missing brackets) that is confusing me."
3853 TupleOperatorClose row col ->
3854 let
3855 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3856 region = toRegion row col
3857 in
3858 Report.Report "UNFINISHED OPERATOR FUNCTION" region [] $
3859 Code.toSnippet source surroundings (Just region)
3861 D.reflow "I was expecting a closing parenthesis here:"
3863 D.stack
3864 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
3865 , D.toSimpleNote $
3866 "I think I am parsing an operator function right now, so I am expecting to see\
3867 \ something like (+) or (&&) where an operator is surrounded by parentheses with\
3868 \ no extra spaces."
3872 TupleOperatorReserved operator row col ->
3873 let
3874 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3875 region = toRegion row col
3876 in
3877 Report.Report "UNEXPECTED SYMBOL" region [] $
3878 Code.toSnippet source surroundings (Just region)
3880 D.reflow $
3881 "I ran into an unexpected symbol here:"
3883 D.fillSep $
3884 case operator of
3885 BadDot -> ["Maybe","you","wanted","a","record","accessor","like",D.dullyellow ".x","or",D.dullyellow ".name","instead?"]
3886 BadPipe -> ["Try",D.dullyellow "(||)","instead?","To","turn","boolean","OR","into","a","function?"]
3887 BadArrow -> ["Maybe","you","wanted",D.dullyellow "(>)","or",D.dullyellow "(>=)","instead?"]
3888 BadEquals -> ["Try",D.dullyellow "(==)","instead?","To","make","a","function","that","checks","equality?"]
3889 BadHasType -> ["Try",D.dullyellow "(::)","instead?","To","add","values","to","the","front","of","lists?"]
3892 TupleIndentExpr1 row col ->
3893 let
3894 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3895 region = toRegion row col
3896 in
3897 Report.Report "UNFINISHED PARENTHESES" region [] $
3898 Code.toSnippet source surroundings (Just region)
3900 D.reflow $
3901 "I just saw an open parenthesis, so I was expecting to see an expression next."
3903 D.stack
3904 [ D.fillSep $
3905 ["Something","like",D.dullyellow "(4 + 5)","or"
3906 ,D.dullyellow "(String.reverse \"desserts\")" <> "."
3907 ,"Anything","where","you","are","putting","parentheses","around","normal","expressions."
3909 , D.toSimpleNote $
3910 "I can get confused by indentation in cases like this, so\
3911 \ maybe you have an expression but it is not indented enough?"
3915 TupleIndentExprN row col ->
3916 let
3917 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3918 region = toRegion row col
3919 in
3920 Report.Report "UNFINISHED TUPLE" region [] $
3921 Code.toSnippet source surroundings (Just region)
3923 D.reflow $
3924 "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."
3926 D.stack
3927 [ D.fillSep $
3928 ["A","tuple","looks","like",D.dullyellow "(3,4)","or"
3929 ,D.dullyellow "(\"Tom\",42)" <> ","
3930 ,"so","I","think","there","is","an","expression","missing","here?"
3932 , D.toSimpleNote $
3933 "I can get confused by indentation in cases like this, so\
3934 \ maybe you have an expression but it is not indented enough?"
3938 TupleIndentEnd row col ->
3939 let
3940 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3941 region = toRegion row col
3942 in
3943 Report.Report "UNFINISHED PARENTHESES" region [] $
3944 Code.toSnippet source surroundings (Just region)
3946 D.reflow $
3947 "I was expecting to see a closing parenthesis next:"
3949 D.stack
3950 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
3951 , D.toSimpleNote $
3952 "I can get confused by indentation in cases like this, so\
3953 \ maybe you have a closing parenthesis but it is not indented enough?"
3958toListReport :: Code.Source -> Context -> List -> Row -> Col -> Report.Report
3959toListReport source context list startRow startCol =
3960 case list of
3961 ListSpace space row col ->
3962 toSpaceReport source space row col
3964 ListOpen row col ->
3965 let
3966 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
3967 region = toRegion row col
3968 in
3969 Report.Report "UNFINISHED LIST" region [] $
3970 Code.toSnippet source surroundings (Just region)
3972 D.reflow $
3973 "I am partway through parsing a list, but I got stuck here:"
3975 D.stack
3976 [ D.fillSep
3977 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
3978 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
3980 , D.toSimpleNote $
3981 "When I get stuck like this, it usually means that there is a missing parenthesis\
3982 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
3986 ListExpr expr row col ->
3987 case expr of
3988 Start r c ->
3989 let
3990 surroundings = A.Region (A.Position startRow startCol) (A.Position r c)
3991 region = toRegion r c
3992 in
3993 Report.Report "UNFINISHED LIST" region [] $
3994 Code.toSnippet source surroundings (Just region)
3996 D.reflow $
3997 "I was expecting to see another list entry after that last comma:"
3999 D.stack
4000 [ D.reflow $
4001 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
4002 , D.toSimpleNote
4003 "I recommend using the following format for lists that span multiple lines:"
4004 , D.indent 4 $ D.vcat $
4005 [ "[ \"Alice\""
4006 , ", \"Bob\""
4007 , ", \"Chuck\""
4008 , "]"
4010 , D.reflow $
4011 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4012 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4013 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4017 _ ->
4018 toExprReport source (InNode NList startRow startCol context) expr row col
4020 ListEnd row col ->
4021 let
4022 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4023 region = toRegion row col
4024 in
4025 Report.Report "UNFINISHED LIST" region [] $
4026 Code.toSnippet source surroundings (Just region)
4028 D.reflow $
4029 "I am partway through parsing a list, but I got stuck here:"
4031 D.stack
4032 [ D.fillSep
4033 ["I","was","expecting","to","see","a","closing","square","bracket","before","this,"
4034 ,"so","try","adding","a",D.dullyellow "]","and","see","if","that","helps?"
4036 , D.toSimpleNote $
4037 "When I get stuck like this, it usually means that there is a missing parenthesis\
4038 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
4042 ListIndentOpen row col ->
4043 let
4044 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4045 region = toRegion row col
4046 in
4047 Report.Report "UNFINISHED LIST" region [] $
4048 Code.toSnippet source surroundings (Just region)
4050 D.reflow $
4051 "I cannot find the end of this list:"
4053 D.stack
4054 [ D.fillSep $
4055 ["You","could","change","it","to","something","like"
4056 ,D.dullyellow "[3,4,5]"
4057 ,"or","even","just"
4058 ,D.dullyellow "[]" <> "."
4059 ,"Anything","where","there","is","an","open","and","close","square","brace,"
4060 ,"and","where","the","elements","of","the","list","are","separated","by","commas."
4062 , D.toSimpleNote
4063 "I may be confused by indentation. For example, if you are trying to define\
4064 \ a list across multiple lines, I recommend using this format:"
4065 , D.indent 4 $ D.vcat $
4066 [ "[ \"Alice\""
4067 , ", \"Bob\""
4068 , ", \"Chuck\""
4069 , "]"
4071 , D.reflow $
4072 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4073 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4074 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4078 ListIndentEnd row col ->
4079 let
4080 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4081 region = toRegion row col
4082 in
4083 Report.Report "UNFINISHED LIST" region [] $
4084 Code.toSnippet source surroundings (Just region)
4086 D.reflow $
4087 "I cannot find the end of this list:"
4089 D.stack
4090 [ D.fillSep $
4091 ["You","can","just","add","a","closing",D.dullyellow "]"
4092 ,"right","here,","and","I","will","be","all","set!"
4094 , D.toSimpleNote
4095 "I may be confused by indentation. For example, if you are trying to define\
4096 \ a list across multiple lines, I recommend using this format:"
4097 , D.indent 4 $ D.vcat $
4098 [ "[ \"Alice\""
4099 , ", \"Bob\""
4100 , ", \"Chuck\""
4101 , "]"
4103 , D.reflow $
4104 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4105 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4106 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4110 ListIndentExpr row col ->
4111 let
4112 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4113 region = toRegion row col
4114 in
4115 Report.Report "UNFINISHED LIST" region [] $
4116 Code.toSnippet source surroundings (Just region)
4118 D.reflow $
4119 "I was expecting to see another list entry after this comma:"
4121 D.stack
4122 [ D.reflow $
4123 "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
4124 , D.toSimpleNote
4125 "I recommend using the following format for lists that span multiple lines:"
4126 , D.indent 4 $ D.vcat $
4127 [ "[ \"Alice\""
4128 , ", \"Bob\""
4129 , ", \"Chuck\""
4130 , "]"
4132 , D.reflow $
4133 "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
4134 \ This style can be jarring for people coming from C-like syntax, but folks generally\
4135 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
4140toFuncReport :: Code.Source -> Context -> Func -> Row -> Col -> Report.Report
4141toFuncReport source context func startRow startCol =
4142 case func of
4143 FuncSpace space row col ->
4144 toSpaceReport source space row col
4146 FuncArg pattern row col ->
4147 toPatternReport source PArg pattern row col
4149 FuncBody expr row col ->
4150 toExprReport source (InNode NFunc startRow startCol context) expr row col
4152 FuncArrow row col ->
4153 case Code.whatIsNext source row col of
4154 Code.Keyword keyword ->
4155 let
4156 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4157 region = toKeywordRegion row col keyword
4158 in
4159 Report.Report "RESERVED WORD" region [] $
4160 Code.toSnippet source surroundings (Just region)
4162 D.reflow $
4163 "I was parsing an anonymous function, but I got stuck here:"
4165 D.reflow $
4166 "It looks like you are trying to use `" ++ keyword ++ "` as an argument, but\
4167 \ it is a reserved word in this language. Try using a different argument name!"
4170 _ ->
4171 let
4172 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4173 region = toRegion row col
4174 in
4175 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4176 Code.toSnippet source surroundings (Just region)
4178 D.reflow $
4179 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
4181 D.fillSep $
4182 ["The","syntax","for","anonymous","functions","is"
4183 ,D.dullyellow "(\\x -> x + 1)"
4184 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
4188 FuncIndentArg row col ->
4189 let
4190 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4191 region = toRegion row col
4192 in
4193 Report.Report "MISSING ARGUMENT" region [] $
4194 Code.toSnippet source surroundings (Just region)
4196 D.reflow $
4197 "I just saw the beginning of an anonymous function, so I was expecting to see an argument next:"
4199 D.stack
4200 [ D.fillSep
4201 ["Something","like",D.dullyellow"x","or",D.dullyellow "name" <> "."
4202 ,"Anything","that","starts","with","a","lower","case","letter!"
4204 , D.toSimpleNote $
4205 "The syntax for anonymous functions is (\\x -> x + 1) where the backslash\
4206 \ is meant to look a bit like a lambda if you squint. This visual pun seemed\
4207 \ like a better idea at the time!"
4211 FuncIndentArrow row col ->
4212 let
4213 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4214 region = toRegion row col
4215 in
4216 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4217 Code.toSnippet source surroundings (Just region)
4219 D.reflow $
4220 "I just saw the beginning of an anonymous function, so I was expecting to see an arrow next:"
4222 D.stack
4223 [ D.fillSep $
4224 ["The","syntax","for","anonymous","functions","is"
4225 ,D.dullyellow "(\\x -> x + 1)"
4226 ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
4228 , D.toSimpleNote $
4229 "It is possible that I am confused about indetation! I generally recommend\
4230 \ switching to named functions if the definition cannot fit inline nicely, so\
4231 \ either (1) try to fit the whole anonymous function on one line or (2) break\
4232 \ the whole thing out into a named function. Things tend to be clearer that way!"
4236 FuncIndentBody row col ->
4237 let
4238 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4239 region = toRegion row col
4240 in
4241 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
4242 Code.toSnippet source surroundings (Just region)
4244 D.reflow $
4245 "I was expecting to see the body of your anonymous function next:"
4247 D.stack
4248 [ D.fillSep $
4249 ["The","syntax","for","anonymous","functions","is"
4250 ,D.dullyellow "(\\x -> x + 1)"
4251 ,"so","I","am","missing","all","the","stuff","after","the","arrow!"
4253 , D.toSimpleNote $
4254 "It is possible that I am confused about indetation! I generally recommend\
4255 \ switching to named functions if the definition cannot fit inline nicely, so\
4256 \ either (1) try to fit the whole anonymous function on one line or (2) break\
4257 \ the whole thing out into a named function. Things tend to be clearer that way!"
4263-- PATTERN
4266data PContext
4267 = PCase
4268 | PArg
4269 | PLet
4272toPatternReport :: Code.Source -> PContext -> Pattern -> Row -> Col -> Report.Report
4273toPatternReport source context pattern startRow startCol =
4274 case pattern of
4275 PRecord record row col ->
4276 toPRecordReport source record row col
4278 PTuple tuple row col ->
4279 toPTupleReport source context tuple row col
4281 PList list row col ->
4282 toPListReport source context list row col
4284 PStart row col ->
4285 case Code.whatIsNext source row col of
4286 Code.Keyword keyword ->
4287 let
4288 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4289 region = toKeywordRegion row col keyword
4290 inThisThing =
4291 case context of
4292 PArg -> "as an argument"
4293 PCase -> "in this pattern"
4294 PLet -> "in this pattern"
4295 in
4296 Report.Report "RESERVED WORD" region [] $
4297 Code.toSnippet source surroundings (Just region)
4299 D.reflow $
4300 "It looks like you are trying to use `" ++ keyword ++ "` " ++ inThisThing ++ ":"
4302 D.reflow $
4303 "This is a reserved word! Try using some other name?"
4306 Code.Operator "-" ->
4307 let
4308 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4309 region = toRegion row col
4310 in
4311 Report.Report "UNEXPECTED SYMBOL" region [] $
4312 Code.toSnippet source surroundings (Just region)
4314 D.reflow $
4315 "I ran into a minus sign unexpectedly in this pattern:"
4317 D.reflow $
4318 "It is not possible to pattern match on negative numbers at this\
4319 \ time. Try using an `if` expression for that sort of thing for now."
4322 _ ->
4323 let
4324 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4325 region = toRegion row col
4326 in
4327 Report.Report "PROBLEM IN PATTERN" region [] $
4328 Code.toSnippet source surroundings (Just region)
4330 D.reflow $
4331 "I wanted to parse a pattern next, but I got stuck here:"
4333 D.fillSep $
4334 ["I","am","not","sure","why","I","am","getting","stuck","exactly."
4335 ,"I","just","know","that","I","want","a","pattern","next."
4336 ,"Something","as","simple","as"
4337 ,D.dullyellow "maybeHeight","or",D.dullyellow "result"
4338 ,"would","work!"
4342 PChar char row col ->
4343 toCharReport source char row col
4345 PString string row col ->
4346 toStringReport source string row col
4348 PNumber number row col ->
4349 toNumberReport source number row col
4351 PFloat width row col ->
4352 let
4353 region = toWiderRegion row col width
4354 in
4355 Report.Report "UNEXPECTED PATTERN" region [] $
4356 Code.toSnippet source region Nothing
4358 D.reflow $
4359 "I cannot pattern match with floating point numbers:"
4361 D.fillSep $
4362 ["Equality","on","floats","can","be","unreliable,","so","you","usually","want"
4363 ,"to","check","that","they","are","nearby","with","some","sort","of"
4364 ,D.dullyellow "(abs (actual - expected) < 0.001)","check."
4368 PAlias row col ->
4369 let
4370 region = toRegion row col
4371 in
4372 Report.Report "UNFINISHED PATTERN" region [] $
4373 Code.toSnippet source region Nothing $
4375 D.reflow $
4376 "I was expecting to see a variable name after the `as` keyword:"
4378 D.stack
4379 [ D.fillSep $
4380 ["The","`as`","keyword","lets","you","write","patterns","like"
4381 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
4382 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
4383 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
4384 ,"thing","with",D.dullyellow "point."
4386 , D.reflow $
4387 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
4388 \ people just want to use `as` as a variable name though. Try using a different name\
4389 \ in that case!"
4393 PWildcardNotVar name width row col ->
4394 let
4395 region = toWiderRegion row col (fromIntegral width)
4396 examples =
4397 case dropWhile (=='_') (Name.toChars name) of
4398 [] -> [D.dullyellow "x","or",D.dullyellow "age"]
4399 c:cs -> [D.dullyellow (D.fromChars (Char.toLower c : cs))]
4400 in
4401 Report.Report "UNEXPECTED NAME" region [] $
4402 Code.toSnippet source region Nothing $
4404 D.reflow $
4405 "Variable names cannot start with underscores like this:"
4407 D.fillSep $
4408 ["You","can","either","have","an","underscore","like",D.dullyellow "_","to"
4409 ,"ignore","the","value,","or","you","can","have","a","name","like"
4410 ] ++ examples ++ ["to","use","the","matched","value." ]
4413 PSpace space row col ->
4414 toSpaceReport source space row col
4416 PIndentStart row col ->
4417 let
4418 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4419 region = toRegion row col
4420 in
4421 Report.Report "UNFINISHED PATTERN" region [] $
4422 Code.toSnippet source surroundings (Just region)
4424 D.reflow $
4425 "I wanted to parse a pattern next, but I got stuck here:"
4427 D.stack
4428 [ D.fillSep $
4429 ["I","am","not","sure","why","I","am","getting","stuck","exactly."
4430 ,"I","just","know","that","I","want","a","pattern","next."
4431 ,"Something","as","simple","as"
4432 ,D.dullyellow "maybeHeight","or",D.dullyellow "result"
4433 ,"would","work!"
4435 , D.toSimpleNote $
4436 "I can get confused by indentation. If you think there is a pattern next, maybe\
4437 \ it needs to be indented a bit more?"
4441 PIndentAlias row col ->
4442 let
4443 region = toRegion row col
4444 in
4445 Report.Report "UNFINISHED PATTERN" region [] $
4446 Code.toSnippet source region Nothing $
4448 D.reflow $
4449 "I was expecting to see a variable name after the `as` keyword:"
4451 D.stack
4452 [ D.fillSep $
4453 ["The","`as`","keyword","lets","you","write","patterns","like"
4454 ,"((" <> D.dullyellow "x" <> "," <> D.dullyellow "y" <> ") " <> D.cyan "as" <> D.dullyellow " point" <> ")"
4455 ,"so","you","can","refer","to","individual","parts","of","the","tuple","with"
4456 ,D.dullyellow "x","and",D.dullyellow "y","or","you","refer","to","the","whole"
4457 ,"thing","with",D.dullyellow "point."
4459 , D.reflow $
4460 "So I was expecting to see a variable name after the `as` keyword here. Sometimes\
4461 \ people just want to use `as` as a variable name though. Try using a different name\
4462 \ in that case!"
4467toPRecordReport :: Code.Source -> PRecord -> Row -> Col -> Report.Report
4468toPRecordReport source record startRow startCol =
4469 case record of
4470 PRecordOpen row col ->
4471 toUnfinishRecordPatternReport source row col startRow startCol $
4472 D.reflow "I was expecting to see a field name next."
4474 PRecordEnd row col ->
4475 toUnfinishRecordPatternReport source row col startRow startCol $
4476 D.fillSep
4477 ["I","was","expecting","to","see","a","closing","curly","brace","next."
4478 ,"Try","adding","a",D.dullyellow "}","here?"
4481 PRecordField row col ->
4482 case Code.whatIsNext source row col of
4483 Code.Keyword keyword ->
4484 let
4485 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4486 region = toKeywordRegion row col keyword
4487 in
4488 Report.Report "RESERVED WORD" region [] $
4489 Code.toSnippet source surroundings (Just region)
4491 D.reflow $
4492 "I was not expecting to see `" ++ keyword ++ "` as a record field name:"
4494 D.reflow $
4495 "This is a reserved word, not available for variable names. Try another name!"
4498 _ ->
4499 toUnfinishRecordPatternReport source row col startRow startCol $
4500 D.reflow "I was expecting to see a field name next."
4502 PRecordSpace space row col ->
4503 toSpaceReport source space row col
4505 PRecordIndentOpen row col ->
4506 toUnfinishRecordPatternReport source row col startRow startCol $
4507 D.reflow "I was expecting to see a field name next."
4509 PRecordIndentEnd row col ->
4510 toUnfinishRecordPatternReport source row col startRow startCol $
4511 D.fillSep
4512 ["I","was","expecting","to","see","a","closing","curly","brace","next."
4513 ,"Try","adding","a",D.dullyellow "}","here?"
4516 PRecordIndentField row col ->
4517 toUnfinishRecordPatternReport source row col startRow startCol $
4518 D.reflow "I was expecting to see a field name next."
4521toUnfinishRecordPatternReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
4522toUnfinishRecordPatternReport source row col startRow startCol message =
4523 let
4524 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4525 region = toRegion row col
4526 in
4527 Report.Report "UNFINISHED RECORD PATTERN" region [] $
4528 Code.toSnippet source surroundings (Just region)
4530 D.reflow $
4531 "I was partway through parsing a record pattern, but I got stuck here:"
4533 D.stack
4534 [ message
4535 , D.toFancyHint $
4536 ["A","record","pattern","looks","like",D.dullyellow "{x,y}","or",D.dullyellow "{name,age}"
4537 ,"where","you","list","the","field","names","you","want","to","access."
4544toPTupleReport :: Code.Source -> PContext -> PTuple -> Row -> Col -> Report.Report
4545toPTupleReport source context tuple startRow startCol =
4546 case tuple of
4547 PTupleOpen row col ->
4548 case Code.whatIsNext source row col of
4549 Code.Keyword keyword ->
4550 let
4551 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4552 region = toKeywordRegion row col keyword
4553 in
4554 Report.Report "RESERVED WORD" region [] $
4555 Code.toSnippet source surroundings (Just region)
4557 D.reflow $
4558 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name:"
4560 D.reflow $
4561 "This is a reserved word! Try using some other name?"
4564 _ ->
4565 let
4566 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4567 region = toRegion row col
4568 in
4569 Report.Report "UNFINISHED PARENTHESES" region [] $
4570 Code.toSnippet source surroundings (Just region)
4572 D.reflow $
4573 "I just saw an open parenthesis, but I got stuck here:"
4575 D.fillSep
4576 ["I","was","expecting","so","see","a","pattern","next."
4577 ,"Maybe","it","will","end","up","being","something"
4578 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
4582 PTupleEnd row col ->
4583 case Code.whatIsNext source row col of
4584 Code.Keyword keyword ->
4585 let
4586 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4587 region = toKeywordRegion row col keyword
4588 in
4589 Report.Report "RESERVED WORD" region [] $
4590 Code.toSnippet source surroundings (Just region) $
4592 D.reflow $
4593 "I ran into a reserved word in this pattern:"
4595 D.reflow $
4596 "The `" ++ keyword ++ "` keyword is reserved. Try using a different name instead!"
4599 Code.Operator op ->
4600 let
4601 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4602 region = toKeywordRegion row col op
4603 in
4604 Report.Report "UNEXPECTED SYMBOL" region [] $
4605 Code.toSnippet source surroundings (Just region) $
4607 D.reflow $
4608 "I ran into the " ++ op ++ " symbol unexpectedly in this pattern:"
4610 D.reflow $
4611 "Only the :: symbol that works in patterns. It is useful if you\
4612 \ are pattern matching on lists, trying to get the first element\
4613 \ off the front. Did you want that instead?"
4616 Code.Close term bracket ->
4617 let
4618 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4619 region = toRegion row col
4620 in
4621 Report.Report ("STRAY " ++ map Char.toUpper term) region [] $
4622 Code.toSnippet source surroundings (Just region) $
4624 D.reflow $
4625 "I ran into a an unexpected " ++ term ++ " in this pattern:"
4627 D.reflow $
4628 "This " ++ bracket : " does not match up with an earlier open " ++ term ++ ". Try deleting it?"
4631 _ ->
4632 let
4633 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4634 region = toRegion row col
4635 in
4636 Report.Report "UNFINISHED PARENTHESES" region [] $
4637 Code.toSnippet source surroundings (Just region) $
4639 D.reflow $
4640 "I was partway through parsing a pattern, but I got stuck here:"
4642 D.fillSep
4643 ["I","was","expecting","a","closing","parenthesis","next,","so"
4644 ,"try","adding","a",D.dullyellow ")","to","see","if","that","helps?"
4648 PTupleExpr pattern row col ->
4649 toPatternReport source context pattern row col
4651 PTupleSpace space row col ->
4652 toSpaceReport source space row col
4654 PTupleIndentEnd row col ->
4655 let
4656 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4657 region = toRegion row col
4658 in
4659 Report.Report "UNFINISHED PARENTHESES" region [] $
4660 Code.toSnippet source surroundings (Just region) $
4662 D.reflow $
4663 "I was expecting a closing parenthesis next:"
4665 D.stack
4666 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
4667 , D.toSimpleNote $
4668 "I can get confused by indentation in cases like this, so\
4669 \ maybe you have a closing parenthesis but it is not indented enough?"
4673 PTupleIndentExpr1 row col ->
4674 let
4675 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4676 region = toRegion row col
4677 in
4678 Report.Report "UNFINISHED PARENTHESES" region [] $
4679 Code.toSnippet source surroundings (Just region) $
4681 D.reflow $
4682 "I just saw an open parenthesis, but then I got stuck here:"
4684 D.fillSep
4685 ["I","was","expecting","so","see","a","pattern","next."
4686 ,"Maybe","it","will","end","up","being","something"
4687 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "?"
4691 PTupleIndentExprN row col ->
4692 let
4693 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4694 region = toRegion row col
4695 in
4696 Report.Report "UNFINISHED TUPLE PATTERN" region [] $
4697 Code.toSnippet source surroundings (Just region) $
4699 D.reflow $
4700 "I am partway through parsing a tuple pattern, but I got stuck here:"
4702 D.stack
4703 [ D.fillSep
4704 ["I","was","expecting","so","see","a","pattern","next."
4705 ,"I","am","expecting","the","final","result","to","be","something"
4706 ,"like",D.dullyellow "(x,y)","or",D.dullyellow "(name, _)" <> "."
4708 , D.toSimpleNote $
4709 "I can get confused by indentation in cases like this, so the problem\
4710 \ may be that the next part is not indented enough?"
4715toPListReport :: Code.Source -> PContext -> PList -> Row -> Col -> Report.Report
4716toPListReport source context list startRow startCol =
4717 case list of
4718 PListOpen row col ->
4719 case Code.whatIsNext source row col of
4720 Code.Keyword keyword ->
4721 let
4722 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4723 region = toKeywordRegion row col keyword
4724 in
4725 Report.Report "RESERVED WORD" region [] $
4726 Code.toSnippet source surroundings (Just region)
4728 D.reflow $
4729 "It looks like you are trying to use `" ++ keyword ++ "` to name an element of a list:"
4731 D.reflow $
4732 "This is a reserved word though! Try using some other name?"
4735 _ ->
4736 let
4737 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4738 region = toRegion row col
4739 in
4740 Report.Report "UNFINISHED LIST PATTERN" region [] $
4741 Code.toSnippet source surroundings (Just region) $
4743 D.reflow $
4744 "I just saw an open square bracket, but then I got stuck here:"
4746 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4749 PListEnd row col ->
4750 let
4751 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4752 region = toRegion row col
4753 in
4754 Report.Report "UNFINISHED LIST PATTERN" region [] $
4755 Code.toSnippet source surroundings (Just region) $
4757 D.reflow $
4758 "I was expecting a closing square bracket to end this list pattern:"
4760 D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4763 PListExpr pattern row col ->
4764 toPatternReport source context pattern row col
4766 PListSpace space row col ->
4767 toSpaceReport source space row col
4769 PListIndentOpen row col ->
4770 let
4771 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4772 region = toRegion row col
4773 in
4774 Report.Report "UNFINISHED LIST PATTERN" region [] $
4775 Code.toSnippet source surroundings (Just region) $
4777 D.reflow $
4778 "I just saw an open square bracket, but then I got stuck here:"
4780 D.stack
4781 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4782 , D.toSimpleNote $
4783 "I can get confused by indentation in cases like this, so\
4784 \ maybe there is something next, but it is not indented enough?"
4788 PListIndentEnd row col ->
4789 let
4790 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4791 region = toRegion row col
4792 in
4793 Report.Report "UNFINISHED LIST PATTERN" region [] $
4794 Code.toSnippet source surroundings (Just region) $
4796 D.reflow $
4797 "I was expecting a closing square bracket to end this list pattern:"
4799 D.stack
4800 [ D.fillSep ["Try","adding","a",D.dullyellow "]","to","see","if","that","helps?"]
4801 , D.toSimpleNote $
4802 "I can get confused by indentation in cases like this, so\
4803 \ maybe you have a closing square bracket but it is not indented enough?"
4807 PListIndentExpr row col ->
4808 let
4809 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4810 region = toRegion row col
4811 in
4812 Report.Report "UNFINISHED LIST PATTERN" region [] $
4813 Code.toSnippet source surroundings (Just region) $
4815 D.reflow $
4816 "I am partway through parsing a list pattern, but I got stuck here:"
4818 D.stack
4819 [ D.reflow $
4820 "I was expecting to see another pattern next. Maybe a variable name."
4821 , D.toSimpleNote $
4822 "I can get confused by indentation in cases like this, so\
4823 \ maybe there is more to this pattern but it is not indented enough?"
4829-- TYPES
4832data TContext
4833 = TC_Annotation Name.Name
4834 | TC_CustomType
4835 | TC_TypeAlias
4836 | TC_Port
4839toTypeReport :: Code.Source -> TContext -> Type -> Row -> Col -> Report.Report
4840toTypeReport source context tipe startRow startCol =
4841 case tipe of
4842 TRecord record row col ->
4843 toTRecordReport source context record row col
4845 TTuple tuple row col ->
4846 toTTupleReport source context tuple row col
4848 TStart row col ->
4849 case Code.whatIsNext source row col of
4850 Code.Keyword keyword ->
4851 let
4852 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4853 region = toKeywordRegion row col keyword
4854 in
4855 Report.Report "RESERVED WORD" region [] $
4856 Code.toSnippet source surroundings (Just region)
4858 D.reflow $
4859 "I was expecting to see a type next, but I got stuck on this reserved word:"
4861 D.reflow $
4862 "It looks like you are trying to use `" ++ keyword ++ "` as a type variable, but \
4863 \ it is a reserved word. Try using a different name!"
4866 _ ->
4867 let
4868 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4869 region = toRegion row col
4871 thing =
4872 case context of
4873 TC_Annotation _ -> "type annotation"
4874 TC_CustomType -> "custom type"
4875 TC_TypeAlias -> "type alias"
4876 TC_Port -> "port"
4878 something =
4879 case context of
4880 TC_Annotation name -> "the `" ++ Name.toChars name ++ "` type annotation"
4881 TC_CustomType -> "a custom type"
4882 TC_TypeAlias -> "a type alias"
4883 TC_Port -> "a port"
4884 in
4885 Report.Report ("PROBLEM IN " ++ map Char.toUpper thing) region [] $
4886 Code.toSnippet source surroundings (Just region)
4888 D.reflow $
4889 "I was partway through parsing " ++ something ++ ", but I got stuck here:"
4891 D.fillSep $
4892 ["I","was","expecting","to","see","a","type","next."
4893 ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4897 TSpace space row col ->
4898 toSpaceReport source space row col
4900 TIndentStart row col ->
4901 let
4902 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4903 region = toRegion row col
4905 thing =
4906 case context of
4907 TC_Annotation _ -> "type annotation"
4908 TC_CustomType -> "custom type"
4909 TC_TypeAlias -> "type alias"
4910 TC_Port -> "port"
4911 in
4912 Report.Report ("UNFINISHED " ++ map Char.toUpper thing) region [] $
4913 Code.toSnippet source surroundings (Just region)
4915 D.reflow $
4916 "I was partway through parsing a " ++ thing ++ ", but I got stuck here:"
4918 D.stack
4919 [ D.fillSep $
4920 ["I","was","expecting","to","see","a","type","next."
4921 ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4923 , D.toSimpleNote $
4924 "I can get confused by indentation. If you think there is already a type\
4925 \ next, maybe it is not indented enough?"
4930toTRecordReport :: Code.Source -> TContext -> TRecord -> Row -> Col -> Report.Report
4931toTRecordReport source context record startRow startCol =
4932 case record of
4933 TRecordOpen row col ->
4934 case Code.whatIsNext source row col of
4935 Code.Keyword keyword ->
4936 let
4937 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4938 region = toKeywordRegion row col keyword
4939 in
4940 Report.Report "RESERVED WORD" region [] $
4941 Code.toSnippet source surroundings (Just region)
4943 D.reflow $
4944 "I just started parsing a record type, but I got stuck on this field name:"
4946 D.reflow $
4947 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
4948 \ that is a reserved word. Try using a different name!"
4951 _ ->
4952 let
4953 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4954 region = toRegion row col
4955 in
4956 Report.Report "UNFINISHED RECORD TYPE" region [] $
4957 Code.toSnippet source surroundings (Just region)
4959 D.reflow $
4960 "I just started parsing a record type, but I got stuck here:"
4962 D.fillSep
4963 ["Record","types","look","like",D.dullyellow "{ name : String, age : Int },"
4964 ,"so","I","was","expecting","to","see","a","field","name","next."
4968 TRecordEnd row col ->
4969 let
4970 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4971 region = toRegion row col
4972 in
4973 Report.Report "UNFINISHED RECORD TYPE" region [] $
4974 Code.toSnippet source surroundings (Just region)
4976 D.reflow $
4977 "I am partway through parsing a record type, but I got stuck here:"
4979 D.stack
4980 [ D.fillSep
4981 ["I","was","expecting","to","see","a","closing","curly","brace","before","this,"
4982 ,"so","try","adding","a",D.dullyellow "}","and","see","if","that","helps?"
4984 , D.toSimpleNote $
4985 "When I get stuck like this, it usually means that there is a missing parenthesis\
4986 \ or bracket somewhere earlier. It could also be a stray keyword or operator."
4990 TRecordField row col ->
4991 case Code.whatIsNext source row col of
4992 Code.Keyword keyword ->
4993 let
4994 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4995 region = toKeywordRegion row col keyword
4996 in
4997 Report.Report "RESERVED WORD" region [] $
4998 Code.toSnippet source surroundings (Just region)
5000 D.reflow $
5001 "I am partway through parsing a record type, but I got stuck on this field name:"
5003 D.reflow $
5004 "It looks like you are trying to use `" ++ keyword ++ "` as a field name, but \
5005 \ that is a reserved word. Try using a different name!"
5008 _ ->
5009 let
5010 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5011 region = toRegion row col
5012 in
5013 Report.Report "UNFINISHED RECORD TYPE" region [] $
5014 Code.toSnippet source surroundings (Just region)
5016 D.reflow $
5017 "I am partway through parsing a record type, but I got stuck after that last comma:"
5019 addNoteForRecordTypeError $
5020 D.reflow $
5021 "Trailing commas are not allowed in records, so I was expecting to see another\
5022 \ record field defined next. If you are done defining the record, the fix may\
5023 \ be to delete that last comma?"
5026 TRecordColon row col ->
5027 let
5028 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5029 region = toRegion row col
5030 in
5031 Report.Report "UNFINISHED RECORD TYPE" region [] $
5032 Code.toSnippet source surroundings (Just region)
5034 D.reflow $
5035 "I am partway through parsing a record type, but I got stuck here:"
5037 addNoteForRecordTypeError $
5038 D.fillSep $
5039 ["I","just","saw","a","field","name,","so","I","was","expecting","to","see"
5040 ,"a","colon","next.","So","try","putting","an",D.green ":","sign","here?"
5044 TRecordType tipe row col ->
5045 toTypeReport source context tipe row col
5047 TRecordSpace space row col ->
5048 toSpaceReport source space row col
5050 TRecordIndentOpen row col ->
5051 let
5052 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5053 region = toRegion row col
5054 in
5055 Report.Report "UNFINISHED RECORD TYPE" region [] $
5056 Code.toSnippet source surroundings (Just region)
5058 D.reflow $
5059 "I just saw the opening curly brace of a record type, but then I got stuck here:"
5061 addNoteForRecordTypeIndentError $
5062 D.fillSep $
5063 ["I","am","expecting","a","record","like",D.dullyellow "{ name : String, age : Int }","here."
5064 ,"Try","defining","some","fields","of","your","own?"
5068 TRecordIndentEnd row col ->
5069 let
5070 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5071 region = toRegion row col
5072 in
5073 Report.Report "UNFINISHED RECORD TYPE" region [] $
5074 Code.toSnippet source surroundings (Just region)
5076 D.reflow $
5077 "I was expecting to see a closing curly brace next:"
5079 addNoteForRecordTypeIndentError $
5080 D.fillSep $
5081 ["Try","putting","an",D.green "}","and","see","if","that","helps?"
5085 TRecordIndentField row col ->
5086 let
5087 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5088 region = toRegion row col
5089 in
5090 Report.Report "UNFINISHED RECORD TYPE" region [] $
5091 Code.toSnippet source surroundings (Just region)
5093 D.reflow $
5094 "I am partway through parsing a record type, but I got stuck after that last comma:"
5096 addNoteForRecordTypeIndentError $
5097 D.reflow $
5098 "Trailing commas are not allowed in record types, so the fix may be to\
5099 \ delete that last comma? Or maybe you were in the middle of defining\
5100 \ an additional field?"
5103 TRecordIndentColon row col ->
5104 let
5105 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5106 region = toRegion row col
5107 in
5108 Report.Report "UNFINISHED RECORD TYPE" region [] $
5109 Code.toSnippet source surroundings (Just region)
5111 D.reflow $
5112 "I am partway through parsing a record type. I just saw a record\
5113 \ field, so I was expecting to see a colon next:"
5115 addNoteForRecordTypeIndentError $
5116 D.fillSep $
5117 ["Try","putting","an",D.green ":","followed","by","a","type?"
5121 TRecordIndentType row col ->
5122 let
5123 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5124 region = toRegion row col
5125 in
5126 Report.Report "UNFINISHED RECORD TYPE" region [] $
5127 Code.toSnippet source surroundings (Just region)
5129 D.reflow $
5130 "I am partway through parsing a record type, and I was expecting to run into a type next:"
5132 addNoteForRecordTypeIndentError $
5133 D.fillSep $
5134 ["Try","putting","something","like"
5135 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
5140addNoteForRecordTypeError :: D.Doc -> D.Doc
5141addNoteForRecordTypeError normalRecommendation =
5142 D.stack $
5143 normalRecommendation
5145 [ D.toSimpleNote
5146 "If you are trying to define a record type across multiple lines, I recommend using this format:"
5147 , D.indent 4 $ D.vcat $
5148 [ "{ name : String"
5149 , ", age : Int"
5150 , ", height : Float"
5151 , "}"
5153 , D.reflow $
5154 "Notice that each line starts with some indentation. Usually two or four spaces. I\
5155 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
5156 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
5160addNoteForRecordTypeIndentError :: D.Doc -> D.Doc
5161addNoteForRecordTypeIndentError normalRecommendation =
5162 D.stack $
5163 normalRecommendation
5165 [ D.toSimpleNote
5166 "I may be confused by indentation. For example, if you are trying to define\
5167 \ a record type across multiple lines, I recommend using this format:"
5168 , D.indent 4 $ D.vcat $
5169 [ "{ name : String"
5170 , ", age : Int"
5171 , ", height : Float"
5172 , "}"
5174 , D.reflow $
5175 "Notice that each line starts with some indentation. Usually two or four spaces. I\
5176 \ know this style can be jarring for people coming from C-like syntax, but folks generally\
5177 \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
5181toTTupleReport :: Code.Source -> TContext -> TTuple -> Row -> Col -> Report.Report
5182toTTupleReport source context tuple startRow startCol =
5183 case tuple of
5184 TTupleOpen row col ->
5185 case Code.whatIsNext source row col of
5186 Code.Keyword keyword ->
5187 let
5188 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5189 region = toKeywordRegion row col keyword
5190 in
5191 Report.Report "RESERVED WORD" region [] $
5192 Code.toSnippet source surroundings (Just region)
5194 D.reflow $
5195 "I ran into a reserved word unexpectedly:"
5197 D.reflow $
5198 "It looks like you are trying to use `" ++ keyword ++ "` as a variable name, but \
5199 \ it is a reserved word. Try using a different name!"
5202 _ ->
5203 let
5204 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5205 region = toRegion row col
5206 in
5207 Report.Report "UNFINISHED PARENTHESES" region [] $
5208 Code.toSnippet source surroundings (Just region)
5210 D.reflow $
5211 "I just saw an open parenthesis, so I was expecting to see a type next."
5213 D.fillSep $
5214 ["Something","like",D.dullyellow "(Maybe Int)","or"
5215 ,D.dullyellow "(List Person)" <> "."
5216 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
5220 TTupleEnd row col ->
5221 let
5222 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5223 region = toRegion row col
5224 in
5225 Report.Report "UNFINISHED PARENTHESES" region [] $
5226 Code.toSnippet source surroundings (Just region)
5228 D.reflow $
5229 "I was expecting to see a closing parentheses next, but I got stuck here:"
5231 D.stack
5232 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps?"]
5233 , D.toSimpleNote $
5234 "I can get stuck when I run into keywords, operators, parentheses, or brackets\
5235 \ unexpectedly. So there may be some earlier syntax trouble (like extra parenthesis\
5236 \ or missing brackets) that is confusing me."
5240 TTupleType tipe row col ->
5241 toTypeReport source context tipe row col
5243 TTupleSpace space row col ->
5244 toSpaceReport source space row col
5246 TTupleIndentType1 row col ->
5247 let
5248 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5249 region = toRegion row col
5250 in
5251 Report.Report "UNFINISHED PARENTHESES" region [] $
5252 Code.toSnippet source surroundings (Just region)
5254 D.reflow $
5255 "I just saw an open parenthesis, so I was expecting to see a type next."
5257 D.stack
5258 [ D.fillSep $
5259 ["Something","like",D.dullyellow "(Maybe Int)","or"
5260 ,D.dullyellow "(List Person)" <> "."
5261 ,"Anything","where","you","are","putting","parentheses","around","normal","types."
5263 , D.toSimpleNote $
5264 "I can get confused by indentation in cases like this, so\
5265 \ maybe you have a type but it is not indented enough?"
5269 TTupleIndentTypeN row col ->
5270 let
5271 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5272 region = toRegion row col
5273 in
5274 Report.Report "UNFINISHED TUPLE TYPE" region [] $
5275 Code.toSnippet source surroundings (Just region)
5277 D.reflow $
5278 "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."
5280 D.stack
5281 [ D.fillSep $
5282 ["A","tuple","type","looks","like",D.dullyellow "(Float,Float)","or"
5283 ,D.dullyellow "(String,Int)" <> ","
5284 ,"so","I","think","there","is","a","type","missing","here?"
5286 , D.toSimpleNote $
5287 "I can get confused by indentation in cases like this, so\
5288 \ maybe you have an expression but it is not indented enough?"
5292 TTupleIndentEnd row col ->
5293 let
5294 surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
5295 region = toRegion row col
5296 in
5297 Report.Report "UNFINISHED PARENTHESES" region [] $
5298 Code.toSnippet source surroundings (Just region)
5300 D.reflow $
5301 "I was expecting to see a closing parenthesis next:"
5303 D.stack
5304 [ D.fillSep ["Try","adding","a",D.dullyellow ")","to","see","if","that","helps!"]
5305 , D.toSimpleNote $
5306 "I can get confused by indentation in cases like this, so\
5307 \ maybe you have a closing parenthesis but it is not indented enough?"