diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-28 12:05:32 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-28 12:05:32 +0100 |
| commit | 2f8e32636912e23552f5b43a2c84109b77fe7a92 (patch) | |
| tree | 3fd8b56052a8dba9ab501f164fd83b2da545c7ab /src/main/java | |
| parent | b1ca0653b7bd80302a287faef3a7c7647f92d23a (diff) | |
Attach source to all compiler errors
Diffstat (limited to 'src/main/java')
6 files changed, 29 insertions, 22 deletions
diff --git a/src/main/java/org/shedlang/compiler/cli/ShedCli.java b/src/main/java/org/shedlang/compiler/cli/ShedCli.java index 29f27161..bdbbfb39 100644 --- a/src/main/java/org/shedlang/compiler/cli/ShedCli.java +++ b/src/main/java/org/shedlang/compiler/cli/ShedCli.java @@ -7,7 +7,7 @@ import org.shedlang.compiler.ShedCompiler; import org.shedlang.compiler.backends.Backends; import org.shedlang.compiler.backends.GenerateInstructionsBackend; import org.shedlang.compiler.backends.PatioBackend; -import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.errors.CompilerError; import java.io.IOException; import java.nio.file.Paths; @@ -17,8 +17,8 @@ public class ShedCli { var args = parseArgs(rawArgs); try { compile(args); - } catch (SourceError error) { - System.err.println(error.getSource().describe()); + } catch (CompilerError error) { + System.err.println(error.source().describe()); System.err.println(error.getMessage()); System.exit(2); } diff --git a/src/main/java/org/shedlang/compiler/errors/CompilerError.java b/src/main/java/org/shedlang/compiler/errors/CompilerError.java index edfd9262..58fe7a8d 100644 --- a/src/main/java/org/shedlang/compiler/errors/CompilerError.java +++ b/src/main/java/org/shedlang/compiler/errors/CompilerError.java @@ -1,7 +1,16 @@ package org.shedlang.compiler.errors; +import org.shedlang.compiler.sources.Source; + public class CompilerError extends RuntimeException { - public CompilerError(String message) { + private final Source source; + + public CompilerError(String message, Source source) { super(message); + this.source = source; + } + + public Source source() { + return source; } } diff --git a/src/main/java/org/shedlang/compiler/errors/InternalCompilerError.java b/src/main/java/org/shedlang/compiler/errors/InternalCompilerError.java index 8ef02b7b..b3b41796 100644 --- a/src/main/java/org/shedlang/compiler/errors/InternalCompilerError.java +++ b/src/main/java/org/shedlang/compiler/errors/InternalCompilerError.java @@ -1,7 +1,13 @@ package org.shedlang.compiler.errors; +import org.shedlang.compiler.sources.Source; + +/** + * An error internal to the compiler. This normally means the compiler is in an + * unexpected state, and has encountered a bug that should be fixed. + */ public class InternalCompilerError extends CompilerError { - public InternalCompilerError(String message) { - super(message); + public InternalCompilerError(String message, Source source) { + super(message, source); } } diff --git a/src/main/java/org/shedlang/compiler/errors/SourceError.java b/src/main/java/org/shedlang/compiler/errors/SourceError.java index 2dfac789..a0558fa9 100644 --- a/src/main/java/org/shedlang/compiler/errors/SourceError.java +++ b/src/main/java/org/shedlang/compiler/errors/SourceError.java @@ -2,15 +2,12 @@ package org.shedlang.compiler.errors; import org.shedlang.compiler.sources.Source; +/** + * An error in the source code. These errors are expected when attempting to + * compile invalid code. + */ public class SourceError extends CompilerError { - private final Source source; - public SourceError(String message, Source source) { - super(message); - this.source = source; - } - - public Source getSource() { - return source; + super(message, source); } } diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java b/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java index b7582135..69c0130d 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java @@ -6,19 +6,13 @@ import org.shedlang.compiler.types.StructType; public class MissingStructTypeInfoError extends InternalCompilerError { private final StructType type; - private final Source source; public MissingStructTypeInfoError(StructType type, Source source) { - super("Missing type info for struct " + type); + super("Missing type info for struct " + type, source); this.type = type; - this.source = source; } public StructType type() { return type; } - - public Source source() { - return source; - } } diff --git a/src/main/java/org/shedlang/compiler/types/NoSuchFieldInternalError.java b/src/main/java/org/shedlang/compiler/types/NoSuchFieldInternalError.java index a465e5b0..d60ed5f5 100644 --- a/src/main/java/org/shedlang/compiler/types/NoSuchFieldInternalError.java +++ b/src/main/java/org/shedlang/compiler/types/NoSuchFieldInternalError.java @@ -1,12 +1,13 @@ package org.shedlang.compiler.types; import org.shedlang.compiler.errors.InternalCompilerError; +import org.shedlang.compiler.sources.Sources; public class NoSuchFieldInternalError extends InternalCompilerError { private final String fieldName; public NoSuchFieldInternalError(String fieldName) { - super("No such field: " + fieldName); + super("No such field: " + fieldName, Sources.NULL); this.fieldName = fieldName; } |
