Skip to content

Commit

Permalink
Improves Compiler Error Matching (#15)
Browse files Browse the repository at this point in the history
* fix: improves compiler error matching

- In some cases the range (line, col) for the compiler errors were not
matching the range (line, col) of the source code.

- adds the `make` flag for the compiler so that it resolves local
imported modules.

* chore: upgrades workflow to frege gradle plugin v.2.0.1-alpha

* chore: updates jacoco code coverage badges

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
tricktron and github-actions[bot] committed Mar 10, 2022
1 parent 8e2a0fd commit 438e4a9
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 338 deletions.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v2
with:
repository: tricktron/frege-gradle-plugin
ref: v1.0.0-alpha
ref: v2.0.1-alpha
path: frege-gradle-plugin
- name: Install Frege Gradle Plugin to MavenLocal
run: |
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'application'
id 'jacoco'
id 'ch.fhnw.thga.frege' version '1.0.0-alpha'
id 'ch.fhnw.thga.frege' version '2.0.1-alpha'
}

repositories {
Expand All @@ -10,7 +10,7 @@ repositories {

dependencies {
def junit5Group = 'org.junit.jupiter'
def junit5Version = '5.7.2'
def junit5Version = '5.8.2'
def mockitoGroup = 'org.mockito'
def mockitoVersion = '3.+'
testImplementation group: junit5Group, name: 'junit-jupiter-api', version: junit5Version
Expand All @@ -21,7 +21,7 @@ dependencies {
implementation files(compileFrege.fregeCompilerJar)
testImplementation group: mockitoGroup, name: 'mockito-core', version: mockitoVersion
testImplementation group: mockitoGroup, name: 'mockito-junit-jupiter', version: mockitoVersion
implementation(enforcedPlatform("org.junit:junit-bom:${junit5Version}"))
testImplementation(enforcedPlatform("org.junit:junit-bom:${junit5Version}"))

}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 2.1.8-alpha
version = 2.1.9-alpha
2 changes: 1 addition & 1 deletion src/main/frege/ch/fhnw/thga/TypeSignature.fr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data ArrayList a = native java.util.ArrayList where
initialReplEnv :: IO ReplEnv
initialReplEnv = do
replEnv <- ReplEnv.initialState
return replEnv.{config <- InterpreterConfig.{compilerFlags = Flags.fromList [WARNINGS, HINTS, INLINE, IDEMODE, IDETOKENS]}}
return replEnv.{config <- InterpreterConfig.{compilerFlags = Flags.fromList [WARNINGS, HINTS, INLINE, IDEMODE, IDETOKENS, MAKE]}}

evalFregeFile :: String -> ReplEnv -> IO (ReplResult, ReplEnv)
evalFregeFile file env = do
Expand Down
57 changes: 51 additions & 6 deletions src/main/java/ch/fhnw/thga/FregeTextDocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public class FregeTextDocumentService implements TextDocumentService {
public static final String FREGE_LANGUAGE_ID = "frege";
private final FregeLanguageServer simpleLanguageServer;
private String currentOpenFileContents;
private List<String> currentOpenFileLines;
private Lazy<TReplEnv> replEnv;

public FregeTextDocumentService(FregeLanguageServer server) {
simpleLanguageServer = server;
currentOpenFileContents = "";
currentOpenFileLines = new ArrayList<>();

replEnv = performUnsafe(TypeSignature.initialReplEnv.call());
}

Expand Down Expand Up @@ -102,20 +105,59 @@ public CompletableFuture<Hover> hover(HoverParams params) {
});
}

private static Diagnostic mapMessageToDiagnostic(TMessage message) {
private String extractTypeErrorExpressionFromCompilerMessage(String compilerMessage)
{
return compilerMessage.lines().skip(1).findFirst().orElse("");
}

private int findTypeErrorLineIndex(String faultyExpression)
{
return (int) this.currentOpenFileLines
.stream()
.takeWhile(line -> !line.contains(faultyExpression))
.count();
}

private Range findTypeErrorRange(String typeErrorMessage)
{
String typeErrorExpression = extractTypeErrorExpressionFromCompilerMessage(typeErrorMessage);
Pattern pattern = Pattern.compile(typeErrorExpression);
int line = findTypeErrorLineIndex(typeErrorExpression);
Matcher matcher = pattern
.matcher(this.currentOpenFileLines.get(line));
return matcher.find() ?
new Range(
new Position(line, matcher.start()),
new Position(line, matcher.end()))
: null;
}

private Diagnostic mapMessageToDiagnostic(TMessage message)
{
String compilerMessage = message.mem$text.call();
/*if (compilerMessage.contains("type error in expression"))
{
errorRange = findTypeErrorRange(compilerMessage);
}*/
int line = message.mem$pos.call().mem$first.mem$line;
int col = message.mem$pos.call().mem$first.mem$col;
short messageType = message.mem$msgType.call();
String compilerMessage = message.mem$text.call();
return new Diagnostic(new Range(new Position(line, col), new Position(line, col + 1)), compilerMessage,
DiagnosticSeverity.forValue(messageType), "fregeCompiler");
Range errorRange = new Range(
new Position(line - 1, 0),
new Position(line - 1, col)
);
return new Diagnostic(
errorRange,
compilerMessage,
DiagnosticSeverity.forValue(message.mem$msgType.call()),
"fregeCompiler"
);
}

private List<Diagnostic> getCompilerDiagnostics(TReplResult result) {
if (result.asReplInfo() != null) {
ArrayList<TMessage> messages = performUnsafe(TArrayList.fromFregeList(result.asReplInfo().mem1.call()))
.call();
return messages.stream().map(FregeTextDocumentService::mapMessageToDiagnostic).collect(Collectors.toList());
return messages.stream().map(message -> mapMessageToDiagnostic(message)).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
Expand All @@ -129,6 +171,7 @@ private void publishCompilerDiagnostics(TReplResult result, String documentUri)
@Override
public void didOpen(DidOpenTextDocumentParams params) {
currentOpenFileContents = params.getTextDocument().getText();
currentOpenFileLines = currentOpenFileContents.lines().collect(Collectors.toList());
TTuple2<TReplResult, TReplEnv> resEnvTuple = performUnsafe(
TypeSignature.evalFregeFile(Thunk.lazy(currentOpenFileContents), replEnv)).call();
replEnv = resEnvTuple.mem2;
Expand All @@ -139,11 +182,13 @@ public void didOpen(DidOpenTextDocumentParams params) {
public void didChange(DidChangeTextDocumentParams params) {
List<TextDocumentContentChangeEvent> changes = params.getContentChanges();
currentOpenFileContents = changes.get(changes.size() - 1).getText();
currentOpenFileLines = currentOpenFileContents.lines().collect(Collectors.toList());
}

@Override
public void didClose(DidCloseTextDocumentParams params) {
currentOpenFileContents = "";
currentOpenFileLines.clear();
}

@Override
Expand Down
Loading

0 comments on commit 438e4a9

Please sign in to comment.