Transform C++ code into XML (another variant)

# to_xml2

# Transform C++ code into XML (another variant)

Description:

This transformation generates a XML tree where nodes relate to C++ source code tokens.

The difference between this version and the one named to_xml is that here nodes have names related to token types, which can make it easier for some further XML transformations.

For example, the following file (file.cpp):

    #include <iostream>

    int main()
    {
        std::cout << "Hello World\n";
    }

will be transformed into new file named file.cpp.xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <cpp-source file-name="test.cpp">
        <pp_hheader line="1" column="0">#include &lt;iostream&gt;</pp_hheader>
        <newline line="1" column="19">![CDATA[
    ]]</newline>
        <newline line="2" column="0">![CDATA[
    ]]</newline>
        <int line="3" column="0">int</int>
        <space line="3" column="3"> </space>
        <identifier line="3" column="4">main</identifier>
        <leftparen line="3" column="8">(</leftparen>
        <rightparen line="3" column="9">)</rightparen>
        <newline line="3" column="10">![CDATA[
    ]]</newline>
        <leftbrace line="4" column="0">{</leftbrace>
        <newline line="4" column="1">![CDATA[
    ]]</newline>
        <space line="5" column="0">    </space>
        <identifier line="5" column="4">std</identifier>
        <colon_colon line="5" column="7">::</colon_colon>
        <identifier line="5" column="9">cout</identifier>
        <space line="5" column="13"> </space>
        <shiftleft line="5" column="14">&lt;&lt;</shiftleft>
        <space line="5" column="16"> </space>
        <stringlit line="5" column="17">"Hello World\n"</stringlit>
        <semicolon line="5" column="32">;</semicolon>
        <newline line="5" column="33">![CDATA[
    ]]</newline>
        <rightbrace line="6" column="0">}</rightbrace>
        <newline line="6" column="1">![CDATA[
    ]]</newline>
        <eof line="7" column="0"></eof>
    </cpp-source>

Note: If the source code does not use line splicing, then concatenation of all XML node values is equivalent to the original C++ code.

Show source code for this transformation

Hide source code

    # transform the source file into XML file (another variant)

    foreach f [getSourceFileNames] {
        set outFileName "${f}.xml"
        set outFile [open $outFileName "w"]
        puts $outFile "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
        puts $outFile "<cpp-source file-name=\"${f}\">"
        foreach t [getTokens $f 1 0 -1 -1 {}] {
            set value [lindex $t 0]
            set line [lindex $t 1]
            set column [lindex $t 2]
            set name [lindex $t 3]

            if {$value == "\n"} {
                set value "!\[CDATA\[\n\]]"
            } else {
                set value [regsub -all "&" $value {\&amp;}]
                set value [regsub -all "<" $value {\&lt;}]
                set value [regsub -all ">" $value {\&gt;}]
            }
            puts $outFile "    <${name} line=\"${line}\" column=\"${column}\">${value}</${name}>"
        }
        puts $outFile "</cpp-source>"
        close $outFile
    }

Transformation index