- Read the complete file from standard input as a string
- Split the string into lines (the command [split $str \n] will do this, \n being the newline character)
- Then split each line into individual characters - this is done with [split $str ""]
- Examine each character: an open brace will increment the count, a close brace will decrement it
- Print the original line with the count of the open braces
- If the open brace count becomes 1, set the line count to 0
- Increase with every line read
- Print the line count rather than the brace count
# 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 set skip 0 foreach c [split $l ""] { append out $c if {$skip} { set skip 0; continue } switch -- $c \{ { incr lev } \} { incr lev -1 } \\ { set skip 1 } } append out \n } return $out } puts [bracelevel [read stdin]]
Output of running bracelevel.tcl with itself as input:
0 0 proc bracelevel {str} { 1 set lev 0 1 set out "" 1 foreach l [split $str \n] { 2 append out $lev \t 2 set skip 0 2 foreach c [split $l ""] { 3 append out $c 3 if {$skip} { set skip 0; continue } 3 switch -- $c \{ { incr lev } \} { incr lev -1 } \\ { set skip 1 } 3 } 2 append out \n 2 } 1 return $out 1 } 0 0 puts [bracelevel [read stdin]] 0
LV To interpret this output - the numbers indicate how many { characters (without matching } characters) have been encountered before the line that follows was read.So, for instance, on the foreach line, before that line was examined, there was one open brace pair. That was the one on the proc statement. Notice that the next line (the first of the append lines), the number has increased to two.
AM Change the first append to:
append out [string repeat "=" $lev] \tfor a more graphical layout.NEM 2008-11-12: Changed proc to deal with backslash-escaped braces too.
frink auto-indents or beautifies Tcl source when invoked as, for example,
frink -egINnz ...
[Explain dump in tkcon.]