From 647bb6daa9df1e67fe3133ea831573c551e4786c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= Date: Mon, 10 Sep 2018 14:15:14 +0200 Subject: [PATCH] Support for DIP1009 (new contracts syntax), #375 (#376) Support for DIP1009 (new contracts syntax), #375 merged-on-behalf-of: BBasile --- dub.json | 2 +- libdparse | 2 +- src/dfmt/formatter.d | 19 +++++++++++++++---- tests/allman/dip1009.d.ref | 21 +++++++++++++++++++++ tests/dip1009.d | 15 +++++++++++++++ tests/otbs/dip1009.d.ref | 17 +++++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 tests/allman/dip1009.d.ref create mode 100644 tests/dip1009.d create mode 100644 tests/otbs/dip1009.d.ref diff --git a/dub.json b/dub.json index 39ee2c0..e8394d1 100644 --- a/dub.json +++ b/dub.json @@ -4,7 +4,7 @@ "targetType": "autodetect", "license": "BSL-1.0", "dependencies": { - "libdparse": "~>0.8.6" + "libdparse": "~>0.9.7" }, "targetPath" : "bin/", "targetName" : "dfmt", diff --git a/libdparse b/libdparse index 086cf06..7ca3cb8 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit 086cf06051bb1f33c94891ba6c39a57f164ee296 +Subproject commit 7ca3cb87b695a1d6b195ad7730c2094d07f22933 diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index e78a132..e39ee26 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -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 diff --git a/tests/allman/dip1009.d.ref b/tests/allman/dip1009.d.ref new file mode 100644 index 0000000..1b65b6b --- /dev/null +++ b/tests/allman/dip1009.d.ref @@ -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; +} diff --git a/tests/dip1009.d b/tests/dip1009.d new file mode 100644 index 0000000..e77cf1f --- /dev/null +++ b/tests/dip1009.d @@ -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; +} diff --git a/tests/otbs/dip1009.d.ref b/tests/otbs/dip1009.d.ref new file mode 100644 index 0000000..e65b139 --- /dev/null +++ b/tests/otbs/dip1009.d.ref @@ -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; +}