forked from beremiz/beremiz
-
Notifications
You must be signed in to change notification settings - Fork 1
Rtop 35 add support for complex variables on debug.c #8
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
Merged
thiagoralves
merged 8 commits into
development
from
RTOP-35-Add-support-for-complex-variables-on-debug.c
Jul 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9502346
create complex parser
dcoutinho1328 cbec3a0
fixing nested structs and structs outside of type declaration
dcoutinho1328 2ddd246
add error prints to sys error and run formatter
dcoutinho1328 6f01bf6
change logic to allow all data types inside struct
dcoutinho1328 55419f5
Code-Rabbit: Update ComplexParser.py
dcoutinho1328 f36573a
Code-rabbit: Update ComplexParser.py
dcoutinho1328 a601391
applying suggested changes from coderabbit
dcoutinho1328 649d72a
changing arguments and parsing complex also in debugging
dcoutinho1328 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import os | ||
| from jinja2 import Environment, FileSystemLoader | ||
| from util import paths | ||
|
|
||
| STRUCT_TOKEN = "STRUCT" | ||
| TYPE_TOKEN = "TYPE" | ||
|
|
||
|
|
||
| class ComplexParser: | ||
|
|
||
| def __init__(self): | ||
| self.__loader = FileSystemLoader( | ||
| os.path.join(paths.AbsDir(__file__), "templates") | ||
| ) | ||
|
|
||
| def __rewriteSTFile(self, lines, complex_vars): | ||
| """ | ||
| Rewrite the ST file with complex variables. | ||
| """ | ||
| template = Environment(loader=self.__loader).get_template( | ||
| "function_block.st.j2" | ||
| ) | ||
| program_text = "" | ||
| for var in complex_vars: | ||
| program_text += f"{template.render(name=var[0], vars=var[1])}\n\n" | ||
| program_text += "".join(lines) | ||
| file_name = f"processed_{os.path.basename(self.__stFile)}" | ||
| with open(os.path.join(paths.AbsDir(self.__stFile), file_name), "w") as f: | ||
| f.write(program_text) | ||
|
|
||
| return program_text | ||
|
|
||
| def ParseSTFile(self, file): | ||
| """ | ||
| Parse ST file to extract complex variables. | ||
| """ | ||
|
|
||
| if not file: | ||
| raise Exception("ST file not valid. Please ") | ||
|
|
||
| self.__stFile = file | ||
|
|
||
| lines = [] | ||
|
|
||
| with open(self.__stFile, "r") as f: | ||
| lines = f.readlines() | ||
|
|
||
| complex_vars = [] | ||
| new_lines = [] | ||
| parsing = False | ||
| type_declaration = False | ||
|
|
||
| for l in lines: | ||
| line = l.strip() | ||
dcoutinho1328 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if line == TYPE_TOKEN: | ||
| type_declaration = True | ||
| if f": {STRUCT_TOKEN}" in line: | ||
| if parsing: | ||
| raise Exception( | ||
| f"Error: Nested {STRUCT_TOKEN} declaration found in ST file." | ||
| ) | ||
|
|
||
| if not type_declaration: | ||
| raise Exception( | ||
| f"Error: {STRUCT_TOKEN} found out of {TYPE_TOKEN} block declaration." | ||
| ) | ||
| name = f"{line.split(':')[0].strip()}" | ||
| complex_vars.append((name, {})) | ||
| parsing = True | ||
| continue | ||
| elif f"END_{STRUCT_TOKEN}" in line: | ||
| parsing = False | ||
| continue | ||
| elif parsing: | ||
| splitted_line = line.replace(":=", ":").split(":") | ||
| if len(splitted_line) == 3: | ||
| # Handle complex variable with type and initial value | ||
| name, type, value = ( | ||
| splitted_line[0].strip(), | ||
| splitted_line[1].strip(), | ||
| splitted_line[2].replace(";", "").strip(), | ||
| ) | ||
| complex_vars[-1][1][name] = {"type": type, "value": value} | ||
| continue | ||
| name, type = ( | ||
| splitted_line[0].strip(), | ||
| splitted_line[1].replace(";", "").strip(), | ||
| ) | ||
| complex_vars[-1][1][name] = {"type": type, "value": None} | ||
| continue | ||
| # Remove type block if empty | ||
| elif f"END_{TYPE_TOKEN}" in line: | ||
| type_declaration = False | ||
| if TYPE_TOKEN in new_lines[-1]: | ||
| new_lines.pop() | ||
| continue | ||
dcoutinho1328 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Skip consecutive empty lines | ||
| elif not l.strip() and not (new_lines and new_lines[-1].strip()): | ||
| continue | ||
|
|
||
| new_lines.append(l) | ||
|
|
||
| return self.__rewriteSTFile(new_lines, complex_vars) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| FUNCTION_BLOCK {{name}} | ||
| VAR_INPUT | ||
| {%-for k,v in vars.items()%} | ||
| {%- if v.value is not none %} | ||
| {{k}} : {{v.type}} := {{v.value}}; | ||
| {%- else %} | ||
| {{k}} : {{v.type}}; | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| END_VAR | ||
| VAR | ||
| local_temp : DINT; | ||
| END_VAR | ||
|
|
||
| local_temp := 0; | ||
| END_FUNCTION_BLOCK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.