Skip to content

Commit 92a6d48

Browse files
committed
Fix: Add line/column numbers to include processing errors
- Updated ResolveIncludePath to accept source file and line number parameters - Enhanced all include error exceptions to include proper source position - Fixed compiler exception handler to extract line/column from ECPException - Include errors now show exact file, line, and column where directive appears Resolves missing line numbers in include processing error output. Before: ERROR: Cannot resolve include path: math.e (no location) After: ERROR: Cannot resolve include path: math.e File: test.cp Line: 5, Column: 1
1 parent 3c6bf99 commit 92a6d48

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed
File renamed without changes.
File renamed without changes.

examples/testbed/UELTestbed.pas

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ procedure CompileFile(const AFileName: string);
8989
CPPrintLn('=== CP-Lang Compiler Test ===');
9090
CPPrintLn('');
9191

92-
if not TFile.Exists(AFileName) then
93-
begin
94-
CPPrintLn('Error: File not found: %s', [AFileName]);
95-
Exit;
96-
end;
97-
9892
LCompiler := TCPCompiler.Create();
9993
try
10094
try // ← EXCEPTION HANDLING BLOCK
@@ -111,9 +105,12 @@ procedure CompileFile(const AFileName: string);
111105

112106
LResult := LCompiler.CompileFile(AFileName);
113107
try // ← RESOURCE CLEANUP FOR LResult
114-
CPPrintLn('');
115-
CPPrintLn('--- SOURCE ---');
116-
CPPrintLn(LResult.MergedSource);
108+
if not LResult.MergedSource.IsEmpty then
109+
begin
110+
CPPrintLn('');
111+
CPPrintLn('--- SOURCE ---');
112+
CPPrintLn(LResult.MergedSource);
113+
end;
117114

118115
CPPrintLn('');
119116
CPPrintLn('Phase reached: %s', [GetEnumName(TypeInfo(TCPCompilationPhase), Ord(LResult.Phase))]);
@@ -199,7 +196,7 @@ procedure CompileFile(const AFileName: string);
199196
procedure RunTests();
200197
begin
201198
try
202-
CompileFile('test.e');
199+
CompileFile('test.cp');
203200
except
204201
on E: ECPException do
205202
begin

src/CPLang.Common.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ interface
2525
System.SysUtils,
2626
CPLang.Platform;
2727

28+
const
29+
CPSourceFileExt = '.cp';
30+
2831
{ Console }
2932
procedure CPClearToEOL();
3033
function CPPrint(const AText: string): string; overload;

src/CPLang.Compiler.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,9 @@ function TCPCompiler.ProcessIncludes(const AMainFileName: string): Boolean;
618618
TCPCompilerError.Create(
619619
E.Message,
620620
'Include',
621-
AMainFileName,
622-
0, 0,
621+
E.SourceFileName,
622+
E.LineNumber,
623+
E.ColumnNumber,
623624
esError
624625
)
625626
);

src/CPLang.Include.pas

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TCPIncludeManager = class
4040
FMergedSource: string;
4141

4242
function ProcessFile(const AFileName: string; var ACurrentCharPos: Integer): string;
43-
function ResolveIncludePath(const AIncludePath: string; const AIsAngleBracket: Boolean): string;
43+
function ResolveIncludePath(const AIncludePath: string; const AIsAngleBracket: Boolean; const ASourceFile: string; const ALineNumber: Integer): string;
4444
function IsAlreadyIncluded(const AFileName: string): Boolean;
4545
function ValidateAngleBracketInclude(const AInclude: string): Boolean;
4646

@@ -125,7 +125,7 @@ function TCPIncludeManager.ValidateAngleBracketInclude(const AInclude: string):
125125

126126
// Must have .e extension
127127
LFileName := AInclude.ToLower();
128-
if not LFileName.EndsWith('.e') then
128+
if not LFileName.EndsWith(CPSourceFileExt) then
129129
Exit;
130130

