!55 feature: 增强错误定位

Merge pull request !55 from zhangxun/feature/enhancement-error-location
This commit is contained in:
Luke 2025-08-04 07:15:07 +00:00 committed by Gitee
commit 5fdb86cd1b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 7 additions and 7 deletions

View File

@ -37,7 +37,7 @@ public class LexerEngine {
* @param sourceName 文件名诊断用 * @param sourceName 文件名诊断用
*/ */
public LexerEngine(String source, String sourceName) { public LexerEngine(String source, String sourceName) {
this.absPath = new File(sourceName).getAbsolutePath(); this.absPath = new File(sourceName).getAbsolutePath().replace('\\', '/');
this.context = new LexerContext(source); this.context = new LexerContext(source);
this.scanners = List.of( this.scanners = List.of(
new WhitespaceTokenScanner(), new WhitespaceTokenScanner(),

View File

@ -50,6 +50,6 @@ public class LexicalError {
*/ */
@Override @Override
public String toString() { public String toString() {
return file + ":" + line + ", 列 " + column + ": " + message; return "file:///" + file + ":" + line + ":" + column + ": " + message;
} }
} }

View File

@ -40,6 +40,6 @@ public class ParseError {
*/ */
@Override @Override
public String toString() { public String toString() {
return file + ":" + line + ", 列 " + column + ": " + message; return "file:///" + file + ":" + line + ":" + column + ": " + message;
} }
} }

View File

@ -39,7 +39,7 @@ public class ParserContext {
*/ */
public ParserContext(List<Token> tokens, String sourceName) { public ParserContext(List<Token> tokens, String sourceName) {
this.tokens = new TokenStream(tokens); this.tokens = new TokenStream(tokens);
this.sourceName = Paths.get(sourceName).toAbsolutePath().toString(); this.sourceName = Paths.get(sourceName).toAbsolutePath().toString().replace('\\', '/');
} }

View File

@ -48,9 +48,9 @@ public record SemanticError(Node node, String message) {
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder("file:///");
if (file != null && !file.isBlank()) sb.append(file).append(":"); if (file != null && !file.isBlank()) sb.append(file).append(":");
sb.append((line >= 0 && col >= 0) ? "" + line + ", 列 " + col : "未知位置"); sb.append((line >= 0 && col >= 0) ? line + ":" + col : "未知位置");
sb.append(": ").append(message); sb.append(": ").append(message);
return sb.toString(); return sb.toString();
} }