-
Notifications
You must be signed in to change notification settings - Fork 316
/
LiteralsCompiler.java
269 lines (245 loc) · 9.91 KB
/
LiteralsCompiler.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*******************************************************************************
* Copyright (c) 2011, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xbase.compiler;
import static org.eclipse.xtext.util.JavaVersion.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.XBooleanLiteral;
import org.eclipse.xtext.xbase.XClosure;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XNullLiteral;
import org.eclipse.xtext.xbase.XNumberLiteral;
import org.eclipse.xtext.xbase.XStringLiteral;
import org.eclipse.xtext.xbase.XTypeLiteral;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.typesystem.computation.NumberLiterals;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import com.google.common.base.Joiner;
import com.google.inject.Inject;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class LiteralsCompiler extends TypeConvertingCompiler {
@Inject
private NumberLiterals numberLiterals;
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
if (obj instanceof XStringLiteral) {
_toJavaExpression((XStringLiteral) obj, appendable);
} else if (obj instanceof XNumberLiteral) {
_toJavaExpression((XNumberLiteral) obj, appendable);
} else if (obj instanceof XNullLiteral) {
_toJavaExpression((XNullLiteral) obj, appendable);
} else if (obj instanceof XBooleanLiteral) {
_toJavaExpression((XBooleanLiteral) obj, appendable);
} else if (obj instanceof XTypeLiteral) {
_toJavaExpression((XTypeLiteral) obj, appendable);
} else {
super.internalToConvertedExpression(obj, appendable);
}
}
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
if (obj instanceof XStringLiteral) {
_toJavaStatement((XStringLiteral) obj, appendable, isReferenced);
} else if (obj instanceof XNumberLiteral) {
_toJavaStatement((XNumberLiteral) obj, appendable, isReferenced);
} else if (obj instanceof XNullLiteral) {
_toJavaStatement((XNullLiteral) obj, appendable, isReferenced);
} else if (obj instanceof XBooleanLiteral) {
_toJavaStatement((XBooleanLiteral) obj, appendable, isReferenced);
} else if (obj instanceof XTypeLiteral) {
_toJavaStatement((XTypeLiteral) obj, appendable, isReferenced);
} else {
super.doInternalToJavaStatement(obj, appendable, isReferenced);
}
}
public void _toJavaExpression(XStringLiteral expr, ITreeAppendable b) {
toJavaExpression(expr, b, true);
}
/**
* @since 2.4
*/
protected void toJavaExpression(XStringLiteral literal, ITreeAppendable appendable, boolean useUnicodeEscapes) {
LightweightTypeReference type = getLightweightType(literal);
if (type.isType(Character.TYPE)) {
String javaString = Strings.convertToJavaString(literal.getValue(), useUnicodeEscapes);
appendable.append("'").append(javaString).append("'");
} else if (type.isType(Character.class)) {
String javaString = Strings.convertToJavaString(literal.getValue(), useUnicodeEscapes);
appendable.append("Character.valueOf('").append(javaString).append("')");
} else {
// Avoid Windows EOL characters from the original parsed text:
// this would result in different generated Java files in Windows
// see https://github.com/eclipse/xtext/issues/2293
// This is aligned with Java text blocks' "Normalization of Line Terminators"
String normalizationOfLineTerminators = Strings.toUnixLineSeparator(literal.getValue());
String javaString = Strings.convertToJavaString(normalizationOfLineTerminators, useUnicodeEscapes);
appendable.append("\"").append(javaString).append("\"");
}
}
public void _toJavaStatement(final XStringLiteral expr, ITreeAppendable b, boolean isReferenced) {
toJavaStatement(expr, b, isReferenced, true);
}
/**
* @since 2.4
*/
protected void toJavaStatement(final XStringLiteral expr, ITreeAppendable b, boolean isReferenced, final boolean useUnicodeEscapes) {
generateComment(new Later() {
@Override
public void exec(ITreeAppendable appendable) {
// we have to escape closing comments in string literals
String escapedClosingComments = expr.getValue().replace("*/", "* /");
String javaString = Strings.convertToJavaString(escapedClosingComments, useUnicodeEscapes);
appendable.append("\"").append(javaString).append("\"");
}
}, b, isReferenced);
}
protected void generateComment(final XExpression expr, ITreeAppendable b, boolean isReferenced) {
generateComment(new Later() {
@Override
public void exec(ITreeAppendable appendable) {
internalToJavaExpression(expr, appendable);
}
}, b, isReferenced);
}
protected void generateComment(Later expr, ITreeAppendable b, boolean isReferenced) {
if (!isReferenced) {
b.newLine().append("/* ");
try {
expr.exec(b);
} finally {
b.append(" */");
}
}
}
public void _toJavaExpression(XNumberLiteral expr, ITreeAppendable b) {
LightweightTypeReference type = getLightweightType(expr);
if(type.isType(BigInteger.class)) {
BigInteger value = numberLiterals.toBigInteger(expr);
if (BigInteger.ZERO.equals(value)) {
b.append(type.getType()).append(".ZERO");
} else if (BigInteger.ONE.equals(value)) {
b.append(type.getType()).append(".ONE");
} else if (BigInteger.TEN.equals(value)) {
b.append(type.getType()).append(".TEN");
} else {
long longValue = value.longValue();
int base = numberLiterals.getBase(expr);
if (base == 10 && BigInteger.valueOf(longValue).equals(value)) {
b.append(type.getType()).append(".valueOf(").append(Long.toString(longValue)).append("L)");
} else {
String digits = numberLiterals.getDigits(expr);
String exponent = numberLiterals.getExponent(expr, digits);
if (exponent != null) {
if (exponent.length() == 1) {
exponent = null;
digits = value.toString(base);
} else {
int e = digits.indexOf('e');
if (e == -1) {
e = digits.indexOf('E');
}
digits = digits.substring(0, e);
}
}
final boolean fracturedDigits = digits.contains(".");
if (fracturedDigits) {
b.append("new ").append(BigDecimal.class).append("(\"")
.append(digits).append("\"");
if (base != 10) {
b.append(", ").append(Integer.toString(base));
}
} else {
b.append("new ").append(type.getType()).append("(\"")
.append(digits).append("\"");
if (base != 10) {
b.append(", ").append(Integer.toString(base));
}
}
b.append(")");
if (exponent != null) {
int exponentAsInt = Integer.parseInt(exponent, base);
String exponentAsString = null;
if (base == 16) {
exponentAsString = "0x" + Integer.toString(exponentAsInt, base);
} else {
exponentAsString = Integer.toString(exponentAsInt);
}
if (fracturedDigits) {
b.append(".multiply(").append(BigDecimal.class).append(".TEN.pow(").append(exponentAsString).append(")).toBigInteger()");
} else {
b.append(".multiply(").append(type.getType()).append(".TEN.pow(").append(exponentAsString).append("))");
}
}
}
}
} else if(type.isType(BigDecimal.class)) {
BigDecimal value = numberLiterals.toBigDecimal(expr);
if (BigDecimal.ZERO.equals(value)) {
b.append(type.getType()).append(".ZERO");
} else if (BigDecimal.ONE.equals(value)) {
b.append(type.getType()).append(".ONE");
} else if (BigDecimal.TEN.equals(value)) {
b.append(type.getType()).append(".TEN");
} else {
long longValue = value.longValue();
if (numberLiterals.getBase(expr) == 10 && BigDecimal.valueOf(longValue).equals(value)) {
b.append(type.getType()).append(".valueOf(").append(Long.toString(longValue)).append("L)");
} else {
b.append("new ").append(type.getType()).append("(\"")
.append(numberLiterals.getDigits(expr)).append("\")");
}
}
} else {
GeneratorConfig config = b.getGeneratorConfig();
if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA7)) {
b.append(numberLiterals.toJavaLiteral(expr, false));
} else {
b.append(numberLiterals.toJavaLiteral(expr, true));
}
}
}
public void _toJavaStatement(XNumberLiteral expr, ITreeAppendable b, boolean isReferenced) {
generateComment(expr, b, isReferenced);
}
/**
* @param expr the expression. Used by the dispatch strategy.
*/
public void _toJavaExpression(XNullLiteral expr, ITreeAppendable b) {
b.append("null");
}
public void _toJavaStatement(XNullLiteral expr, ITreeAppendable b, boolean isReferenced) {
generateComment(expr, b, isReferenced);
}
public void _toJavaExpression(XBooleanLiteral expr, ITreeAppendable b) {
b.append(Boolean.toString(expr.isIsTrue()));
}
public void _toJavaStatement(XBooleanLiteral expr, ITreeAppendable b, boolean isReferenced) {
generateComment(expr, b, isReferenced);
}
public void _toJavaExpression(XTypeLiteral expr, ITreeAppendable b) {
b.append(expr.getType()).append(Joiner.on("").join(expr.getArrayDimensions())).append(".class");
}
public void _toJavaStatement(XTypeLiteral expr, ITreeAppendable b, boolean isReferenced) {
generateComment(expr, b, isReferenced);
}
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
if (expr instanceof XBooleanLiteral
|| expr instanceof XStringLiteral
|| expr instanceof XNumberLiteral
|| expr instanceof XTypeLiteral
|| expr instanceof XClosure
|| expr instanceof XNullLiteral)
return false;
return super.isVariableDeclarationRequired(expr,b, recursive);
}
}