Add hasCurrent to check if a token is available

This commit is contained in:
Jan Jurzitza 2020-04-02 13:38:02 +02:00
parent 1964f807cf
commit 7b955c18d1
1 changed files with 25 additions and 19 deletions

View File

@ -127,7 +127,7 @@ struct TokenFormatter(OutputRange)
/// Runs the formatting process /// Runs the formatting process
void format() void format()
{ {
while (index < tokens.length) while (hasCurrent)
formatStep(); formatStep();
} }
@ -198,7 +198,7 @@ private:
{ {
import std.range : assumeSorted; import std.range : assumeSorted;
assert(index < tokens.length); assert(hasCurrent);
if (currentIs(tok!"comment")) if (currentIs(tok!"comment"))
{ {
formatComment(); formatComment();
@ -207,7 +207,7 @@ private:
|| isNumberLiteral(current.type) || currentIs(tok!"characterLiteral")) || isNumberLiteral(current.type) || currentIs(tok!"characterLiteral"))
{ {
writeToken(); writeToken();
if (index < tokens.length) if (hasCurrent)
{ {
immutable t = tokens[index].type; immutable t = tokens[index].type;
if (t == tok!"identifier" || isStringLiteral(t) if (t == tok!"identifier" || isStringLiteral(t)
@ -249,7 +249,7 @@ private:
{ {
writeToken(); writeToken();
write(" "); write(" ");
while (index < tokens.length) while (hasCurrent)
{ {
if (currentIs(tok!"(")) if (currentIs(tok!"("))
formatLeftParenOrBracket(); formatLeftParenOrBracket();
@ -281,14 +281,14 @@ private:
else if (currentIs(tok!"asm")) else if (currentIs(tok!"asm"))
{ {
formatKeyword(); formatKeyword();
while (index < tokens.length && !currentIs(tok!"{")) while (hasCurrent && !currentIs(tok!"{"))
formatStep(); formatStep();
if (index < tokens.length) if (hasCurrent)
{ {
int depth = 1; int depth = 1;
formatStep(); formatStep();
inAsm = true; inAsm = true;
while (index < tokens.length && depth > 0) while (hasCurrent && depth > 0)
{ {
if (currentIs(tok!"{")) if (currentIs(tok!"{"))
++depth; ++depth;
@ -335,7 +335,7 @@ private:
{ {
writeToken(); writeToken();
//dfmt off //dfmt off
if (index < tokens.length && ( currentIs(tok!"identifier") if (hasCurrent && ( currentIs(tok!"identifier")
|| ( index > 1 && config.dfmt_space_before_function_parameters || ( index > 1 && config.dfmt_space_before_function_parameters
&& ( isBasicType(peekBack(2).type) && ( isBasicType(peekBack(2).type)
|| peekBack2Is(tok!"identifier") || peekBack2Is(tok!"identifier")
@ -480,9 +480,9 @@ private:
newline(); newline();
justAddedExtraNewline = j; justAddedExtraNewline = j;
} }
else if (index < tokens.length) else if (hasCurrent)
{ {
if (index < tokens.length && prevTokenEndLine == tokens[index].line) if (prevTokenEndLine == tokens[index].line)
{ {
if (currentIs(tok!"}")) if (currentIs(tok!"}"))
{ {
@ -517,7 +517,7 @@ private:
return; return;
} }
write(" "); write(" ");
while (index < tokens.length) while (hasCurrent)
{ {
if (currentIs(tok!";")) if (currentIs(tok!";"))
{ {
@ -654,7 +654,7 @@ private:
detail.breakEveryItem = astInformation.assocArrayStartLocations.canFindIndex(tokens[index - 1].index); detail.breakEveryItem = astInformation.assocArrayStartLocations.canFindIndex(tokens[index - 1].index);
// array of (possibly associative) array, let's put each item on its own line // array of (possibly associative) array, let's put each item on its own line
if (!detail.breakEveryItem && index < tokens.length && current == tok!"[") if (!detail.breakEveryItem && currentIs(tok!"["))
detail.breakEveryItem = true; detail.breakEveryItem = true;
// the '[' is immediately followed by an item instead of a newline here so // the '[' is immediately followed by an item instead of a newline here so
@ -755,13 +755,13 @@ private:
if (tokens[index].type == tok!"{") if (tokens[index].type == tok!"{")
return; return;
if (index < tokens.length && tokens[index - 1].line < tokens[index].line if (hasCurrent && tokens[index - 1].line < tokens[index].line
&& astInformation.atAttributeStartLocations.canFindIndex(atIndex)) && astInformation.atAttributeStartLocations.canFindIndex(atIndex))
newline(); newline();
else else
write(" "); write(" ");
} }
else if (index < tokens.length && (currentIs(tok!"@") else if (hasCurrent && (currentIs(tok!"@")
|| isBasicType(tokens[index].type) || isBasicType(tokens[index].type)
|| currentIs(tok!"extern") || currentIs(tok!"extern")
|| currentIs(tok!"identifier")) || currentIs(tok!"identifier"))
@ -1322,7 +1322,7 @@ private:
bool currentIsIndentedTemplateConstraint() bool currentIsIndentedTemplateConstraint()
{ {
return index < tokens.length return hasCurrent
&& astInformation.constraintLocations.canFindIndex(current.index) && astInformation.constraintLocations.canFindIndex(current.index)
&& (config.dfmt_template_constraint_style == TemplateConstraintStyle.always_newline && (config.dfmt_template_constraint_style == TemplateConstraintStyle.always_newline
|| config.dfmt_template_constraint_style == TemplateConstraintStyle.always_newline_indent || config.dfmt_template_constraint_style == TemplateConstraintStyle.always_newline_indent
@ -1654,7 +1654,7 @@ private:
if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1])) if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1]))
return; return;
immutable bool hasCurrent = index < tokens.length; immutable bool hasCurrent = this.hasCurrent;
if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent && tokens[index].type == tok!"}" if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent && tokens[index].type == tok!"}"
&& !assumeSorted(astInformation.funLitEndLocations).equalRange( && !assumeSorted(astInformation.funLitEndLocations).equalRange(
@ -1868,7 +1868,8 @@ private:
else else
formatStep(); formatStep();
} }
while (index < tokens.length && parenDepth > 0); // TODO: obviously getting stuck here?
while (hasCurrent && parenDepth > 0);
if (indents.topIs(tok!"!")) if (indents.topIs(tok!"!"))
indents.pop(); indents.pop();
parenDepth = depth; parenDepth = depth;
@ -1980,10 +1981,15 @@ const pure @safe @nogc:
return tokenLength(tokens[i]); return tokenLength(tokens[i]);
} }
bool hasCurrent() nothrow const
{
return index < tokens.length;
}
ref current() nothrow ref current() nothrow
in in
{ {
assert(index < tokens.length); assert(hasCurrent);
} }
do do
{ {
@ -2145,7 +2151,7 @@ const pure @safe @nogc:
bool currentIs(IdType tokenType) nothrow bool currentIs(IdType tokenType) nothrow
{ {
return index < tokens.length && tokens[index].type == tokenType; return hasCurrent && tokens[index].type == tokenType;
} }
bool onNextLine() @nogc nothrow pure @safe bool onNextLine() @nogc nothrow pure @safe