Version 8 of Brace-level pretty printer

Updated 2002-07-09 11:56:47

Script bracelevel.tcl:

# prefix the current brace level on each line -jcw

# I guess the reason for this script is to help in finding missing braces AK.

 proc bracelevel {str} {
   set lev 0
   set out ""
   foreach l [split $str \n] {
     append out $lev \t
     foreach c [split $l ""] {
       append out $c
       switch -- $c \{ { incr lev } \} { incr lev -1 }
     }
     append out \n
   }
   return $out
 }

 puts [bracelevel [read stdin]]

Output of running bracelevel.tcl with itself as input:

 0       # prefix the current brace level on each line -[jcw]
 0
 0       proc bracelevel {str} {
 1         set lev 0
 1         set out ""
 1         foreach l [split $str \n] {
 2           append out $lev \t
 2           foreach c [split $l ""] {
 3             append out $c
 3             switch -- $c \{ { incr lev } \} { incr lev -1 }
 3           }
 2           append out \n
 2         }
 1         return $out
 1       }
 0
 0       puts [bracelevel [read stdin]]
 0

AM Change the first append to:

   append out [string repeat "=" $lev] \t

for a more graphical layout