Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 43 additions & 16 deletions src/System.Management.Automation/engine/parser/tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,17 +1460,28 @@ private char Backtick(char c, out char surrogateCharacter)

switch (c)
{
case '0': return '\0';
case 'a': return '\a';
case 'b': return '\b';
case 'e': return '\u001b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'u': return ScanUnicodeEscape(out surrogateCharacter);
case 'v': return '\v';
default: return c;
case '0':
return '\0';
case 'a':
return '\a';
case 'b':
return '\b';
case 'e':
return '\u001b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'u':
return ScanUnicodeEscape(out surrogateCharacter);
case 'v':
return '\v';
default:
return c;
}
}

Expand Down Expand Up @@ -3557,8 +3568,9 @@ private static bool TryGetNumberValue(
{
try
{
NumberStyles style = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint |
NumberStyles.AllowExponent;
NumberStyles style = NumberStyles.AllowLeadingSign
| NumberStyles.AllowDecimalPoint
| NumberStyles.AllowExponent;

if (real)
{
Expand Down Expand Up @@ -3685,9 +3697,21 @@ private static bool TryGetNumberValue(
}

// If we're expecting a sign bit, remove the leading 0 added in ScanNumberHelper
if (!suffix.HasFlag(NumberSuffixFlags.Unsigned) && ((strNum.Length - 1) & 7) == 0)
if (!suffix.HasFlag(NumberSuffixFlags.Unsigned))
{
strNum = strNum.Slice(1);
var expectedLength = suffix switch
{
NumberSuffixFlags.SignedByte => 2,
NumberSuffixFlags.Short => 4,
NumberSuffixFlags.Long => 16,
// No suffix flag can mean int or long depending on input string length
_ => strNum.Length < 16 ? 8 : 16
};

if (strNum.Length == expectedLength + 1)
{
strNum = strNum.Slice(1);
}
}

style = NumberStyles.AllowHexSpecifier;
Expand Down Expand Up @@ -4988,7 +5012,10 @@ internal Token NextToken()
_currentIndex = _tokenStart;
c = GetChar();

if (strNum == null) { return ScanGenericToken(c); }
if (strNum == null)
{
return ScanGenericToken(c);
}
}

return NewToken(TokenKind.Exclaim);
Expand Down
9 changes: 8 additions & 1 deletion test/powershell/Language/Parser/Parser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ foo``u{2195}abc
if ( $IsLinux -or $IsMacOS ) {
# because we execute on *nix based on executable bit, and the file name doesn't matter
# so we can use the same filename as for windows, just make sure it's executable with chmod
"#!/bin/sh`necho ""Hello World""" | out-file -encoding ASCII $shellfile
"#!/bin/sh`necho ""Hello World""" | Out-File -encoding ASCII $shellfile
/bin/chmod +x $shellfile
}
else {
Expand Down Expand Up @@ -931,6 +931,7 @@ foo``u{2195}abc
@{ Script = "0x0y"; ExpectedValue = "0"; ExpectedType = [sbyte] }
@{ Script = "0x41y"; ExpectedValue = "65"; ExpectedType = [sbyte] }
@{ Script = "-0x41y"; ExpectedValue = "-65"; ExpectedType = [sbyte] }
@{ Script = "0xFFy"; ExpectedValue = "-1"; ExpectedType = [sbyte] }
#Binary
@{ Script = "0b0y"; ExpectedValue = "0"; ExpectedType = [sbyte] }
@{ Script = "0b10y"; ExpectedValue = "2"; ExpectedType = [sbyte] }
Expand All @@ -957,6 +958,7 @@ foo``u{2195}abc
@{ Script = "0x0s"; ExpectedValue = "0"; ExpectedType = [short] }
@{ Script = "0x41s"; ExpectedValue = "65"; ExpectedType = [short] }
@{ Script = "-0x41s"; ExpectedValue = "-65"; ExpectedType = [short] }
@{ Script = "0xFFFFs"; ExpectedValue = "-1"; ExpectedType = [short] }
#Binary
@{ Script = "0b0s"; ExpectedValue = "0"; ExpectedType = [short] }
@{ Script = "0b10s"; ExpectedValue = "2"; ExpectedType = [short] }
Expand Down Expand Up @@ -985,6 +987,7 @@ foo``u{2195}abc
@{ Script = "0x0l"; ExpectedValue = "0"; ExpectedType = [long] }
@{ Script = "0x41l"; ExpectedValue = "65"; ExpectedType = [long] }
@{ Script = "-0x41l"; ExpectedValue = "-65"; ExpectedType = [long] }
@{ Script = "0xFFFFFFFFFFFFFFFFl"; ExpectedValue = "-1"; ExpectedType = [long] }
#Binary
@{ Script = "0b0l"; ExpectedValue = "0"; ExpectedType = [long] }
@{ Script = "0b10l"; ExpectedValue = "2"; ExpectedType = [long] }
Expand Down Expand Up @@ -1078,6 +1081,7 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0uy"; ExpectedValue = "0"; ExpectedType = [byte] }
@{ Script = "0x41uy"; ExpectedValue = "65"; ExpectedType = [byte] }
@{ Script = "0xFFuy"; ExpectedValue = [byte]::MaxValue; ExpectedType = [byte] }
#Binary
@{ Script = "0b0uy"; ExpectedValue = "0"; ExpectedType = [byte] }
@{ Script = "0b10uy"; ExpectedValue = "2"; ExpectedType = [byte] }
Expand All @@ -1098,6 +1102,8 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0us"; ExpectedValue = "0"; ExpectedType = [ushort] }
@{ Script = "0x41us"; ExpectedValue = "65"; ExpectedType = [ushort] }
@{ Script = "0x41us"; ExpectedValue = "65"; ExpectedType = [ushort] }
@{ Script = "0xFFFFus"; ExpectedValue = [ushort]::MaxValue; ExpectedType = [ushort] }
#Binary
@{ Script = "0b0us"; ExpectedValue = "0"; ExpectedType = [ushort] }
@{ Script = "0b10us"; ExpectedValue = "2"; ExpectedType = [ushort] }
Expand All @@ -1119,6 +1125,7 @@ foo``u{2195}abc
#Hexadecimal
@{ Script = "0x0ul"; ExpectedValue = "0"; ExpectedType = [ulong] }
@{ Script = "0x41ul"; ExpectedValue = "65"; ExpectedType = [ulong] }
@{ Script = "0xFFFFFFFFFFFFFFFFul"; ExpectedValue = [ulong]::MaxValue; ExpectedType = [ulong] }
#Binary
@{ Script = "0b0ul"; ExpectedValue = "0"; ExpectedType = [ulong] }
@{ Script = "0b10ul"; ExpectedValue = "2"; ExpectedType = [ulong] }
Expand Down