From 342c1aea8a3f7dc4db031e0870f0e5632ba2a31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 18 Feb 2023 02:08:11 +0100 Subject: [PATCH] Simplify error reporting in Language{Lexer,Parser}. --- src/syntax/LanguageLexer.cs | 24 +++++++++++------------- src/syntax/LanguageParser.cs | 33 +++++++++++++++++---------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/syntax/LanguageLexer.cs b/src/syntax/LanguageLexer.cs index 46b0ead3..155c7ce6 100644 --- a/src/syntax/LanguageLexer.cs +++ b/src/syntax/LanguageLexer.cs @@ -66,10 +66,9 @@ private void Error(SourceLocation location, string message) token => SourceDiagnostic.Create(token, SourceDiagnosticSeverity.Error, location, message)); } - private void ErrorExpected(string expected, char? found) + private void ErrorExpected(string expected) { - // TODO: Better printing of special characters (white space, control, etc). - Error(_location, $"Expected {expected}, but found {(found != null ? $"'{found}'" : "end of input")}"); + Error(_location, $"Expected {expected}"); } private SyntaxTrivia CreateTrivia(SourceLocation location, SyntaxTriviaKind kind) @@ -348,8 +347,7 @@ public IReadOnlyList Lex() { Advance(); - // TODO: Better printing of special characters (white space, control, etc). - Error(location, $"Unrecognized character '{cur}'"); + Error(location, $"Unrecognized character"); } LexTrivia(_location, _trailing); @@ -459,7 +457,7 @@ private void LexComment(SourceLocation location, ImmutableArray.Bu if (ch1 == '!') { - ErrorExpected("'='", ch2); + ErrorExpected("'='"); return (location, SyntaxTokenKind.ExclamationEquals); } @@ -570,7 +568,7 @@ bool ConsumeDigits(int radix) if (!ConsumeDigits(radix) && radix != 10) { - ErrorExpected($"base-{radix} digit", Peek1()); + ErrorExpected($"base-{radix} digit"); return (location, SyntaxTokenKind.IntegerLiteral); } @@ -583,7 +581,7 @@ bool ConsumeDigits(int radix) if (!ConsumeDigits(10)) { - ErrorExpected("digit", Peek1()); + ErrorExpected("digit"); return (location, SyntaxTokenKind.RealLiteral); } @@ -598,7 +596,7 @@ bool ConsumeDigits(int radix) Advance(); if (!ConsumeDigits(10)) - ErrorExpected("digit", Peek1()); + ErrorExpected("digit"); return (location, SyntaxTokenKind.RealLiteral); } @@ -645,7 +643,7 @@ bool ConsumeDigits(int radix) if (cur is null or '\n' or '\r') { - ErrorExpected("closing '\"'", cur); + ErrorExpected("closing '\"'"); break; } @@ -676,7 +674,7 @@ bool ConsumeDigits(int radix) if (digit is not (>= '0' and <= '9') or (>= 'a' and <= 'f') or (>= 'A' and <= 'F')) { - ErrorExpected("Unicode escape sequence digit", digit); + ErrorExpected("Unicode escape sequence digit"); break; } @@ -685,11 +683,11 @@ bool ConsumeDigits(int radix) } if (!Rune.IsValid(int.Parse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture))) - Error(loc, $"Expected valid Unicode escape sequence, but found '{hex}'"); + Error(loc, $"Invalid Unicode escape sequence"); break; default: - ErrorExpected("escape sequence code", code); + ErrorExpected("escape sequence code"); break; } } diff --git a/src/syntax/LanguageParser.cs b/src/syntax/LanguageParser.cs index d6b3a621..19792b9f 100644 --- a/src/syntax/LanguageParser.cs +++ b/src/syntax/LanguageParser.cs @@ -59,7 +59,7 @@ private SyntaxToken ExpectCodeIdentifier() var missing = Missing(); - Error(missing, next?.Location, "Expected lowercase identifier"); + ErrorExpected(missing, next?.Location, "lowercase identifier"); return missing; } @@ -74,7 +74,7 @@ private SyntaxToken ExpectBindingIdentifier() var missing = Missing(); - Error(missing, next?.Location, "Expected lowercase or discard identifier"); + ErrorExpected(missing, next?.Location, "lowercase or discard identifier"); return missing; } @@ -88,7 +88,7 @@ private SyntaxToken ExpectNumericLiteral() var missing = Missing(); - Error(missing, next?.Location, "Expected real or integer literal"); + ErrorExpected(missing, next?.Location, "real or integer literal"); return missing; } @@ -108,7 +108,7 @@ SyntaxTokenKind.AtomLiteral or var missing = Missing(); - Error(missing, next?.Location, "Expected literal"); + ErrorExpected(missing, next?.Location, "literal"); return missing; } @@ -123,10 +123,10 @@ private SyntaxToken Expect(SyntaxTokenKind kind1, SyntaxTokenKind kind2) var missing = Missing(); - Error( + ErrorExpected( missing, next?.Location, - $"Expected {SyntaxFacts.GetFriendlyName(kind1)} or {SyntaxFacts.GetFriendlyName(kind2)}"); + $"{SyntaxFacts.GetFriendlyName(kind1)} or {SyntaxFacts.GetFriendlyName(kind2)}"); return missing; } @@ -140,7 +140,7 @@ private SyntaxToken Expect(SyntaxTokenKind kind) var missing = Missing(); - Error(missing, next?.Location, $"Expected {SyntaxFacts.GetFriendlyName(kind)}"); + ErrorExpected(missing, next?.Location, SyntaxFacts.GetFriendlyName(kind)); return missing; } @@ -165,10 +165,11 @@ private SyntaxToken Missing() return new(_path); } - private void Error(SyntaxItem item, SourceLocation? location, string message) + private void ErrorExpected(SyntaxItem item, SourceLocation? location, string expected) { _diagnostics.Add( - SourceDiagnostic.Create(item, SourceDiagnosticSeverity.Error, location ?? _eoiLocation, message)); + SourceDiagnostic.Create( + item, SourceDiagnosticSeverity.Error, location ?? _eoiLocation, $"Expected {expected}")); } private static ImmutableArray.Builder Builder() @@ -229,7 +230,7 @@ void DrainToMissingDeclaration() { var first = skipped[0]; - Error(first, first.Location, "Expected declaration"); + ErrorExpected(first, first.Location, "declaration"); decls.Add(new MissingDeclarationSyntax(DrainList(dattrs), Missing(), DrainList(skipped))); } @@ -274,7 +275,7 @@ void DrainToMissingStatement(SyntaxToken? semicolon) { var first = skipped[0]; - Error(first, first.Location, "Expected declaration or statement"); + ErrorExpected(first, first.Location, "declaration or statement"); stmts.Add(new MissingStatementSyntax(DrainList(attrs), DrainList(skipped), semicolon ?? Missing())); } @@ -530,7 +531,7 @@ _ when IsMinus(next3?.First) => ParseLiteralType(), }; if (type is MissingTypeSyntax) - Error(type, Peek1()?.Location, "Expected type"); + ErrorExpected(type, Peek1()?.Location, "type"); return type; } @@ -1176,7 +1177,7 @@ private ExpressionSyntax ParsePrimaryExpression() }; if (expr is MissingExpressionSyntax) - Error(expr, Peek1()?.Location, "Expected expression"); + ErrorExpected(expr, Peek1()?.Location, "expression"); return ParsePostfixExpression(expr); } @@ -1227,7 +1228,7 @@ void DrainToMissingStatement(SyntaxToken? semicolon) { var first = skipped[0]; - Error(first, first.Location, "Expected statement"); + ErrorExpected(first, first.Location, "statement"); stmts.Add(new MissingStatementSyntax(DrainList(attrs), DrainList(skipped), semicolon ?? Missing())); } @@ -1843,7 +1844,7 @@ _ when IsMinus(next2?.First) => ParseLiteralPattern(), }; if (pat is MissingPatternSyntax) - Error(pat, Peek1()?.Location, "Expected pattern"); + ErrorExpected(pat, Peek1()?.Location, "pattern"); return pat; } @@ -1868,7 +1869,7 @@ private PatternBindingSyntax ParsePatternBinding() }; if (binding is MissingPatternBindingSyntax) - Error(binding, next?.Location, "Expected pattern binding"); + ErrorExpected(binding, next?.Location, "pattern binding"); return binding; }