Skip to content

Commit c58e54f

Browse files
bbasilestefan-koch-sociomantic
authored andcommitted
Handle do as contract header and handle body as identifier (#360)
Handle `do` as contract header and handle `body` as identifier merged-on-behalf-of: Brian Schott <Hackerpilot@users.noreply.github.com> * libdparse 687c0ca(687c0ca)...086cf06(086cf06) (28 commits) > Merge pull request #242 from BBasile/issue-241 > Merge pull request #239 from kubo39/add-isIntegerLiteral > Merge pull request #237 from BBasile/parse-virt-helpers > Merge pull request #236 from JinShil/patch-2 > Merge pull request #235 from BBasile/upd-stdx-alloc (...)
1 parent c2aea82 commit c58e54f

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

src/dfmt/formatter.d

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ private:
283283
{
284284
formatKeyword();
285285
}
286+
else if (current.text == "body" && peekBackIsFunctionDeclarationEnding())
287+
{
288+
formatKeyword();
289+
}
286290
else if (isBasicType(current.type))
287291
{
288292
writeToken();
@@ -591,8 +595,10 @@ private:
591595
indents.pop();
592596

593597
if (parenDepth == 0 && (peekIs(tok!"is") || peekIs(tok!"in")
594-
|| peekIs(tok!"out") || peekIs(tok!"body")))
598+
|| peekIs(tok!"out") || peekIs(tok!"do") || peekIsBody))
599+
{
595600
writeToken();
601+
}
596602
else if (peekIsLiteralOrIdent() || peekIsBasicType())
597603
{
598604
writeToken();
@@ -947,9 +953,11 @@ private:
947953
if (!currentIs(tok!"{") && !currentIs(tok!";"))
948954
write(" ");
949955
}
950-
else if (!currentIs(tok!"{") && !currentIs(tok!";")
951-
&& !currentIs(tok!"in") && !currentIs(tok!"out") && !currentIs(tok!"body"))
956+
else if (!currentIs(tok!"{") && !currentIs(tok!";") && !currentIs(tok!"in") &&
957+
!currentIs(tok!"out") && !currentIs(tok!"do") && current.text != "body")
958+
{
952959
newline();
960+
}
953961
else if (currentIs(tok!"{") && indents.topAre(tok!"static", tok!"if"))
954962
{
955963
// Hacks to format braced vs non-braced static if declarations.
@@ -1038,7 +1046,12 @@ private:
10381046
if (!currentIs(tok!"{"))
10391047
newline();
10401048
break;
1041-
case tok!"body":
1049+
case tok!"identifier":
1050+
if (current.text == "body")
1051+
goto case tok!"do";
1052+
else
1053+
goto default;
1054+
case tok!"do":
10421055
if (!peekBackIs(tok!"}"))
10431056
newline();
10441057
writeToken();
@@ -1827,6 +1840,18 @@ const pure @safe @nogc:
18271840
return peekImplementation(tokenType, 1, ignoreComments);
18281841
}
18291842

1843+
bool peekIsBody() nothrow
1844+
{
1845+
return index + 1 < tokens.length && tokens[index + 1].text == "body";
1846+
}
1847+
1848+
bool peekBackIsFunctionDeclarationEnding() nothrow
1849+
{
1850+
return peekBackIsOneOf(false, tok!")", tok!"const", tok!"immutable",
1851+
tok!"inout", tok!"shared", tok!"@", tok!"pure", tok!"nothrow",
1852+
tok!"return", tok!"scope");
1853+
}
1854+
18301855
bool peekBackIsSlashSlash() nothrow
18311856
{
18321857
return index > 0 && tokens[index - 1].type == tok!"comment"

src/dfmt/tokens.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private string generateFixedLengthCases()
217217
a => format(`case tok!"%s": return %d + 1;`, a, a.length)).join("\n\t");
218218

219219
string[] identifierTokens = [
220-
"abstract", "alias", "align", "asm", "assert", "auto", "body", "bool",
220+
"abstract", "alias", "align", "asm", "assert", "auto", "bool",
221221
"break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", "char", "class",
222222
"const", "continue", "creal", "dchar", "debug", "default", "delegate", "delete", "deprecated",
223223
"do", "double", "else", "enum", "export", "extern", "false", "final", "finally", "float",

tests/allman/do_body.d.ref

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import character.body;
2+
3+
void body() @nogc
4+
in
5+
{
6+
}
7+
body
8+
{
9+
body = null;
10+
}
11+
12+
void body()
13+
in
14+
{
15+
}
16+
do
17+
{
18+
body = null;
19+
}

tests/do_body.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import character.body;
2+
3+
void body() @nogc
4+
in{} body{body = null;}
5+
6+
void body()
7+
in{} do{ body = null;}

tests/otbs/do_body.d.ref

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import character.body;
2+
3+
void body() @nogc
4+
in {
5+
}
6+
body {
7+
body = null;
8+
}
9+
10+
void body()
11+
in {
12+
}
13+
do {
14+
body = null;
15+
}

0 commit comments

Comments
 (0)