Change identifier naming convention from CamelCase to standard_lowercase

# to_lower

# Change identifier naming convention from CamelCase to standard_lowercase

Description:

This transformation allows to modify the naming convention of all identifiers from CamelCase to standard_lowercase, as used by the standard library or Boost.

For example, the following code:

    namespace MyTools
    {

    class MyClass
    {
    public:
        void myFunction();
    };

    }

will be transformed into this:

    namespace my_tools
    {

    class my_class
    {
    public:
        void my_function();
    };

    }

Note: The transformation is performed in place, which means that the source files are modified.

Note: This transformation does not modify comments and string literals.

Show source code for this transformation

Hide source code

    # transform indentifier names from CamelCase to standard_lower_case

    # this list contains exceptional mappings as pairs ?original new?

    set exceptions {SOMESpecialName some_special_name SOMEOTHER some_other}

    proc transformIdentifier {old} {
        global exceptions

        if [string is lower $old] {
            set new $old
        } else {
            set e [lsearch $exceptions $old]
            if {$e != -1} {
                set new [lindex $exceptions [expr $e + 1]]
            } else {
                set state "upper"
                set new ""
                for {set i 0} {$i != [string length $old]} {incr i} {
                    set c [string index $old $i]
                    if [string is upper $c] {
                        if {$state == "upper"} {
                            set new ${new}[string tolower $c]
                        } else {
                            set new ${new}_[string tolower $c]
                            set state "upper"
                        }
                    } else {
                        set new ${new}${c}
                        set state "lower"
                    }
                }
            }
        }

        return $new
    }

    foreach fileName [getSourceFileNames] {
        set tokens [getTokens $fileName 1 0 -1 -1 {}]
        set newFile [open $fileName "w"]
        foreach t $tokens {
            set tokenValue [lindex $t 0]
            set tokenType [lindex $t 3]
            if {$tokenType == "identifier"} {
                set newToken [transformIdentifier $tokenValue]
            } else {
                set newToken $tokenValue
            }
            puts -nonewline $newFile $newToken
        }
        close $newFile
    }

Transformation index