Generate keyword strings for DescriptorRenderer

This is done in order to kill dependency of DescriptorRendererImpl on JetTokens
This commit is contained in:
Alexander Udalov
2013-09-17 20:35:18 +04:00
parent df875b892f
commit 3da06ee110
5 changed files with 167 additions and 14 deletions
@@ -19,7 +19,6 @@ package org.jetbrains.jet.renderer;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
@@ -33,21 +32,10 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.*;
public class DescriptorRendererImpl implements DescriptorRenderer {
private static final Set<String> KEYWORDS = Sets.newHashSet();
static {
for (IElementType elementType : JetTokens.KEYWORDS.getTypes()) {
assert elementType instanceof JetKeywordToken;
assert !((JetKeywordToken) elementType).isSoft();
KEYWORDS.add(((JetKeywordToken) elementType).getValue());
}
}
private final boolean shortNames;
private final boolean withDefinedIn;
private final Set<DescriptorRenderer.Modifier> modifiers;
@@ -161,7 +149,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
@NotNull
private String renderName(@NotNull Name identifier) {
String asString = identifier.toString();
return escape(KEYWORDS.contains(asString) ? '`' + asString + '`' : asString);
return escape(KeywordStringsGenerated.KEYWORDS.contains(asString) ? '`' + asString + '`' : asString);
}
private void renderName(@NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) {
@@ -692,7 +680,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
}
private void renderNamespace(@NotNull NamespaceDescriptor namespace, @NotNull StringBuilder builder) {
builder.append(renderKeyword(JetTokens.PACKAGE_KEYWORD.getValue())).append(" ");
builder.append(renderKeyword("package")).append(" ");
renderName(namespace, builder);
}
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.renderer;
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
/** This class is generated by {@link "org.jetbrains.jet.generators.frontend.GenerateKeywordStrings"}. DO NOT MODIFY MANUALLY */
public class KeywordStringsGenerated {
private KeywordStringsGenerated() {}
public static final Set<String> KEYWORDS = new HashSet<String>(Arrays.asList(
"package",
"as",
"type",
"class",
"this",
"super",
"val",
"var",
"fun",
"for",
"null",
"true",
"false",
"is",
"in",
"throw",
"return",
"break",
"continue",
"object",
"if",
"try",
"else",
"while",
"do",
"when",
"trait",
"This",
"AS_SAFE",
"NOT_IN",
"NOT_IS"
));
}
+1
View File
@@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -0,0 +1,72 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.generators.frontend;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.di.GeneratorsFileUtil;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.io.IOException;
public class GenerateKeywordStrings {
public static final File DEST_FILE = new File("compiler/frontend/src/org/jetbrains/jet/renderer/KeywordStringsGenerated.java");
@NotNull
public static String generate() throws IOException {
StringBuilder sb = new StringBuilder();
Printer p = new Printer(sb);
p.println(FileUtil.loadFile(new File("injector-generator/copyright.txt")));
p.println("package org.jetbrains.jet.renderer;");
p.println();
p.println("import java.util.Arrays;");
p.println("import java.util.Set;");
p.println("import java.util.HashSet;");
p.println();
p.println("/** This class is generated by {@link \"org.jetbrains.jet.generators.frontend.GenerateKeywordStrings\"}. DO NOT MODIFY MANUALLY */");
p.println("public class KeywordStringsGenerated {");
p.pushIndent();
p.println("private KeywordStringsGenerated() {}");
p.println();
p.println("public static final Set<String> KEYWORDS = new HashSet<String>(Arrays.asList(");
p.pushIndent();
IElementType[] types = JetTokens.KEYWORDS.getTypes();
for (int i = 0, length = types.length; i < length; i++) {
assert types[i] instanceof JetKeywordToken : "Not a keyword in JetTokens.KEYWORDS: " + types[i];
JetKeywordToken keyword = (JetKeywordToken) types[i];
assert !keyword.isSoft() : "Soft keyword in JetTokens.KEYWORDS: " + keyword.getValue();
p.println("\"" + keyword.getValue() + "\""+ (i + 1 < length ? "," : ""));
}
p.popIndent();
p.println("));");
p.popIndent();
p.println("}");
return sb.toString();
}
public static void main(String[] args) throws IOException {
GeneratorsFileUtil.writeFileIfContentChanged(DEST_FILE, generate());
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.generators.frontend;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.UsefulTestCase;
import java.io.IOException;
public class GenerateKeywordStringsTest extends UsefulTestCase {
public void testGeneratedDataIsUpToDate() throws IOException {
String text = GenerateKeywordStrings.generate();
assertEquals("Contents differ. Regenerate " + GenerateKeywordStrings.class.getName(),
StringUtil.convertLineSeparators(text),
FileUtil.loadFile(GenerateKeywordStrings.DEST_FILE));
}
}