Support for DIP1009 (new contracts syntax), #375

This commit is contained in:
Laurent Tréguier 2018-09-05 23:02:39 +02:00
parent fb60cc1bbb
commit fee9bc8805
No known key found for this signature in database
GPG Key ID: 09530BFFC32D4A69
6 changed files with 68 additions and 4 deletions

View File

@ -4,7 +4,7 @@
"targetType": "autodetect", "targetType": "autodetect",
"license": "BSL-1.0", "license": "BSL-1.0",
"dependencies": { "dependencies": {
"libdparse": "~>0.8.6" "libdparse": "~>0.9.7"
}, },
"targetPath" : "bin/", "targetPath" : "bin/",
"targetName" : "dfmt", "targetName" : "dfmt",

@ -1 +1 @@
Subproject commit 086cf06051bb1f33c94891ba6c39a57f164ee296 Subproject commit 7ca3cb87b695a1d6b195ad7730c2094d07f22933

View File

@ -1906,7 +1906,7 @@ const pure @safe @nogc:
bool isBlockHeaderToken(IdType t) bool isBlockHeaderToken(IdType t)
{ {
return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse" return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse"
|| t == tok!"while" || t == tok!"if" || t == tok!"out" || t == tok!"while" || t == tok!"if" || t == tok!"in"|| t == tok!"out"
|| t == tok!"do" || t == tok!"catch" || t == tok!"with" || t == tok!"do" || t == tok!"catch" || t == tok!"with"
|| t == tok!"synchronized" || t == tok!"scope"; || t == tok!"synchronized" || t == tok!"scope";
} }
@ -1916,7 +1916,18 @@ const pure @safe @nogc:
if (i + index < 0 || i + index >= tokens.length) if (i + index < 0 || i + index >= tokens.length)
return false; return false;
auto t = tokens[i + index].type; auto t = tokens[i + index].type;
return isBlockHeaderToken(t); bool isExpressionContract;
if (i + index + 3 < tokens.length)
{
isExpressionContract = (t == tok!"in" && peekImplementation(tok!"(", i + 1, true))
|| (t == tok!"out" && (peekImplementation(tok!"(", i + 1, true)
&& (peekImplementation(tok!";", i + 2, true)
|| (peekImplementation(tok!"identifier", i + 2, true)
&& peekImplementation(tok!";", i + 3, true)))));
}
return isBlockHeaderToken(t) && !isExpressionContract;
} }
bool isSeparationToken(IdType t) nothrow bool isSeparationToken(IdType t) nothrow

View File

@ -0,0 +1,21 @@
int foo(int arg)
in
{
assert(arg > 0);
}
out (result)
{
assert(result == 0);
}
do
{
return 0;
}
int bar(int arg)
in(arg > 0)
out(; true)
out /*Major*/ ( /*Tom*/ result /*To ground control*/ ; result == 0)
{
return 0;
}

15
tests/dip1009.d Normal file
View File

@ -0,0 +1,15 @@
int foo(int arg)
in { assert(arg > 0); }
out (result) {assert(result == 0);}
do
{
return 0;
}
int bar(int arg)
in ( arg > 0 )
out(; true)
out/*Major*/ ( /*Tom*/ result /*To ground control*/; result==0)
{
return 0;
}

17
tests/otbs/dip1009.d.ref Normal file
View File

@ -0,0 +1,17 @@
int foo(int arg)
in {
assert(arg > 0);
}
out (result) {
assert(result == 0);
}
do {
return 0;
}
int bar(int arg)
in(arg > 0)
out(; true)
out /*Major*/ ( /*Tom*/ result /*To ground control*/ ; result == 0) {
return 0;
}