Support for DIP1009 (new contracts syntax), #375 (#376)

Support for DIP1009 (new contracts syntax), #375
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
Laurent Tréguier 2018-09-10 14:15:14 +02:00 committed by The Dlang Bot
parent 1fd19a1375
commit 647bb6daa9
6 changed files with 70 additions and 6 deletions

View File

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

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

View File

@ -1048,7 +1048,7 @@ private:
else if (peekBackIsKeyword)
write(" ");
writeToken();
if (!currentIs(tok!"(") && !currentIs(tok!"{"))
if (!currentIs(tok!"(") && !currentIs(tok!"{") && !currentIs(tok!"comment"))
write(" ");
break;
case tok!"try":
@ -1090,7 +1090,7 @@ private:
tok!"}", tok!"=", tok!"&&", tok!"||") && !peekBackIsKeyword())
write(" ");
writeToken();
if (!currentIs(tok!"(") && !currentIs(tok!"{"))
if (!currentIs(tok!"(") && !currentIs(tok!"{") && !currentIs(tok!"comment"))
write(" ");
break;
case tok!"case":
@ -1906,7 +1906,7 @@ const pure @safe @nogc:
bool isBlockHeaderToken(IdType t)
{
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!"synchronized" || t == tok!"scope";
}
@ -1916,7 +1916,18 @@ const pure @safe @nogc:
if (i + index < 0 || i + index >= tokens.length)
return false;
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

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;
}