Skip to content

Commit

Permalink
Merge pull request #215 from lopekpl/KT-1019
Browse files Browse the repository at this point in the history
KT-1019 recover from missing parentheses in function declaration
  • Loading branch information
abreslav committed Mar 1, 2013
2 parents 89017fb + bc59ed7 commit db99492
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,12 @@ IElementType parseFunction() {
typeParameterListOccurred = true;
}

parseValueParameterList(false, valueParametersFollow);
if (at(LPAR)) {
parseValueParameterList(false, valueParametersFollow);
}
else {
error("Expecting '('");
}

if (at(COLON)) {
advance(); // COLON
Expand Down Expand Up @@ -1648,10 +1653,11 @@ private PsiBuilder.Marker parseFunctionTypeContents() {
* ;
*/
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lexer.JetTokens;

import java.util.Collections;
Expand Down Expand Up @@ -83,7 +84,7 @@ public JetExpression getInitializer() {

/**
* Returns full qualified name for function "package_fqn.function_name"
* Not null for top level functions.
* Not null for top level functions unless syntax errors are present.
* @return
*/
@Nullable
Expand All @@ -101,7 +102,10 @@ public FqName getFqName() {
}
JetFile jetFile = (JetFile) parent;
final FqName fileFQN = JetPsiUtil.getFQName(jetFile);
return fileFQN.child(getNameAsName());
Name nameAsName = getNameAsName();
if (nameAsName != null) {
return fileFQN.child(nameAsName);
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fun<!SYNTAX, SYNTAX, SYNTAX, SYNTAX, SYNTAX!><!>
fun<!SYNTAX, SYNTAX!><!>
6 changes: 6 additions & 0 deletions compiler/testData/psi/FunctionNoParameterList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A {
fun foo
class B {}
fun bar
}
class C {}
50 changes: 50 additions & 0 deletions compiler/testData/psi/FunctionNoParameterList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
JetFile: FunctionNoParameterList.kt
NAMESPACE_HEADER
<empty list>
CLASS
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('A')
PsiWhiteSpace(' ')
TYPE_PARAMETER_LIST
<empty list>
CLASS_BODY
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('foo')
PsiErrorElement:Expecting '('
<empty list>
PsiWhiteSpace('\n ')
CLASS
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('B')
PsiWhiteSpace(' ')
TYPE_PARAMETER_LIST
<empty list>
CLASS_BODY
PsiElement(LBRACE)('{')
PsiElement(RBRACE)('}')
PsiWhiteSpace('\n ')
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('bar')
PsiErrorElement:Expecting '('
<empty list>
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
PsiWhiteSpace('\n')
CLASS
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('C')
PsiWhiteSpace(' ')
TYPE_PARAMETER_LIST
<empty list>
CLASS_BODY
PsiElement(LBRACE)('{')
PsiElement(RBRACE)('}')
1 change: 0 additions & 1 deletion compiler/testData/psi/Functions_ERR.jet
Original file line number Diff line number Diff line change
@@ -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<T, , T>(a : foo) : bar
Expand Down
33 changes: 4 additions & 29 deletions compiler/testData/psi/Functions_ERR.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,10 @@ JetFile: Functions_ERR.jet
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('foo')
VALUE_PARAMETER_LIST
PsiErrorElement:Expecting '(
<empty list>
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
<empty list>
PsiElement(RPAR)(')')
PsiErrorElement:Expecting '('
<empty list>
PsiErrorElement:Expecting package directive or top level declaration
PsiElement(RPAR)(')')
PsiWhiteSpace('\n')
FUN
PsiElement(fun)('fun')
Expand Down

0 comments on commit db99492

Please sign in to comment.