Skip to content

Commit

Permalink
Merge pull request #55 from engineerjoe440/bugfix/newline-separated-o…
Browse files Browse the repository at this point in the history
…op-logic

ENH: Add Support of Chained Method/Function Calls
  • Loading branch information
klauer authored Apr 7, 2023
2 parents f423b1b + 5169bdc commit c6e0e18
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions blark/iec.lark
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ _statement: ";"
| reset_statement
| reference_assignment_statement
| return_statement
| chained_function_call_statement
| function_call_statement
| if_statement
| case_statement
Expand Down Expand Up @@ -752,6 +753,8 @@ return_statement.1: "RETURN"i ";"*

function_call_statement: function_call ";"+

chained_function_call_statement: function_call ("." function_call)+ ";"+

param_assignment: [ LOGICAL_NOT ] variable_name "=>" [ expression ] -> output_parameter_assignment
| variable_name ":=" [ expression ]
| expression
Expand Down
19 changes: 19 additions & 0 deletions blark/tests/source/object_oriented_dotaccess.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(* This program uses some object-oriented function-block with multiline access. *)
PROGRAM oop_test
VAR
uut : SomeFunctionBlock;
END_VAR

// Use an object that has a different method called on each line
uut.DoSomething(input_1:='foo', input_2:='bar')
.DoSomethingElse(input_1:=5)
.Finish();

// Or perhaps something that's a little more tame
uut.DoSomething(input_1:='foo', input_2:='bar')
.DoSomethingElse(input_1:=5).Finish();
// Or something tamer, still
uut.DoSomething(input_1:='foo', input_2:='bar').DoSomethingElse(input_1:=5).Finish();
END_PROGRAM
5 changes: 5 additions & 0 deletions blark/tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,11 @@ def test_action_roundtrip(rule_name, value):
END_FOR
"""
)),
param("chained_function_call_statement", tf.multiline_code_block(
"""
uut.call1().call2().call3.call4().done();
"""
)),
],
)
def test_statement_roundtrip(rule_name, value):
Expand Down
17 changes: 17 additions & 0 deletions blark/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,23 @@ def __str__(self):
return f"{invoc};"


@dataclass
@_rule_handler("chained_function_call_statement", comments=True)
class ChainedFunctionCallStatement(Statement):
invocations: List[FunctionCall]
meta: Optional[Meta] = meta_field()

@staticmethod
def from_lark(*invocations: FunctionCall) -> ChainedFunctionCallStatement:
return ChainedFunctionCallStatement(
invocations=list(invocations)
)

def __str__(self) -> str:
invoc = ".".join(str(invocation) for invocation in self.invocations)
return f"{invoc};"


@dataclass
@_rule_handler("else_if_clause", comments=True)
class ElseIfClause:
Expand Down

0 comments on commit c6e0e18

Please sign in to comment.