Skip to content

Commit

Permalink
Simplify error reporting in Language{Lexer,Parser}.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Feb 18, 2023
1 parent db87e58 commit 342c1ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
24 changes: 11 additions & 13 deletions src/syntax/LanguageLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -348,8 +347,7 @@ public IReadOnlyList<SyntaxToken> Lex()
{
Advance();

// TODO: Better printing of special characters (white space, control, etc).
Error(location, $"Unrecognized character '{cur}'");
Error(location, $"Unrecognized character");
}

LexTrivia(_location, _trailing);
Expand Down Expand Up @@ -459,7 +457,7 @@ private void LexComment(SourceLocation location, ImmutableArray<SyntaxTrivia>.Bu

if (ch1 == '!')
{
ErrorExpected("'='", ch2);
ErrorExpected("'='");

return (location, SyntaxTokenKind.ExclamationEquals);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -583,7 +581,7 @@ bool ConsumeDigits(int radix)

if (!ConsumeDigits(10))
{
ErrorExpected("digit", Peek1());
ErrorExpected("digit");

return (location, SyntaxTokenKind.RealLiteral);
}
Expand All @@ -598,7 +596,7 @@ bool ConsumeDigits(int radix)
Advance();

if (!ConsumeDigits(10))
ErrorExpected("digit", Peek1());
ErrorExpected("digit");

return (location, SyntaxTokenKind.RealLiteral);
}
Expand Down Expand Up @@ -645,7 +643,7 @@ bool ConsumeDigits(int radix)

if (cur is null or '\n' or '\r')
{
ErrorExpected("closing '\"'", cur);
ErrorExpected("closing '\"'");

break;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down
33 changes: 17 additions & 16 deletions src/syntax/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -108,7 +108,7 @@ SyntaxTokenKind.AtomLiteral or

var missing = Missing();

Error(missing, next?.Location, "Expected literal");
ErrorExpected(missing, next?.Location, "literal");

return missing;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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<T>.Builder Builder<T>()
Expand Down Expand Up @@ -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)));
}
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 342c1ae

Please sign in to comment.