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

Bug: problems with persistence #16

Open
Ruslan227 opened this issue Feb 20, 2024 · 0 comments
Open

Bug: problems with persistence #16

Ruslan227 opened this issue Feb 20, 2024 · 0 comments

Comments

@Ruslan227
Copy link
Collaborator

Ruslan227 commented Feb 20, 2024

When saving while, for, repeat, or maybe other statements, some problems occur. For example, we have the following algorithm:

ALGORITHM A1 IN ST: 
  VAR_TEMP 
    Index : INT; 
    Sum: INT;
  END_VAR 
  Index := 0; 
  Sum := 0; 
  WHILE Index < 10 DO 
    Sum := Sum + Index; 
    Index := Index + 1; 
  END_WHILE; 
END_ALGORITHM                                                                                                                                                                                   

After reopening MPS, it shows the algorithm in a changed way:

ALGORITHM A1 IN ST: 
  VAR_TEMP 
    Index : INT; 
    Sum : INT;   
  END_VAR 
  Index := 0; 
  Sum := 0; 
  WHILE Index < 10 DO 
    no statements 
  END_WHILE; 
  Sum := Sum + Index; 
  Index := Index + 2; 
END_ALGORITHM                                                                                                                                                           

And when trying to parse this algorithm in code: org.fbme.lib.iec61499.parser.STConverter.parseStatementList, the org.antlr.v4.runtime.InputMismatchException occurs because input text is Index := 0;&#10;Sum := 0;&#10;WHILE Index < 10 DO&#10; Sum := Sum + Index;&#10; Index := Index + 1;&#10; END_WHILE;&#10; and the error is at the 49th symbol - after WHILE Index < 10 DO

I think the problem is in org.fbme.lib.iec61499.stringify.PrinterBase.escapeXML:

text = text.replace("\n", "&#10;")
text = text.replace("\"", "&#34;")
text = text.replace("&", "&#38;")

After "\n" is replaced by &#10;, the "&" is replaced by &#38;. So, "\n" is replaced by &#38;#10; and it is written to a file (saved)

Returning to the text when the ANTLR exception is thrown. The parser reads the saved model with our algorithm. After that, the model is unescaped in org.fbme.lib.iec61499.parser.BasicFBTypeConverter.AlgorithmConverter.extractDeclarationBody:

val stText = stBodyElement.getAttributeValue("Text")?.unescapeXML()

Let's look at unescapeXML() block:

.replace("&#10;", "\n")
.replace("&#34;", "\"")
.replace("&#38;", "&")

As you see, our &#38;#10; is replaced by &#10;. After that, ANTLR throwsorg.antlr.v4.runtime.InputMismatchException.

I believe that, in order to address this issue, it would be advisable to adjust the order of escaping in the following manner: org.fbme.lib.iec61499.stringify.PrinterBase.escapeXML

text = text.replace("\n", "&#10;")
text = text.replace("\"", "&#34;")
text = text.replace("&", "&#38;") // this line must be the first in replace commands 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant