KT-622 Add when without condition. (Only changes for parsing were added. A special task for semantic level is KT-657)
This commit is contained in:
@@ -670,7 +670,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* when
|
* when
|
||||||
* : "when" "(" (modifiers "val" SimpleName "=")? element ")" "{"
|
* : "when" ("(" (modifiers "val" SimpleName "=")? element ")")? "{"
|
||||||
* whenEntry*
|
* whenEntry*
|
||||||
* "}"
|
* "}"
|
||||||
* ;
|
* ;
|
||||||
@@ -682,22 +682,26 @@ public class JetExpressionParsing extends AbstractJetParsing {
|
|||||||
|
|
||||||
advance(); // WHEN_KEYWORD
|
advance(); // WHEN_KEYWORD
|
||||||
|
|
||||||
|
// Parse condition
|
||||||
myBuilder.disableNewlines();
|
myBuilder.disableNewlines();
|
||||||
expect(LPAR, "Expecting '('");
|
if (at(LPAR)) {
|
||||||
|
advanceAt(LPAR);
|
||||||
|
|
||||||
int valPos = matchTokenStreamPredicate(new FirstBefore(new At(VAL_KEYWORD), new AtSet(RPAR, LBRACE, RBRACE, SEMICOLON, EQ)));
|
int valPos = matchTokenStreamPredicate(new FirstBefore(new At(VAL_KEYWORD), new AtSet(RPAR, LBRACE, RBRACE, SEMICOLON, EQ)));
|
||||||
if (valPos >= 0) {
|
if (valPos >= 0) {
|
||||||
PsiBuilder.Marker property = mark();
|
PsiBuilder.Marker property = mark();
|
||||||
myJetParsing.parseModifierList(MODIFIER_LIST, true);
|
myJetParsing.parseModifierList(MODIFIER_LIST, true);
|
||||||
myJetParsing.parseProperty(true);
|
myJetParsing.parseProperty(true);
|
||||||
property.done(PROPERTY);
|
property.done(PROPERTY);
|
||||||
} else {
|
} else {
|
||||||
parseExpression();
|
parseExpression();
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(RPAR, "Expecting ')'");
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(RPAR, "Expecting ')'");
|
|
||||||
myBuilder.restoreNewlinesState();
|
myBuilder.restoreNewlinesState();
|
||||||
|
|
||||||
|
// Parse when block
|
||||||
myBuilder.enableNewlines();
|
myBuilder.enableNewlines();
|
||||||
expect(LBRACE, "Expecting '{'");
|
expect(LBRACE, "Expecting '{'");
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class JetWhenExpression extends JetExpression {
|
|||||||
return findChildrenByType(JetNodeTypes.WHEN_ENTRY);
|
return findChildrenByType(JetNodeTypes.WHEN_ENTRY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable @IfNotParsed
|
@Nullable
|
||||||
public JetExpression getSubjectExpression() {
|
public JetExpression getSubjectExpression() {
|
||||||
return findChildByClass(JetExpression.class);
|
return findChildByClass(JetExpression.class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,3 +70,14 @@ fun foo() {
|
|||||||
is a.a @ (val b, b) => c
|
is a.a @ (val b, b) => c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun whenWithoutCondition(i : Int) {
|
||||||
|
val j = 12
|
||||||
|
when {
|
||||||
|
3 => -1
|
||||||
|
i == 3 => -1
|
||||||
|
j < i, j == i => -1
|
||||||
|
i is Int => 1
|
||||||
|
else => 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1206,3 +1206,140 @@ JetFile: When.jet
|
|||||||
PsiElement(RBRACE)('}')
|
PsiElement(RBRACE)('}')
|
||||||
PsiWhiteSpace('\n')
|
PsiWhiteSpace('\n')
|
||||||
PsiElement(RBRACE)('}')
|
PsiElement(RBRACE)('}')
|
||||||
|
PsiWhiteSpace('\n\n')
|
||||||
|
FUN
|
||||||
|
PsiElement(fun)('fun')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(IDENTIFIER)('whenWithoutCondition')
|
||||||
|
VALUE_PARAMETER_LIST
|
||||||
|
PsiElement(LPAR)('(')
|
||||||
|
VALUE_PARAMETER
|
||||||
|
PsiElement(IDENTIFIER)('i')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(COLON)(':')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
TYPE_REFERENCE
|
||||||
|
USER_TYPE
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('Int')
|
||||||
|
PsiElement(RPAR)(')')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
BLOCK
|
||||||
|
PsiElement(LBRACE)('{')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
PROPERTY
|
||||||
|
PsiElement(val)('val')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(IDENTIFIER)('j')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(EQ)('=')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('12')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN
|
||||||
|
PsiElement(when)('when')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(LBRACE)('{')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN_ENTRY
|
||||||
|
WHEN_CONDITION_IS_PATTERN
|
||||||
|
EXPRESSION_PATTERN
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('3')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(DOUBLE_ARROW)('=>')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PREFIX_EXPRESSION
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(MINUS)('-')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('1')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN_ENTRY
|
||||||
|
WHEN_CONDITION_IS_PATTERN
|
||||||
|
EXPRESSION_PATTERN
|
||||||
|
BINARY_EXPRESSION
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('i')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(EQEQ)('==')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('3')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(DOUBLE_ARROW)('=>')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PREFIX_EXPRESSION
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(MINUS)('-')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('1')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN_ENTRY
|
||||||
|
WHEN_CONDITION_IS_PATTERN
|
||||||
|
EXPRESSION_PATTERN
|
||||||
|
BINARY_EXPRESSION
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('j')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(LT)('<')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('i')
|
||||||
|
PsiElement(COMMA)(',')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
WHEN_CONDITION_IS_PATTERN
|
||||||
|
EXPRESSION_PATTERN
|
||||||
|
BINARY_EXPRESSION
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('j')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(EQEQ)('==')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('i')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(DOUBLE_ARROW)('=>')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PREFIX_EXPRESSION
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(MINUS)('-')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('1')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN_ENTRY
|
||||||
|
WHEN_CONDITION_IS_PATTERN
|
||||||
|
EXPRESSION_PATTERN
|
||||||
|
BINARY_WITH_PATTERN
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('i')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
OPERATION_REFERENCE
|
||||||
|
PsiElement(is)('is')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
TYPE_PATTERN
|
||||||
|
TYPE_REFERENCE
|
||||||
|
USER_TYPE
|
||||||
|
REFERENCE_EXPRESSION
|
||||||
|
PsiElement(IDENTIFIER)('Int')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(DOUBLE_ARROW)('=>')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('1')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
WHEN_ENTRY
|
||||||
|
PsiElement(else)('else')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
PsiElement(DOUBLE_ARROW)('=>')
|
||||||
|
PsiWhiteSpace(' ')
|
||||||
|
INTEGER_CONSTANT
|
||||||
|
PsiElement(INTEGER_LITERAL)('2')
|
||||||
|
PsiWhiteSpace('\n ')
|
||||||
|
PsiElement(RBRACE)('}')
|
||||||
|
PsiWhiteSpace('\n')
|
||||||
|
PsiElement(RBRACE)('}')
|
||||||
@@ -5,7 +5,7 @@ bq. See [Pattern matching]
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
when
|
when
|
||||||
: "when" "(" (modifiers "val" SimpleName "=")? expression ")" "{"
|
: "when" ("(" (modifiers "val" SimpleName "=")? expression ")")? "{"
|
||||||
whenEntry*
|
whenEntry*
|
||||||
"}"
|
"}"
|
||||||
;
|
;
|
||||||
|
|||||||
Reference in New Issue
Block a user