Version 7 of Is there a a decent tcl script for finding matching brackets

Updated 2003-05-24 13:33:39

TV apr 16 03 13:16

I was wondering how to find matching brackets, or braces would be fine too, I guess even quotes would do, in *ANY* syntactically corrent tcl/tk script?

I mean preferably a way which is applicable to any code which can be fed to the interpreters, I mean complete general applicability, including comments.

I tried various things as in bwise amoung which some working methods, but I need a nice proc for a simple enough application, like the tml pages I want to generate html pages from a html + tcl extended page in a text editor.

While I'm asking, maybe there is a tcl coded tcl parser of some generality somewhere to be downloaded?


  1. I use frink to do checking for brackets, braces, etc.
  2. I also use TDK's procheck - formerly I used TclPro's version.
  3. See also bracechecker
  4. A my favorite free editor "ASED Tcl/Tk IDE" will format braces, etc. to make errors standout

RS: info complete will bring you most of the way, I think: "info complete command Returns 1 if command is a complete Tcl command in the sense of having no unclosed quotes, braces, brackets or array element names, If the command doesn't appear to be complete then 0 is returned. This command is typically used in line-oriented input environments to allow users to type in commands that span multiple lines; if the command isn't complete, the script can delay evaluating it until additional lines have been typed to complete the command. " (TclHelp)


17apr03 jcw - A bit of end-of-week hacking led to:

  set a "{ a { b{ { c {d}}e } }} f {g h}"
  puts a=$a
  set b [regexp -all -indices -inline \{ $a]
  puts b=$b
  set e [regexp -all -indices -inline \} $a]
  puts e=$e

  set n [split [string repeat 0 [string length $a]] ""]
  foreach x $b { lset n [lindex $x 0] +1 }
  foreach x $e { lset n [lindex $x 0] -1 }
  puts n=$n

  set l 0
  set r {}
  foreach x $n { append r [incr l $x] }

  puts a=$a
  puts r=$r

  puts "matching prefix: [string range $a 0 [string first 0 $r]]"

Output:

  a={ a { b{ { c {d}}e } }} f {g h}
  b={0 0} {4 4} {7 7} {9 9} {13 13} {26 26}
  e={15 15} {16 16} {19 19} {21 21} {22 22} {30 30}
  n=+1 0 0 0 +1 0 0 +1 0 +1 0 0 0 +1 0 -1 -1 0 0 -1 0 -1 -1 0 0 0 +1 0 0 0 -1
  a={ a { b{ { c {d}}e } }} f {g h}
  r=1111222334444554333221000011110
  matching prefix: { a { b{ { c {d}}e } }}