Skip to content

Commit

Permalink
FIX: TYPE/END_TYPE only supports single declaration
Browse files Browse the repository at this point in the history
While documentation seems to indicate otherwise, but in the end the goal
is "parse what Beckhoff parses"
  • Loading branch information
klauer committed Jan 14, 2022
1 parent ead47e9 commit c2b209d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
18 changes: 8 additions & 10 deletions blark/iec.lark
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,14 @@ REFERENCE_TO: /REFERENCE\s*TO/i
| subrange_type_name
| enumerated_type_name

data_type_declaration: "TYPE"i _type_declaration* "END_TYPE"i ";"*

_type_declaration: array_type_declaration ";"+
| structure_type_declaration ";"*
| string_type_declaration ";"+
| _single_element_type_declaration ";"+

_single_element_type_declaration: simple_type_declaration
| subrange_type_declaration
| enumerated_type_declaration
data_type_declaration: "TYPE"i [ _type_declaration ] ";"* "END_TYPE"i ";"*

_type_declaration: array_type_declaration
| structure_type_declaration
| string_type_declaration
| simple_type_declaration
| subrange_type_declaration
| enumerated_type_declaration

simple_type_declaration: simple_type_name [ extends ] ":" simple_spec_init

Expand Down
10 changes: 5 additions & 5 deletions blark/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def from_function_block(

@dataclass
class DataTypeSummary(Summary):
"""Summary representation of a single function block."""
"""Summary representation of a single data type."""
name: str
source_code: str
type: str
Expand Down Expand Up @@ -402,12 +402,12 @@ def get_code_by_meta(meta: Optional[tf.Meta]) -> str:
result.functions[item.name] = summary
last_function_block = None
elif isinstance(item, tf.DataTypeDeclaration):
for subitem in item.items:
if isinstance(item.declaration, tf.StructureTypeDeclaration):
summary = DataTypeSummary.from_data_type(
subitem,
source_code=get_code_by_meta(subitem.meta)
item.declaration,
source_code=get_code_by_meta(item.declaration.meta)
)
result.data_types[subitem.name] = summary
result.data_types[item.declaration.name] = summary
last_function_block = None
elif isinstance(item, tf.Method):
if last_function_block is not None:
Expand Down
15 changes: 7 additions & 8 deletions blark/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3128,23 +3128,22 @@ def __str__(self):
@dataclass
@_rule_handler("data_type_declaration", comments=True)
class DataTypeDeclaration:
items: List[TypeDeclarationItem]
declaration: Optional[TypeDeclarationItem]
meta: Optional[Meta] = meta_field()

@staticmethod
def from_lark(*args: TypeDeclarationItem) -> DataTypeDeclaration:
return DataTypeDeclaration(list(args))
def from_lark(
declaration: Optional[TypeDeclarationItem] = None,
) -> DataTypeDeclaration:
return DataTypeDeclaration(declaration)

def __str__(self) -> str:
if not self.items:
if not self.declaration:
return "TYPE\nEND_TYPE"

items = "\n".join(
indent(f"{item};") for item in self.items
)
return "\n".join(
(
f"TYPE {items.lstrip()}",
"TYPE " + indent(f"{self.declaration};").lstrip(),
"END_TYPE",
)
)
Expand Down

0 comments on commit c2b209d

Please sign in to comment.