Skip to content

Commit 39b8ab4

Browse files
committed
Implement errorToString
1 parent 5fc4ef3 commit 39b8ab4

10 files changed

Lines changed: 243 additions & 2 deletions

File tree

src/Compiler/Parse/Declaration.gren

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Compiler.Parse.Declaration exposing
44
, Error (..)
55
, parser
66
, docParser
7+
, errorToString
78
)
89

910

@@ -40,6 +41,28 @@ type Error
4041
| SpaceError Space.Error
4142

4243

44+
errorToString : Error -> String
45+
errorToString error =
46+
when error is
47+
ExpectedKeyword keyword ->
48+
"Expected keyword '" ++ keyword ++ "'"
49+
50+
ExpectedChar char ->
51+
"Expected character '" ++ String.fromChar char ++ "'"
52+
53+
VariableError varError ->
54+
Variable.errorToString varError
55+
56+
TypeError typeError ->
57+
Type.errorToString typeError
58+
59+
ExprError exprError ->
60+
Expr.errorToString exprError
61+
62+
SpaceError spaceError ->
63+
Space.errorToString spaceError
64+
65+
4366
spaceParser : Parser Context Error {}
4467
spaceParser =
4568
Parser.mapError SpaceError Space.parser

src/Compiler/Parse/Expression.gren

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Compiler.Parse.Expression exposing
22
( Error (..)
33
, parser
44
, letDefNamedParser
5+
, errorToString
56
)
67

78

@@ -38,6 +39,58 @@ type Error
3839
| InternalError
3940

4041

42+
errorToString : Error -> String
43+
errorToString error =
44+
when error is
45+
ExpectedChar char ->
46+
"Expected character '" ++ String.fromChar char ++ "'"
47+
48+
ExpectedKeyword keyword ->
49+
"Expected keyword '" ++ keyword ++ "'"
50+
51+
VariableError varError ->
52+
Variable.errorToString varError
53+
54+
NumberError numError ->
55+
Number.errorToString numError
56+
57+
StringError strError ->
58+
String.errorToString strError
59+
60+
PatternError patError ->
61+
Pattern.errorToString patError
62+
63+
TypeError typeError ->
64+
Type.errorToString typeError
65+
66+
WildcardAttempt ->
67+
"Wildcard patterns are not allowed in expressions"
68+
69+
ExpectedLowerVariable _ ->
70+
"Expected lower case variable"
71+
72+
NameMismatch { first, second } ->
73+
"Name mismatch: expected '" ++ first ++ "' but got '" ++ second ++ "'"
74+
75+
IndentError ->
76+
"Indentation error"
77+
78+
BadOperator op ->
79+
"Invalid operator: '" ++ op ++ "'"
80+
81+
InvalidOperatorChar ->
82+
"Invalid operator character"
83+
84+
SpaceError spaceError ->
85+
Space.errorToString spaceError
86+
87+
OperatorError opError ->
88+
Operator.errorToString opError
89+
90+
InternalError ->
91+
"Internal parser error"
92+
93+
4194
spaceParser : Parser Context Error {}
4295
spaceParser =
4396
Parser.mapError SpaceError Space.parser

src/Compiler/Parse/Module.gren

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Compiler.Parse.Module exposing
22
( Error (..)
33
, parser
44
, errorsToString
5+
, errorToString
56
)
67

78

@@ -27,6 +28,31 @@ type Error
2728
| SpaceError Space.Error
2829

2930

31+
errorToString : Error -> String
32+
errorToString error =
33+
when error is
34+
ExpectedKeyword keyword ->
35+
"Expected keyword '" ++ keyword ++ "'"
36+
37+
ExpectedChar char ->
38+
"Expected character '" ++ String.fromChar char ++ "'"
39+
40+
VariableError varError ->
41+
Variable.errorToString varError
42+
43+
OperatorError opError ->
44+
Operator.errorToString opError
45+
46+
ExpectedIntPrecedence ->
47+
"Expected integer precedence value"
48+
49+
DeclarationError declError ->
50+
Declaration.errorToString declError
51+
52+
SpaceError spaceError ->
53+
Space.errorToString spaceError
54+
55+
3056
spaceParser : Parser Context Error {}
3157
spaceParser =
3258
Parser.mapError SpaceError Space.parser
@@ -408,8 +434,7 @@ errorsToString src deadEnds =
408434
, linesOfExtraContext = 2
409435
}
410436
{ deadEndToString = \de -> { row = de.row, col = de.col, context = "" }
411-
-- TODO: get rid of Debug
412-
, problemToString = \err -> Parser.Other (Debug.toString err)
437+
, problemToString = \err -> Parser.Other (errorToString err)
413438
}
414439
src
415440
(Array.map (\de -> { row = de.row, col = de.col, payload = {}, problem = de.problem }) deadEnds)

src/Compiler/Parse/Number.gren

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Compiler.Parse.Number exposing
33
, Error (..)
44
, parser
55
, hexParser
6+
, errorToString
67
)
78

89

@@ -23,6 +24,22 @@ type Error
2324
| ExpectedHex
2425

2526

27+
errorToString : Error -> String
28+
errorToString error =
29+
when error is
30+
NotANumber ->
31+
"Not a valid number"
32+
33+
LeadingZero ->
34+
"Leading zero is not allowed"
35+
36+
ExpectedInt ->
37+
"Expected integer"
38+
39+
ExpectedHex ->
40+
"Expected hexadecimal digits"
41+
42+
2643
parser : Parser Context Error Outcome
2744
parser =
2845
Parser.oneOf

