From 62b97aa3b6a9686b2d47b5cc840ca1537a0169a3 Mon Sep 17 00:00:00 2001 From: duqoo <92306417+duqoo@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:05:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Canon.Core/LexicalParser/SemanticToken.cs | 12 +++++++++++- Canon.Tests/LexicalParserTests/NumberTests.cs | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Canon.Core/LexicalParser/SemanticToken.cs b/Canon.Core/LexicalParser/SemanticToken.cs index 358c291..72d8cf3 100644 --- a/Canon.Core/LexicalParser/SemanticToken.cs +++ b/Canon.Core/LexicalParser/SemanticToken.cs @@ -215,8 +215,9 @@ public class NumberSemanticToken : SemanticToken bool hasDecimalPoint = false; bool hasExponent = false; + bool hasMinusSign = false; - while (now != null && (char.IsDigit(now.Value) || now.Value == '.' || now.Value == 'e' || now.Value == 'E')) + while (now != null && (char.IsDigit(now.Value) || now.Value == '.' || now.Value == 'e' || now.Value == 'E' || now.Value == '-' || now.Value == '+')) { if (now.Value == '.') { @@ -236,6 +237,15 @@ public class NumberSemanticToken : SemanticToken hasExponent = true; } + if (now.Value == '-' || now.Value == '+') + { + if (hasMinusSign) + { + break; + } + hasMinusSign = true; + } + buffer.Append(now.Value); now = now.Next; } diff --git a/Canon.Tests/LexicalParserTests/NumberTests.cs b/Canon.Tests/LexicalParserTests/NumberTests.cs index 9e8ab0f..eb2bd94 100644 --- a/Canon.Tests/LexicalParserTests/NumberTests.cs +++ b/Canon.Tests/LexicalParserTests/NumberTests.cs @@ -24,7 +24,7 @@ namespace Canon.Tests.LexicalParserTests [InlineData("1E-7", 1E-7, NumberType.Real)] [InlineData("1E", 0, NumberType.Real, false)] [InlineData("abc", 0, NumberType.Integer, false)] - [InlineData("123abc", 0, NumberType.Integer, false)] + [InlineData("123abc", 123, NumberType.Integer, true)] public void TestParseNumber(string input, double expected, NumberType expectedNumberType, bool expectedResult = true) { LinkedList content = Utils.GetLinkedList(input);