Parser for JetBlockCodeFragment
This commit is contained in:
@@ -152,4 +152,5 @@ public interface JetNodeTypes {
|
||||
|
||||
IFileElementType TYPE_CODE_FRAGMENT = new JetTypeCodeFragmentType();
|
||||
IFileElementType EXPRESSION_CODE_FRAGMENT = new JetExpressionCodeFragmentType();
|
||||
IFileElementType BLOCK_CODE_FRAGMENT = new JetBlockCodeFragmentType();
|
||||
}
|
||||
|
||||
@@ -65,4 +65,11 @@ public class JetParser implements PsiParser {
|
||||
jetParsing.parseExpressionCodeFragment();
|
||||
return psiBuilder.getTreeBuilt();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ASTNode parseBlockCodeFragment(PsiBuilder psiBuilder) {
|
||||
JetParsing jetParsing = JetParsing.createForTopLevel(new SemanticWhitespaceAwarePsiBuilderImpl(psiBuilder));
|
||||
jetParsing.parseBlockCodeFragment();
|
||||
return psiBuilder.getTreeBuilt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,9 @@ public class JetParserDefinition implements ParserDefinition {
|
||||
if (elementType instanceof JetStubElementType) {
|
||||
return ((JetStubElementType) elementType).createPsiFromAst(astNode);
|
||||
}
|
||||
else if (elementType == JetNodeTypes.TYPE_CODE_FRAGMENT || elementType == JetNodeTypes.EXPRESSION_CODE_FRAGMENT) {
|
||||
else if (elementType == JetNodeTypes.TYPE_CODE_FRAGMENT ||
|
||||
elementType == JetNodeTypes.EXPRESSION_CODE_FRAGMENT ||
|
||||
elementType == JetNodeTypes.BLOCK_CODE_FRAGMENT) {
|
||||
return new ASTWrapperPsiElement(astNode);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -106,10 +106,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
PsiBuilder.Marker marker = mark();
|
||||
parseTypeRef();
|
||||
|
||||
while (!eof()) {
|
||||
error("unexpected symbol");
|
||||
advance();
|
||||
}
|
||||
checkForUnexpectedSymbols();
|
||||
|
||||
marker.done(TYPE_CODE_FRAGMENT);
|
||||
}
|
||||
@@ -118,14 +115,29 @@ public class JetParsing extends AbstractJetParsing {
|
||||
PsiBuilder.Marker marker = mark();
|
||||
myExpressionParsing.parseExpression();
|
||||
|
||||
while (!eof()) {
|
||||
error("unexpected symbol");
|
||||
advance();
|
||||
}
|
||||
checkForUnexpectedSymbols();
|
||||
|
||||
marker.done(EXPRESSION_CODE_FRAGMENT);
|
||||
}
|
||||
|
||||
void parseBlockCodeFragment() {
|
||||
PsiBuilder.Marker marker = mark();
|
||||
PsiBuilder.Marker blockMarker = mark();
|
||||
|
||||
if (at(PACKAGE_KEYWORD) || at(IMPORT_KEYWORD)) {
|
||||
PsiBuilder.Marker err = mark();
|
||||
parsePreamble();
|
||||
err.error("Package directive and imports are forbidden in code fragments");
|
||||
}
|
||||
|
||||
myExpressionParsing.parseStatements();
|
||||
|
||||
checkForUnexpectedSymbols();
|
||||
|
||||
blockMarker.done(BLOCK);
|
||||
marker.done(BLOCK_CODE_FRAGMENT);
|
||||
}
|
||||
|
||||
void parseScript() {
|
||||
PsiBuilder.Marker fileMarker = mark();
|
||||
|
||||
@@ -137,11 +149,19 @@ public class JetParsing extends AbstractJetParsing {
|
||||
|
||||
myExpressionParsing.parseStatements();
|
||||
|
||||
checkForUnexpectedSymbols();
|
||||
|
||||
blockMarker.done(BLOCK);
|
||||
scriptMarker.done(SCRIPT);
|
||||
fileMarker.done(JET_FILE);
|
||||
}
|
||||
|
||||
private void checkForUnexpectedSymbols() {
|
||||
while (!eof()) {
|
||||
errorAndAdvance("unexpected symbol");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*preamble
|
||||
* : packageDirective? import*
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.lang.psi
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.JetNodeTypes
|
||||
|
||||
public class JetBlockCodeFragment(
|
||||
project: Project,
|
||||
name: String,
|
||||
text: CharSequence,
|
||||
context: PsiElement?
|
||||
) : JetCodeFragment(project, name, text, JetNodeTypes.BLOCK_CODE_FRAGMENT, context) {
|
||||
|
||||
fun getBlock() = findChildByClass(javaClass<JetBlockExpression>()) ?: throw IllegalStateException("Block expression should be parsed for BlockCodeFragment")
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.lang.psi;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.PsiBuilder;
|
||||
import com.intellij.lang.PsiBuilderFactory;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.parsing.JetParser;
|
||||
import org.jetbrains.jet.lang.psi.stubs.elements.JetFileElementType;
|
||||
|
||||
public class JetBlockCodeFragmentType extends JetFileElementType {
|
||||
private static final String NAME = "jet.BLOCK_CODE_FRAGMENT";
|
||||
|
||||
public JetBlockCodeFragmentType() {
|
||||
super(NAME);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getExternalId() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ASTNode doParseContents(@NotNull ASTNode chameleon, @NotNull PsiElement psi) {
|
||||
Project project = psi.getProject();
|
||||
Language languageForParser = getLanguageForParser(psi);
|
||||
PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, languageForParser, chameleon.getChars());
|
||||
return JetParser.parseBlockCodeFragment(builder).getFirstChildNode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,6 +344,11 @@ public class JetPsiFactory {
|
||||
return new JetExpressionCodeFragment(project, "fragment.kt", text, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetBlockCodeFragment createBlockCodeFragment(Project project, String text, PsiElement context) {
|
||||
return new JetBlockCodeFragment(project, "fragment.kt", text, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetReturnExpression createReturn(Project project, @NotNull String text) {
|
||||
return (JetReturnExpression) createExpression(project, "return " + text);
|
||||
|
||||
Reference in New Issue
Block a user