Skip to content

Commit

Permalink
Add support for datatype sizes and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Jan 21, 2022
1 parent 33e0440 commit af2e1ef
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
6 changes: 4 additions & 2 deletions scripts/idl/matter_grammar.lark
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct: "struct"i id "{" struct_field* "}"
enum: "enum"i id ":" type "{" enum_entry* "}"
enum: "enum"i id ":" data_type "{" enum_entry* "}"

event: event_priority "event"i id "=" number "{" struct_field* "}"

Expand Down Expand Up @@ -34,9 +34,11 @@ struct_field: member_attribute* field
member_attribute: "optional"i -> optional
| "nullable"i -> nullable

field: type id list_marker? "=" number ";"
field: data_type id list_marker? "=" number ";"
list_marker: "[" "]"

data_type: type ("<" number ">")?

id: ID
type: ID

Expand Down
9 changes: 9 additions & 0 deletions scripts/idl/matter_idl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def type(self, tokens):
raise Error("Unexpected argument counts")
return tokens[0].value

def data_type(self, tokens):
if len(tokens) == 1:
return DataType(name=tokens[0])
# Just a string for data type
elif len(tokens) == 2:
return DataType(name=tokens[0], max_length=tokens[1])
else:
raise Error("Unexpected size for data type")

@v_args(inline=True)
def enum_entry(self, id, number):
return EnumEntry(name=id, code=number)
Expand Down
12 changes: 10 additions & 2 deletions scripts/idl/matter_idl_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import enum

from dataclasses import dataclass, field
from typing import List, Set
from typing import List, Set, Union


class FieldAttribute(enum.Enum):
Expand Down Expand Up @@ -36,6 +36,14 @@ class EndpointContentType(enum.Enum):
CLIENT_BINDING = enum.auto()


@dataclass
class DataType:
name: str

# Applies for strings (char or binary)
max_length: Union[int, None] = None


@dataclass
class Field:
data_type: str
Expand Down Expand Up @@ -67,7 +75,7 @@ def is_global(self):
class Struct:
name: str
fields: List[Field]
tag: StructTag = None
tag: Union[StructTag, None] = None


@dataclass
Expand Down
41 changes: 31 additions & 10 deletions scripts/idl/test_matter_idl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_global_enum(self):
""")

expected = Idl(enums=[
Enum(name='GlobalEnum', base_type='ENUM8',
Enum(name='GlobalEnum', base_type=DataType(name='ENUM8'),
entries=[
EnumEntry(name="kValue1", code=1),
EnumEntry(name="kOther", code=0x12),
Expand All @@ -77,10 +77,10 @@ def test_global_struct(self):
Struct(name='Something',
fields=[
Field(
data_type="CHAR_STRING", code=1, name="astring", ),
Field(data_type="CLUSTER_ID", code=2, name="idlist", is_list=True, attributes=set(
data_type=DataType(name="CHAR_STRING"), code=1, name="astring", ),
Field(data_type=DataType(name="CLUSTER_ID"), code=2, name="idlist", is_list=True, attributes=set(
[FieldAttribute.OPTIONAL])),
Field(data_type="int", code=0x123, name="valueThatIsNullable", attributes=set(
Field(data_type=DataType(name="int"), code=0x123, name="valueThatIsNullable", attributes=set(
[FieldAttribute.NULLABLE])),
])]
)
Expand All @@ -102,13 +102,34 @@ def test_cluster_attribute(self):
code=0x321,
attributes=[
Attribute(tags=set([AttributeTag.READABLE]), definition=Field(
data_type="int8u", code=1, name="roAttr")),
data_type=DataType(name="int8u"), code=1, name="roAttr")),
Attribute(tags=set([AttributeTag.READABLE, AttributeTag.WRITABLE]), definition=Field(
data_type="int32u", code=123, name="rwAttr", is_list=True)),
data_type=DataType(name="int32u"), code=123, name="rwAttr", is_list=True)),
Attribute(tags=set([AttributeTag.GLOBAL, AttributeTag.READABLE, AttributeTag.WRITABLE]), definition=Field(
data_type="int32u", code=124, name="grwAttr", is_list=True)),
data_type=DataType(name="int32u"), code=124, name="grwAttr", is_list=True)),
Attribute(tags=set([AttributeTag.GLOBAL, AttributeTag.READABLE]), definition=Field(
data_type="int32u", code=125, name="groAttr", is_list=True)),
data_type=DataType(name="int32u"), code=125, name="groAttr", is_list=True)),
]
)])
self.assertEqual(actual, expected)

def test_sized_attribute(self):
actual = parseText("""
server cluster MyCluster = 1 {
attribute char_string<11> attr1 = 1;
attribute octet_string<33> attr2[] = 2;
}
""")

expected = Idl(clusters=[
Cluster(side=ClusterSide.SERVER,
name="MyCluster",
code=1,
attributes=[
Attribute(tags=set([AttributeTag.READABLE, AttributeTag.WRITABLE]), definition=Field(
data_type=DataType(name="char_string", max_length=11), code=1, name="attr1")),
Attribute(tags=set([AttributeTag.READABLE, AttributeTag.WRITABLE]), definition=Field(
data_type=DataType(name="octet_string", max_length=33), code=2, name="attr2", is_list=True)),
]
)])
self.assertEqual(actual, expected)
Expand Down Expand Up @@ -158,7 +179,7 @@ def test_cluster_enum(self):
name="WithEnums",
code=0xab,
enums=[
Enum(name="TestEnum", base_type="ENUM16",
Enum(name="TestEnum", base_type=DataType(name="ENUM16"),
entries=[
EnumEntry(name="A", code=0x123),
EnumEntry(name="B", code=0x234),
Expand All @@ -182,7 +203,7 @@ def test_cluster_events(self):
code=0x123,
events=[
Event(priority=EventPriority.CRITICAL, name="StartUp", code=0, fields=[
Field(data_type="INT32U",
Field(data_type=DataType(name="INT32U"),
code=0, name="softwareVersion"),
]),
Event(priority=EventPriority.INFO,
Expand Down

0 comments on commit af2e1ef

Please sign in to comment.