diff --git a/src/AuDaGe/iec.py b/src/AuDaGe/iec.py index f75ac1f..8a264af 100644 --- a/src/AuDaGe/iec.py +++ b/src/AuDaGe/iec.py @@ -12,7 +12,7 @@ import re import warnings from collections.abc import Callable from enum import Enum, EnumType, IntEnum -from math import ceil +from math import ceil, prod from pandas import NA, Series @@ -23,8 +23,11 @@ class DataUnit(IntEnum): """The length in bits of various data units.""" BIT = 1 + NIBBLE = 4 BYTE = 8 WORD = 16 + DWORD = 32 + QWORD = 64 class IECType: @@ -189,7 +192,10 @@ class IECTypes(Enum, metaclass=AliasEnumType): class IECArray(IECType): """An IEC 61131-3 array data type variable.""" - ARRAY_SPEC_PATTERN = r"^\s*arr(?:ay)? *\[(\d+)\.{2,3}(\d+)\] *(?:of +)?(\w+)\s*$" + BOUNDS_PATTERN = r"\d+" + ARRAY_SPEC_PATTERN = ( + r"^\s*arr(?:ay)?\s*\[((?:\d+\s*\.{2,3}\s*\d+\s*,?\s*)+)\]\s*(?:of\s+)?(\w+)\s*$" + ) def __init__(self, spec: str, *args, **kwargs): """Create an instance of IECArray based on an IEC 61131-3 array @@ -228,16 +234,18 @@ class IECArray(IECType): if round_function is None: round_function = self.round # Validate the array specification. - m = re.match(self.ARRAY_SPEC_PATTERN, self.spec, re.IGNORECASE) + match = re.match(self.ARRAY_SPEC_PATTERN, self.spec, re.IGNORECASE) + bounds = re.findall(self.BOUNDS_PATTERN, match.groups()[0], re.IGNORECASE) # Parse the array specification. - start_index = int(m.groups()[0]) - end_index = int(m.groups()[1]) - data_type = m.groups()[2].upper() + data_type = match.groups()[-1].upper() # Calculate the length of the array in bits. length_bits = ( - (end_index - start_index + 1) + prod( + (int(ubound) - int(lbound) + 1) + for (lbound, ubound) in zip(bounds[::2], bounds[1::2]) + ) * IECTypes[data_type].value.length_bits - ) # fmt: skip + ) if length_bits % unit != 0: warnings.warn( f"Array length is not divisible by the selected unit. The " @@ -260,18 +268,21 @@ class IECArray(IECType): """ if array_spec is None: array_spec = self.spec - m = re.match(self.ARRAY_SPEC_PATTERN, array_spec, re.IGNORECASE) + match = re.match(self.ARRAY_SPEC_PATTERN, array_spec, re.IGNORECASE) # Check whether the regex matches at all. - if m is None: + if match is None: return False + bounds = re.findall(self.BOUNDS_PATTERN, match.groups()[0], re.IGNORECASE) # Check whether the data type is valid. - if not m.groups()[2].upper() in IECTypes: + if not match.groups()[-1].upper() in IECTypes: return False try: - # Check that the upper bound is greater than the lower bound. - if int(m.groups()[0]) > int(m.groups()[1]): - return False - # ValueError is raised if the array bounds are not integers. + # Check that each dimension's upper bound is greater than or equal + # to its lower bound. + for lbound, ubound in zip(bounds[::2], bounds[1::2]): + if int(lbound) > int(ubound): + return False + # ValueError is raised if the array bounds are not coercible to integers. except ValueError: return False # Return True if all the tests succeeded. @@ -318,6 +329,7 @@ class IECString(IECType): round_function = self.round m = re.match(self.STRING_SPEC_PATTERN, self.spec, re.IGNORECASE) try: + # 1 ASCII character == 1 byte. length_bits = int(m.groups()[0]) * DataUnit.BYTE except (AttributeError, ValueError): raise ValueError("The provided string specification is invalid.") diff --git a/tests/test_iec.py b/tests/test_iec.py index ac2f38a..d9b82ec 100644 --- a/tests/test_iec.py +++ b/tests/test_iec.py @@ -24,13 +24,13 @@ class TestIEC(unittest.TestCase): ("CHAR", DataUnit.BIT, 8), ("INT", DataUnit.BIT, 16), ("BOOL", DataUnit.WORD, 1), - ("ARRAY [0..10] OF INT", DataUnit.WORD, 11), + ("ARRAY [1..10, 0..7] OF INT", DataUnit.WORD, 80), ("STRING[10]", DataUnit.WORD, 5), ("UNKNOWN_UDT", DataUnit.WORD, -1), ] for data_type in data_types: self.assertEqual( - get_data_length(data_type[0], data_type[1]), data_type[2], data_type + get_data_length(data_type[0], data_type[1]), data_type[2], data_type[0] ) def test_iec_definitions(self): @@ -43,7 +43,10 @@ class TestIECArray(unittest.TestCase): def test_array_length(self): good_array_specs = [ ("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4), + ("ARRAY [0..0] OF REAL", DataUnit.WORD, ceil, 2), + ("ARRAY [0..3, 0..3] OF INT", DataUnit.WORD, ceil, 16), ("array[15..20] of bool", DataUnit.BIT, ceil, 6), + ("ARRAY [0..1, 0..2, 0..3, 0..4] OF INT", DataUnit.WORD, ceil, 120), ] warning_array_specs = [ ("array[15..20] of bool", DataUnit.WORD, ceil, 1), @@ -51,12 +54,10 @@ class TestIECArray(unittest.TestCase): ] with warnings.catch_warnings(): warnings.simplefilter("ignore") - self.assertTrue( - all( - IECArray(spec[0]).get_length(spec[1], spec[2]) == spec[3] - for spec in good_array_specs + warning_array_specs + for spec in good_array_specs + warning_array_specs: + self.assertEqual( + IECArray(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0] ) - ) for spec in warning_array_specs: self.assertWarns( RuntimeWarning, IECArray(spec[0]).get_length, spec[1], spec[2] @@ -77,7 +78,8 @@ class TestIECArray(unittest.TestCase): "array [1..7] of float", # Invalid data type "array [5..1] of dint", # Lower bound greater than upper bound ) - self.assertTrue(all(IECArray(spec).is_valid() for spec in valid_array_specs)) + for spec in valid_array_specs: + self.assertTrue(IECArray(spec).is_valid(), spec) for spec in invalid_array_specs: self.assertRaises(ValueError, IECArray, spec) @@ -100,12 +102,10 @@ class TestIECString(unittest.TestCase): ] with warnings.catch_warnings(): warnings.simplefilter("ignore") - self.assertTrue( - all( - IECString(spec[0]).get_length(spec[1], spec[2]) == spec[3] - for spec in good_string_specs + warning_string_specs + for spec in good_string_specs + warning_string_specs: + self.assertEqual( + IECString(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0] ) - ) for spec in warning_string_specs: self.assertWarns( RuntimeWarning, IECString(spec[0]).get_length, spec[1], spec[2]