diff --git a/compiler/frontend/src/org/jetbrains/jet/renderer/DescriptorRendererImpl.java b/compiler/frontend/src/org/jetbrains/jet/renderer/DescriptorRendererImpl.java index 4c87f28f09c..5dfdbd71d42 100644 --- a/compiler/frontend/src/org/jetbrains/jet/renderer/DescriptorRendererImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/renderer/DescriptorRendererImpl.java @@ -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 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 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); } diff --git a/compiler/frontend/src/org/jetbrains/jet/renderer/KeywordStringsGenerated.java b/compiler/frontend/src/org/jetbrains/jet/renderer/KeywordStringsGenerated.java new file mode 100644 index 00000000000..8834fe5ab04 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/renderer/KeywordStringsGenerated.java @@ -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 KEYWORDS = new HashSet(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" + )); +} diff --git a/generators/generators.iml b/generators/generators.iml index 5dff90c1d0a..35a63f68884 100644 --- a/generators/generators.iml +++ b/generators/generators.iml @@ -4,6 +4,7 @@ + diff --git a/generators/src/org/jetbrains/jet/generators/frontend/GenerateKeywordStrings.java b/generators/src/org/jetbrains/jet/generators/frontend/GenerateKeywordStrings.java new file mode 100644 index 00000000000..3403650a10a --- /dev/null +++ b/generators/src/org/jetbrains/jet/generators/frontend/GenerateKeywordStrings.java @@ -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 KEYWORDS = new HashSet(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()); + } +} diff --git a/generators/tests/org/jetbrains/jet/generators/frontend/GenerateKeywordStringsTest.java b/generators/tests/org/jetbrains/jet/generators/frontend/GenerateKeywordStringsTest.java new file mode 100644 index 00000000000..ca2664fd4e0 --- /dev/null +++ b/generators/tests/org/jetbrains/jet/generators/frontend/GenerateKeywordStringsTest.java @@ -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)); + } +}