Skip to content

Commit 0a1a5fe

Browse files
committed
GH-548 Return Result<Application, Throwable> by Interpreter
1 parent 0f4b8ed commit 0a1a5fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+104
-250
lines changed

panda-framework/src/main/java/org/panda_lang/language/Failure.java

+8
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616

1717
package org.panda_lang.language;
1818

19+
import org.panda_lang.language.interpreter.source.IndicatedSource;
1920
import org.panda_lang.utilities.commons.function.Option;
2021

2122
/**
2223
* Represents errors occurred in the framework
2324
*/
2425
public interface Failure {
2526

27+
/**
28+
* Get indicated source
29+
*
30+
* @return the indicated source
31+
*/
32+
IndicatedSource getIndicatedSource();
33+
2634
/**
2735
* Get additional information about the failure
2836
*

panda-framework/src/main/java/org/panda_lang/language/architecture/expression/ExpressionUtils.java

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public static <T> T evaluateConstExpression(Expression expression) {
4545
try {
4646
return expression.evaluate(new PandaProcessStack(PROCESS, PandaRuntimeConstants.DEFAULT_STACK_SIZE), null);
4747
} catch (Exception e) {
48-
e.printStackTrace();
4948
throw new PandaRuntimeException("Cannot evaluate static expression: " + expression, e);
5049
}
5150
}

panda-framework/src/main/java/org/panda_lang/language/architecture/module/PandaModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class PandaModule extends PandaModules implements Module {
2727

2828
protected final String name;
2929
protected final TypesMap types;
30-
protected @Nullable Module parent;
30+
protected @Nullable final Module parent;
3131

3232
public PandaModule(Module parent, String name) {
3333
this.name = name;

panda-framework/src/main/java/org/panda_lang/language/architecture/statement/StandardizedFramedScope.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public interface StandardizedFramedScope extends FramedScope {
2929
*
3030
* @return instance of the frame
3131
*/
32-
Frame revive(ProcessStack stack, Object instance) throws Exception;
32+
Frame revive(ProcessStack stack, Object instance);
3333

3434
}

