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

REF/ENH: standalone solution parser and rewrite of input/output format support #64

Closed klauer closed 1 year ago

klauer commented 1 year ago

If this breaks your usage of blark, let me know!

This is a pretty large refactor/enhancement, comments/ideas/complaints are welcome.

Goals

Primarily: to close https://github.com/klauer/blark/issues/53

More specifically (caveat: some goals may be walked back; some may not be written here yet) -

At some point after this PR, I might consider:

Status

Example

Take, for example, a TwinCAT POU like FB_DummyHA.TcPOU. It's broken up into multiple XML sections, which we have no interest in adding grammar for.

In blark master:

FUNCTION_BLOCK FB_DummyHA IMPLEMENTS I_HigherAuthority, I_LowerAuthority
VAR_INPUT
    ReqAcknowledged : BOOL := TRUE; // Use to control when the HA CHeckRequest Returns TRUE
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR

END_FUNCTION_BLOCK

METHOD CheckRequest : BOOL
VAR_INPUT
        nReqID  : DWORD;
END_VAR
CheckRequest := ReqAcknowledged;
END_METHOD

// <Arbiter Internal>
// Elevates the arbitrated BP set to something above.
// Could be another arbiter, or a BP requester/ IO,
// or an FB that locks in a specific portion of the BP set.
METHOD ElevateRequest : BOOL
VAR_INPUT
    HigherAuthority : I_HigherAuthority;
END_VAR

END_METHOD

METHOD RemoveRequest : BOOL
VAR_INPUT
        (*StateID to remove*)
        nReqID  : DWORD;
END_VAR
RemoveRequest := TRUE;
END_METHOD

METHOD RequestBP : BOOL
VAR_INPUT
        (*StateID of state requesting beam parameter set*)
        nReqID  : DWORD;
        (*Requested beam params*)
        stReqBP : ST_BeamParams;
END_VAR
RequestBP := TRUE;
END_METHOD

PROPERTY nLowerAuthorityID : DWORD
VAR
END_VAR
nLowerAuthorityID := PMPS_GVL.EXCLUDED_ASSERTION_ID;
END_PROPERTY

After this PR is finished

In our above example:

What about other input/output formats?

I haven't decided, maybe one or more of the following paths forward:

codecov-commenter commented 1 year ago

Codecov Report

Merging #64 (04c98d5) into master (4ea1ea0) will increase coverage by 4.9%. The diff coverage is 77.6%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/klauer/blark/pull/64/graphs/tree.svg?width=650&height=150&src=pr&token=QZDTKF30TH&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer)](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer) ```diff @@ Coverage Diff @@ ## master #64 +/- ## ======================================== + Coverage 72.7% 77.7% +4.9% ======================================== Files 18 23 +5 Lines 3814 4884 +1070 ======================================== + Hits 2774 3796 +1022 - Misses 1040 1088 +48 ``` | [Impacted Files](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer) | Coverage Δ | | |---|---|---| | [blark/\_\_main\_\_.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvX19tYWluX18ucHk=) | `0.0% <0.0%> (ø)` | | | [blark/summary.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-Ymxhcmsvc3VtbWFyeS5weQ==) | `66.2% <ø> (+3.7%)` | :arrow_up: | | [blark/dependency\_store.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvZGVwZW5kZW5jeV9zdG9yZS5weQ==) | `72.3% <69.2%> (+72.3%)` | :arrow_up: | | [blark/solution.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-Ymxhcmsvc29sdXRpb24ucHk=) | `72.7% <72.7%> (ø)` | | | [blark/format.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvZm9ybWF0LnB5) | `81.3% <77.4%> (+23.3%)` | :arrow_up: | | [blark/parse.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvcGFyc2UucHk=) | `79.3% <79.4%> (+16.6%)` | :arrow_up: | | [blark/typing.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvdHlwaW5nLnB5) | `82.2% <82.2%> (ø)` | | | [blark/util.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvdXRpbC5weQ==) | `83.8% <83.4%> (+1.7%)` | :arrow_up: | | [blark/tests/test\_dependency\_store.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvdGVzdHMvdGVzdF9kZXBlbmRlbmN5X3N0b3JlLnB5) | `84.2% <84.2%> (ø)` | | | [blark/tests/test\_cli.py](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer#diff-YmxhcmsvdGVzdHMvdGVzdF9jbGkucHk=) | `90.3% <86.3%> (-2.2%)` | :arrow_down: | | ... and [7 more](https://codecov.io/gh/klauer/blark/pull/64?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer) | | ... and [2 files with indirect coverage changes](https://codecov.io/gh/klauer/blark/pull/64/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=K+Lauer)
klauer commented 1 year ago

Functionally, I think this has most of what I want from the solution parser, reformatting tools and such.

I'll claim that the API is subject to change if/when I look to adding more input/output formats. For now, it's probably fine.

I'll look back into some TODO items and aim to merge this in the nearish future.

klauer commented 1 year ago

This is too big of a refactor to ask for a real review of anyone (well, blark's one contributor...).

As noted above, comments/complaints/requests on this change are welcome - just make an issue. All API introduced here subject to change at my own whim otherwise