Skip to content

Commit

Permalink
Merge pull request #48 from engineerjoe440/feature/add-continue-state…
Browse files Browse the repository at this point in the history
…ment-to-grammar

Enhancement: feature/add continue statement to grammar
  • Loading branch information
klauer authored Mar 14, 2023
2 parents 336831c + 834fe2b commit 505e68e
Show file tree
Hide file tree
Showing 3 changed files with 37 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 @@ -729,6 +729,7 @@ _statement: ";"
| while_statement
| repeat_statement
| exit_statement
| continue_statement


// B.3.2.1
Expand Down Expand Up @@ -787,3 +788,5 @@ while_statement: "WHILE"i expression "DO"i statement_list "END_WHILE"i ";"*
repeat_statement: "REPEAT"i statement_list "UNTIL"i expression "END_REPEAT"i ";"*

exit_statement.1: "EXIT"i ";"*

continue_statement.1: "CONTINUE"i ";"*
25 changes: 25 additions & 0 deletions blark/tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,16 @@ def test_type_name_roundtrip(rule_name, value):
END_FUNCTION_BLOCK
"""
)),
param("function_block_type_declaration", tf.multiline_code_block(
"""
FUNCTION_BLOCK fbName
Method();
IF 1 THEN
CONTINUE;
END_IF
END_FUNCTION_BLOCK
"""
)),
param("function_block_method_declaration", tf.multiline_code_block(
"""
METHOD PRIVATE MethodName : RETURNTYPE
Expand Down Expand Up @@ -1498,3 +1508,18 @@ def test_meta(code: str, comments: List[str], pragmas: List[str]):
found_comments, found_pragmas = meta.get_comments_and_pragmas()
assert [str(comment) for comment in found_comments] == comments
assert [str(pragma) for pragma in found_pragmas] == pragmas


@pytest.mark.parametrize(
"statement, cls",
[
("CONTINUE;", tf.ContinueStatement),
("EXIT;", tf.ExitStatement),
("RETURN;", tf.ReturnStatement),
]
)
def test_statement_priority(statement: str, cls: type):
transformed = roundtrip_rule("statement_list", statement)
assert isinstance(transformed, tf.StatementList)
transformed_statement, = transformed.statements
assert isinstance(transformed_statement, cls)
9 changes: 9 additions & 0 deletions blark/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,15 @@ def __str__(self):
return "EXIT;"


@dataclass
@_rule_handler("continue_statement", comments=True)
class ContinueStatement(Statement):
meta: Optional[Meta] = meta_field()

def __str__(self):
return "CONTINUE;"


@dataclass
@_rule_handler("return_statement", comments=True)
class ReturnStatement(Statement):
Expand Down

0 comments on commit 505e68e

Please sign in to comment.