[Parser] Add parsing of function's contract either before or after type constraints

This commit is contained in:
Arsen Nagdalian
2020-07-18 22:57:23 +03:00
parent 1b57889773
commit 32a64b888e
6 changed files with 401 additions and 188 deletions
@@ -1671,10 +1671,14 @@ public class KotlinParsing extends AbstractKotlinParsing {
parseTypeRef();
}
parseFunctionContract();
boolean functionContractOccurred = parseFunctionContract();
parseTypeConstraintsGuarded(typeParameterListOccurred);
if (!functionContractOccurred) {
parseFunctionContract();
}
if (at(SEMICOLON)) {
advance(); // SEMICOLON
}
@@ -1995,10 +1999,12 @@ public class KotlinParsing extends AbstractKotlinParsing {
constraint.done(TYPE_CONSTRAINT);
}
private void parseFunctionContract() {
private boolean parseFunctionContract() {
if (at(CONTRACT_KEYWORD)) {
myExpressionParsing.parseContractDescriptionBlock();
return true;
}
return false;
}
/*
@@ -1,12 +0,0 @@
fun someFunctionWithTypeConstraints<T, E>(arg: E?, block: () -> T): String
contract [
returns() implies (arg != null),
callsInPlace(block, EXACTLY_ONCE),
]
where T : MyClass,
E : MyOtherClass
{
block()
arg ?: throw NullArgumentException()
return "some string"
}
@@ -1,169 +0,0 @@
KtFile: FunctionWithTypeConstraintsAndContract.kt
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('someFunctionWithTypeConstraints')
TYPE_PARAMETER_LIST
PsiElement(LT)('<')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('T')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('E')
PsiElement(GT)('>')
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('arg')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
NULLABLE_TYPE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('E')
PsiElement(QUEST)('?')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('block')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
FUNCTION_TYPE
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
PsiElement(ARROW)('->')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiElement(RPAR)(')')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('String')
PsiWhiteSpace('\n ')
PsiElement(contract)('contract')
PsiWhiteSpace(' ')
CONTRACT_EFFECT_LIST
PsiElement(LBRACKET)('[')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
BINARY_EXPRESSION
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('returns')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(IDENTIFIER)('implies')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('callsInPlace')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('block')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('EXACTLY_ONCE')
PsiElement(RPAR)(')')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
PsiElement(RBRACKET)(']')
PsiWhiteSpace('\n ')
PsiElement(where)('where')
PsiWhiteSpace(' ')
TYPE_CONSTRAINT_LIST
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('MyClass')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('E')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('MyOtherClass')
PsiWhiteSpace('\n')
BLOCK
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('block')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(ELVIS)('?:')
PsiWhiteSpace(' ')
THROW
PsiElement(throw)('throw')
PsiWhiteSpace(' ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('NullArgumentException')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiWhiteSpace(' ')
STRING_TEMPLATE
PsiElement(OPEN_QUOTE)('"')
LITERAL_STRING_TEMPLATE_ENTRY
PsiElement(REGULAR_STRING_PART)('some string')
PsiElement(CLOSING_QUOTE)('"')
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
@@ -0,0 +1,28 @@
// the following functions have type constraints and contracts written in different order
// any order is correct
fun someFunctionWithTypeConstraints<T, E>(arg: E?, block: () -> T): String
contract [
returns() implies (arg != null),
callsInPlace(block, EXACTLY_ONCE),
]
where T : MyClass,
E : MyOtherClass
{
block()
arg ?: throw NullArgumentException()
return "some string"
}
fun anotherFunctionWithTypeConstraints<D, T>(data: D?, arg: T?, block: () -> Unit)
where D : SuperType,
T : SomeType
contract [
returns() implies (data != null),
returns() implies (arg != null)
]
{
require(data != null)
require(arg != null)
block()
}
@@ -0,0 +1,360 @@
KtFile: FunctionsWithTypeConstraintsAndContract.kt
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
PsiComment(EOL_COMMENT)('// the following functions have type constraints and contracts written in different order')
PsiWhiteSpace('\n')
PsiComment(EOL_COMMENT)('// any order is correct')
PsiWhiteSpace('\n\n')
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('someFunctionWithTypeConstraints')
TYPE_PARAMETER_LIST
PsiElement(LT)('<')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('T')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('E')
PsiElement(GT)('>')
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('arg')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
NULLABLE_TYPE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('E')
PsiElement(QUEST)('?')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('block')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
FUNCTION_TYPE
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
PsiElement(ARROW)('->')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiElement(RPAR)(')')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('String')
PsiWhiteSpace('\n ')
PsiElement(contract)('contract')
PsiWhiteSpace(' ')
CONTRACT_EFFECT_LIST
PsiElement(LBRACKET)('[')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
BINARY_EXPRESSION
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('returns')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(IDENTIFIER)('implies')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('callsInPlace')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('block')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('EXACTLY_ONCE')
PsiElement(RPAR)(')')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
PsiElement(RBRACKET)(']')
PsiWhiteSpace('\n ')
PsiElement(where)('where')
PsiWhiteSpace(' ')
TYPE_CONSTRAINT_LIST
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('MyClass')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('E')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('MyOtherClass')
PsiWhiteSpace('\n')
BLOCK
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('block')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(ELVIS)('?:')
PsiWhiteSpace(' ')
THROW
PsiElement(throw)('throw')
PsiWhiteSpace(' ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('NullArgumentException')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiWhiteSpace(' ')
STRING_TEMPLATE
PsiElement(OPEN_QUOTE)('"')
LITERAL_STRING_TEMPLATE_ENTRY
PsiElement(REGULAR_STRING_PART)('some string')
PsiElement(CLOSING_QUOTE)('"')
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
PsiWhiteSpace('\n\n')
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('anotherFunctionWithTypeConstraints')
TYPE_PARAMETER_LIST
PsiElement(LT)('<')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('D')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
TYPE_PARAMETER
PsiElement(IDENTIFIER)('T')
PsiElement(GT)('>')
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('data')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
NULLABLE_TYPE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('D')
PsiElement(QUEST)('?')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('arg')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
NULLABLE_TYPE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiElement(QUEST)('?')
PsiElement(COMMA)(',')
PsiWhiteSpace(' ')
VALUE_PARAMETER
PsiElement(IDENTIFIER)('block')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
FUNCTION_TYPE
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
PsiElement(ARROW)('->')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('Unit')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
PsiElement(where)('where')
PsiWhiteSpace(' ')
TYPE_CONSTRAINT_LIST
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('D')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('SuperType')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
TYPE_CONSTRAINT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('T')
PsiWhiteSpace(' ')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
TYPE_REFERENCE
USER_TYPE
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('SomeType')
PsiWhiteSpace('\n ')
PsiElement(contract)('contract')
PsiWhiteSpace(' ')
CONTRACT_EFFECT_LIST
PsiElement(LBRACKET)('[')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
BINARY_EXPRESSION
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('returns')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(IDENTIFIER)('implies')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('data')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
CONTRACT_EFFECT
BINARY_EXPRESSION
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('returns')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(IDENTIFIER)('implies')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
PsiElement(RBRACKET)(']')
PsiWhiteSpace('\n')
BLOCK
PsiElement(LBRACE)('{')
PsiWhiteSpace('\n ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('require')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('data')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('require')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
BINARY_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('arg')
PsiWhiteSpace(' ')
OPERATION_REFERENCE
PsiElement(EXCLEQ)('!=')
PsiWhiteSpace(' ')
NULL
PsiElement(null)('null')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n ')
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('block')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
@@ -385,16 +385,16 @@ public class ParsingTestGenerated extends AbstractParsingTest {
runTest("compiler/testData/psi/FunctionWithMultilineContract.kt");
}
@TestMetadata("FunctionWithTypeConstraintsAndContract.kt")
public void testFunctionWithTypeConstraintsAndContract() throws Exception {
runTest("compiler/testData/psi/FunctionWithTypeConstraintsAndContract.kt");
}
@TestMetadata("Functions.kt")
public void testFunctions() throws Exception {
runTest("compiler/testData/psi/Functions.kt");
}
@TestMetadata("FunctionsWithTypeConstraintsAndContract.kt")
public void testFunctionsWithTypeConstraintsAndContract() throws Exception {
runTest("compiler/testData/psi/FunctionsWithTypeConstraintsAndContract.kt");
}
@TestMetadata("FunctionsWithoutName.kt")
public void testFunctionsWithoutName() throws Exception {
runTest("compiler/testData/psi/FunctionsWithoutName.kt");