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

Parse long.MinValue in Design Script #11122

Merged
merged 2 commits into from
Sep 16, 2020
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
17 changes: 10 additions & 7 deletions src/Engine/ProtoCore/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,13 +2253,14 @@ void Associative_Level(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
}
Expect(2);
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.AssociativeAST.IntNode(sign*value);
node = new ProtoCore.AST.AssociativeAST.IntNode(value);
}
else
{
errors.SemErr(t.line, t.col, String.Format(Resources.kInvalidListLevelName, t.val));
errors.SemErr(t.line, t.col, String.Format(Resources.kInvalidListLevelName, tokenWithSign));
}

NodeUtils.SetNodeLocation(node, t);
Expand All @@ -2283,9 +2284,10 @@ void Associative_Number(out ProtoCore.AST.AssociativeAST.AssociativeNode node) {
if (la.kind == 2) {
Get();
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.AssociativeAST.IntNode(value * sign);
node = new ProtoCore.AST.AssociativeAST.IntNode(value);
}
else
{
Expand Down Expand Up @@ -3742,9 +3744,10 @@ void Imperative_num(out ProtoCore.AST.ImperativeAST.ImperativeNode node) {
if (la.kind == 2) {
Get();
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.ImperativeAST.IntNode(value * sign);
node = new ProtoCore.AST.ImperativeAST.IntNode(value);
}
else
{
Expand Down
12 changes: 7 additions & 5 deletions src/Engine/ProtoCore/Parser/atg/Associative.atg
Original file line number Diff line number Diff line change
Expand Up @@ -1361,13 +1361,14 @@ Associative_Level<out ProtoCore.AST.AssociativeAST.AssociativeNode node>
] number
(.
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.AssociativeAST.IntNode(sign*value);
node = new ProtoCore.AST.AssociativeAST.IntNode(value);
}
else
{
errors.SemErr(t.line, t.col, String.Format(Resources.kInvalidListLevelName, t.val));
errors.SemErr(t.line, t.col, String.Format(Resources.kInvalidListLevelName, tokenWithSign));
}

NodeUtils.SetNodeLocation(node, t);
Expand Down Expand Up @@ -1395,9 +1396,10 @@ Associative_Number<out ProtoCore.AST.AssociativeAST.AssociativeNode node>
number
(.
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.AssociativeAST.IntNode(value * sign);
node = new ProtoCore.AST.AssociativeAST.IntNode(value);
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/Engine/ProtoCore/Parser/atg/Imperative.atg
Original file line number Diff line number Diff line change
Expand Up @@ -1135,9 +1135,10 @@ Imperative_num<out ProtoCore.AST.ImperativeAST.ImperativeNode node>
number
(.
Int64 value;
if (Int64.TryParse(t.val, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
var tokenWithSign = sign == -1 ? "-" + t.val : t.val;
if (Int64.TryParse(tokenWithSign, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value))
{
node = new ProtoCore.AST.ImperativeAST.IntNode(value * sign);
node = new ProtoCore.AST.ImperativeAST.IntNode(value);
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions test/Engine/ProtoTest/ParserTest/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ParserTest : ProtoTestBase
[Test]
public void TestInvalidEscape1()
{
string code = @"x = ""\\""";
string code = @"x = ""\"";";
Assert.Throws(typeof(ProtoCore.Exceptions.CompileErrorsOccured), () =>
{
thisTest.RunScriptSource(code);
Expand All @@ -21,12 +21,19 @@ public void TestInvalidEscape1()
[Test]
public void TestInvalidEscape2()
{
string code = @"x = ""\\X""";
string code = @"x = ""\X"";";
Assert.Throws(typeof(ProtoCore.Exceptions.CompileErrorsOccured), () =>
{
thisTest.RunScriptSource(code);
});
}

[Test]
public void CanParseMinLongValue()
{
var code = "x = -9223372036854775808;";
thisTest.RunScriptSource(code);
thisTest.Verify("x", long.MinValue);
}
}
}
Expand Down