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

Grammar bugfixes #8

Merged
merged 5 commits into from
Nov 14, 2024
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "rapidchecker"
version = "0.1.0"
version = "0.1.1"
description = " Grammar and format checker for ABB RAPID code. "
readme = "README.md"
requires-python = ">=3.10"
Expand Down
15 changes: 8 additions & 7 deletions rapidchecker/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
register_indent_checks,
)
from .literals import RAPIDLITERAL
from .operators import infix_op
from .operators import infix_op, prefix_op

pp.ParserElement.enable_packrat()

Expand Down Expand Up @@ -39,7 +39,7 @@
| variable
| RAPIDLITERAL
| "(" + expression + ")"
| T.NOT + expression
| prefix_op + expression
| array
)
expression <<= (term + infix_op + expression) | term
Expand All @@ -60,12 +60,13 @@

# Variable definition
var_def = (
(T.PERS | T.VAR | T.CONST)
pp.Optional(T.LOCAL | T.TASK)
+ (T.PERS | T.VAR | T.CONST)
+ datatype
+ identifier
+ pp.Optional("{" + expression + "}")
+ pp.Optional(":=" + expression)
+ ";"
- identifier
- pp.Optional("{" + expression + "}")
- pp.Optional(":=" + expression)
- ";"
)
var_def_section = pp.ZeroOrMore(var_def)

Expand Down
4 changes: 2 additions & 2 deletions rapidchecker/parser/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@


parameter = (
pp.Optional(T.INOUT | T.PERS)
+ pp.Optional("\\")
pp.Optional("\\")
+ pp.Optional(T.INOUT | T.PERS)
+ datatype
+ identifier
+ elem_index
Expand Down
1 change: 1 addition & 0 deletions rapidchecker/parser/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
boolean_op.set_name("bool_op")

infix_op = pp.oneOf("+ - * / = <= < >= > <>") | boolean_op | T.DIV | T.MOD
prefix_op = "-" | T.NOT
5 changes: 5 additions & 0 deletions rapidchecker/parser/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(self, *args, **kwargs) -> None:
DEFAULT = RapidKeyword("DEFAULT")
ERROR = RapidKeyword("ERROR")
INOUT = RapidKeyword("INOUT")
TASK = RapidKeyword("TASK")
LOCAL = RapidKeyword("LOCAL")

RESERVED_WORD = (
MODULE
Expand Down Expand Up @@ -98,6 +100,9 @@ def __init__(self, *args, **kwargs) -> None:
| CASE
| DEFAULT
| ERROR
| INOUT
| TASK
| LOCAL
)

MODULE_OPTIONS = oneOf("SYSMODULE NOSTEPIN VIEWONLY READONLY")
17 changes: 16 additions & 1 deletion tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ def test_eval() -> None:
assert result == ["%", "procCall%"]


@pytest.mark.parametrize("valid_expression", ["a + 1", "a{100}", "b.c.d{*} AND TRUE"])
@pytest.mark.parametrize(
"valid_expression",
[
"a + 1",
"a{100}",
"b.c.d{*} AND TRUE",
"-a",
"-10",
"-funCall(a,b)",
"a = b AND b <> z",
],
)
def test_expression(valid_expression: str) -> None:
assert expression.parseString(valid_expression, parseAll=True).as_list()

Expand Down Expand Up @@ -111,6 +122,7 @@ def test_array(input_str: str) -> None:
'"string"',
"(1+1)",
"NOT a AND B",
"-(a AND B)",
"[1,2,3,4]",
],
)
Expand Down Expand Up @@ -142,6 +154,7 @@ def test_record_def(input_str: str) -> None:
"VAR robtarget targets{1000};",
"VAR robtarget targets{var1 + var2};",
"CONST num number := 1 + 1;",
"LOCAL CONST num number := 1 + 1;",
],
)
def test_var_def(input_str: str) -> None:
Expand All @@ -164,6 +177,7 @@ def test_var_def_section(input_str: str) -> None:
"IF a THEN\n callProc;\nELSE\n callAnotherProc;\nENDIF",
"IF a THEN\n callProc;\nELSEIF new_condition AND B THEN\n callNewProc;\nELSE\n callAnotherProc;\nENDIF",
"IF a THEN\n callProc;\n callProc2;\nELSEIF new_condition AND B THEN\n callNewProc;\n callProc2;\nELSE\n callAnotherProc;\nENDIF",
"IF a = b AND b <> 1 THEN\n var1 := 0;\nENDIF",
],
)
def test_valid_if_stmt(input_str: str) -> None:
Expand Down Expand Up @@ -237,6 +251,7 @@ def test_for_stmt(input_str: str) -> None:
"callProc arg1, arg2, \\switch;",
"callProc arg1, arg2, \\opt:=(1+1), \\switch;",
"callProc arg1, name:=arg2 \\opt:=(1+1) \\switch;",
"MoveL RelTool(target, 0, 0, -Abs(z)), v1000, fine, tool, \\WObj:=wobj0;",
],
)
def test_proc_call_stmt(input_str: str) -> None:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def test_invalid_variable(invalid_variable: str) -> None:

@pytest.mark.parametrize(
"valid_parameter",
["INOUT string name{100}", "string name", "\\robtarget target"],
[
"INOUT string name{100}",
"\\INOUT string name{100}",
"string name",
"\\robtarget target",
],
)
def test_parameter(valid_parameter: str) -> None:
assert parameter.parseString(valid_parameter, parseAll=True)
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.