Skip to content

Commit

Permalink
Merge pull request #179 from fprime-community/issue-178-xml-strings
Browse files Browse the repository at this point in the history
Fix bug in XML code gen
  • Loading branch information
bocchino authored Oct 27, 2022
2 parents f82af60 + eb0369c commit 31001b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions compiler/lib/src/main/scala/codegen/XmlWriter/XmlTags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ object XmlTags extends LineUtils {

def openTag(name: String, pairs: List[(String, String)] = Nil): String = openTagPrefix(name, pairs) ++ ">"

def quoted(s: String): String = "\"" ++ s ++ "\""
def quoted(s: String): String = {
// If s represents a C++ literal string value, then remove the enclosing
// quotation marks from the value now. The F Prime autocoder will
// re-insert them during code gen. If s represents a non-string value, then
// the next line is a no-op.
val s1 = s.replaceAll("^\"", "").replaceAll("\"$", "")
// Escape any remaining quotation marks in the XML way
val s2 = s1.replaceAll("\"", """)
// Add the enclosing quotation marks for the XML attribute
"\"" ++ s2 ++ "\""
}

def taggedLines (tags: (String, String)) (ls: List[Line]): List[Line] = {
val (openTag, closeTag) = tags
(line(openTag) :: ls) :+ line(closeTag)
}

def taggedLines (name: String, pairs: List[(String, String)] = Nil) (ls: List[Line]): List[Line] =
def taggedLines (name: String, pairs: List[(String, String)] = Nil) (ls: List[Line]): List[Line] =
ls match {
case Nil => lines(openCloseTag(name, pairs))
case _ => taggedLines (tags(name, pairs)) (ls)
}

def taggedLinesOpt (name: String, pairs: List[(String, String)] = Nil) (ls: List[Line]): List[Line] =
def taggedLinesOpt (name: String, pairs: List[(String, String)] = Nil) (ls: List[Line]): List[Line] =
ls match {
case Nil => Nil
case ls1 => taggedLines (name, pairs) (ls1)
Expand All @@ -35,7 +45,7 @@ object XmlTags extends LineUtils {

def tags(name: String, pairs: List[(String, String)] = Nil): (String, String) = (openTag(name, pairs), closeTag(name))

private def openTagPrefix(name: String, pairs: List[(String, String)]) =
private def openTagPrefix(name: String, pairs: List[(String, String)]) =
pairs.foldLeft(s"<$name")({ case (s, key -> value) => s ++ s" $key=${quoted(value)}" })

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Generated by fpp-to-xml
</parameter>
<parameter id="0x1" set_opcode="0x100" save_opcode="0x101" name="P2" data_type="F32"/>
<parameter id="0x2" set_opcode="0x102" save_opcode="0x103" name="P3" data_type="I32" default="10"/>
<parameter id="0x3" set_opcode="0x104" save_opcode="0x105" name="P4" data_type="string" size="80" default="a\&quot;bc"/>
</parameters>

</component>
1 change: 1 addition & 0 deletions compiler/tools/fpp-to-xml/test/component/parameters.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ passive component Parameters {
param P1: U32
param P2: F32 set opcode 0x100 save opcode 0x101
param P3: I32 default 10
param P4: string default "a\"bc"
}

0 comments on commit 31001b7

Please sign in to comment.