Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow digit separator after base specifier #20449

Merged
merged 6 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Compilers/CSharp/Portable/Parser/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,18 @@ private void ScanNumericLiteralSingleInteger(ref bool underscoreInWrongPlace, re
{
if (TextWindow.PeekChar() == '_')
{
underscoreInWrongPlace = true;
if (isHex || isBinary)
{
// TODO(leading-digit-separator): check feature flag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't let new TODOs into the master branch.
Blockers should be addressed. Non-blockers should be tracked with a github issue.

}
else
{
underscoreInWrongPlace = true;
}
}

char ch;
var lastCharWasUnderscore = false;
bool lastCharWasUnderscore = false;
while (true)
{
ch = TextWindow.PeekChar();
Expand All @@ -950,9 +957,9 @@ private void ScanNumericLiteralSingleInteger(ref bool underscoreInWrongPlace, re
usedUnderscore = true;
lastCharWasUnderscore = true;
}
else if ((isHex && !SyntaxFacts.IsHexDigit(ch))
|| (isBinary && !SyntaxFacts.IsBinaryDigit(ch))
|| (!isHex && !isBinary && !SyntaxFacts.IsDecDigit(ch)))
else if (!(isHex ? SyntaxFacts.IsHexDigit(ch) :
isBinary ? SyntaxFacts.IsBinaryDigit(ch) :
SyntaxFacts.IsDecDigit(ch)))
{
break;
}
Expand Down
47 changes: 25 additions & 22 deletions src/Compilers/CSharp/Test/Syntax/LexicalAndXml/LexicalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,31 @@ public void TestNumericWithUnderscoresWithoutFeatureFlag()
Assert.Equal(text, token.Text);
}

[Fact]
[Trait("Feature", "Literals")]
public void TestNumericWithLeadingUnderscores()
{
var text = "0x_A";
Copy link
Member

@jcouv jcouv Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test with multiple underscores. @gafter can confirm expected language/spec behavior. #Resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test for trailing underscore: "0x_"


In reply to: 131779530 [](ancestors = 131779530)

var token = LexToken(text, _underscoreOptions);

Assert.NotNull(token);
Assert.Equal(SyntaxKind.NumericLiteralToken, token.Kind());
Assert.Equal(0, token.Errors().Length);
Assert.Equal(0xA, token.Value);
Assert.Equal(text, token.Text);

text = "0b_1";
token = LexToken(text, _binaryUnderscoreOptions);

Assert.NotNull(token);
Assert.Equal(SyntaxKind.NumericLiteralToken, token.Kind());
Assert.Equal(0, token.Errors().Length);
Assert.Equal(1, token.Value);
Assert.Equal(text, token.Text);

// TODO(leading-digit-separator): test feature flag
}

[Fact]
[Trait("Feature", "Literals")]
public void TestNumericWithBadUnderscores()
Expand Down Expand Up @@ -2693,17 +2718,6 @@ public void TestNumericWithBadUnderscores()
Assert.Equal("error CS1013: Invalid number", errors[0].ToString(EnsureEnglishUICulture.PreferredOrNull));
Assert.Equal(text, token.Text);
Copy link
Member

@cston cston Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for 1E+_2, 1E-_2, and 1E_. #Resolved


text = "0x_A";
token = LexToken(text, _underscoreOptions);

Assert.NotNull(token);
Assert.Equal(SyntaxKind.NumericLiteralToken, token.Kind());
errors = token.Errors();
Assert.Equal(1, errors.Length);
Assert.Equal((int)ErrorCode.ERR_InvalidNumber, errors[0].Code);
Assert.Equal("error CS1013: Invalid number", errors[0].ToString(EnsureEnglishUICulture.PreferredOrNull));
Assert.Equal(text, token.Text);

text = "0xA_";
token = LexToken(text, _underscoreOptions);

Expand All @@ -2715,17 +2729,6 @@ public void TestNumericWithBadUnderscores()
Assert.Equal("error CS1013: Invalid number", errors[0].ToString(EnsureEnglishUICulture.PreferredOrNull));
Assert.Equal(text, token.Text);

text = "0b_1";
token = LexToken(text, _binaryUnderscoreOptions);

Assert.NotNull(token);
Assert.Equal(SyntaxKind.NumericLiteralToken, token.Kind());
errors = token.Errors();
Assert.Equal(1, errors.Length);
Assert.Equal((int)ErrorCode.ERR_InvalidNumber, errors[0].Code);
Assert.Equal("error CS1013: Invalid number", errors[0].ToString(EnsureEnglishUICulture.PreferredOrNull));
Assert.Equal(text, token.Text);

text = "0b1_";
token = LexToken(text, _binaryUnderscoreOptions);

Expand Down
15 changes: 12 additions & 3 deletions src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,10 @@ FullWidthRepeat:
IntegerLiteralStart = Here
Base = LiteralBase.Hexadecimal

UnderscoreInWrongPlace = (CanGet(Here) AndAlso Peek(Here) = "_"c)
If (CanGet(Here) AndAlso Peek(Here) = "_"c) Then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary parentheses around entire Boolean expression, here and below.

' TODO(leading-digit-separator): check feature flag
End If

While CanGet(Here)
ch = Peek(Here)
If Not IsHexDigit(ch) AndAlso ch <> "_"c Then
Expand All @@ -1712,7 +1715,10 @@ FullWidthRepeat:
IntegerLiteralStart = Here
Base = LiteralBase.Binary

UnderscoreInWrongPlace = (CanGet(Here) AndAlso Peek(Here) = "_"c)
If (CanGet(Here) AndAlso Peek(Here) = "_"c) Then
' TODO(leading-digit-separator): check feature flag
End If

While CanGet(Here)
ch = Peek(Here)
If Not IsBinaryDigit(ch) AndAlso ch <> "_"c Then
Expand All @@ -1730,7 +1736,10 @@ FullWidthRepeat:
IntegerLiteralStart = Here
Base = LiteralBase.Octal

UnderscoreInWrongPlace = (CanGet(Here) AndAlso Peek(Here) = "_"c)
If (CanGet(Here) AndAlso Peek(Here) = "_"c) Then
' TODO(leading-digit-separator): check feature flag
End If

While CanGet(Here)
ch = Peek(Here)
If Not IsOctalDigit(ch) AndAlso ch <> "_"c Then
Expand Down
13 changes: 7 additions & 6 deletions src/Compilers/VisualBasic/Test/Syntax/Scanner/ScannerTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,13 @@ End If]]>.Value,
Assert.Equal(&H42L, tk.Value)
Assert.Equal(" &H4_2L ", tk.ToFullString())

Str = " &H_1 "
tk = ScanOnce(Str)
Assert.Equal(SyntaxKind.IntegerLiteralToken, tk.Kind)
Assert.Equal(LiteralBase.Hexadecimal, tk.GetBase())
Assert.Equal(&H1, tk.Value)
Assert.Equal(" &H_1 ", tk.ToFullString())

Str = " &H42L &H42& "
Dim tks = ScanAllCheckDw(Str)
Assert.Equal(SyntaxKind.IntegerLiteralToken, tks(0).Kind)
Expand Down Expand Up @@ -1227,12 +1234,6 @@ End If]]>.Value,
Assert.Equal(30035, tk.GetSyntaxErrorsNoTree()(0).Code)
Assert.Equal(0, CInt(tk.Value))

Str = "&H_1"
tk = ScanOnce(Str)
Assert.Equal(SyntaxKind.IntegerLiteralToken, tk.Kind)
Assert.Equal(30035, tk.GetSyntaxErrorsNoTree()(0).Code)
Assert.Equal(0, CInt(tk.Value))

Str = "&H1_"
tk = ScanOnce(Str)
Assert.Equal(SyntaxKind.IntegerLiteralToken, tk.Kind)
Expand Down