From 7bb9f97b113ba210a328e46829bd3ee3f0089616 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 19 Nov 2019 19:14:27 +0100 Subject: [PATCH] Allow to separate top-level script declarations and statements with semicolon This fix allows to write one-liner scripts like "val x = 3; println(x)", which were previously rejected by the parser. --- .../parsing/KotlinExpressionParsing.java | 2 +- compiler/testData/psi/script/OneLiner.kts | 1 + compiler/testData/psi/script/OneLiner.txt | 35 +++++++++++++++++++ .../kotlin/parsing/ParsingTestGenerated.java | 5 +++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/psi/script/OneLiner.kts create mode 100644 compiler/testData/psi/script/OneLiner.txt diff --git a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java index b7ba9fa381e..12840cf4ffc 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java +++ b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java @@ -1261,7 +1261,7 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing { else if (at(RBRACE)) { break; } - else if (!myBuilder.newlineBeforeCurrentToken()) { + else if (!isScriptTopLevel && !myBuilder.newlineBeforeCurrentToken()) { String severalStatementsError = "Unexpected tokens (use ';' to separate expressions on the same line)"; if (atSet(STATEMENT_NEW_LINE_QUICK_RECOVERY_SET)) { diff --git a/compiler/testData/psi/script/OneLiner.kts b/compiler/testData/psi/script/OneLiner.kts new file mode 100644 index 00000000000..85bcd04e998 --- /dev/null +++ b/compiler/testData/psi/script/OneLiner.kts @@ -0,0 +1 @@ +val x = 6; println(x * 7) diff --git a/compiler/testData/psi/script/OneLiner.txt b/compiler/testData/psi/script/OneLiner.txt new file mode 100644 index 00000000000..eb3f5b8a5a8 --- /dev/null +++ b/compiler/testData/psi/script/OneLiner.txt @@ -0,0 +1,35 @@ +KtFile: OneLiner.kts + PACKAGE_DIRECTIVE + + IMPORT_LIST + + SCRIPT + BLOCK + PROPERTY + PsiElement(val)('val') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('x') + PsiWhiteSpace(' ') + PsiElement(EQ)('=') + PsiWhiteSpace(' ') + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('6') + PsiElement(SEMICOLON)(';') + PsiWhiteSpace(' ') + SCRIPT_INITIALIZER + CALL_EXPRESSION + REFERENCE_EXPRESSION + PsiElement(IDENTIFIER)('println') + VALUE_ARGUMENT_LIST + PsiElement(LPAR)('(') + VALUE_ARGUMENT + BINARY_EXPRESSION + REFERENCE_EXPRESSION + PsiElement(IDENTIFIER)('x') + PsiWhiteSpace(' ') + OPERATION_REFERENCE + PsiElement(MUL)('*') + PsiWhiteSpace(' ') + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('7') + PsiElement(RPAR)(')') \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java index 512d18e56fa..3e36d1e36ce 100644 --- a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java @@ -2607,6 +2607,11 @@ public class ParsingTestGenerated extends AbstractParsingTest { runTest("compiler/testData/psi/script/manyAnnotationsOnFile.kts"); } + @TestMetadata("OneLiner.kts") + public void testOneLiner() throws Exception { + runTest("compiler/testData/psi/script/OneLiner.kts"); + } + @TestMetadata("Shebang.kts") public void testShebang() throws Exception { runTest("compiler/testData/psi/script/Shebang.kts");