-
Notifications
You must be signed in to change notification settings - Fork 360
/
PPGeneratedToken.java
105 lines (95 loc) · 3.07 KB
/
PPGeneratedToken.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* C++ Community Plugin (cxx plugin)
* Copyright (C) 2010-2023 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.preprocessor;
import com.sonar.cxx.sslr.api.Token;
import com.sonar.cxx.sslr.api.TokenType;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
final class PPGeneratedToken {
private PPGeneratedToken() {
}
/**
* Mark token "generated".
*/
static Token build(Token token) {
if (!token.isGeneratedCode()) {
return Token.builder(token)
.setGeneratedCode(true)
.build();
}
return token;
}
/**
* Copy token, set new type and value and mark it "generated".
*/
static Token build(Token token, TokenType type, String valueAndOriginalValue) {
return Token.builder()
.setLine(token.getLine())
.setColumn(token.getColumn())
.setURI(token.getURI())
.setValueAndOriginalValue(valueAndOriginalValue)
.setType(type)
.setGeneratedCode(true)
.build();
}
/**
* Copy token, set new position and mark it "generated".
*/
static Token build(Token token, URI uri, int line, int column) {
return Token.builder()
.setLine(line)
.setColumn(column)
.setURI(uri)
.setValueAndOriginalValue(token.getValue())
.setType(token.getType())
.setGeneratedCode(true)
.build();
}
/**
* Create new token and mark it "generated".
*/
@SuppressWarnings({"java:S1075", "java:S112"})
static Token build(TokenType type, String valueAndOriginalValue, int line, int column) {
try {
return Token.builder()
.setLine(line)
.setColumn(column)
.setValueAndOriginalValue(valueAndOriginalValue)
.setType(type)
.setURI(new URI("tests://unittest"))
.setGeneratedCode(true)
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
/**
* Mark all tokens in the list as generated.
*/
static List<Token> markAllAsGenerated(List<Token> tokens) {
var result = new ArrayList<Token>(tokens.size());
for (int i = 0; i < tokens.size(); i++) {
result.add(PPGeneratedToken.build(tokens.get(i)));
}
return result;
}
}