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.lang.ASTNode; 020import com.intellij.lang.PsiBuilder; 021import com.intellij.lang.PsiParser; 022import com.intellij.openapi.project.Project; 023import com.intellij.psi.PsiFile; 024import com.intellij.psi.tree.IElementType; 025import org.jetbrains.annotations.NotNull; 026 027public class JetParser implements PsiParser { 028 029 public static final String STD_SCRIPT_EXT = "." + JetParserDefinition.KTSCRIPT_FILE_SUFFIX; 030 private final JetScriptDefinitionProvider scriptDefinitionProvider; 031 032 public JetParser(Project project) { 033 scriptDefinitionProvider = JetScriptDefinitionProvider.getInstance(project); 034 } 035 036 @Override 037 @NotNull 038 public ASTNode parse(IElementType iElementType, PsiBuilder psiBuilder) { 039 throw new IllegalStateException("use another parse"); 040 } 041 042 // we need this method because we need psiFile 043 @NotNull 044 public ASTNode parse(IElementType iElementType, PsiBuilder psiBuilder, PsiFile psiFile) { 045 JetParsing jetParsing = JetParsing.createForTopLevel(new SemanticWhitespaceAwarePsiBuilderImpl(psiBuilder)); 046 if (scriptDefinitionProvider != null && scriptDefinitionProvider.isScript(psiFile) || psiFile.getName().endsWith(STD_SCRIPT_EXT)) { 047 jetParsing.parseScript(); 048 } 049 else { 050 jetParsing.parseFile(); 051 } 052 return psiBuilder.getTreeBuilt(); 053 } 054 055 @NotNull 056 public static ASTNode parseTypeCodeFragment(PsiBuilder psiBuilder) { 057 JetParsing jetParsing = JetParsing.createForTopLevel(new SemanticWhitespaceAwarePsiBuilderImpl(psiBuilder)); 058 jetParsing.parseTypeCodeFragment(); 059 return psiBuilder.getTreeBuilt(); 060 } 061 062 @NotNull 063 public static ASTNode parseExpressionCodeFragment(PsiBuilder psiBuilder) { 064 JetParsing jetParsing = JetParsing.createForTopLevel(new SemanticWhitespaceAwarePsiBuilderImpl(psiBuilder)); 065 jetParsing.parseExpressionCodeFragment(); 066 return psiBuilder.getTreeBuilt(); 067 } 068}