Is there a a decent tcl script for finding matching brackets

TV 2003-04-16T13: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. See also Nagelfar and Static syntax analysis.
  2. I also use TDK's procheck - formerly I used TclPro's version.
  3. See also bracechecker
  4. The editor Ased [L1 ] will format all braces and if the cursor is placed on one end it will show the other end. It's written in Tcl, too.

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)


jcw 2003-04-17: 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 } }}

atk: The code does not work for masked brackets: \{ \}.

AMG: Perhaps improve the regular expressions to match braces preceded by an even number (e.g. zero) of backslashes. It's possible to do this. See [L2 ] for an example of the concept.


2003-05-26 Johann Tienhaara: I have a small package called Tcl-Golems that contains a braces / brackets / quotes / etc checker. Of course it's written in Tcl. Feedback is welcome!


l2f 2013-12-06 : TclCheck, written in C, is my saver.


PYK: 2015-04-03: scriptSplit, followed by cmdSplit, followed by wordparts, completely breaks a valid Tcl script up into its syntactic components. info complete underlies these commands.