From 3d959061959d2d561996d8bc59bfc89e0a9b51dd Mon Sep 17 00:00:00 2001 From: Chris Penner Date: Wed, 13 Mar 2024 11:52:29 -0700 Subject: [PATCH] Update transcripts --- parser-typechecker/src/Unison/PrintError.hs | 2 +- .../transcripts/error-messages.output.md | 47 ++++++-- .../transcripts/generic-parse-errors.md | 26 +++++ .../generic-parse-errors.output.md | 107 ++++++++++++++++++ unison-syntax/src/Unison/Syntax/Lexer.hs | 30 ++--- 5 files changed, 178 insertions(+), 34 deletions(-) create mode 100644 unison-src/transcripts/generic-parse-errors.md create mode 100644 unison-src/transcripts/generic-parse-errors.output.md diff --git a/parser-typechecker/src/Unison/PrintError.hs b/parser-typechecker/src/Unison/PrintError.hs index efe220712..a0583db7f 100644 --- a/parser-typechecker/src/Unison/PrintError.hs +++ b/parser-typechecker/src/Unison/PrintError.hs @@ -1524,7 +1524,7 @@ renderParseErrors s = \case Nothing -> [] Just ts -> rangeForToken <$> Foldable.toList ts excerpt = showSource s ((\r -> (r, ErrorSite)) <$> ranges) - msg = L.formatTrivialError _ unexpectedTokenStrs expectedTokenStrs + msg = L.formatTrivialError unexpectedTokenStrs expectedTokenStrs in [ ( Pr.lines [ "I got confused here:", "", diff --git a/unison-src/transcripts/error-messages.output.md b/unison-src/transcripts/error-messages.output.md index 1a2095978..be2c178f8 100644 --- a/unison-src/transcripts/error-messages.output.md +++ b/unison-src/transcripts/error-messages.output.md @@ -210,10 +210,16 @@ foo = match 1 with Loading changes detected in scratch.u. - offset=8: - unexpected - expecting ",", case match, or pattern guard + I got confused here: + 3 | + + I was surprised to find an end of section here. + I was hoping for one of these instead: + + * "," + * case match + * pattern guard ``` ```unison @@ -250,10 +256,19 @@ x = match Some a with Loading changes detected in scratch.u. - offset=16: - unexpected - expecting ",", blank, case match, false, pattern guard, or true + I got confused here: + 7 | + + I was surprised to find an end of section here. + I was hoping for one of these instead: + + * "," + * blank + * case match + * false + * pattern guard + * true ``` ```unison @@ -268,11 +283,15 @@ x = match Some a with Loading changes detected in scratch.u. - offset=12: - unexpected -> - expecting newline or semicolon + I got confused here: + 4 | -> 2 + + I was surprised to find a -> here. + I was hoping for one of these instead: + + * newline or semicolon ``` ```unison @@ -286,11 +305,15 @@ x = match Some a with Loading changes detected in scratch.u. - offset=12: - unexpected | - expecting newline or semicolon + I got confused here: + 4 | | true -> 2 + + I was surprised to find a '|' here. + I was hoping for one of these instead: + + * newline or semicolon ``` ### Watches diff --git a/unison-src/transcripts/generic-parse-errors.md b/unison-src/transcripts/generic-parse-errors.md new file mode 100644 index 000000000..b22b2f039 --- /dev/null +++ b/unison-src/transcripts/generic-parse-errors.md @@ -0,0 +1,26 @@ +Just a bunch of random parse errors to test the error formatting. + +```unison:error +x = + foo.123 +``` + +```unison:error +namespace.blah = 1 +``` + +```unison:error +x = 1 ] +``` + +```unison:error +x = a.#abc +``` + +```unison:error +x = "hi +``` + +```unison:error +y : a +``` diff --git a/unison-src/transcripts/generic-parse-errors.output.md b/unison-src/transcripts/generic-parse-errors.output.md new file mode 100644 index 000000000..0e1a2cd39 --- /dev/null +++ b/unison-src/transcripts/generic-parse-errors.output.md @@ -0,0 +1,107 @@ +Just a bunch of random parse errors to test the error formatting. + +```unison +x = + foo.123 +``` + +```ucm + + Loading changes detected in scratch.u. + + I got confused here: + + 2 | foo.123 + + + I was surprised to find a 1 here. + I was hoping for one of these instead: + + * end of input + * hash (ex: #af3sj3) + * identifier (ex: abba1, snake_case, .foo.bar#xyz, .foo.++#xyz, or 🌻) + +``` +```unison +namespace.blah = 1 +``` + +```ucm + + Loading changes detected in scratch.u. + + The identifier used here isn't allowed to be a reserved keyword: + + 1 | namespace.blah = 1 + + +``` +```unison +x = 1 ] +``` + +```ucm + + Loading changes detected in scratch.u. + + I found a closing ']' here without a matching '['. + + 1 | x = 1 ] + + +``` +```unison +x = a.#abc +``` + +```ucm + + Loading changes detected in scratch.u. + + I got confused here: + + 1 | x = a.#abc + + + I was surprised to find a '.' here. + +``` +```unison +x = "hi +``` + +```ucm + + Loading changes detected in scratch.u. + + I got confused here: + + 2 | + + I was surprised to find an end of input here. + I was hoping for one of these instead: + + * " + * \s + * literal character + +``` +```unison +y : a +``` + +```ucm + + Loading changes detected in scratch.u. + + I got confused here: + + 2 | + + I was surprised to find an end of section here. + I was hoping for one of these instead: + + * -> + * newline or semicolon + +``` diff --git a/unison-syntax/src/Unison/Syntax/Lexer.hs b/unison-syntax/src/Unison/Syntax/Lexer.hs index 8804485e5..4a2a3997c 100644 --- a/unison-syntax/src/Unison/Syntax/Lexer.hs +++ b/unison-syntax/src/Unison/Syntax/Lexer.hs @@ -282,11 +282,7 @@ lexer0' scope rem = expectedStr = expectedTokens & Set.map errorItemToString - startsWithVowel :: String -> Bool - startsWithVowel = \case - [] -> False - (ch : _) -> ch `elem` ("aeiou" :: String) - err = UnexpectedTokens $ formatTrivialError startsWithVowel unexpectedStr expectedStr + err = UnexpectedTokens $ formatTrivialError unexpectedStr expectedStr in [Token (Err err) (toPos top) (toPos top)] in errsWithSourcePos >>= errorToTokens Right ts -> Token (Open scope) topLeftCorner topLeftCorner : tweak ts @@ -322,28 +318,20 @@ lexer0' scope rem = tweak (h : t) = h : tweak t isSigned num = all (\ch -> ch == '-' || ch == '+') $ take 1 num -formatTrivialError :: - (IsString s, Monoid s) => - -- | A function that returns True if the given string starts with a vowel, - -- Used for selecting the correct article. - (s -> Bool) -> - Set s -> - Set s -> - s -formatTrivialError startsWithVowel unexpectedTokens expectedTokens = +formatTrivialError :: Set String -> Set String -> [Char] +formatTrivialError unexpectedTokens expectedTokens = let unexpectedMsg = case Set.toList unexpectedTokens of [] -> "I found something I didn't expect." [x] -> - let article = - if startsWithVowel x - then "an" - else "a" + let article = case x of + (c : _) | c `elem` ("aeiou" :: String) -> "an" + _ -> "a" in "I was surprised to find " <> article <> " " <> x <> " here." - xs -> "I was surprised to find these:\n\n* " <> intercalateMap "\n* " id xs + xs -> "I was surprised to find these:\n\n* " <> List.intercalate "\n* " xs expectedMsg = case Set.toList expectedTokens of [] -> Nothing - xs -> Just $ "\nI was hoping for one of these instead:\n\n* " <> intercalateMap "\n* " id xs - in mconcat $ catMaybes [Just unexpectedMsg, expectedMsg] + xs -> Just $ "\nI was hoping for one of these instead:\n\n* " <> List.intercalate "\n* " xs + in concat $ catMaybes [Just unexpectedMsg, expectedMsg] displayLexeme :: Lexeme -> String displayLexeme = \case