From bc59ed75ca4f8feac78639ad9be98547202aa86f Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 27 Feb 2013 14:42:14 +0100 Subject: [PATCH] KT-1019 parser should recover from missing parentheses in function declaration #KT-1019 Fixed --- .../jet/lang/parsing/JetParsing.java | 10 +++- .../diagnosticWithSyntaxError/funKeyword.kt | 2 +- .../testData/psi/FunctionNoParameterList.kt | 6 +++ .../testData/psi/FunctionNoParameterList.txt | 50 +++++++++++++++++++ compiler/testData/psi/Functions_ERR.jet | 1 - compiler/testData/psi/Functions_ERR.txt | 33 ++---------- 6 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 compiler/testData/psi/FunctionNoParameterList.kt create mode 100644 compiler/testData/psi/FunctionNoParameterList.txt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetParsing.java b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetParsing.java index 1b977bcf9a2..f6758ba19d0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetParsing.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetParsing.java @@ -1042,7 +1042,12 @@ public class JetParsing extends AbstractJetParsing { typeParameterListOccurred = true; } - parseValueParameterList(false, valueParametersFollow); + if (at(LPAR)) { + parseValueParameterList(false, valueParametersFollow); + } + else { + error("Expecting '('"); + } if (at(COLON)) { advance(); // COLON @@ -1648,10 +1653,11 @@ public class JetParsing extends AbstractJetParsing { * ; */ void parseValueParameterList(boolean isFunctionTypeContents, TokenSet recoverySet) { + assert _at(LPAR); PsiBuilder.Marker parameters = mark(); myBuilder.disableNewlines(); - expect(LPAR, "Expecting '(", recoverySet); + advance(); // LPAR if (!parseIdeTemplate()) { if (!at(RPAR) && !atSet(recoverySet)) { diff --git a/compiler/testData/diagnostics/tests/incompleteCode/diagnosticWithSyntaxError/funKeyword.kt b/compiler/testData/diagnostics/tests/incompleteCode/diagnosticWithSyntaxError/funKeyword.kt index 2f860b37dbf..f773d983a7d 100644 --- a/compiler/testData/diagnostics/tests/incompleteCode/diagnosticWithSyntaxError/funKeyword.kt +++ b/compiler/testData/diagnostics/tests/incompleteCode/diagnosticWithSyntaxError/funKeyword.kt @@ -1 +1 @@ -fun \ No newline at end of file +fun \ No newline at end of file diff --git a/compiler/testData/psi/FunctionNoParameterList.kt b/compiler/testData/psi/FunctionNoParameterList.kt new file mode 100644 index 00000000000..eea4b5a8f38 --- /dev/null +++ b/compiler/testData/psi/FunctionNoParameterList.kt @@ -0,0 +1,6 @@ +class A { + fun foo + class B {} + fun bar +} +class C {} \ No newline at end of file diff --git a/compiler/testData/psi/FunctionNoParameterList.txt b/compiler/testData/psi/FunctionNoParameterList.txt new file mode 100644 index 00000000000..634c8564f80 --- /dev/null +++ b/compiler/testData/psi/FunctionNoParameterList.txt @@ -0,0 +1,50 @@ +JetFile: FunctionNoParameterList.kt + NAMESPACE_HEADER + + CLASS + PsiElement(class)('class') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('A') + PsiWhiteSpace(' ') + TYPE_PARAMETER_LIST + + CLASS_BODY + PsiElement(LBRACE)('{') + PsiWhiteSpace('\n ') + FUN + PsiElement(fun)('fun') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('foo') + PsiErrorElement:Expecting '(' + + PsiWhiteSpace('\n ') + CLASS + PsiElement(class)('class') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('B') + PsiWhiteSpace(' ') + TYPE_PARAMETER_LIST + + CLASS_BODY + PsiElement(LBRACE)('{') + PsiElement(RBRACE)('}') + PsiWhiteSpace('\n ') + FUN + PsiElement(fun)('fun') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('bar') + PsiErrorElement:Expecting '(' + + PsiWhiteSpace('\n') + PsiElement(RBRACE)('}') + PsiWhiteSpace('\n') + CLASS + PsiElement(class)('class') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('C') + PsiWhiteSpace(' ') + TYPE_PARAMETER_LIST + + CLASS_BODY + PsiElement(LBRACE)('{') + PsiElement(RBRACE)('}') \ No newline at end of file diff --git a/compiler/testData/psi/Functions_ERR.jet b/compiler/testData/psi/Functions_ERR.jet index 3c0949fd5c5..ca0dbba2cb1 100644 --- a/compiler/testData/psi/Functions_ERR.jet +++ b/compiler/testData/psi/Functions_ERR.jet @@ -1,5 +1,4 @@ fun foo) -fun [a] f foo() fun [a] T.foo(a : ) : bar fun [a()] T.foo<>(a : foo) : bar fun [a()] T.foo(a : foo) : bar diff --git a/compiler/testData/psi/Functions_ERR.txt b/compiler/testData/psi/Functions_ERR.txt index e15ae543ba2..cc181ad3f01 100644 --- a/compiler/testData/psi/Functions_ERR.txt +++ b/compiler/testData/psi/Functions_ERR.txt @@ -5,35 +5,10 @@ JetFile: Functions_ERR.jet PsiElement(fun)('fun') PsiWhiteSpace(' ') PsiElement(IDENTIFIER)('foo') - VALUE_PARAMETER_LIST - PsiErrorElement:Expecting '( - - PsiElement(RPAR)(')') - PsiWhiteSpace('\n') - FUN - PsiElement(fun)('fun') - PsiWhiteSpace(' ') - ANNOTATION - PsiElement(LBRACKET)('[') - ANNOTATION_ENTRY - CONSTRUCTOR_CALLEE - TYPE_REFERENCE - USER_TYPE - REFERENCE_EXPRESSION - PsiElement(IDENTIFIER)('a') - PsiElement(RBRACKET)(']') - PsiWhiteSpace(' ') - PsiElement(IDENTIFIER)('f') - PsiWhiteSpace(' ') - VALUE_PARAMETER_LIST - PsiErrorElement:Expecting '( - PsiElement(IDENTIFIER)('foo') - VALUE_PARAMETER - PsiErrorElement:Parameter name expected - PsiElement(LPAR)('(') - PsiErrorElement:Parameters must have type annotation - - PsiElement(RPAR)(')') + PsiErrorElement:Expecting '(' + + PsiErrorElement:Expecting package directive or top level declaration + PsiElement(RPAR)(')') PsiWhiteSpace('\n') FUN PsiElement(fun)('fun')