src/Compiler/Parse/Operator.gren

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Compiler.Parse.Operator exposing
22
( parser
33
, Error (..)
4+
, errorToString
45
)
56

67

@@ -18,6 +19,28 @@ type Error
1819
| BadOperatorCharacter
1920

2021

22+
errorToString : Error -> String
23+
errorToString error =
24+
when error is
25+
BadDot ->
26+
"Invalid use of '.'"
27+
28+
BadPipe ->
29+
"Invalid use of '|>'"
30+
31+
BadArrow ->
32+
"Invalid use of '->'"
33+
34+
BadEquals ->
35+
"Invalid use of '='"
36+
37+
BadHasType ->
38+
"Invalid use of ':'"
39+
40+
BadOperatorCharacter ->
41+
"Invalid operator character"
42+
43+
2144
parser : Parser Context Error String
2245
parser =
2346
Parser.succeed (String.join "")

src/Compiler/Parse/Pattern.gren

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Compiler.Parse.Pattern exposing
22
( Error (..)
33
, parser
4+
, errorToString
45
)
56

67

@@ -25,6 +26,31 @@ type Error
2526
| SpaceError Space.Error
2627

2728

29+
errorToString : Error -> String
30+
errorToString error =
31+
when error is
32+
ExpectedChar char ->
33+
"Expected character '" ++ String.fromChar char ++ "'"
34+
35+
ExpectedKeyword keyword ->
36+
"Expected keyword '" ++ keyword ++ "'"
37+
38+
VariableError varError ->
39+
Variable.errorToString varError
40+
41+
NumberError numError ->
42+
Number.errorToString numError
43+
44+
FloatNotSupported ->
45+
"Float patterns are not supported"
46+
47+
StringError strError ->
48+
String.errorToString strError
49+
50+
SpaceError spaceError ->
51+
Space.errorToString spaceError
52+
53+
2854
spaceParser : Parser Context Error {}
2955
spaceParser =
3056
Parser.mapError SpaceError Space.parser

src/Compiler/Parse/Space.gren

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Compiler.Parse.Space exposing
22
( Error (..)
33
, parser
44
, checkIndent
5+
, errorToString
56
)
67

78

@@ -15,6 +16,16 @@ type Error
1516
| InvalidWhiteSpace
1617

1718

19+
errorToString : Error -> String
20+
errorToString error =
21+
when error is
22+
ExpectedLineComment ->
23+
"Expected line comment"
24+
25+
InvalidWhiteSpace ->
26+
"Invalid whitespace character"
27+
28+
1829
parser : Parser Context Error {}
1930
parser =
2031
Parser.loop {} parseLoop

src/Compiler/Parse/String.gren

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Compiler.Parse.String exposing
22
( Error (..)
33
, char
44
, string
5+
, errorToString
56
)
67

78

@@ -21,6 +22,34 @@ type Error
2122
| MisalignedMultiQuotes
2223

2324

25+
errorToString : Error -> String
26+
errorToString error =
27+
when error is
28+
ExpectedQuote ->
29+
"Expected quote"
30+
31+
ExpectedChar ->
32+
"Expected a character"
33+
34+
ExpectedEscapeChar escapeSeq ->
35+
"Invalid escape sequence: '" ++ escapeSeq ++ "'"
36+
37+
ExpectedUnicodeOpening ->
38+
"Expected unicode opening 'u{'"
39+
40+
ExpectedValidUnicode ->
41+
"Invalid unicode value"
42+
43+
ExpectedUnicodeClosing ->
44+
"Expected unicode closing '}'"
45+
46+
ExpectedNewline ->
47+
"Expected newline"
48+
49+
MisalignedMultiQuotes ->
50+
"Multi-line string quotes are misaligned"
51+
52+
2453
char : Parser Context Error Char
2554
char =
2655
Parser.succeed identity

src/Compiler/Parse/Type.gren

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Compiler.Parse.Type exposing
22
( Error (..)
33
, expression
4+
, errorToString
45
)
56

67

@@ -20,6 +21,25 @@ type Error
2021
| SpaceError Space.Error
2122

2223

24+
errorToString : Error -> String
25+
errorToString error =
26+
when error is
27+
ExpectedChar char ->
28+
"Expected '" ++ String.fromChar char ++ "'"
29+
30+
ExpectedString str ->
31+
"Expected string '" ++ str ++ "'"
32+
33+
VariableError varError ->
34+
Variable.errorToString varError
35+
36+
ExpectedIndent ->
37+
"Expected indentation"
38+
39+
SpaceError spaceError ->
40+
Space.errorToString spaceError
41+
42+
2343
spaceParser : Parser Context Error {}
2444
spaceParser =
2545
Parser.mapError SpaceError Space.parser

src/Compiler/Parse/Variable.gren

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Compiler.Parse.Variable exposing
77
, foreignUpper
88
, foreignVar
99
, reservedWords
10+
, errorToString
1011
)
1112

1213

@@ -22,6 +23,19 @@ type Error
2223
| ReservedWord String
2324

2425

26+
errorToString : Error -> String
27+
errorToString error =
28+
when error is
29+
InvalidCharacter ->
30+
"Invalid character in variable name"
31+
32+
ExpectedDot ->
33+
"Expected '.' character"
34+
35+
ReservedWord word ->
36+
"Reserved word used as variable: '" ++ word ++ "'"
37+
38+
2539
lowerCase : Parser Context Error String
2640
lowerCase =
2741
-- TODO: simply stops parsing at invalid character, in lack of better alternatives

0 commit comments

Comments
 (0)