r/PLC • 800xA|Ac400/500/800|S+ • 1d ago

Engineering Question Formatter for Structured Text

I need to consult the hive-mind here.

Does anybody have a good formatter for Structured Text (dialect is secondary). I need it to be able to properly indent multiple commented lines as, unfortunately, the original creator of the programs (a lot) has decided to disable certain parts with the (# and #) comments and yet uses the (* and *) comments in between the outer comments.

I've inherited this program (which is quite huge) and need to sift through it. Can't just simply omit/delete the commented out code as it is absolutely possible that it will be used at any given time in the future.

The original creator didn't want to think about a mechanism to disable certain parts (each part reflects a particular "Control Unit").

I would be very grateful for any recommendation. My google-fu and my VSCode extension search didn't bring decent results.

Edit: I cannot use any AI as assistant because of the sensitivity of the data. This is also the reason I have to be so vague here in my descriptions. (The bane of working in system-critical infrastructure.)

6 Upvotes

40 comments sorted by

11

u/dopabot 1d ago

You can write a fake sample of the content with non customer data, have AI build a Python script to generate the result you want, then run the Python script against the customer data.

This way no customer data ever enters AI.

3

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

I like that thought. Didn't even think about that approach.

Thank you!

1

u/TheFern3 Software Engineer 1d ago

Bingo!

5

u/Wandigon 1d ago edited 1d ago

What i did some time ago was to ask Claude to make a single html file with all the libraries included in the file, which formats the st code based some setting preferences i had from stweep previously. I copy in the the code, and it applies my preffered format to the code, and i can then copy it back in.

It's all local and settings can be saved locally in your browser. It has been wonderful to work with, and litteraly all engineering PC's i have been given has some kind of webbrowser, and since it's one file it super easy to transfer and doesn't require python to be installed or similar.

2

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

That would be roughly what I am looking for.

2

u/Wandigon 11h ago

I have added it to github. If you're like me and don't like using code from the internet, pass the file through a security tool before opening it in your browser.

Here's the repository: https://github.com/Flareux/ST-Beautifier

Here's the github pages (still local to you), if you wanna test it: https://flareux.github.io/ST-Beautifier/

Hope it can be of use to someone.

2

u/desrtfx 800xA|Ac400/500/800|S+ 11h ago

Thank you so much!

Did a quick try and it is very close to what I need. I only need to tune it a tiny bit and it will work as intended.

As expected, it did not like the non-standard ((# and #)) comment tags. That should be solvable.

3

u/Wandigon 1d ago

I also went a bit further here, and made it so it refers to our software design guide, and marks variables that are not formatted with the correct hungarian notation required by our standard. Helps me fix times where i have forgotten to prefix the variables...

4

u/r2k-in-the-vortex 1d ago

(# this is not a valid comment accoding to IEC standard #)

I don't think most ST formatters will help you unless you replace them all with (* actual comments *)

Also, use version control, then deleting old garbage becomes perfectly fine, if you ever need it again (you wont), go look it up in repo history.

2

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

Unfortunately, in the dialect the program was written in, it is a valid comment.

Version control opens up another, different rabbit hole. The whole is in a fully integrated system with a mix of ST and FBD code and I'd have to copy-paste everything out of and back into the system.

The bigger problem is that there is no "old garbage". It's actually just temporarily deactivated parts.

Even the creator of the original program warned me about it being "quite a piece of work" to get straightened out. Unfortunately, he has retired and now the whole chaos is on my shoulders.

3

u/d4_mich4 1d ago

I don't have a tool but not sure if using AI would be an option bad thing is you give ai your code but if that's not a problem for you I think it can help with that.

3

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

bad thing is you give ai your code

And that's exactly what I cannot do.

2

u/d4_mich4 1d ago

So maybe make a tool with ai then explain what you need from which state to what else and you'll maybe get a python script which can convert it for you?

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

I like that thought. Didn't even think about that approach.

Thank you!

2

u/el_extrano 1d ago

How many lines of code are you faced with?

  1. Any decent text editor (Vim, Emacs, Notepad++, VSCode) has some faculty for find-and-replace, or macro recording, that you can use to make a "jig" to solve such repetitive editing tasks.
  2. Use a Unix filter program (e.g. sed or awk) to operate line-wise over the source. For example, cat your source into sed 's/^(\#/\t(\#/'
  3. Write your own simple script in your favorite scripting language (e.g. Python). Indenting certain lines beginning with (#, would be about 5-10 lines of code for the whole program if that.

If you're going to be working with plaintext any significant amount over your career, I would recommend invessting some time into your skills with each of the three approaches above.

If this is truly a one-off task, then you could probably do it by hand in an afternoon and be done with it.

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

I'm facing somewhat around 10000 lines in total across many different POUs/Programs.

Thanks for the general advice. I am quite fit in programming in multiple languages (have been doing it professionally for nearly 4 decades). I am quite good at writing my own parsers, lexers, filters for most proprietary formats.

Unix/Linux tools are out of the question. Windows only shop. Regex would not be any problem.

I just thought to consult the wisdom of the subreddit to figure if anybody has something already so that I don't have to invest the time and effort to reinvent the wheel.

1

u/green_gold_purple 1d ago

Python runs great on windows. I use it here and there for glue code on interfaces.

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

I know that and use it a lot. I just said that Unix/Linux utilities, like sed, etc. are not possible. We cannot use WDL, we cannot whip up Linux VMs, we cannot install the Linux (mingw, etc.) tools that would work under Windows.

1

u/el_extrano 1d ago

If you aren't allowed to install anything or run foreign executables, or move files in and out of the DMZ, then how would you use any solution someone "already has" anyway? We'd have to know exactly what is in your environment, but you can't tell us that or you'd have to kill us.

And I didn't mean to malign your programming ability, so apologies if I came across that way.

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

I can copy text in and out of the DMZ. I can do very limited installs, but not everything.

I can, to a certain degree add plugins to editors.

9

u/UseraM1 Student 1d ago

You can use notepad++ and use regex in find and replace

1

u/Paup27 18h ago

Long time ago I created a custom ST format in Notepad++ gave me full linting and syntax highlighting, made it super easy to read and make sense of larger programs.

1

u/TheFern3 Software Engineer 1d ago

I dunno if you know this but you don’t have to feed sensitive data to ai. Use a local model to grab examples of what you need and make those examples with generic data. I’m sure Gemma local can tell you what to do is a simple parsing problem.

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

Of course I know about local models, but even these are off-limits for us. We cannot install any non-authorized software on our company computers and even less so on the project/plant computers.

System critical infrastructure has its own set of rules and restrictions.

1

u/TheFern3 Software Engineer 1d ago

Then you gotta learn how to use grep awk or make a script to parse examples of what you have replace sensitive data then ask ai or just figure out how to do that by yourself like we did before ai lol

But what’s the issue you have with the project? If is just comments what are you really fixing here?

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

The comments are a symptom.

The whole thing is a complex calculation with roughly 1200 redundant input signals coming into the system via IEC 60870-104 (multiple sources and destinations) and the whole is application redundant across 2 DCS clusters with 2 DCS controllers each. (Which is a fairly small setup for my normal standards.)

The comments indicate "out of service control units" - these units might be temporarily out of service or permanently. Yet, the commenting out is scattered around all of the project.

We are now in the process of upgrading and improving said project and can't miss any of the commented out parts, which is often not that easy to find because of nested ifs (not good practice, I know), because of multiple levels (also not the best practice), and other factors.

As I've said before: I do know how to make such a script but was just asking to see if someone else had something readily available.

I know my trade well, don't worry.

0

u/TheFern3 Software Engineer 22h ago

I mean if you know “your” trade and can’t use ai I don’t understand why you would think someone would have a custom script for an oddly specific codebase… script would have been done faster than writing a reddit post 🤷‍♂️

2

u/drbitboy 1d ago

There are "linters" that will handle that kind of thing. Setting up a configuration file is a pretty deep rabbit hole, but I suspect somewhere on GitHub or similar you will find a PLC/ST-oriented Quick Start that may work for you.

2

u/drbitboy 1d ago

Whoops, I mixed up what a linter is: technically a linter evaluates code against a style spec (also looks at worst- and best-practice programming constructs)

A Code Formatter, or Beautifier, will fix the style issues (justification, indenting, line breaks, ...). Some linters include a Code Formatter.

1

u/Deepu_ 1d ago

VScode had a structured text plugin. Haven't tried it though.

1

u/West-Word-604 :doge: 1d ago

I use my copilot notebook that is loaded up with PDF manual references and use iterative prompting, a good set of instructions, and contextual partnership throughout project development. It helps me generate structured text quite well.

1

u/zoute_haring 1d ago

Notepad++

0

u/WandererHD 1d ago

Notepad, CTRL-H 🤪

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

Find and replace won't help.

3

u/WandererHD 1d ago

If you didn't find what you wanted in VSCode I don't think you will anywhere else. So I will suggest a cardinal sin: Use ChatGPT or Claude.

1

u/desrtfx 800xA|Ac400/500/800|S+ 1d ago

Generally, I would do that, but the sensitivity of the program does not allow for using any kind of AI.

1

u/WandererHD 1d ago

Maybe tell it to make a program with the features you want.

2

u/luv2sploodge 1d ago

No sin here, is perfect for this

2

u/WandererHD 1d ago

Many around here don't like it.

1

u/luv2sploodge 1d ago

Not sure why, its one of the most useful tools ever invented for us.