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

NumberLiteral works properly when the decimal separator is not a dot #48

Merged
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
18 changes: 17 additions & 1 deletion Irony.Tests/NumberLiteralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,24 @@ public void TestNumber_General() {
Assert.IsTrue(token.Value.ToString() == sbig, "Failed to read big integer value");
}//method

[TestMethod]
public void TestNumber_CommaAsDecimalSeparator()
{
Parser parser; Token token;

var number = new NumberLiteral("number") { DecimalSeparator = ',' };
number.AddPrefix("0", NumberOptions.Octal);
parser = TestHelper.CreateParser(number);
token = parser.ParseInput("123,4");
Assert.AreEqual(123.4, Convert.ToDouble(token.Value), 0.000001, "Failed to read float value");
token = parser.ParseInput("1,2");
Assert.AreEqual(1.2, Convert.ToDouble(token.Value), 0.000001, "Failed to read float value");
token = parser.ParseInput("0,123");
Assert.AreEqual(0.123, Convert.ToDouble(token.Value), 0.000001, "Failed to read float value");
}

//The following "sign" test methods and a fix are contributed by ashmind codeplex user
[TestMethod]
[TestMethod]
public void TestNumber_SignedDoesNotMatchSingleMinus() {
Parser parser; Token token;

Expand Down
4 changes: 2 additions & 2 deletions Irony/Parsing/Terminals/NumberLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected override Token QuickParse(ParsingContext context, ISourceStream source
char current = source.PreviewChar;
//it must be a digit followed by a whitespace or delimiter
if (!char.IsDigit(current)) return null;
if (!Grammar.IsWhitespaceOrDelimiter(source.NextPreviewChar))
if (!Grammar.IsWhitespaceOrDelimiter(source.NextPreviewChar) || source.NextPreviewChar == DecimalSeparator)
return null;
int iValue = current - '0';
object value = null;
Expand All @@ -159,7 +159,7 @@ protected override void InitDetails(ParsingContext context, CompoundTokenDetails
protected override void ReadPrefix(ISourceStream source, CompoundTokenDetails details) {
//check that is not a 0 followed by dot;
//this may happen in Python for number "0.123" - we can mistakenly take "0" as octal prefix
if (source.PreviewChar == '0' && source.NextPreviewChar == '.') return;
if (source.PreviewChar == '0' && source.NextPreviewChar == DecimalSeparator) return;
base.ReadPrefix(source, details);
}//method

Expand Down