Skip to content

Commit 1922eed

Browse files
authored
Merge pull request #116 from klauer/pr111_update
Adding on to #111
2 parents 86f9df8 + 5e599ca commit 1922eed

File tree

8 files changed

+373
-122
lines changed

8 files changed

+373
-122
lines changed

blark/iec.lark

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ _library_element_declaration: data_type_declaration
3333
| ";"
3434

3535
// B.1.1
36-
IDENTIFIER: /[A-Za-z_][A-Za-z0-9_]*/i
36+
// Identifiers need to ignore certain keywords, but NOT typenames, as those may be used
37+
// 'as values' with SIZEOF for example
38+
IDENTIFIER: /\b(?!(ABSTRACT|ACTION|AND|AND_THEN|AT|BY|CASE|CONTINUE|DO|ELSE|ELSIF|END_ACTION|END_CASE|END_FOR|END_FUNCTION|END_FUNCTIONBLOCK|END_FUNCTION_BLOCK|END_IF|END_INTERFACE|END_METHOD|END_PROGRAM|END_PROPERTY|END_REPEAT|END_STRUCT|END_TYPE|END_UNION|END_VAR|END_WHILE|EXIT|EXTENDS|FINAL|FOR|FUNCTION|FUNCTIONBLOCK|FUNCTION_BLOCK|IF|IMPLEMENTS|INTERFACE|INTERNAL|JMP|METHOD|MOD|NOT|OF|OR|OR_ELSE|PERSISTENT|PRIVATE|PROGRAM|PROPERTY|PROTECTED|PUBLIC|READ_ONLY|READ_WRITE|REFERENCE|REPEAT|RETURN|STRUCT|THEN|TO|TYPE|UNION|UNTIL|VAR|VAR_ACCESS|VAR_EXTERNAL|VAR_GLOBAL|VAR_INPUT|VAR_INST|VAR_IN_OUT|VAR_OUTPUT|VAR_STAT|VAR_TEMP|WHILE|XOR)\b)[A-Za-z_][A-Za-z0-9_]*\b/i
3739

3840
// B.1.2
3941
constant.1: time_literal
@@ -346,7 +348,7 @@ _array_initial_element: expression
346348
| enumerated_value
347349
| array_initialization
348350

349-
structure_type_declaration: structure_type_name_declaration [ extends ] ":" [ indirection_type ] _STRUCT ( structure_element_declaration ";"+ )* _END_STRUCT
351+
structure_type_declaration: structure_type_name_declaration [ extends ] ":" [ indirection_type ] _STRUCT ";"* ( structure_element_declaration ";"+ )* _END_STRUCT
350352

351353
initialized_structure: structure_type_name ":=" structure_initialization
352354

