From dc65f2c81943235eb0e4c2dc4965f75a57fb2844 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Fri, 8 Jul 2022 21:59:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(more=5Fcommand=5Fnodes):=20=F0=9F=90=9B=20f?= =?UTF-8?q?ix=20parse=20of=20FloatsArgument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../more_command_nodes/__init__.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/more_command_nodes/more_command_nodes/__init__.py b/more_command_nodes/more_command_nodes/__init__.py index 78c1085..3946e7c 100644 --- a/more_command_nodes/more_command_nodes/__init__.py +++ b/more_command_nodes/more_command_nodes/__init__.py @@ -13,13 +13,13 @@ class IllegalFloat(CommandSyntaxError): - def __init__(self, char_read: int): - super().__init__('Invalid Point', char_read) + def __init__(self, char_read: str): + super().__init__('Invalid Float', char_read) class IncompleteFloat(CommandSyntaxError): - def __init__(self, char_read: int): - super().__init__('Incomplete Point', char_read) + def __init__(self, char_read: str): + super().__init__('Incomplete Float', char_read) class FloatsArgument(ArgumentNode): @@ -32,17 +32,15 @@ def __init__(self, name: str, number: int): self.__number = number def parse(self, text: str) -> ParseResult: - total_read = 0 - coords = [] - for i in range(self.__number): - value, read = command_builder_util.get_float(text[total_read:]) - if read == 0: - raise IncompleteFloat(total_read) - total_read += read - if value is None: - raise IllegalFloat(total_read) - coords.append(value) - return ParseResult(coords, total_read) + try: + texts = text.split()[:self.__number] + coords = list(map(float, texts)) + if len(coords) < self.__number: + raise IncompleteFloat(text) + except ValueError: + raise IllegalFloat(text) + else: + return ParseResult(coords, len(' '.join(texts))) class Position(FloatsArgument):