Skip to content

Commit

Permalink
Merge pull request #5108 from sellout/lexer-error-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aryairani committed Jun 24, 2024
2 parents f74565d + 782ac41 commit 6798520
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
51 changes: 28 additions & 23 deletions parser-typechecker/src/Unison/PrintError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ styleAnnotated sty a = (,sty) <$> rangeForAnnotated a
style :: s -> String -> Pretty (AnnotatedText s)
style sty str = Pr.lit . AT.annotate sty $ fromString str

-- | Applies the color highlighting for `Code`, but also quotes the code, to separate it from the containing context.
quoteCode :: String -> Pretty ColorText
quoteCode = Pr.backticked . style Code

stylePretty :: Color -> Pretty ColorText -> Pretty ColorText
stylePretty = Pr.map . AT.annotate

Expand Down Expand Up @@ -1366,31 +1370,31 @@ renderParseErrors s = \case
<> style ErrorSite (fromString open)
<> ".\n\n"
<> excerpt
L.InvalidWordyId _id ->
L.ReservedWordyId id ->
Pr.lines
[ "This identifier isn't valid syntax: ",
[ "The identifier " <> quoteCode id <> " used here is a reserved keyword: ",
"",
excerpt,
"Here's a few examples of valid syntax: "
<> style Code "abba1', snake_case, Foo.zoink!, 🌻"
]
L.ReservedWordyId _id ->
Pr.lines
[ "The identifier used here isn't allowed to be a reserved keyword: ",
"",
excerpt
Pr.wrap $
"You can avoid this problem either by renaming the identifier or wrapping it in backticks (like "
<> style Code ("`" <> id <> "`")
<> ")."
]
L.InvalidSymbolyId _id ->
L.InvalidSymbolyId id ->
Pr.lines
[ "This infix identifier isn't valid syntax: ",
[ "The infix identifier " <> quoteCode id <> " isnt valid syntax: ",
"",
excerpt,
"Here's a few valid examples: "
<> style Code "++, Float./, `List.map`"
"Here are a few valid examples: "
<> quoteCode "++"
<> ", "
<> quoteCode "Float./"
<> ", and "
<> quoteCode "List.map"
]
L.ReservedSymbolyId _id ->
L.ReservedSymbolyId id ->
Pr.lines
[ "This identifier is reserved by Unison and can't be used as an operator: ",
[ "The identifier " <> quoteCode id <> " is reserved by Unison and can't be used as an operator: ",
"",
excerpt
]
Expand Down Expand Up @@ -1444,11 +1448,12 @@ renderParseErrors s = \case
"",
excerpt,
Pr.wrap $
"I was expecting some digits after the '.',"
<> "for example: "
<> style Code (n <> "0")
"I was expecting some digits after the "
<> quoteCode "."
<> ", for example: "
<> quoteCode (n <> "0")
<> "or"
<> Pr.group (style Code (n <> "1e37") <> ".")
<> Pr.group (quoteCode (n <> "1e37") <> ".")
]
L.MissingExponent n ->
Pr.lines
Expand All @@ -1458,7 +1463,7 @@ renderParseErrors s = \case
Pr.wrap $
"I was expecting some digits for the exponent,"
<> "for example: "
<> Pr.group (style Code (n <> "37") <> ".")
<> Pr.group (quoteCode (n <> "37") <> ".")
]
L.TextLiteralMissingClosingQuote _txt ->
Pr.lines
Expand All @@ -1474,7 +1479,7 @@ renderParseErrors s = \case
"",
"I only know about the following escape characters:",
"",
let s ch = style Code (fromString $ "\\" <> [ch])
let s ch = quoteCode (fromString $ "\\" <> [ch])
in Pr.indentN 2 $ intercalateMap "," s (fst <$> L.escapeChars)
]
L.LayoutError ->
Expand Down Expand Up @@ -1705,7 +1710,7 @@ renderParseErrors s = \case
let msg =
mconcat
[ "This looks like the start of an expression here but I was expecting a binding.",
"\nDid you mean to use a single " <> style Code ":",
"\nDid you mean to use a single " <> quoteCode ":",
" here for a type signature?",
"\n\n",
tokenAsErrorSite s t
Expand Down
14 changes: 8 additions & 6 deletions unison-src/transcripts/error-messages.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ x = 1. -- missing some digits after the decimal
1 | x = 1. -- missing some digits after the decimal
I was expecting some digits after the '.', for example: 1.0 or
1.1e37.
I was expecting some digits after the `.` , for example: `1.0`
or `1.1e37`.
```
```unison
Expand All @@ -36,7 +36,7 @@ x = 1e -- missing an exponent
1 | x = 1e -- missing an exponent
I was expecting some digits for the exponent, for example:
1e37.
`1e37`.
```
```unison
Expand All @@ -52,7 +52,7 @@ x = 1e- -- missing an exponent
1 | x = 1e- -- missing an exponent
I was expecting some digits for the exponent, for example:
1e-37.
`1e-37`.
```
```unison
Expand All @@ -68,7 +68,7 @@ x = 1E+ -- missing an exponent
1 | x = 1E+ -- missing an exponent
I was expecting some digits for the exponent, for example:
1e+37.
`1e+37`.
```
### Hex, octal, and bytes literals
Expand Down Expand Up @@ -343,10 +343,12 @@ use.keyword.in.namespace = 1
Loading changes detected in scratch.u.
The identifier used here isn't allowed to be a reserved keyword:
The identifier `namespace` used here is a reserved keyword:
1 | use.keyword.in.namespace = 1
You can avoid this problem either by renaming the identifier
or wrapping it in backticks (like `namespace` ).
```
```unison
Expand Down
4 changes: 3 additions & 1 deletion unison-src/transcripts/generic-parse-errors.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ namespace.blah = 1
Loading changes detected in scratch.u.
The identifier used here isn't allowed to be a reserved keyword:
The identifier `namespace` used here is a reserved keyword:
1 | namespace.blah = 1
You can avoid this problem either by renaming the identifier
or wrapping it in backticks (like `namespace` ).
```
```unison
Expand Down
3 changes: 1 addition & 2 deletions unison-syntax/src/Unison/Syntax/Lexer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ parseFailure :: EP.ParseError [Char] (Token Err) -> P a
parseFailure e = PI.ParsecT $ \s _ _ _ eerr -> eerr e s

data Err
= InvalidWordyId String
| ReservedWordyId String
= ReservedWordyId String
| InvalidSymbolyId String
| ReservedSymbolyId String
| InvalidShortHash String
Expand Down

0 comments on commit 6798520

Please sign in to comment.