Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support of Chained Method/Function Calls #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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