@@ -445,7 +447,7 @@ fb_decl: fb_decl_name_list ":" function_block_type_name [ ":=" structure_initial
445447

446448
fb_decl_name_list: fb_name ( "," fb_name )*
447449

448-
var_body: ( var_init_decl ";"+ )*
450+
var_body: ";"* ( var_init_decl ";"+ )*
449451

450452
array_var_declaration: var1_list ":" array_specification
451453

@@ -562,7 +564,7 @@ ACCESS_SPECIFIER: _ABSTRACT
562564
| _INTERNAL
563565
| _FINAL
564566
access_specifier: ACCESS_SPECIFIER+
565-
extends: _EXTENDS DOTTED_IDENTIFIER
567+
extends: _EXTENDS DOTTED_IDENTIFIER ("," DOTTED_IDENTIFIER)*
566568
implements: _IMPLEMENTS DOTTED_IDENTIFIER ("," DOTTED_IDENTIFIER)*
567569

568570
function_block_type_declaration: FUNCTION_BLOCK [ access_specifier ] derived_function_block_name [ extends ] [ implements ] fb_var_declaration* [ function_block_body ] END_FUNCTION_BLOCK ";"*
@@ -578,6 +580,7 @@ END_FUNCTION_BLOCK: _END_FUNCTION_BLOCK
578580
| input_output_declarations
579581
| external_var_declarations
580582
| var_declarations
583+
| var_inst_declaration
581584
| temp_var_decls
582585
| static_var_declarations
583586
| incomplete_located_var_declarations
@@ -600,7 +603,7 @@ function_block_method_declaration: _METHOD [ access_specifier ] DOTTED_IDENTIFIE
600603

601604
?property_return_type: _located_var_spec_init
602605

603-
function_block_property_declaration: _PROPERTY [ access_specifier ] DOTTED_IDENTIFIER [ ":" property_return_type ] ";"* property_var_declaration* [ function_block_body ] _END_PROPERTY ";"*
606+
function_block_property_declaration: _PROPERTY [ access_specifier ] DOTTED_IDENTIFIER [ ":" property_return_type ] [ access_specifier ] ";"* property_var_declaration* [ function_block_body ] _END_PROPERTY ";"*
604607

605608
// B.1.5.3
606609
?program_type_name: IDENTIFIER
@@ -638,7 +641,7 @@ program_access_decl: access_name ":" symbolic_variable ":" non_generic_type_name
638641
| external_var_declarations
639642
| var_declarations
640643

641-
interface_declaration: _INTERFACE IDENTIFIER [ extends ] interface_var_declaration* _END_INTERFACE ";"*
644+
interface_declaration: _INTERFACE IDENTIFIER [ extends ] interface_var_declaration* _END_INTERFACE? ";"*
642645

643646
// B.2.1, B.3.1
644647
LOGICAL_OR: _OR

blark/plain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .input import (BlarkCompositeSourceItem, BlarkSourceItem,
99
register_input_handler)
1010
from .output import OutputBlock, register_output_handler
11-
from .util import AnyPath, SourceType, find_pou_type_and_identifier
11+
from .util import AnyPath, SourceType, find_pou_type_and_identifier_plain
1212

1313

1414
@dataclasses.dataclass
@@ -40,7 +40,7 @@ def load(
4040
with open(filename, "rt") as fp:
4141
contents = fp.read()
4242

43-
source_type, identifier = find_pou_type_and_identifier(contents)
43+
source_type, identifier = find_pou_type_and_identifier_plain(contents)
4444
# if source_type is None:
4545
# return []
4646
source_type = SourceType.general

blark/solution.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ def from_xml(
351351
) -> Self:
352352
declaration = get_child_located_text(xml, "Declaration", filename=filename)
353353
if declaration is not None:
354-
source_type, identifier = util.find_pou_type_and_identifier(
355-
declaration.value
356-
)
354+
source_type, identifier = util.find_pou_type_and_identifier_xml(xml)
357355
else:
358356
source_type, identifier = None, None
359357

@@ -366,7 +364,7 @@ def from_xml(
366364
implementation=get_child_located_text(
367365
xml, "Implementation/ST", filename=filename
368366
),
369-
metadata=xml.attrib,
367+
metadata=dict(xml.attrib),
370368
source_type=source_type,
371369
filename=filename,
372370
)
@@ -948,7 +946,11 @@ def to_blark(self) -> list[Union[BlarkCompositeSourceItem, BlarkSourceItem]]:
948946
property_ident = Identifier.from_string(base_decl.identifier)
949947

950948
parts = []
951-
for get_or_set, obj in (("get", self.get), ("set", self.set)):
949+
get_set = [
950+
("get", SourceType.property_get, self.get),
951+
("set", SourceType.property_set, self.set),
952+
]
953+
for get_or_set, source_type, obj in get_set:
952954
if obj is None:
953955
continue
954956

@@ -961,7 +963,7 @@ def to_blark(self) -> list[Union[BlarkCompositeSourceItem, BlarkSourceItem]]:
961963
parts=[*property_ident.parts, get_or_set],
962964
decl_impl="declaration",
963965
).to_string(),
964-
type=SourceType.property,
966+
type=source_type,
965967
lines=base_decl.lines + decl.lines,
966968
grammar_rule=SourceType.property.get_grammar_rule(),
967969
implicit_end="END_PROPERTY",
@@ -977,7 +979,7 @@ def to_blark(self) -> list[Union[BlarkCompositeSourceItem, BlarkSourceItem]]:
977979
parts=[*property_ident.parts, get_or_set],
978980
decl_impl="implementation",
979981
).to_string(),
980-
type=SourceType.property,
982+
type=source_type,
981983
lines=impl.lines,
982984
grammar_rule=SourceType.statement_list.get_grammar_rule(),
983985
implicit_end="",
@@ -1020,7 +1022,7 @@ def from_xml(
10201022
xml: lxml.etree.Element,
10211023
parent: TcSource,
10221024
) -> Self:
1023-
return cls(metadata=xml.attrib, xml=xml, parent=parent)
1025+
return cls(metadata=dict(xml.attrib), xml=xml, parent=parent)
10241026

10251027

10261028
@dataclasses.dataclass

0 commit comments

Comments
 (0)