From 4a83f2259db4213eb70a2c7932469f22a0d3c25f Mon Sep 17 00:00:00 2001 From: Pilow Date: Tue, 14 May 2024 23:46:20 +0700 Subject: [PATCH] Add file and line to every log --- .../java/leekscript/compiler/JavaWriter.java | 7 +++++ .../compiler/bloc/AbstractLeekBlock.java | 1 - .../compiler/bloc/MainLeekBlock.java | 1 - src/main/java/leekscript/runner/AI.java | 28 +++++++++++++++++-- .../java/leekscript/runner/BasicAILog.java | 6 +++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main/java/leekscript/compiler/JavaWriter.java b/src/main/java/leekscript/compiler/JavaWriter.java index ad70766..ea461e1 100644 --- a/src/main/java/leekscript/compiler/JavaWriter.java +++ b/src/main/java/leekscript/compiler/JavaWriter.java @@ -108,6 +108,13 @@ public void writeErrorFunction(IACompiler comp, String ai) { mCode.append(", "); } mCode.append("};}\n\n"); + + mCode.append("protected int[] getErrorFilesID() { return new int[] {"); + for (var f : mFilesList) { + mCode.append(f.getId()); + mCode.append(", "); + } + mCode.append("};}\n\n"); } public void addCounter(int count) { diff --git a/src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java b/src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java index 8821b10..5aeb151 100644 --- a/src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java +++ b/src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java @@ -5,7 +5,6 @@ import leekscript.compiler.Token; import leekscript.compiler.AnalyzeError; -import leekscript.compiler.IACompiler; import leekscript.compiler.JavaWriter; import leekscript.compiler.WordCompiler; import leekscript.compiler.AnalyzeError.AnalyzeErrorLevel; diff --git a/src/main/java/leekscript/compiler/bloc/MainLeekBlock.java b/src/main/java/leekscript/compiler/bloc/MainLeekBlock.java index f07230a..bbf36d4 100644 --- a/src/main/java/leekscript/compiler/bloc/MainLeekBlock.java +++ b/src/main/java/leekscript/compiler/bloc/MainLeekBlock.java @@ -21,7 +21,6 @@ import leekscript.compiler.exceptions.LeekCompilerException; import leekscript.compiler.expression.LeekExpressionException; import leekscript.compiler.expression.LeekNumber; -import leekscript.compiler.expression.LeekVariable; import leekscript.compiler.expression.LeekVariable.VariableType; import leekscript.compiler.instruction.ClassDeclarationInstruction; import leekscript.compiler.instruction.LeekGlobalDeclarationInstruction; diff --git a/src/main/java/leekscript/runner/AI.java b/src/main/java/leekscript/runner/AI.java index db1bb88..f30638b 100644 --- a/src/main/java/leekscript/runner/AI.java +++ b/src/main/java/leekscript/runner/AI.java @@ -35,6 +35,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Random; import java.util.Set; import java.util.TreeMap; @@ -476,11 +477,28 @@ public String getErrorMessage(StackTraceElement[] elements) { return sb.toString(); } + public record LeekScriptPosition(int file, int line) {} + + public LeekScriptPosition getCurrentLeekScriptPosition() { + for (StackTraceElement element : Thread.currentThread().getStackTrace()) { + if (element.getClassName().startsWith("AI_")) { + var mapping = getLineMapping(element.getLineNumber()); + if (mapping != null) { + var files = getErrorFilesID(); + var f = mapping.getAI(); + int file = f < files.length ? files[f] : 0; + return new LeekScriptPosition(file, mapping.getLeekScriptLine()); + } + } + } + return null; + } + public String getErrorMessage(Throwable e) { return getErrorMessage(e.getStackTrace()); } - protected String getErrorLocalisation(int javaLine) { + protected LineMapping getLineMapping(int javaLine) { if (mLinesMapping.isEmpty() && this.filesLines != null && this.filesLines.exists()) { try (Stream stream = Files.lines(this.filesLines.toPath())) { stream.forEach(l -> { @@ -490,7 +508,11 @@ protected String getErrorLocalisation(int javaLine) { } catch (IOException e) {} thisObject = getAIString(); } - var lineMapping = mLinesMapping.get(javaLine); + return mLinesMapping.get(javaLine); + } + + protected String getErrorLocalisation(int javaLine) { + var lineMapping = getLineMapping(javaLine); if (lineMapping != null) { var files = getErrorFiles(); var f = lineMapping.getAI(); @@ -639,6 +661,8 @@ else if (type == AILog.STANDARD) protected String[] getErrorFiles() { return null; } + protected int[] getErrorFilesID() { return null; } + protected String getAIString() { return ""; } public Object runIA() throws LeekRunException { diff --git a/src/main/java/leekscript/runner/BasicAILog.java b/src/main/java/leekscript/runner/BasicAILog.java index d9daccb..7bb71cc 100644 --- a/src/main/java/leekscript/runner/BasicAILog.java +++ b/src/main/java/leekscript/runner/BasicAILog.java @@ -52,10 +52,14 @@ public void addSystemLog(AI ai, int type, String trace, int key, Object[] parame public void addLog(int type, String message) { message = message.replaceAll("\t", " "); - addLog(type, message, 0); + addLog(type, message, 0, 0, 0); } public void addLog(int type, String message, int color) { + addLog(type, message, color, 0, 0); + } + + public void addLog(int type, String message, int color, int ai, int line) { if (message == null || !addSize(20 + message.length())) { return;