Skip to content

Bug: problems with persistence #16

@Ruslan227

Description

@Ruslan227

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions