forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GhsMultiParser.java
86 lines (69 loc) · 2.86 KB
/
GhsMultiParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package edu.hm.hafner.analysis.parser;
import java.util.Optional;
import java.util.regex.Matcher;
import org.apache.commons.lang3.StringUtils;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.util.LookaheadStream;
/**
* A parser for the GHS Multi compiler warnings.
*
* @author Joseph Boulos
*/
public class GhsMultiParser extends LookaheadParser {
private static final long serialVersionUID = 8149238560432255036L;
/**
* Regex Pattern to match start of Warning / Error. Groups are used to identify FileName, StartLine, Type, Category,
* Start of message.
*/
private static final String GHS_MULTI_WARNING_PATTERN = "\"(?<file>.*)\"\\,\\s*line\\s*"
+ "(?<line>\\d+)(?:\\s+\\(col\\.\\s*(?<column>\\d+)\\))?:\\s*"
+ "(?<severity>warning|error)\\s*(?<category>[^:]+):\\s*(?m)(?<message>[^\\^]*)";
/** Regex Pattern to match the ending of the Warning / Error Message. */
private static final String MESSAGE_END_REGEX = "\\s*\\^";
/**
* Creates a new instance of {@link GhsMultiParser}.
*/
public GhsMultiParser() {
super(GHS_MULTI_WARNING_PATTERN);
}
@Override
protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
final IssueBuilder builder) {
String type = StringUtils.capitalize(matcher.group("severity"));
String messageStart = matcher.group("message");
String message = extractMessage(messageStart, lookahead);
builder.setFileName(matcher.group("file"))
.setLineStart(matcher.group("line"))
.setCategory(matcher.group("category"))
.setMessage(message)
.setSeverity(Severity.guessFromString(type));
if (StringUtils.isNotBlank(matcher.group("column"))) {
builder.setColumnStart(matcher.group("column"));
}
return builder.buildOptional();
}
/**
* Go through all following lines appending the message until a line with only the ^ Symbol is found.
*
* @param messageStart
* start of the message
* @param lookahead
* lines used for message creation
*
* @return concatenated message string
*/
private String extractMessage(final String messageStart, final LookaheadStream lookahead) {
StringBuilder messageBuilder = new StringBuilder(messageStart).append("\n");
while (!lookahead.hasNext(MESSAGE_END_REGEX) && lookahead.hasNext()) {
messageBuilder.append(lookahead.next()).append("\n");
}
return messageBuilder.toString();
}
@Override
protected boolean isLineInteresting(final String line) {
return line.contains("warning") || line.contains("error");
}
}