001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.lang.Language;
021 import com.intellij.lang.PsiBuilder;
022 import com.intellij.lang.PsiBuilderFactory;
023 import com.intellij.lexer.Lexer;
024 import com.intellij.openapi.project.Project;
025 import com.intellij.psi.impl.source.CharTableImpl;
026 import com.intellij.psi.tree.IElementType;
027 import com.intellij.psi.tree.IErrorCounterReparseableElementType;
028 import com.intellij.psi.tree.IFileElementType;
029 import com.intellij.util.CharTable;
030 import org.jetbrains.annotations.Nullable;
031 import org.jetbrains.kotlin.idea.KotlinLanguage;
032 import org.jetbrains.kotlin.lexer.KotlinLexer;
033 import org.jetbrains.kotlin.lexer.KtTokens;
034 import org.jetbrains.kotlin.parsing.KotlinParser;
035 import org.jetbrains.kotlin.psi.*;
036 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
037
038 public interface KtNodeTypes {
039 IFileElementType KT_FILE = new IFileElementType(KotlinLanguage.INSTANCE);
040
041 IElementType CLASS = KtStubElementTypes.CLASS;
042 IElementType FUN = KtStubElementTypes.FUNCTION;
043 IElementType PROPERTY = KtStubElementTypes.PROPERTY;
044 IElementType DESTRUCTURING_DECLARATION = new KtNodeType("DESTRUCTURING_DECLARATION", KtDestructuringDeclaration.class);
045 IElementType DESTRUCTURING_DECLARATION_ENTRY = new KtNodeType("DESTRUCTURING_DECLARATION_ENTRY", KtDestructuringDeclarationEntry.class);
046
047 //KtNodeType TYPEDEF = new KtNodeType("TYPEDEF", KtTypedef.class);
048 IElementType OBJECT_DECLARATION = KtStubElementTypes.OBJECT_DECLARATION;
049
050 IElementType ENUM_ENTRY = KtStubElementTypes.ENUM_ENTRY;
051 IElementType CLASS_INITIALIZER = KtStubElementTypes.CLASS_INITIALIZER;
052 IElementType SCRIPT_INITIALIZER = new KtNodeType("SCRIPT_INITIALIZER", KtScriptInitializer.class);
053 IElementType SECONDARY_CONSTRUCTOR = KtStubElementTypes.SECONDARY_CONSTRUCTOR;
054 IElementType PRIMARY_CONSTRUCTOR = KtStubElementTypes.PRIMARY_CONSTRUCTOR;
055
056 IElementType TYPE_PARAMETER_LIST = KtStubElementTypes.TYPE_PARAMETER_LIST;
057 IElementType TYPE_PARAMETER = KtStubElementTypes.TYPE_PARAMETER;
058 IElementType SUPER_TYPE_LIST = KtStubElementTypes.SUPER_TYPE_LIST;
059 IElementType DELEGATED_SUPER_TYPE_ENTRY = KtStubElementTypes.DELEGATED_SUPER_TYPE_ENTRY;
060 IElementType SUPER_TYPE_CALL_ENTRY = KtStubElementTypes.SUPER_TYPE_CALL_ENTRY;
061 IElementType SUPER_TYPE_ENTRY = KtStubElementTypes.SUPER_TYPE_ENTRY;
062 KtNodeType PROPERTY_DELEGATE = new KtNodeType("PROPERTY_DELEGATE", KtPropertyDelegate.class);
063 IElementType CONSTRUCTOR_CALLEE = KtStubElementTypes.CONSTRUCTOR_CALLEE;
064 IElementType VALUE_PARAMETER_LIST = KtStubElementTypes.VALUE_PARAMETER_LIST;
065 IElementType VALUE_PARAMETER = KtStubElementTypes.VALUE_PARAMETER;
066
067 IElementType CLASS_BODY = KtStubElementTypes.CLASS_BODY;
068 IElementType IMPORT_LIST = KtStubElementTypes.IMPORT_LIST;
069 IElementType FILE_ANNOTATION_LIST = KtStubElementTypes.FILE_ANNOTATION_LIST;
070 IElementType IMPORT_DIRECTIVE = KtStubElementTypes.IMPORT_DIRECTIVE;
071 IElementType MODIFIER_LIST = KtStubElementTypes.MODIFIER_LIST;
072 IElementType ANNOTATION = KtStubElementTypes.ANNOTATION;
073 IElementType ANNOTATION_ENTRY = KtStubElementTypes.ANNOTATION_ENTRY;
074 IElementType ANNOTATION_TARGET = KtStubElementTypes.ANNOTATION_TARGET;
075
076 IElementType TYPE_ARGUMENT_LIST = KtStubElementTypes.TYPE_ARGUMENT_LIST;
077 KtNodeType VALUE_ARGUMENT_LIST = new KtNodeType("VALUE_ARGUMENT_LIST", KtValueArgumentList.class);
078 KtNodeType VALUE_ARGUMENT = new KtNodeType("VALUE_ARGUMENT", KtValueArgument.class);
079 KtNodeType LAMBDA_ARGUMENT = new KtNodeType("LAMBDA_ARGUMENT", KtLambdaArgument.class);
080 KtNodeType VALUE_ARGUMENT_NAME = new KtNodeType("VALUE_ARGUMENT_NAME", KtValueArgumentName.class);
081 IElementType TYPE_REFERENCE = KtStubElementTypes.TYPE_REFERENCE;
082
083 IElementType USER_TYPE = KtStubElementTypes.USER_TYPE;
084 IElementType DYNAMIC_TYPE = KtStubElementTypes.DYNAMIC_TYPE;
085 IElementType FUNCTION_TYPE = KtStubElementTypes.FUNCTION_TYPE;
086 IElementType FUNCTION_TYPE_RECEIVER = KtStubElementTypes.FUNCTION_TYPE_RECEIVER;
087 KtNodeType SELF_TYPE = new KtNodeType("SELF_TYPE", KtSelfType.class);
088 IElementType NULLABLE_TYPE = KtStubElementTypes.NULLABLE_TYPE;
089 IElementType TYPE_PROJECTION = KtStubElementTypes.TYPE_PROJECTION;
090
091 // TODO: review
092 IElementType PROPERTY_ACCESSOR = KtStubElementTypes.PROPERTY_ACCESSOR;
093 IElementType INITIALIZER_LIST = KtStubElementTypes.INITIALIZER_LIST;
094 IElementType TYPE_CONSTRAINT_LIST = KtStubElementTypes.TYPE_CONSTRAINT_LIST;
095 IElementType TYPE_CONSTRAINT = KtStubElementTypes.TYPE_CONSTRAINT;
096
097 IElementType CONSTRUCTOR_DELEGATION_CALL = new KtNodeType.KtLeftBoundNodeType("CONSTRUCTOR_DELEGATION_CALL", KtConstructorDelegationCall.class);
098 KtNodeType CONSTRUCTOR_DELEGATION_REFERENCE = new KtNodeType.KtLeftBoundNodeType("CONSTRUCTOR_DELEGATION_REFERENCE", KtConstructorDelegationReferenceExpression.class);
099
100 // TODO: Not sure if we need separate NT for each kind of constants
101 KtNodeType NULL = new KtNodeType("NULL", KtConstantExpression.class);
102 KtNodeType BOOLEAN_CONSTANT = new KtNodeType("BOOLEAN_CONSTANT", KtConstantExpression.class);
103 KtNodeType FLOAT_CONSTANT = new KtNodeType("FLOAT_CONSTANT", KtConstantExpression.class);
104 KtNodeType CHARACTER_CONSTANT = new KtNodeType("CHARACTER_CONSTANT", KtConstantExpression.class);
105 KtNodeType INTEGER_CONSTANT = new KtNodeType("INTEGER_CONSTANT", KtConstantExpression.class);
106
107 KtNodeType STRING_TEMPLATE = new KtNodeType("STRING_TEMPLATE", KtStringTemplateExpression.class);
108 KtNodeType LONG_STRING_TEMPLATE_ENTRY = new KtNodeType("LONG_STRING_TEMPLATE_ENTRY", KtBlockStringTemplateEntry.class);
109 KtNodeType SHORT_STRING_TEMPLATE_ENTRY = new KtNodeType("SHORT_STRING_TEMPLATE_ENTRY", KtSimpleNameStringTemplateEntry.class);
110 KtNodeType LITERAL_STRING_TEMPLATE_ENTRY = new KtNodeType("LITERAL_STRING_TEMPLATE_ENTRY", KtLiteralStringTemplateEntry.class);
111 KtNodeType ESCAPE_STRING_TEMPLATE_ENTRY = new KtNodeType("ESCAPE_STRING_TEMPLATE_ENTRY", KtEscapeStringTemplateEntry.class);
112
113 KtNodeType PARENTHESIZED = new KtNodeType("PARENTHESIZED", KtParenthesizedExpression.class);
114 KtNodeType RETURN = new KtNodeType("RETURN", KtReturnExpression.class);
115 KtNodeType THROW = new KtNodeType("THROW", KtThrowExpression.class);
116 KtNodeType CONTINUE = new KtNodeType("CONTINUE", KtContinueExpression.class);
117 KtNodeType BREAK = new KtNodeType("BREAK", KtBreakExpression.class);
118 KtNodeType IF = new KtNodeType("IF", KtIfExpression.class);
119 KtNodeType CONDITION = new KtNodeType("CONDITION", KtContainerNode.class);
120 KtNodeType THEN = new KtNodeType("THEN", KtContainerNodeForControlStructureBody.class);
121 KtNodeType ELSE = new KtNodeType("ELSE", KtContainerNodeForControlStructureBody.class);
122 KtNodeType TRY = new KtNodeType("TRY", KtTryExpression.class);
123 KtNodeType CATCH = new KtNodeType("CATCH", KtCatchClause.class);
124 KtNodeType FINALLY = new KtNodeType("FINALLY", KtFinallySection.class);
125 KtNodeType FOR = new KtNodeType("FOR", KtForExpression.class);
126 KtNodeType WHILE = new KtNodeType("WHILE", KtWhileExpression.class);
127 KtNodeType DO_WHILE = new KtNodeType("DO_WHILE", KtDoWhileExpression.class);
128 KtNodeType LOOP_RANGE = new KtNodeType("LOOP_RANGE", KtContainerNode.class);
129 KtNodeType BODY = new KtNodeType("BODY", KtContainerNodeForControlStructureBody.class);
130 KtNodeType BLOCK = new KtNodeType("BLOCK", KtBlockExpression.class);
131
132 IElementType LAMBDA_EXPRESSION = new IErrorCounterReparseableElementType("LAMBDA_EXPRESSION", KotlinLanguage.INSTANCE) {
133 @Override
134 public ASTNode parseContents(ASTNode chameleon) {
135 Project project = chameleon.getPsi().getProject();
136 PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, KotlinLanguage.INSTANCE, chameleon.getChars());
137 return KotlinParser.parseLambdaExpression(builder).getFirstChildNode();
138 }
139
140 @Nullable
141 @Override
142 public ASTNode createNode(CharSequence text) {
143 return new KtLambdaExpression(text);
144 }
145
146 @Override
147 public int getErrorsCount(CharSequence seq, Language fileLanguage, Project project) {
148 Lexer lexer = new KotlinLexer();
149
150 lexer.start(seq);
151 if (lexer.getTokenType() != KtTokens.LBRACE) return IErrorCounterReparseableElementType.FATAL_ERROR;
152 lexer.advance();
153 int balance = 1;
154 while (true) {
155 IElementType type = lexer.getTokenType();
156 if (type == null) break;
157 if (balance == 0) {
158 return IErrorCounterReparseableElementType.FATAL_ERROR;
159 }
160 if (type == KtTokens.LBRACE) {
161 balance++;
162 }
163 else if (type == KtTokens.RBRACE) {
164 balance--;
165 }
166 lexer.advance();
167 }
168 return balance;
169 }
170 };
171
172
173 KtNodeType FUNCTION_LITERAL = new KtNodeType("FUNCTION_LITERAL", KtFunctionLiteral.class);
174 KtNodeType ANNOTATED_EXPRESSION = new KtNodeType("ANNOTATED_EXPRESSION", KtAnnotatedExpression.class);
175
176 IElementType REFERENCE_EXPRESSION = KtStubElementTypes.REFERENCE_EXPRESSION;
177 IElementType ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION = KtStubElementTypes.ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION;
178 KtNodeType OPERATION_REFERENCE = new KtNodeType("OPERATION_REFERENCE", KtOperationReferenceExpression.class);
179 KtNodeType LABEL = new KtNodeType("LABEL", KtLabelReferenceExpression.class);
180
181 KtNodeType LABEL_QUALIFIER = new KtNodeType("LABEL_QUALIFIER", KtContainerNode.class);
182
183 KtNodeType THIS_EXPRESSION = new KtNodeType("THIS_EXPRESSION", KtThisExpression.class);
184 KtNodeType SUPER_EXPRESSION = new KtNodeType("SUPER_EXPRESSION", KtSuperExpression.class);
185 KtNodeType BINARY_EXPRESSION = new KtNodeType("BINARY_EXPRESSION", KtBinaryExpression.class);
186 KtNodeType BINARY_WITH_TYPE = new KtNodeType("BINARY_WITH_TYPE", KtBinaryExpressionWithTypeRHS.class);
187 KtNodeType IS_EXPRESSION = new KtNodeType("IS_EXPRESSION", KtIsExpression.class); // TODO:
188 KtNodeType PREFIX_EXPRESSION = new KtNodeType("PREFIX_EXPRESSION", KtPrefixExpression.class);
189 KtNodeType POSTFIX_EXPRESSION = new KtNodeType("POSTFIX_EXPRESSION", KtPostfixExpression.class);
190 KtNodeType LABELED_EXPRESSION = new KtNodeType("LABELED_EXPRESSION", KtLabeledExpression.class);
191 KtNodeType CALL_EXPRESSION = new KtNodeType("CALL_EXPRESSION", KtCallExpression.class);
192 KtNodeType ARRAY_ACCESS_EXPRESSION = new KtNodeType("ARRAY_ACCESS_EXPRESSION", KtArrayAccessExpression.class);
193 KtNodeType INDICES = new KtNodeType("INDICES", KtContainerNode.class);
194 IElementType DOT_QUALIFIED_EXPRESSION = KtStubElementTypes.DOT_QUALIFIED_EXPRESSION;
195 KtNodeType CALLABLE_REFERENCE_EXPRESSION = new KtNodeType("CALLABLE_REFERENCE_EXPRESSION", KtCallableReferenceExpression.class);
196 KtNodeType CLASS_LITERAL_EXPRESSION = new KtNodeType("CLASS_LITERAL_EXPRESSION", KtClassLiteralExpression.class);
197 KtNodeType SAFE_ACCESS_EXPRESSION = new KtNodeType("SAFE_ACCESS_EXPRESSION", KtSafeQualifiedExpression.class);
198
199 KtNodeType OBJECT_LITERAL = new KtNodeType("OBJECT_LITERAL", KtObjectLiteralExpression.class);
200
201 KtNodeType WHEN = new KtNodeType("WHEN", KtWhenExpression.class);
202 KtNodeType WHEN_ENTRY = new KtNodeType("WHEN_ENTRY", KtWhenEntry.class);
203
204 KtNodeType WHEN_CONDITION_IN_RANGE = new KtNodeType("WHEN_CONDITION_IN_RANGE", KtWhenConditionInRange.class);
205 KtNodeType WHEN_CONDITION_IS_PATTERN = new KtNodeType("WHEN_CONDITION_IS_PATTERN", KtWhenConditionIsPattern.class);
206 KtNodeType WHEN_CONDITION_EXPRESSION = new KtNodeType("WHEN_CONDITION_WITH_EXPRESSION", KtWhenConditionWithExpression.class);
207
208 IElementType PACKAGE_DIRECTIVE = KtStubElementTypes.PACKAGE_DIRECTIVE;
209
210 IElementType SCRIPT = KtStubElementTypes.SCRIPT;
211
212 IFileElementType TYPE_CODE_FRAGMENT = new KtTypeCodeFragmentType();
213 IFileElementType EXPRESSION_CODE_FRAGMENT = new KtExpressionCodeFragmentType();
214 IFileElementType BLOCK_CODE_FRAGMENT = new KtBlockCodeFragmentType();
215 }