diff --git a/Source/DelphiAST.pas b/Source/DelphiAST.pas index 5ff7b5c..0b79f42 100644 --- a/Source/DelphiAST.pas +++ b/Source/DelphiAST.pas @@ -2672,7 +2672,7 @@ function TPasSyntaxTreeBuilder.Run(SourceStream: TStream): TSyntaxNode; on E: EParserException do raise ESyntaxTreeException.Create(E.Line, E.Col, Lexer.FileName, E.Message, Result); on E: ESyntaxError do - raise ESyntaxTreeException.Create(E.PosXY.X, E.PosXY.Y, Lexer.FileName, E.Message, Result); + raise ESyntaxTreeException.Create(E.PosXY.Y, E.PosXY.X, Lexer.FileName, E.Message, Result); else FreeAndNil(Result); raise; diff --git a/Test/UnitTests/DelphiAST.Tests.pas b/Test/UnitTests/DelphiAST.Tests.pas index c46a2bb..da6354a 100644 --- a/Test/UnitTests/DelphiAST.Tests.pas +++ b/Test/UnitTests/DelphiAST.Tests.pas @@ -258,6 +258,28 @@ procedure TestInvalidSyntax; AssertTrue(Raised, 'Invalid syntax did not raise ESyntaxTreeException'); end; +procedure TestUnexpectedEndOfFilePosition; +var + Root: TSyntaxNode; + Raised: Boolean; +begin + Root := nil; + Raised := False; + try + Root := ParseSource('unit Broken;' + sLineBreak + 'interface' + sLineBreak + + 'implementation' + sLineBreak + 'procedure Run;' + sLineBreak + 'begin'); + except + on E: ESyntaxTreeException do + begin + Raised := True; + AssertEquals(5, E.Line, 'The end of file is on line 5.'); + AssertEquals(6, E.Col, 'The end of file is after the 5 characters of "begin".'); + end; + end; + Root.Free; + AssertTrue(Raised, 'An unexpected end of file did not raise ESyntaxTreeException'); +end; + {$IFNDEF FPC} procedure TestBinarySerializationRoundTrip; var @@ -296,6 +318,7 @@ procedure RunAllTests; RunTest('AST.ConstantEndPosition', TestConstantEndPosition); RunTest('AST.VariableEndPosition', TestVariableEndPosition); RunTest('Parser.InvalidSyntax', TestInvalidSyntax); + RunTest('Parser.UnexpectedEndOfFilePosition', TestUnexpectedEndOfFilePosition); {$IFNDEF FPC} RunTest('Serialization.BinaryRoundTrip', TestBinarySerializationRoundTrip); {$ENDIF}