Skip to content

Commit

Permalink
Preserve EOF exception line number
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBrinks committed Nov 2, 2024
1 parent b242d12 commit 7c0548e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/SqlParser.Tests/ParserCommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using static SqlParser.Ast.Expression;
using Action = SqlParser.Ast.Action;
using DataType = SqlParser.Ast.DataType;
using Double = System.Double;
using Map = SqlParser.Ast.Map;
using Subscript = SqlParser.Ast.Subscript;

Expand Down Expand Up @@ -36,6 +37,18 @@ void TestExplain(
}
}

[Fact]
public void Parser_EOF_Exception_Preserves_Line_And_Column()
{
var exception = Assert.Throws<ParserException>(
() => ParseSqlStatements("""
select *
from table
""", [new PostgreSqlDialect()]));
Assert.Equal(2, exception.Line);
Assert.Equal(7, exception.Column);
}

[Fact]
public void Parse_Insert_Values()
{
Expand Down
20 changes: 16 additions & 4 deletions src/SqlParser/Parser.Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public Token PeekNthToken(int nth)

if (position >= _tokens.Count)
{
return new EOF();
var eof = new EOF();
eof.SetLocation(_tokens[^1].Location);
return eof;
}

var token = _tokens[position];
Expand All @@ -109,7 +111,9 @@ public Token PeekNthTokenNoSkip(int nth)
{
if (_index + nth >= _tokens.Count)
{
return new EOF();
var eof = new EOF();
eof.SetLocation(_tokens[^1].Location);
return eof;
}

return _tokens[_index + nth];
Expand Down Expand Up @@ -152,7 +156,9 @@ public IEnumerable<Token> PeekTokensWithLocation(int count)
{
if (index >= _tokens.Count)
{
yield return new EOF();
var eof = new EOF();
eof.SetLocation(_tokens[^1].Location);
yield return eof;
break;
}

Expand Down Expand Up @@ -186,7 +192,13 @@ public Token NextToken()
var position = _index - 1;
if (position >= _tokens.Count)
{
return new EOF();
var eof = new EOF();
if (_tokens.Count > 0)
{
eof.SetLocation(_tokens[^1].Location);
}

return eof;
}

var token = _tokens[_index - 1];
Expand Down
5 changes: 5 additions & 0 deletions src/SqlParser/Tokens/EOF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace SqlParser.Tokens;
/// </summary>
public class EOF() : StringToken("EOF")
{
public EOF(Location location) : this()
{
SetLocation(location);
}

public override bool Equals(object? obj)
{
return obj is EOF;
Expand Down

0 comments on commit 7c0548e

Please sign in to comment.