131131
// Must be a valid filename (not empty after removing extension)
@@ -166,7 +166,7 @@ function TCPIncludeManager.ProcessFile(const AFileName: string; var ACurrentChar
166166
Result := '';
167167

168168
if not TFile.Exists(AFileName) then
169-
raise ECPException.Create('Include file not found: %s', [AFileName]);
169+
raise ECPException.Create('File not found: %s', [AFileName]);
170170

171171
if IsAlreadyIncluded(AFileName) then
172172
Exit; // Prevent circular includes
@@ -207,7 +207,7 @@ function TCPIncludeManager.ProcessFile(const AFileName: string; var ACurrentChar
207207
AddIncludePath(LIncludePath);
208208
end
209209
else
210-
raise ECPException.Create('Invalid #includepath directive: path must be enclosed in quotes');
210+
raise ECPException.Create('Invalid #includepath directive: path must be enclosed in quotes', [], LFullPath, LLineIndex + 1, 1);
211211

212212
// After includepath, prepare for next content block
213213
LContentStartPos := ACurrentCharPos;
@@ -233,7 +233,7 @@ function TCPIncludeManager.ProcessFile(const AFileName: string; var ACurrentChar
233233
begin
234234
// Quoted include - allows paths, relative resolution
235235
LIncludePath := LLine.Substring(1, LLine.Length - 2);
236-
LIncludeFile := ResolveIncludePath(LIncludePath, False);
236+
LIncludeFile := ResolveIncludePath(LIncludePath, False, LFullPath, LLineIndex + 1);
237237
LIncludeContent := ProcessFile(LIncludeFile, ACurrentCharPos);
238238
Result := Result + LIncludeContent;
239239
end
@@ -243,14 +243,14 @@ function TCPIncludeManager.ProcessFile(const AFileName: string; var ACurrentChar
243243
LIncludePath := LLine.Substring(1, LLine.Length - 2);
244244

245245
if not ValidateAngleBracketInclude(LIncludePath) then
246-
raise ECPException.Create('Invalid angle bracket include "%s": must be filename.e format only (no paths)', [LIncludePath]);
246+
raise ECPException.Create('Invalid angle bracket include "%s": must be filename.e format only (no paths)', [LIncludePath], LFullPath, LLineIndex + 1, 1);
247247

248-
LIncludeFile := ResolveIncludePath(LIncludePath, True);
248+
LIncludeFile := ResolveIncludePath(LIncludePath, True, LFullPath, LLineIndex + 1);
249249
LIncludeContent := ProcessFile(LIncludeFile, ACurrentCharPos);
250250
Result := Result + LIncludeContent;
251251
end
252252
else
253-
raise ECPException.Create('Invalid #include directive: must be "filename" or <filename>');
253+
raise ECPException.Create('Invalid #include directive: must be "filename" or <filename>', [], LFullPath, LLineIndex + 1, 1);
254254

255255
// After include, prepare for next content block
256256
LContentStartPos := ACurrentCharPos;
@@ -280,7 +280,7 @@ function TCPIncludeManager.ProcessFile(const AFileName: string; var ACurrentChar
280280
end;
281281
end;
282282

283-
function TCPIncludeManager.ResolveIncludePath(const AIncludePath: string; const AIsAngleBracket: Boolean): string;
283+
function TCPIncludeManager.ResolveIncludePath(const AIncludePath: string; const AIsAngleBracket: Boolean; const ASourceFile: string; const ALineNumber: Integer): string;
284284
var
285285
LSearchPaths: string;
286286
LIndex: Integer;
@@ -289,7 +289,7 @@ function TCPIncludeManager.ResolveIncludePath(const AIncludePath: string; const
289289
begin
290290
// Angle bracket include - search in include paths only
291291
if FIncludePaths.Count = 0 then
292-
raise ECPException.Create('No include paths configured for angle bracket include: <%s>', [AIncludePath]);
292+
raise ECPException.Create('No include paths configured for angle bracket include: <%s>', [AIncludePath], ASourceFile, ALineNumber, 1);
293293

294294
// Build semicolon-separated search paths for FileSearch
295295
LSearchPaths := '';
@@ -302,7 +302,7 @@ function TCPIncludeManager.ResolveIncludePath(const AIncludePath: string; const
302302

303303
Result := FileSearch(AIncludePath, LSearchPaths);
304304
if Result = '' then
305-
raise ECPException.Create('Cannot find angle bracket include <%s> in configured include paths', [AIncludePath]);
305+
raise ECPException.Create('Cannot find angle bracket include <%s> in configured include paths', [AIncludePath], ASourceFile, ALineNumber, 1);
306306
end
307307
else
308308
begin
@@ -319,7 +319,7 @@ function TCPIncludeManager.ResolveIncludePath(const AIncludePath: string; const
319319
Exit;
320320
end;
321321

322-
raise ECPException.Create('Cannot resolve include path: %s', [AIncludePath]);
322+
raise ECPException.Create('Cannot resolve include path: %s', [AIncludePath], ASourceFile, ALineNumber, 1);
323323
end;
324324
end;
325325

0 commit comments

Comments
 (0)