Skip to content

Commit ab923e0

Browse files
committed
implement Diagnostic collector
1 parent dcfbcaf commit ab923e0

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 interface Diagnostic<S> {
24+
/**
25+
* Used to signal that no position is available.
26+
*/
27+
public final static long NOPOS = -1;
28+
29+
Kind getKind();
30+
31+
/**
32+
* Gets the source object associated with this diagnostic.
33+
*
34+
* @return the source object associated with this diagnostic.
35+
* {@code null} if no source object is associated with the
36+
* diagnostic.
37+
*/
38+
S getSource();
39+
40+
/**
41+
* Gets a character offset from the beginning of the source object
42+
* associated with this diagnostic that indicates the location of
43+
* the problem. In addition, the following must be true:
44+
* <p>
45+
* <p>{@code getStartPostion() <= getPosition()}
46+
* <p>{@code getPosition() <= getEndPosition()}
47+
*
48+
* @return character offset from beginning of source; {@link
49+
* #NOPOS} if {@link #getSource()} would return {@code null} or if
50+
* no location is suitable
51+
*/
52+
long getPosition();
53+
54+
/**
55+
* Gets the character offset from the beginning of the file
56+
* associated with this diagnostic that indicates the start of the
57+
* problem.
58+
*
59+
* @return offset from beginning of file; {@link #NOPOS} if and
60+
* only if {@link #getPosition()} returns {@link #NOPOS}
61+
*/
62+
long getStartPosition();
63+
64+
/**
65+
* Gets the character offset from the beginning of the file
66+
* associated with this diagnostic that indicates the end of the
67+
* problem.
68+
*
69+
* @return offset from beginning of file; {@link #NOPOS} if and
70+
* only if {@link #getPosition()} returns {@link #NOPOS}
71+
*/
72+
long getEndPosition();
73+
74+
/**
75+
* Gets the line number of the character offset returned by
76+
* {@linkplain #getPosition()}.
77+
*
78+
* @return a line number or {@link #NOPOS} if and only if {@link
79+
* #getPosition()} returns {@link #NOPOS}
80+
*/
81+
long getLineNumber();
82+
83+
/**
84+
* Gets the column number of the character offset returned by
85+
* {@linkplain #getPosition()}.
86+
*
87+
* @return a column number or {@link #NOPOS} if and only if {@link
88+
* #getPosition()} returns {@link #NOPOS}
89+
*/
90+
long getColumnNumber();
91+
92+
/**
93+
* Gets a diagnostic code indicating the type of diagnostic. The
94+
* code is implementation-dependent and might be {@code null}.
95+
*
96+
* @return a diagnostic code
97+
*/
98+
String getCode();
99+
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+
}
119+
120+
121+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 interface DiagnosticListener<S> {
20+
/**
21+
* Call when a problem is found.
22+
*
23+
* @param diagnostic a diagnostic representing the problem that was found
24+
*/
25+
void report(Diagnostic<? extends S> diagnostic);
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.ArrayList;
20+
21+
/**
22+
* Provides an easy way to collect diagnostics in a list.
23+
* <p>
24+
* Created by Duy on 28-Apr-18.
25+
*/
26+
27+
public class DiagnosticsCollector<S> implements DiagnosticListener<S> {
28+
private final ArrayList<Diagnostic<? extends S>> diagnostics = new ArrayList<>();
29+
30+
@Override
31+
public void report(Diagnostic<? extends S> diagnostic) {
32+
diagnostics.add(diagnostic);
33+
}
34+
35+
public ArrayList<Diagnostic<? extends S>> getDiagnostics() {
36+
return diagnostics;
37+
}
38+
}

0 commit comments

Comments
 (0)