Merge pull request #5108 from sellout/lexer-error-messages

This commit is contained in:
Arya Irani 2024-06-24 08:19:16 -04:00 committed by GitHub
commit 67985204e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 32 deletions

View File

@ -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
@ -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!, 🌻"
Pr.wrap $
"You can avoid this problem either by renaming the identifier or wrapping it in backticks (like "
<> style Code ("`" <> id <> "`")
<> ")."
]
L.ReservedWordyId _id ->
L.InvalidSymbolyId id ->
Pr.lines
[ "The identifier used here isn't allowed to be a reserved keyword: ",
"",
excerpt
]
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
]
@ -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
@ -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
@ -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 ->
@ -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

View File

@ -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
@ -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
@ -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
@ -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
@ -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

View File

@ -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

View File

@ -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