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