From 8825b27a2143f95ea7b708620d5c92437fd54036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20N=C3=A9meti?= Date: Thu, 26 Jan 2023 17:36:30 +0100 Subject: [PATCH 1/2] NumberLiteral test for the case when comma is used as decimal separator instead of a dot --- Irony.Tests/NumberLiteralTests.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Irony.Tests/NumberLiteralTests.cs b/Irony.Tests/NumberLiteralTests.cs index 97ec647..a794b23 100644 --- a/Irony.Tests/NumberLiteralTests.cs +++ b/Irony.Tests/NumberLiteralTests.cs @@ -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; From a52670b4b63ccfd078e5b4b1ad0e880193626453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20N=C3=A9meti?= Date: Thu, 26 Jan 2023 17:45:23 +0100 Subject: [PATCH 2/2] NumberLiteral: fix for the case when the decimal separator is not a dot - QuickParse: fix the case when the NumberLiteral begins with a single digit immediately followed by the decimal separator (which is not a dot) - ReadPrefix: fix the case when the NumberLiteral begins with the '0' digit immediately followed by the decimal separator (which is not a dot), and the "0" string is being registered as prefix for octal numbers --- Irony/Parsing/Terminals/NumberLiteral.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Irony/Parsing/Terminals/NumberLiteral.cs b/Irony/Parsing/Terminals/NumberLiteral.cs index 3dfa53d..f79bcae 100644 --- a/Irony/Parsing/Terminals/NumberLiteral.cs +++ b/Irony/Parsing/Terminals/NumberLiteral.cs @@ -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; @@ -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