klauer / blark

Beckhoff TwinCAT ST (IEC 61131-3) code parsing in Python using Lark (Earley)
https://klauer.github.io/blark/
GNU General Public License v2.0
42 stars 5 forks source link

Add Support for Recursive "POINTER TO" Syntax for Variable Type Declaration #32

Closed engineerjoe440 closed 2 years ago

engineerjoe440 commented 2 years ago

Description

In some of the 61131 that I've reviewed, I've seen "recursive" POINTER TO variable type declarations as shown below:

FUNCTION_BLOCK fb_test
    VAR
        _pt_Strings : POINTER TO POINTER TO someObject;
        _pt_Current : POINTER TO POINTER TO someObject;
    END_VAR
END_FUNCTION_BLOCK

This simply makes a pointer to a pointer to some object. It's "legal" in 61131, and is helpful in editors/IDE's which understand the recursive dereference. That said, blark does not seem to be able to parse the variable type.

Work in Progress:

I've started to work on adding support for this declaration type in a branch of my fork: https://github.com/klauer/blark/commit/fe1d37d11c5d455a36d8ef8819b084550f952e7a, but I'm afraid that it's failing quite effectively in pytest: https://github.com/engineerjoe440/blark/runs/6085242624?check_suite_focus=true

Goal/Request:

I was hoping that you might be able to provide some suggestions for how I might modify the transform.py functionality to accommodate this new syntax behavior. Do you have any thoughts or recommendations?

klauer commented 2 years ago

This is a bad oversight on my part - it's clearly valid grammar, but the transformer classes aren't set up to handle it (as you've found). I've been fortunate to not run into such code in the wild at least 😁

It seems like IndirectionType will no longer be an enum but a regular dataclass supporting the concept of "pointer depth" so to speak. The interface could then be something like: pointer_depth and reference_depth. There could be helper properties is_pointer or is_reference, maybe.

Hmm, can you determine if REFERENCE TO REFERENCE TO is valid? I don't have TwinCAT open at the moment to poke around with it.

If it ends up being too complicated, let me know. You could also open up a draft PR with what you come up with and I can try to help finish it.

engineerjoe440 commented 2 years ago

Thank you!

I think that makes a lot of sense. I'll try out that REFERENCE TO REFERENCE TO and report back! more soon!

engineerjoe440 commented 2 years ago

Looks like REFERENCE TO REFERENCE TO does not work.

image

engineerjoe440 commented 2 years ago

I'm afraid I'll be a bit slower with my contributions for the next little while, a bit more sporadic. My regular daily workload is picking back up, so I've been having less time to work on projects like this one where I'm "exploring". I'm not dropping this, just wanted to give you some heads up on my slow-down.

Thank you, again!

klauer commented 2 years ago

Thanks for taking a look into it!

No problem at all - your contributions are always welcome, if/when you have the time. 😃

klauer commented 2 years ago

Closed in #33