panda-framework/src/main/java/org/panda_lang/language/architecture/type/TypeScope.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public TypeInstance createInstance(ProcessStack stack, @Nullable Object instance
4949

5050
try {
5151
typeInstance = getConstructor(parameterTypes).newInstance(ArrayUtils.merge(typeFrame, arguments, Object[]::new));
52-
} catch (InvocationTargetException e) {
53-
e.printStackTrace();
54-
throw new PandaRuntimeException(e.getTargetException().getMessage(), e.getTargetException());
52+
} catch (InvocationTargetException targetException) {
53+
throw new PandaRuntimeException(targetException.getTargetException().getMessage(), targetException.getTargetException());
5554
}
5655

5756
for (TypeField field : type.getFields().getDeclaredProperties()) {

panda-framework/src/main/java/org/panda_lang/language/architecture/type/dynamic/DynamicClassGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class DynamicClassGenerator {
4949

5050
private final Type type;
5151
private final CtClass generatedStructure;
52-
private boolean isInterface;
52+
private final boolean isInterface;
5353

5454
DynamicClassGenerator(Type type, CtClass generatedStructure) {
5555
this.type = type;

panda-framework/src/main/java/org/panda_lang/language/interpreter/Interpreter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.panda_lang.language.architecture.Application;
2020
import org.panda_lang.language.architecture.Environment;
2121
import org.panda_lang.language.interpreter.source.Source;
22+
import org.panda_lang.utilities.commons.function.Result;
2223

2324
/**
2425
* Translate source code into efficient intermediate representation and build an application
@@ -30,7 +31,7 @@ public interface Interpreter {
3031
*
3132
* @return interpreted application
3233
*/
33-
Application interpret(Source source);
34+
Result<Application, Throwable> interpret(Source source);
3435

3536
/**
3637
* Get associated environment

panda-framework/src/main/java/org/panda_lang/language/interpreter/InterpreterFailure.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,37 @@
1616

1717
package org.panda_lang.language.interpreter;
1818

19-
import org.panda_lang.language.PandaFrameworkException;
19+
import org.jetbrains.annotations.Nullable;
2020
import org.panda_lang.language.Failure;
21+
import org.panda_lang.language.PandaFrameworkException;
2122
import org.panda_lang.language.interpreter.source.IndicatedSource;
23+
import org.panda_lang.utilities.commons.function.Option;
2224

23-
/**
24-
* InterpreterFailures are dedicated exceptions thrown by the Panda Framework.
25-
* It contains extra data about the error and may be used to enhance logging/tooling.
26-
*/
27-
public abstract class InterpreterFailure extends PandaFrameworkException implements Failure {
25+
public class InterpreterFailure extends PandaFrameworkException implements Failure {
2826

29-
protected InterpreterFailure(String message, Throwable cause) {
27+
private final IndicatedSource indicatedSource;
28+
private final String note;
29+
30+
public InterpreterFailure(IndicatedSource indicatedSource, String message, @Nullable String note) {
31+
super(message);
32+
this.indicatedSource = indicatedSource;
33+
this.note = note;
34+
}
35+
36+
public InterpreterFailure(Throwable cause, IndicatedSource indicatedSource, String message, String note) {
3037
super(message, cause);
38+
this.indicatedSource = indicatedSource;
39+
this.note = note;
3140
}
3241

33-
protected InterpreterFailure(String message) {
34-
super(message);
42+
@Override
43+
public Option<String> getNote() {
44+
return Option.of(note);
3545
}
3646

37-
/**
38-
* Get indicated source
39-
*
40-
* @return the indicated source
41-
*/
42-
public abstract IndicatedSource getIndicatedSource();
47+
@Override
48+
public IndicatedSource getIndicatedSource() {
49+
return indicatedSource;
50+
}
4351

4452
}

panda-framework/src/main/java/org/panda_lang/language/interpreter/PandaInterpreterFailure.java

-50
This file was deleted.

panda-framework/src/main/java/org/panda_lang/language/interpreter/lexer/PandaLexerFailure.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import org.jetbrains.annotations.Nullable;
2020
import org.panda_lang.language.interpreter.source.Location;
2121
import org.panda_lang.language.interpreter.token.Snippetable;
22-
import org.panda_lang.language.interpreter.PandaInterpreterFailure;
22+
import org.panda_lang.language.interpreter.InterpreterFailure;
2323
import org.panda_lang.language.interpreter.source.PandaIndicatedSource;
2424
import org.panda_lang.language.interpreter.token.PandaToken;
2525
import org.panda_lang.language.interpreter.token.PandaTokenInfo;
2626
import org.panda_lang.language.resource.syntax.TokenTypes;
2727

28-
public final class PandaLexerFailure extends PandaInterpreterFailure {
28+
public final class PandaLexerFailure extends InterpreterFailure {
2929

3030
public PandaLexerFailure(Snippetable line, Snippetable indicated, String message, @Nullable String note) {
3131
super(new PandaIndicatedSource(line, indicated), message, note);

panda-framework/src/main/java/org/panda_lang/language/interpreter/logging/Channel.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public class Channel implements Comparable<Channel> {
2727

2828
private static final Collection<Channel> VALUES = new ArrayList<>();
2929

30-
public static Channel FATAL = add(VALUES, new Channel("fatal", 6.0));
31-
public static Channel ERROR = add(VALUES, new Channel("error", 5.0));
32-
public static Channel WARN = add(VALUES, new Channel("warn", 4.0));
33-
public static Channel INFO = add(VALUES, new Channel("info", 3.0));
34-
public static Channel DEBUG = add(VALUES, new Channel("debug", 2.0));
35-
public static Channel TRACE = add(VALUES, new Channel("trace", 1.0));
30+
public static final Channel FATAL = add(VALUES, new Channel("fatal", 6.0));
31+
public static final Channel ERROR = add(VALUES, new Channel("error", 5.0));
32+
public static final Channel WARN = add(VALUES, new Channel("warn", 4.0));
33+
public static final Channel INFO = add(VALUES, new Channel("info", 3.0));
34+
public static final Channel DEBUG = add(VALUES, new Channel("debug", 2.0));
35+
public static final Channel TRACE = add(VALUES, new Channel("trace", 1.0));
3636

3737
private final String channel;
3838
private final double priority;

panda-framework/src/main/java/org/panda_lang/language/interpreter/logging/SystemLogger.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.panda_lang.language.interpreter.logging;
1818

19+
import org.panda_lang.language.Failure;
1920
import org.panda_lang.utilities.commons.StackTraceUtils;
2021
import org.panda_lang.utilities.commons.console.Effect;
2122

@@ -47,6 +48,10 @@ public void error(String message) {
4748
public void exception(Throwable throwable) {
4849
StackTraceElement[] stackTrace = StackTraceUtils.startsWith(throwable.getStackTrace(), element -> element.toString().contains("org.junit"));
4950

51+
if (throwable instanceof Failure) {
52+
53+
}
54+
5055
error("");
5156
error("&b- - ~ ~< Exception >~ ~ - -&r");
5257
error("");
@@ -64,8 +69,6 @@ public void exception(Throwable throwable) {
6469
error("");
6570
error("Environment:");
6671
error("");
67-
68-
throwable.printStackTrace(System.err);
6972
}
7073

7174
}

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/PandaParserFailure.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.panda_lang.language.interpreter.parser;
1818

1919
import org.jetbrains.annotations.Nullable;
20-
import org.panda_lang.language.interpreter.PandaInterpreterFailure;
20+
import org.panda_lang.language.interpreter.InterpreterFailure;
2121
import org.panda_lang.language.interpreter.source.Location;
2222
import org.panda_lang.language.interpreter.source.PandaIndicatedSource;
2323
import org.panda_lang.language.interpreter.token.Snippetable;
2424

25-
public class PandaParserFailure extends PandaInterpreterFailure implements ParserFailure {
25+
public class PandaParserFailure extends InterpreterFailure implements ParserFailure {
2626

2727
private final Context context;
2828

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/pipeline/HandleResult.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
package org.panda_lang.language.interpreter.parser.pipeline;
1818

19-
import org.panda_lang.language.interpreter.InterpreterFailure;
19+
import org.panda_lang.language.Failure;
2020
import org.panda_lang.language.interpreter.parser.Parser;
21-
2221
import org.panda_lang.utilities.commons.function.Option;
2322

2423
/**
@@ -42,7 +41,7 @@ default boolean isFound() {
4241
*
4342
* @return the failure
4443
*/
45-
Option<InterpreterFailure> getFailure();
44+
Option<Failure> getFailure();
4645

4746
/**
4847
* Get matched parser

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/pipeline/PandaHandleResult.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@
1616

1717
package org.panda_lang.language.interpreter.parser.pipeline;
1818

19-
import org.panda_lang.language.interpreter.InterpreterFailure;
19+
import org.panda_lang.language.Failure;
2020
import org.panda_lang.language.interpreter.parser.Parser;
21-
2221
import org.panda_lang.utilities.commons.function.Option;
2322

2423
final class PandaHandleResult<T extends Parser> implements HandleResult<T> {
2524

2625
private final T parser;
27-
private final InterpreterFailure failure;
26+
private final Failure failure;
2827

2928
public PandaHandleResult(T parser) {
3029
this(parser, null);
3130
}
3231

33-
public PandaHandleResult(InterpreterFailure failure) {
32+
public PandaHandleResult(Failure failure) {
3433
this(null, failure);
3534
}
3635

37-
private PandaHandleResult(T parser, InterpreterFailure failure) {
36+
private PandaHandleResult(T parser, Failure failure) {
3837
this.parser = parser;
3938
this.failure = failure;
4039
}
4140

4241
@Override
43-
public Option<InterpreterFailure> getFailure() {
42+
public Option<Failure> getFailure() {
4443
return Option.of(failure);
4544
}
4645

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/pipeline/PandaPipeline.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package org.panda_lang.language.interpreter.parser.pipeline;
1818

19+
import org.panda_lang.language.Failure;
1920
import org.panda_lang.language.PandaFrameworkException;
20-
import org.panda_lang.language.interpreter.InterpreterFailure;
2121
import org.panda_lang.language.interpreter.parser.Context;
22+
import org.panda_lang.language.interpreter.parser.LocalChannel;
2223
import org.panda_lang.language.interpreter.parser.Parser;
2324
import org.panda_lang.language.interpreter.parser.ParserRepresentation;
24-
import org.panda_lang.language.interpreter.parser.LocalChannel;
2525
import org.panda_lang.language.interpreter.token.Snippet;
2626

2727
import java.util.ArrayList;
@@ -69,14 +69,14 @@ public HandleResult<P> handle(Context context, LocalChannel channel, Snippet sou
6969

7070
private HandleResult<P> handle(Context context, LocalChannel channel, Snippet source, Collection<? extends ParserRepresentation<P>> representations) {
7171
long currentTime = System.nanoTime();
72-
InterpreterFailure failure = null;
72+
Failure failure = null;
7373

7474
for (ParserRepresentation<P> representation : representations) {
7575
Handler handler = representation.getHandler();
7676
Object value = handler.handle(context, channel, source);
7777

78-
if (value instanceof InterpreterFailure) {
79-
failure = (InterpreterFailure) value;
78+
if (value instanceof Failure) {
79+
failure = (Failure) value;
8080
continue;
8181
}
8282

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/pipeline/PipelineComponent.java

-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ private PipelineComponent(String name, Class<P> type) {
3636
super(name, type, 0);
3737
}
3838

39-
@Override
40-
public String toString() {
41-
return super.toString();
42-
}
43-
4439
/**
4540
* Create component
4641
*

panda-framework/src/main/java/org/panda_lang/language/interpreter/parser/pipeline/PipelineParser.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import org.panda_lang.language.interpreter.parser.Components;
2020
import org.panda_lang.language.interpreter.parser.Context;
2121
import org.panda_lang.language.interpreter.parser.ContextParser;
22-
import org.panda_lang.language.interpreter.parser.Parser;
2322
import org.panda_lang.language.interpreter.parser.LocalChannel;
23+
import org.panda_lang.language.interpreter.parser.PandaParserFailure;
24+
import org.panda_lang.language.interpreter.parser.Parser;
2425
import org.panda_lang.language.interpreter.token.Snippet;
2526
import org.panda_lang.language.interpreter.token.SourceStream;
26-
import org.panda_lang.language.interpreter.parser.PandaParserFailure;
2727
import org.panda_lang.language.resource.syntax.separator.Separators;
2828

2929
public final class PipelineParser<T extends ContextParser<?>> implements Parser {
@@ -56,9 +56,9 @@ public boolean parse(Context context, SourceStream stream) {
5656
HandleResult<T> result = pipeline.handle(context, channel, source);
5757

5858
ContextParser<?> parser = result.getParser().orThrow(() -> {
59-
return result.getFailure().orElseGet(() -> {
60-
throw new PandaParserFailure(delegatedContext, source, "Unrecognized syntax");
61-
});
59+
return result.getFailure()
60+
.map(failure -> (RuntimeException) failure)
61+
.orElseGet(() -> new PandaParserFailure(delegatedContext, source, "Unrecognized syntax"));
6262
});
6363

6464
int sourceLength = stream.getUnreadLength();

0 commit comments

Comments
 (0)