Skip to content

Commit d8f6a0a

Browse files
committed
suggestion
1 parent 7b320ba commit d8f6a0a

File tree

11 files changed

+289
-259
lines changed

11 files changed

+289
-259
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/Diagnostic.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import android.content.Context;
2020
import android.os.Parcelable;
21+
import android.support.annotation.Nullable;
22+
23+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
2124

2225
/**
2326
* Created by Duy on 28-Apr-18.
@@ -38,6 +41,7 @@ public interface Diagnostic<S> extends Parcelable {
3841
* {@code null} if no source object is associated with the
3942
* diagnostic.
4043
*/
44+
@Nullable
4145
S getSource();
4246

4347
/**
@@ -98,8 +102,11 @@ public interface Diagnostic<S> extends Parcelable {
98102
*
99103
* @return a diagnostic code
100104
*/
105+
@Nullable
101106
String getCode();
102107

103108
String getMessage(Context context);
104109

110+
@Nullable
111+
ISuggestion getSuggestion();
105112
}

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/DiagnosticFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.duy.ccppcompiler.compiler.diagnostic;
1818

19+
import com.duy.ccppcompiler.compiler.diagnostic.model.SimpleDiagnostic;
20+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.DiagnosticSuggestion;
21+
1922
/**
2023
* Created by Duy on 28-Apr-18.
2124
*/
@@ -49,4 +52,7 @@ public static Kind createType(String type) {
4952
return Kind.OTHER;
5053
}
5154

55+
public static Diagnostic createFixIt(String filePath, int lineStart, int colStart, int lineEnd, int colEnd, String suggestion) {
56+
return new DiagnosticSuggestion(filePath, lineStart, colStart, lineEnd, colEnd, suggestion);
57+
}
5258
}

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/FileObject.java

Lines changed: 0 additions & 149 deletions
This file was deleted.

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/OutputParser.java

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package com.duy.ccppcompiler.compiler.diagnostic;
1818

19+
import android.support.annotation.NonNull;
20+
import android.support.annotation.Nullable;
21+
22+
import java.io.LineNumberReader;
23+
import java.io.StringReader;
1924
import java.util.regex.Matcher;
2025
import java.util.regex.Pattern;
2126

@@ -28,13 +33,20 @@
2833
*/
2934

3035
public class OutputParser {
31-
public static final Pattern PATTERN = Pattern.compile(
32-
"(.*):" + /*File name*/
36+
public static final Pattern DIAGNOSTICS_PATTERN = Pattern.compile(
37+
"(.*):" + /*File path*/
3338
"([0-9]+):" + /*Line*/
3439
"([0-9]+):" + /*Col*/
3540
"(\\s+.*:\\s+)" + /*Type*/
36-
"(.*)" /*Message*/
37-
, Pattern.CASE_INSENSITIVE);
41+
"(.*)" /*Message*/);
42+
43+
//fix-it:"/storage/emulated/0/examples/simple/bit_print.c":{6:7-6:11}:"printf"
44+
public static final Pattern FIX_IT_PATTERN = Pattern.compile(
45+
"(fix-it):" +/*prefix*/
46+
"(.*):" +/*File path*/
47+
"\\{([0-9]+):([0-9]+)-([0-9]+):([0-9]+)}" + /*Index (line:col)-(line:col)*/
48+
"\"(.*)\"" /*Message*/
49+
);
3850

3951
private DiagnosticsCollector diagnosticsCollector;
4052

@@ -44,21 +56,56 @@ public OutputParser(DiagnosticsCollector diagnosticsCollector) {
4456

4557
@SuppressWarnings("unchecked")
4658
public void parse(String inputData) {
47-
Matcher matcher = PATTERN.matcher(inputData);
48-
while (matcher.find()) {
49-
try {
50-
String file = matcher.group(1);
51-
int line = Integer.parseInt(matcher.group(2));
52-
int col = Integer.parseInt(matcher.group(3));
59+
try {
60+
StringReader stringReader = new StringReader(inputData);
61+
LineNumberReader lineNumberReader = new LineNumberReader(stringReader);
62+
63+
String line;
64+
while ((line = lineNumberReader.readLine()) != null) {
65+
processLine(line, lineNumberReader.readLine());
66+
}
67+
68+
} catch (Exception e) {
69+
//should not happened
70+
e.printStackTrace();
71+
}
72+
}
73+
74+
@SuppressWarnings("unchecked")
75+
private void processLine(@NonNull String line, @Nullable String nextLine) {
76+
try {
77+
Matcher matcher = DIAGNOSTICS_PATTERN.matcher(line);
78+
if (matcher.find()) {
79+
String filePath = matcher.group(1);
80+
int lineNumber = Integer.parseInt(matcher.group(2));
81+
int colNumber = Integer.parseInt(matcher.group(3));
5382
Kind type = DiagnosticFactory.createType(matcher.group(4));
5483
String message = matcher.group(5);
5584

56-
Diagnostic diagnostic = DiagnosticFactory.create(type, file, line, col, message);
85+
Diagnostic diagnostic = DiagnosticFactory.create(type, filePath, lineNumber, colNumber, message);
86+
diagnosticsCollector.report(diagnostic);
87+
return;
88+
}
89+
if (nextLine == null) {
90+
return;
91+
}
92+
93+
matcher = FIX_IT_PATTERN.matcher(line);
94+
if (matcher.find()) {
95+
String filePath = matcher.group(2);
96+
int lineStart = Integer.parseInt(matcher.group(3));
97+
int colStart = Integer.parseInt(matcher.group(4));
98+
int lineEnd = Integer.parseInt(matcher.group(5));
99+
int colEnd = Integer.parseInt(matcher.group(6));
100+
String suggestion = matcher.group(7);
101+
Diagnostic diagnostic = DiagnosticFactory.createFixIt(filePath, lineStart, colStart, lineEnd, colEnd, suggestion);
57102
diagnosticsCollector.report(diagnostic);
58-
} catch (Exception e) {
59-
//should not happened
60-
e.printStackTrace();
103+
} else {
104+
processLine(nextLine, null);
61105
}
106+
} catch (Exception e) {
107+
e.printStackTrace();
108+
//should not happend
62109
}
63110
}
64111
}

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/SimpleFileObject.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)