Skip to content

Commit

Permalink
Fix IDL parser for older lark (#26145)
Browse files Browse the repository at this point in the history
* Fix Matter IDL parser

* Fix tuple conversion

* Code review updates

---------

Co-authored-by: Andrei Litvin <andreilitvin@google.com>
  • Loading branch information
2 people authored and pull[bot] committed May 26, 2023
1 parent d2aa49e commit 4701241
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions scripts/py_matter_idl/matter_idl/matter_idl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import functools
import logging
from typing import Optional

from lark import Lark
from lark.lexer import Token
Expand Down Expand Up @@ -256,12 +257,12 @@ def struct_field(self, args):
return field

@v_args(meta=True)
def server_cluster(self, meta, _):
def server_cluster(self, meta, unused_args):
self._cluster_start_pos = meta and meta.start_pos
return ClusterSide.SERVER

@v_args(meta=True)
def client_cluster(self, meta, _):
@v_args(meta=True, inline=True)
def client_cluster(self, meta, *unused_args):
self._cluster_start_pos = meta and meta.start_pos
return ClusterSide.CLIENT

Expand All @@ -280,10 +281,13 @@ def command_with_access(self, args):

return init_args

@v_args(meta=True)
def command(self, meta, args):
# NOTE: awkward inline because the order of 'meta, children' vs 'children, meta' was flipped
# between lark versions in https://github.com/lark-parser/lark/pull/993
@v_args(meta=True, inline=True)
def command(self, meta, *args):
# The command takes 4 arguments if no input argument, 5 if input
# argument is provided
args = list(args) # convert from tuple
if len(args) != 5:
args.insert(2, None)

Expand Down Expand Up @@ -513,7 +517,7 @@ def __init__(self, skip_meta: bool):
}
)

def parse(self, file: str, file_name: str = None):
def parse(self, file: str, file_name: Optional[str] = None):
idl = self.transformer.transform(self.parser.parse(file))
idl.parse_file_name = file_name

Expand Down
6 changes: 3 additions & 3 deletions scripts/py_matter_idl/matter_idl/matter_idl_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import enum
from dataclasses import dataclass, field
from typing import List, Optional, Union
from typing import List, Optional, Set, Union

from lark.tree import Meta

Expand All @@ -13,7 +13,7 @@ class ParseMetaData:
column: Optional[int]
start_pos: Optional[int]

def __init__(self, meta: Meta = None, line: int = None, column: int = None, start_pos: int = None):
def __init__(self, meta: Optional[Meta] = None, line: Optional[int] = None, column: Optional[int] = None, start_pos: Optional[int] = None):
if meta:
self.line = meta.line
self.column = meta.column
Expand Down Expand Up @@ -236,7 +236,7 @@ class AttributeInstantiation:
class ServerClusterInstantiation:
name: str
attributes: List[AttributeInstantiation] = field(default_factory=list)
events_emitted: List[str] = field(default_factory=set)
events_emitted: Set[str] = field(default_factory=set)

# Parsing meta data missing only when skip meta data is requested
parse_meta: Optional[ParseMetaData] = field(default=None)
Expand Down

0 comments on commit 4701241

Please sign in to comment.