Add a general purpose hashtable pattern matcher
[jimtcl.git] / make-index
blob8dba9202fd06a8f82803b5e4b66f59c20c983bcb
1 #!/usr/bin/env tclsh
2 # vim:se syn=tcl:
4 set filename [lindex $argv 0]
5 set f [open $filename]
7 # Read the file looking for command definitions
8 set lines {}
9 set commands {}
10 array set cdict {}
11 set c 0
13 while {[gets $f buf] >= 0} {
14         if {[string match "~~*" $buf]} {
15                 if {[string match "*:*" $prev]} {
16                         incr c
17                         set target cmd_$c
18                         set lines [linsert $lines end-1 "\[\[$target\]\]"]
19                 } else {
20                         set target _$prev
21                 }
22                 foreach cmd [split $prev ":,"] {
23                         set cmd [string trim $cmd]
24                         if {[regexp {^[a-z.]+$} $cmd]} {
25                                 lappend commands [list $cmd $target]
26                                 set cdict($cmd) $target
27                         }
28                 }
29         }
30         lappend lines $buf
31         set prev $buf
33 close $f
35 # Build the command index in the list: $index
36 lappend index {[frame="none",grid="none"]}
37 lappend index {|=========================}
38 set i 0
39 set row {}
40 foreach command [lsort $commands] {
41         lassign $command cmd target
43         append row "|<<$target,*`$cmd`*>> "
44         incr i
45         if {$i % 8 == 0} {
46                 lappend index $row
47                 set row {}
48         }
50 while {$i % 8 != 0} {
51         incr i
52         append row "| "
54 lappend index $row
55 lappend index {|=========================}
57 # Map all `cmd` to <<$target,`cmd`>>
58 set mapping {}
59 foreach c [array names cdict] {
60         lappend mapping `$c` <<$cdict($c),*`$c`*>>
61         lappend mapping "`$c " "<<$cdict($c),*`$c`*>> `"
64 # And the command index
65 lappend mapping @INSERTINDEX@ [join $index \n]
67 # Output the result
68 foreach line $lines {
69         puts [string map $mapping $line]