001    /*
002     * Copyright 2010-2013 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.jet.lang.parsing;
018    
019    import com.intellij.extapi.psi.ASTWrapperPsiElement;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.lang.LanguageParserDefinitions;
022    import com.intellij.lang.ParserDefinition;
023    import com.intellij.lang.PsiParser;
024    import com.intellij.lexer.Lexer;
025    import com.intellij.openapi.project.Project;
026    import com.intellij.psi.FileViewProvider;
027    import com.intellij.psi.PsiElement;
028    import com.intellij.psi.PsiFile;
029    import com.intellij.psi.tree.IElementType;
030    import com.intellij.psi.tree.IFileElementType;
031    import com.intellij.psi.tree.TokenSet;
032    import org.jetbrains.annotations.NotNull;
033    import org.jetbrains.jet.JetNodeType;
034    import org.jetbrains.jet.JetNodeTypes;
035    import org.jetbrains.jet.lang.psi.JetFile;
036    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementType;
037    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
038    import org.jetbrains.jet.lexer.JetLexer;
039    import org.jetbrains.jet.lexer.JetTokens;
040    import org.jetbrains.jet.plugin.JetLanguage;
041    
042    public class JetParserDefinition implements ParserDefinition {
043        public static final String STD_SCRIPT_SUFFIX = "kts";
044        public static final String STD_SCRIPT_EXT = "." + STD_SCRIPT_SUFFIX;
045    
046        public JetParserDefinition() {
047            //todo: ApplicationManager.getApplication() is null during JetParsingTest setting up
048    
049            /*if (!ApplicationManager.getApplication().isCommandLine()) {
050            }*/
051        }
052    
053        @NotNull
054        public static JetParserDefinition getInstance() {
055            return (JetParserDefinition)LanguageParserDefinitions.INSTANCE.forLanguage(JetLanguage.INSTANCE);
056        }
057    
058        @Override
059        @NotNull
060        public Lexer createLexer(Project project) {
061            return new JetLexer();
062        }
063    
064        @Override
065        public PsiParser createParser(Project project) {
066            return new JetParser(project);
067        }
068    
069        @Override
070        public IFileElementType getFileNodeType() {
071            return JetStubElementTypes.FILE;
072        }
073    
074        @Override
075        @NotNull
076        public TokenSet getWhitespaceTokens() {
077            return JetTokens.WHITESPACES;
078        }
079    
080        @Override
081        @NotNull
082        public TokenSet getCommentTokens() {
083            return JetTokens.COMMENTS;
084        }
085    
086        @Override
087        @NotNull
088        public TokenSet getStringLiteralElements() {
089            return JetTokens.STRINGS;
090        }
091    
092        @Override
093        @NotNull
094        public PsiElement createElement(ASTNode astNode) {
095            IElementType elementType = astNode.getElementType();
096    
097            if (elementType instanceof JetStubElementType) {
098                return ((JetStubElementType) elementType).createPsiFromAst(astNode);
099            }
100            else if (elementType == JetNodeTypes.TYPE_CODE_FRAGMENT ||
101                     elementType == JetNodeTypes.EXPRESSION_CODE_FRAGMENT  ||
102                     elementType == JetNodeTypes.BLOCK_CODE_FRAGMENT) {
103                return new ASTWrapperPsiElement(astNode);
104            }
105            else {
106                return ((JetNodeType) elementType).createPsi(astNode);
107            }
108        }
109    
110        @Override
111        public PsiFile createFile(FileViewProvider fileViewProvider) {
112            return new JetFile(fileViewProvider, false);
113        }
114    
115        @Override
116        public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode astNode, ASTNode astNode1) {
117            return SpaceRequirements.MAY;
118        }
119    }