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 improvements #4

Merged
merged 5 commits into from
Nov 12, 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ If any grammar or format errors are found, they are printed to stdout and the co

- Add package to pip.
- Checks for procedure, variable, function and signal names (enforce camel_case or snakeCase).

## References

- ABB RAPID [docs](https://library.e.abb.com/public/f23f1c3e506a4383b635cff165cc6993/3HAC050946+TRM+RAPID+Kernel+RW+6-en.pdf?x-sign=oUq9VZeSx%2Fve4%2BCCAYZVeAQoLxtMdzw6S2BkJobVIFhUVtPrZ8dmV1VIHdk%2B6Yfg)
- [PyParsing](https://pyparsing-docs.readthedocs.io/en/latest/)
13 changes: 8 additions & 5 deletions rapidchecker/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
stmt_block = pp.Forward()
stmt = pp.Forward()

# Function call
named_arg = pp.Combine("\\" + identifier) + pp.Optional(":=" + expression)
# TODO: Allow missing commas before named args
argument_list = pp.Optional(pp.delimitedList(named_arg | expression))
# Arguments for proc and functon calls
optional_arg = pp.Combine("\\" + identifier) + pp.Optional(":=" + expression)
required_arg = pp.Optional(identifier + ":=") + expression
first_argument = optional_arg | required_arg
argument = ("," + (optional_arg | required_arg)) | optional_arg
argument_list = pp.Optional(first_argument + pp.ZeroOrMore(argument))

function_call = identifier + "(" - argument_list - ")"

array = "[" + pp.delimitedList(expression) + "]"
Expand Down Expand Up @@ -60,7 +63,7 @@
(T.PERS | T.VAR | T.CONST)
+ datatype
+ identifier
+ pp.Optional("{" + pp.pyparsing_common.integer + "}")
+ pp.Optional("{" + expression + "}")
+ pp.Optional(":=" + expression)
+ ";"
)
Expand Down
9 changes: 6 additions & 3 deletions tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
function_call,
if_stmt,
module,
named_arg,
optional_arg,
proc_call_stmt,
proc_def,
record_def,
Expand Down Expand Up @@ -76,8 +76,8 @@ def test_stmt_block(valid_block: str) -> None:


@pytest.mark.parametrize("input_str", ["\\a", "\\a:=c"])
def test_named_arg(input_str: str) -> None:
assert named_arg.parseString(input_str, parseAll=True).as_list()
def test_optional_arg(input_str: str) -> None:
assert optional_arg.parseString(input_str, parseAll=True).as_list()


@pytest.mark.parametrize("input_str", ["\\a", "\\a, \\b:=c", "\\a, c+1", "a, \\b:=c"])
Expand Down Expand Up @@ -140,6 +140,7 @@ def test_record_def(input_str: str) -> None:
[
"PERS string varName;",
"VAR robtarget targets{1000};",
"VAR robtarget targets{var1 + var2};",
"CONST num number := 1 + 1;",
],
)
Expand Down Expand Up @@ -235,6 +236,7 @@ def test_for_stmt(input_str: str) -> None:
"callProc arg1, arg2, A AND B;",
"callProc arg1, arg2, \\switch;",
"callProc arg1, arg2, \\opt:=(1+1), \\switch;",
"callProc arg1, name:=arg2 \\opt:=(1+1) \\switch;",
],
)
def test_proc_call_stmt(input_str: str) -> None:
Expand All @@ -248,6 +250,7 @@ def test_proc_call_stmt(input_str: str) -> None:
"callFunc(arg1, arg2, A AND B);",
"callFunc(arg1, arg2, \\switch);",
"callFunc(arg1, arg2, \\opt:=(1+1), \\switch);",
"callFunc(arg1, name:=arg2 \\opt:=(1+1) \\switch);",
],
)
def test_func_call_stmt(input_str: str) -> None:
Expand Down