Skip to content

Commit 31ce8e3

Browse files
committed
implement Diagnostic collector
1 parent ab923e0 commit 31ce8e3

File tree

9 files changed

+586
-19
lines changed

9 files changed

+586
-19
lines changed

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,6 @@ public interface Diagnostic<S> {
9797
*/
9898
String getCode();
9999

100-
public enum Kind {
101-
/**
102-
* Problem which prevents the tool's normal completion.
103-
*/
104-
ERROR,
105-
/**
106-
* Problem which does not usually prevent the tool from
107-
* completing normally.
108-
*/
109-
WARNING,
110-
/**
111-
* Informative message from the tool.
112-
*/
113-
NOTE,
114-
/**
115-
* Diagnostic which does not fit within the other kinds.
116-
*/
117-
OTHER,
118-
}
119100

120101

121102
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2018 Mr Duy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duy.ccppcompiler.compiler.diagnostic;
18+
19+
/**
20+
* Created by Duy on 28-Apr-18.
21+
*/
22+
23+
public class DiagnosticFactory {
24+
public static Diagnostic error(String filePath, int line, int col, String message) {
25+
return create(Kind.ERROR, filePath, line, col, message);
26+
}
27+
28+
public static Diagnostic warn(String filePath, int line, int col, String message) {
29+
return create(Kind.WARNING, filePath, line, col, message);
30+
}
31+
32+
public static Diagnostic note(String filePath, int line, int col, String message) {
33+
return create(Kind.NOTE, filePath, line, col, message);
34+
}
35+
36+
public static Diagnostic create(Kind kind, String filePath, int line, int col, String message) {
37+
return new SimpleDiagnostic(kind, filePath, line, col, message);
38+
}
39+
40+
public static Kind createType(String type) {
41+
type = type.trim().toLowerCase();
42+
if (type.startsWith("error")) {
43+
return Kind.ERROR;
44+
} else if (type.startsWith("warn")) {
45+
return Kind.WARNING;
46+
} else if (type.startsWith("note")) {
47+
return Kind.NOTE;
48+
}
49+
return Kind.OTHER;
50+
}
51+
52+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2018 Mr Duy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duy.ccppcompiler.compiler.diagnostic;
18+
19+
import android.net.Uri;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import java.io.Reader;
25+
import java.io.Writer;
26+
27+
/**
28+
* File abstraction for tools. In this context, <em>file</em> means
29+
* an abstraction of regular files and other sources of data. For
30+
* example, a file object can be used to represent regular files,
31+
* memory cache, or data in databases.
32+
* <p>
33+
* <p>All methods in this interface might throw a SecurityException if
34+
* a security exception occurs.
35+
* <p>
36+
* <p>Unless explicitly allowed, all methods in this interface might
37+
* throw a NullPointerException if given a {@code null} argument.
38+
*/
39+
public interface FileObject {
40+
41+
/**
42+
* Returns a URI identifying this file object.
43+
*
44+
* @return a URI
45+
*/
46+
Uri toUri();
47+
48+
/**
49+
* Gets a user-friendly name for this file object. The exact
50+
* value returned is not specified but implementations should take
51+
* care to preserve names as given by the user. For example, if
52+
* the user writes the filename {@code "BobsApp\Test.java"} on
53+
* the command line, this method should return {@code
54+
* "BobsApp\Test.java"} whereas the {@linkplain #toUri toUri}
55+
* method might return {@code
56+
* file:///C:/Documents%20and%20Settings/UncleBob/BobsApp/Test.java}.
57+
*
58+
* @return a user-friendly name
59+
*/
60+
String getName();
61+
62+
/**
63+
* Gets an InputStream for this file object.
64+
*
65+
* @return an InputStream
66+
* @throws IllegalStateException if this file object was
67+
* opened for writing and does not support reading
68+
* @throws UnsupportedOperationException if this kind of file
69+
* object does not support byte access
70+
* @throws IOException if an I/O error occurred
71+
*/
72+
InputStream openInputStream() throws IOException;
73+
74+
/**
75+
* Gets an OutputStream for this file object.
76+
*
77+
* @return an OutputStream
78+
* @throws IllegalStateException if this file object was
79+
* opened for reading and does not support writing
80+
* @throws UnsupportedOperationException if this kind of
81+
* file object does not support byte access
82+
* @throws IOException if an I/O error occurred
83+
*/
84+
OutputStream openOutputStream() throws IOException;
85+
86+
/**
87+
* Gets a reader for this object. The returned reader will
88+
* replace bytes that cannot be decoded with the default
89+
* translation character. In addition, the reader may report a
90+
* diagnostic unless {@code ignoreEncodingErrors} is true.
91+
*
92+
* @param ignoreEncodingErrors ignore encoding errors if true
93+
* @return a Reader
94+
* @throws IllegalStateException if this file object was
95+
* opened for writing and does not support reading
96+
* @throws UnsupportedOperationException if this kind of
97+
* file object does not support character access
98+
* @throws IOException if an I/O error occurred
99+
*/
100+
Reader openReader(boolean ignoreEncodingErrors) throws IOException;
101+
102+
/**
103+
* Gets the character content of this file object, if available.
104+
* Any byte that cannot be decoded will be replaced by the default
105+
* translation character. In addition, a diagnostic may be
106+
* reported unless {@code ignoreEncodingErrors} is true.
107+
*
108+
* @param ignoreEncodingErrors ignore encoding errors if true
109+
* @return a CharSequence if available; {@code null} otherwise
110+
* @throws IllegalStateException if this file object was
111+
* opened for writing and does not support reading
112+
* @throws UnsupportedOperationException if this kind of
113+
* file object does not support character access
114+
* @throws IOException if an I/O error occurred
115+
*/
116+
CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException;
117+
118+
/**
119+
* Gets a Writer for this file object.
120+
*
121+
* @return a Writer
122+
* @throws IllegalStateException if this file object was
123+
* opened for reading and does not support writing
124+
* @throws UnsupportedOperationException if this kind of
125+
* file object does not support character access
126+
* @throws IOException if an I/O error occurred
127+
*/
128+
Writer openWriter() throws IOException;
129+
130+
/**
131+
* Gets the time this file object was last modified. The time is
132+
* measured in milliseconds since the epoch (00:00:00 GMT, January
133+
* 1, 1970).
134+
*
135+
* @return the time this file object was last modified; or 0 if
136+
* the file object does not exist, if an I/O error occurred, or if
137+
* the operation is not supported
138+
*/
139+
long getLastModified();
140+
141+
/**
142+
* Deletes this file object. In case of errors, returns false.
143+
*
144+
* @return true if and only if this file object is successfully
145+
* deleted; false otherwise
146+
*/
147+
boolean delete();
148+
149+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 Mr Duy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duy.ccppcompiler.compiler.diagnostic;
18+
19+
public enum Kind {
20+
/**
21+
* Problem which prevents the tool's normal completion.
22+
*/
23+
ERROR("error"),
24+
/**
25+
* Problem which does not usually prevent the tool from
26+
* completing normally.
27+
*/
28+
WARNING("warning"),
29+
/**
30+
* Informative message from the tool.
31+
*/
32+
NOTE("note"),
33+
/**
34+
* Diagnostic which does not fit within the other kinds.
35+
*/
36+
OTHER("other"),;
37+
38+
private String name;
39+
40+
Kind(String name) {
41+
this.name = name;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2018 Mr Duy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duy.ccppcompiler.compiler.diagnostic;
18+
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
/**
23+
* Created by Duy on 28-Apr-18.
24+
*/
25+
26+
public class OutputParser<S> {
27+
public static final Pattern PATTERN = Pattern.compile(
28+
"(.*):" + /*File name*/
29+
"([0-9]+):" + /*Line*/
30+
"([0-9]+):" + /*Col*/
31+
"(\\s+.*:\\s+)" + /*Type*/
32+
"(.*)" /*Message*/
33+
, Pattern.CASE_INSENSITIVE);
34+
35+
private DiagnosticsCollector<? extends S> diagnosticsCollector;
36+
37+
public OutputParser(DiagnosticsCollector<? extends S> diagnosticsCollector) {
38+
this.diagnosticsCollector = diagnosticsCollector;
39+
}
40+
41+
public void parse(String inputData) {
42+
Matcher matcher = PATTERN.matcher(inputData);
43+
while (matcher.find()) {
44+
try {
45+
String file = matcher.group(1);
46+
int line = Integer.parseInt(matcher.group(2));
47+
int col = Integer.parseInt(matcher.group(3));
48+
Kind type = DiagnosticFactory.createType(matcher.group(4));
49+
String message = matcher.group(5);
50+
51+
Diagnostic diagnostic = DiagnosticFactory.create(type, file, line, col, message);
52+
diagnosticsCollector.report(diagnostic);
53+
} catch (Exception e) {
54+
//should not happened
55+
e.printStackTrace();
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)