Fix lexing of unpaired brackets

This commit is contained in:
Alexander Luzgarev 2020-07-07 22:02:40 +02:00
parent 078eb003d0
commit 552f960299
3 changed files with 33 additions and 5 deletions

View File

@ -89,8 +89,6 @@ namespace Parser.Tests
let text = SyntaxFacts.GetText(kind) let text = SyntaxFacts.GetText(kind)
where !(text is null) where !(text is null)
where !(SyntaxFacts.IsUnaryTokenKind(kind) where !(SyntaxFacts.IsUnaryTokenKind(kind)
|| SyntaxFacts.IsOpeningToken(kind)
|| SyntaxFacts.IsClosingToken(kind)
|| kind == TokenKind.ApostropheToken) || kind == TokenKind.ApostropheToken)
select (kind, text); select (kind, text);
@ -254,6 +252,22 @@ namespace Parser.Tests
{ {
return true; return true;
} }
if (kind1 == TokenKind.CloseBraceToken && kind2 == TokenKind.StringLiteralToken)
{
return true;
}
if (kind1 == TokenKind.CloseParenthesisToken && kind2 == TokenKind.StringLiteralToken)
{
return true;
}
if (kind1 == TokenKind.CloseSquareBracketToken && kind2 == TokenKind.StringLiteralToken)
{
return true;
}
return false; return false;
} }

View File

@ -57,6 +57,16 @@ namespace Parser.Internal
Report($"Unexpected token '{actual}', expected '{expected}'."); Report($"Unexpected token '{actual}', expected '{expected}'.");
} }
internal void ReportUnmatchedCloseParenthesis(TextSpan span, TokenKind kind)
{
Report(span, $"Unmatched close parenthesis '{kind}'.");
}
internal void ReportUnmatchedOpenParenthesisByEndOfFile(TextSpan span)
{
Report(span, "Unmatched open parenthesis by the end of file.");
}
public IEnumerator<Diagnostic> GetEnumerator() public IEnumerator<Diagnostic> GetEnumerator()
{ {
return _diagnostics.GetEnumerator(); return _diagnostics.GetEnumerator();
@ -66,5 +76,6 @@ namespace Parser.Internal
{ {
return GetEnumerator(); return GetEnumerator();
} }
} }
} }

View File

@ -798,19 +798,22 @@ namespace Parser.Internal
} }
else else
{ {
throw new ParsingException($"Unmatched \"{tokenInfo.Text}\" at {Window.Position}."); Diagnostics.ReportUnmatchedCloseParenthesis(
new TextSpan(Window.Position.Offset, 1), tokenInfo.Kind);
} }
} }
else else
{ {
throw new ParsingException($"Unmatched \"{tokenInfo.Text}\" at {Window.Position}."); Diagnostics.ReportUnmatchedCloseParenthesis(
new TextSpan(Window.Position.Offset, 1), tokenInfo.Kind);
} }
} }
if (tokenInfo.Kind == TokenKind.EndOfFileToken if (tokenInfo.Kind == TokenKind.EndOfFileToken
&& TokenStack.Any()) && TokenStack.Any())
{ {
throw new ParsingException($"Unmatched \"{TokenStack.Pop()}\" by the end of file."); Diagnostics.ReportUnmatchedOpenParenthesisByEndOfFile(
new TextSpan(Window.Position.Offset, 1));
} }
var result = Create( var result = Create(