Documentation Source Text

Check-in [f73a84dfb0]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Continuing work on the syntax bubble diagrams.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f73a84dfb073d4a038540ced733b12e4b1595c36
User & Date: drh 2008-10-01 19:48:34
Context
2008-10-01
21:28
Continuing work on the bubble syntax diagram generator. check-in: 4b56572316 user: drh tags: trunk
19:48
Continuing work on the syntax bubble diagrams. check-in: f73a84dfb0 user: drh tags: trunk
17:25
Continuing work on the bubble syntax diagrams. check-in: 2bb38606e0 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to art/syntax/bubble-generator.tcl.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
















26
27
28

29
30
31
32

33


34
35
36
37
38
39
40
41
42
43
44
45
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

132













133
134
135



136
137
138
139



140
141
142
143
144
145
146
147
...
153
154
155
156
157
158
159
160


161
162
163
164
165
166
167
...
210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

494
495
496
497
498
499
500
501

502
503
504
505
506
507
508
...
509
510
511
512
513
514
515

516
517
518
519
520
521
522
523
...
524
525
526
527
528
529
530

531
532
533
534
535
536
537
538
...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
...
619
620
621
622
623
624
625




626









627
628
629
630
631
632
633
wm withdraw .

# Graphs:
#
set all_graphs {
  alter-table-stmt {
    stack
       {line ALTER TABLE {opt {line database-name .}} table-name}
       {tailbranch
          {line RENAME TO new-table-name}
          {line ADD {opt COLUMN} column-def}
       }
  }
  X-create-table-stmt-1 {
















    stack
       {line CREATE {or {} TEMP TEMPORARY} TABLE}
       {opt {line IF NOT EXISTS}}

       {line {opt {line database-name .}} table-name}
       {tailbranch
          {line ( {loop column-def ,} {loop {} {, table-constraint}} )}
          {line AS select-stmt}

       }


  }
  create-table-stmt {
    stack
       {line CREATE {or {} TEMP TEMPORARY} TABLE {opt {line IF NOT EXISTS}}}
       {line {opt {line database-name .}} table-name
          {tailbranch
            {line ( {loop column-def ,} {loop {} {, table-constraint}} )}
            {line AS select-stmt}
          }
       }
  }
  column-def {
................................................................................
  }
  type-name {
     line {loop name {}} {or {}
        {line ( number )}
        {line ( number , number )}
     }
  }
  X-column-def-c {
    line column-name {or
        {}
        {line type-name}
        {line type-name ( number )}
        {line type-name ( number , number )}
    } {loop {} column-constraint}
  }
  X-column-def-b {
    line column-name {opt
        {line type-name 
            {opt
               {line ( number {opt {line , number}} )}
            }
        }
    } {loop {} column-constraint}
  }
  column-constraint {
    stack
      {opt {line CONSTRAINT name}}
      {or
         {line PRIMARY KEY conflict-clause {opt AUTOINCREMENT}}
         {line NOT NULL conflict-clause}
         {line UNIQUE conflict-clause}
         {line CHECK ( expr )}
         {line DEFAULT {or value {line ( expr )}}}
         {line COLLATE collation-name}
         {line foreign-key-clause}

      }
  }
  table-constraint {
     stack
       {opt {line CONSTRAINT name}}
       {or
          {line {or {line PRIMARY KEY} UNIQUE}
                ( column-list ) conflict-clause}
          {line CHECK ( expr )}
          {line FOREIGN KEY ( column-list ) foreign-key-clause }
       }
  }
  foreign-key-clause {
      stack 
        {line REFERENCES foreign-table {opt {line ( column-list )}}}
        {toploop
           {or
              {line ON {or DELETE UPDATE INSERT}
                       {or {line SET NULL} {line SET DEFAULT}
                           CASCADE RESTRICT
                       }
              }
              {line MATCH name}
              nil
           }
           {}
        }
        {or
          {line {opt NOT} DEFERRABLE 
             {or 
                {}
                {line INITIALLY DEFERRED}
                {line INITIALLY IMMEDIATE}

             }
          }
          nil
        }
  }
  X-foreign-key-action {
      or
         {line MATCH name}
         {line ON {or DELETE UPDATE INSERT}
                  {or {line SET NULL} {line SET DEFAULT}
                      CASCADE RESTRICT
                  }
         }
  }
  X-foreign-key-deferment {
     line {opt NOT} DEFERRABLE 

          {or 













              {}
              {line INITIALLY DEFERRED}
              {line INITIALLY IMMEDIATE}



          }
  }
  conflict-clause {
    opt {line ON CONFLICT {or ROLLBACK ABORT FAIL IGNORE REPLACE}}



  }
}

# Draw the button bar
#
set bn 0
foreach {name graph} $all_graphs {
  incr bn
................................................................................
set b .bb.b$bn
button $b -text Everything -command {draw_all_graphs}
pack $b -side top -fill x -expand 1

set tagcnt 0                      ;# tag counter
set font1 {Helvetica 16 bold}     ;# default token font
set font2 {Helvetica 15}          ;# default variable font
set RADIUS 13                     ;# default turn radius


set DPI 80                        ;# dots per inch


# Draw a right-hand turn around.  Approximately a ")"
#
proc draw_right_turnback {tag x y0 y1} {
  global RADIUS
................................................................................
# Draw a bubble containing $txt. 
#
proc draw_bubble {txt} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  if {$txt=="nil"} {

    return [list $tag 0 0]
  }
  if {[regexp {^[a-z]} $txt]} {
    set font $::font2
    set istoken 0
  } else {
    set font $::font1
    set istoken 1
................................................................................
# descripts a single term.
#
proc draw_line {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::RADIUS
  set exx 0
  set exy 0
  foreach term $lx {
    set m [draw_diagram $term]
    foreach {t texx texy} $m break
    if {$exx>0} {
      set xn [expr {$exx+$sep}]
................................................................................
# Draw a sequence of terms from right to left.
#
proc draw_backwards_line {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::RADIUS
  set exx 0
  set exy 0
  set lb {}
  set n [llength $lx]
  for {set i [expr {$n-1}]} {$i>=0} {incr i -1} {
    lappend lb [lindex $lx $i]
  }
................................................................................
# Draw a sequence of terms from top to bottom.
#
proc draw_stack {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::RADIUS
  set btm 0
  foreach term $lx {
    set m [draw_diagram $term]
    foreach {t exx exy} $m break
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
    if {$btm==0} {
      set btm $ty1
................................................................................
  return [list $tag $exit_x $exit_y]
}

proc draw_loop {forward back} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::RADIUS
  set vsep [expr {$sep/2}]

  foreach {ft fexx fexy} [draw_diagram $forward] break
  foreach {fx0 fy0 fx1 fy1} [.c bbox $ft] break
  set fw [expr {$fx1-$fx0}]
  foreach {bt bexx bexy} [draw_backwards_line $back] break
  foreach {bx0 by0 bx1 by1} [.c bbox $bt] break
................................................................................
  return [list $tag $x1 $fexy]
}

proc draw_toploop {forward back} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::RADIUS
  set vsep [expr {$sep/2}]

  foreach {ft fexx fexy} [draw_diagram $forward] break
  foreach {fx0 fy0 fx1 fy1} [.c bbox $ft] break
  set fw [expr {$fx1-$fx0}]
  foreach {bt bexx bexy} [draw_backwards_line $back] break
  foreach {bx0 by0 bx1 by1} [.c bbox $bt] break
................................................................................
  return [list $tag $x1 $fexy]
}

proc draw_or {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::RADIUS
  set vsep [expr {$sep/2}]
  set n [llength $lx]
  set i 0
  set mxw 0
  foreach term $lx {
    set m($i) [set mx [draw_diagram $term]]
    set tx [lindex $mx 0]
    foreach {x0 y0 x1 y1} [.c bbox $tx] break
    set w [expr {$x1-$x0}]

    if {$w>$mxw} {set mxw $w}
    incr i
  }
  set mxw [expr {$mxw+20}]        ;# extra space for arrowheads

  set x0 0                        ;# entry x
  set x1 $sep                     ;# decender 
  set x2 [expr {$sep*2}]          ;# start of choice

  set x3 [expr {$mxw+$x2}]        ;# end of choice
  set x4 [expr {$x3+$sep}]        ;# accender
  set x5 [expr {$x4+$sep}]        ;# exit x

  for {set i 0} {$i<$n} {incr i} {
    foreach {t texx texy} $m($i) break
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
................................................................................
    set w [expr {$tx1-$tx0}]
    set dx [expr {($mxw-$w)/2 + $x2}]
    .c move $t $dx 0
    set texx [expr {$texx+$dx}]
    set m($i) [list $t $texx $texy]
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
    if {$i==0} {

      .c create line 0 0 $dx 0 -width 2 -tags $tag
      .c create line $texx $texy [expr {$x5+1}] $texy -width 2 -tags $tag
      set exy $texy
      .c create arc -$sep 0 $sep [expr {$sep*2}] \
         -width 2 -start 90 -extent -90 -tags $tag -style arc
      .c create arc $x4 0 [expr {$x4+2*$sep}] [expr {2*$sep}] \
         -width 2 -start 180 -extent -90 -tags $tag -style arc
      set btm $ty1
................................................................................
    } else {
      set dy [expr {$btm - $ty0 + $vsep}]
      if {$dy<2*$sep} {set dy [expr {2*$sep}]}
      .c move $t 0 $dy
      set texy [expr {$texy+$dy}]
      if {$dx>$x2} {
        .c create line $x2 $dy $dx $dy -width 2 -tags $tag -arrow last

        .c create line $texx $texy $x3 $texy -width 2 -tags $tag -arrow last
      }
      set y1 [expr {$dy-2*$sep}]
      .c create arc $x1 $y1 [expr {$x1+2*$sep}] $dy \
          -width 2 -start 180 -extent 90 -style arc -tags $tag
      set y2 [expr {$texy-2*$sep}]
      .c create arc [expr {$x3-$sep}] $y2 $x4 $texy \
          -width 2 -start 270 -extent 90 -style arc -tags $tag
................................................................................
  return [list $tag $x5 $exy]   
}

proc draw_tail_branch {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::RADIUS
  set vsep [expr {$sep/2}]
  set n [llength $lx]
  set i 0
  foreach term $lx {
    set m($i) [set mx [draw_diagram $term]]
    incr i
  }
................................................................................

proc draw_diagram {spec} {
  set n [llength $spec]
  if {$n==1} {
    return [draw_bubble $spec]
  }
  if {$n==0} {
    return [draw_line {}]
  }
  set cmd [lindex $spec 0]
  if {$cmd=="line"} {
    return [draw_line [lrange $spec 1 end]]
  }
  if {$cmd=="stack"} {
    return [draw_stack [lrange $spec 1 end]]
................................................................................
  if {$cmd=="toploop"} {
    return [draw_toploop [lindex $spec 1] [lindex $spec 2]]
  }
  if {$cmd=="or"} {
    return [draw_or [lrange $spec 1 end]]
  }
  if {$cmd=="opt"} {




    return [draw_or [list {} [lindex $spec 1]]]









  }
  if {$cmd=="tailbranch"} {
    return [draw_tail_branch [lrange $spec 1 end]]
  }
  error "unknown operator: $cmd"
}








|


|


|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
<
>
|
<
<
<
>
|
>
>



|
|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


|








<




|









|













|

<


>





|
|
<
<
<
<
|
<
<
<
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
>
>
>
|
|
<
<
>
>
>








 







|
>
>







 







>
|







 







|







 







|







 







|







 







|







 







|







 







|









>



<




>







 







>
|







 







>
|







 







|







 







|







 







>
>
>
>
|
>
>
>
>
>
>
>
>
>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


43
44



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
..
62
63
64
65
66
67
68

















69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
118
119




120




121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


137
138
139
140
141


142
143
144
145
146
147
148
149
150
151
152
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
...
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

506
507
508
509
510
511
512
513
514
515
516
517
...
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
...
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
...
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
...
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
wm withdraw .

# Graphs:
#
set all_graphs {
  alter-table-stmt {
    stack
       {line ALTER TABLE {optx database-name .} table-name}
       {tailbranch
          {line RENAME TO new-table-name}
          {line ADD {optx COLUMN} column-def}
       }
  }
  analyze-stmt {
     line ANALYZE {optx database-name .} table-name
  }
  attach-stmt {
     line ATTACH {or DATABASE nil} database-filename AS attachment-name
  }
  begin-stmt {
     line BEGIN {or nil DEFERRED IMMEDIATE EXCLUSIVE}
          {optx TRANSACTION {optx name}}
  }
  commit-stmt {
     line {or COMMIT END} {optx TRANSACTION {optx name}}
  }
  rollback-stmt {
     line ROLLBACK {optx TRANSACTION {optx name}}
  }
  create-index-stmt {
    stack


       {line CREATE {opt UNIQUE} INDEX {opt IF NOT EXISTS}}
       {line {optx database-name .} index-name



             ON table-name ( {loop indexed-column {}} )}
  }
  indexed-column {
      line column-name {optx COLLATE collation-name} {or ASC DESC nil} 
  }
  create-table-stmt {
    stack
       {line CREATE {or {} TEMP TEMPORARY} TABLE {opt IF NOT EXISTS}}
       {line {optx database-name .} table-name
          {tailbranch
            {line ( {loop column-def ,} {loop {} {, table-constraint}} )}
            {line AS select-stmt}
          }
       }
  }
  column-def {
................................................................................
  }
  type-name {
     line {loop name {}} {or {}
        {line ( number )}
        {line ( number , number )}
     }
  }

















  column-constraint {
    stack
      {optx CONSTRAINT name}
      {or
         {line PRIMARY KEY conflict-clause {opt AUTOINCREMENT}}
         {line NOT NULL conflict-clause}
         {line UNIQUE conflict-clause}
         {line CHECK ( expr )}
         {line DEFAULT {or value {line ( expr )}}}
         {line COLLATE collation-name}
         {line foreign-key-clause}

      }
  }
  table-constraint {
     stack
       {optx CONSTRAINT name}
       {or
          {line {or {line PRIMARY KEY} UNIQUE}
                ( column-list ) conflict-clause}
          {line CHECK ( expr )}
          {line FOREIGN KEY ( column-list ) foreign-key-clause }
       }
  }
  foreign-key-clause {
      stack 
        {line REFERENCES foreign-table {optx ( column-list )}}
        {toploop
           {or
              {line ON {or DELETE UPDATE INSERT}
                       {or {line SET NULL} {line SET DEFAULT}
                           CASCADE RESTRICT
                       }
              }
              {line MATCH name}
              nil
           }
           {}
        }
        {or
          {line {optx NOT} DEFERRABLE 
             {or 

                {line INITIALLY DEFERRED}
                {line INITIALLY IMMEDIATE}
                {}
             }
          }
          nil
        }
  }
  conflict-clause {
    opt {line ON CONFLICT {or ROLLBACK ABORT FAIL IGNORE REPLACE}}




  }




  expr {
    or
     {line literal-value}
     {line {optx {optx database-name .} table-name .}  column-name}
     {line unary-operator expr}
     {line expr binary-operator expr}
     {line function-name ( {or {toploop expr ,} *} )}
     {line ( expr )}
     {line CAST ( expr AS type-name )}
     {line expr COLLATE collation-name}
     {line expr {optx NOT} {or LIKE GLOB REGEXP MATCH} expr
           {optx ESCAPE expr}}
     {line expr {or ISNULL NOTNULL {line IS NULL} {line NOT NULL}}}
     {line expr {optx NOT} BETWEEN expr AND expr}
     {line expr {optx NOT} IN 
            {or


               {line ( select-stmt )}
               {line ( {loop expr ,} )}
               {line {optx database-name .} table-name}
            }
     }


     {line {optx {optx NOT} EXISTS} ( select-stmt )}
     {line CASE {optx expr} {loop {line WHEN expr THEN expr} {}}
           {optx ELSE expr} END}
  }
}

# Draw the button bar
#
set bn 0
foreach {name graph} $all_graphs {
  incr bn
................................................................................
set b .bb.b$bn
button $b -text Everything -command {draw_all_graphs}
pack $b -side top -fill x -expand 1

set tagcnt 0                      ;# tag counter
set font1 {Helvetica 16 bold}     ;# default token font
set font2 {Helvetica 15}          ;# default variable font
set RADIUS 9                      ;# default turn radius
set HSEP 17                       ;# horizontal separation
set VSEP 9                        ;# vertical separation
set DPI 80                        ;# dots per inch


# Draw a right-hand turn around.  Approximately a ")"
#
proc draw_right_turnback {tag x y0 y1} {
  global RADIUS
................................................................................
# Draw a bubble containing $txt. 
#
proc draw_bubble {txt} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  if {$txt=="nil"} {
    .c create line 0 0 1 0 -width 2 -tags $tag
    return [list $tag 1 0]
  }
  if {[regexp {^[a-z]} $txt]} {
    set font $::font2
    set istoken 0
  } else {
    set font $::font1
    set istoken 1
................................................................................
# descripts a single term.
#
proc draw_line {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::HSEP
  set exx 0
  set exy 0
  foreach term $lx {
    set m [draw_diagram $term]
    foreach {t texx texy} $m break
    if {$exx>0} {
      set xn [expr {$exx+$sep}]
................................................................................
# Draw a sequence of terms from right to left.
#
proc draw_backwards_line {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::HSEP
  set exx 0
  set exy 0
  set lb {}
  set n [llength $lx]
  for {set i [expr {$n-1}]} {$i>=0} {incr i -1} {
    lappend lb [lindex $lx $i]
  }
................................................................................
# Draw a sequence of terms from top to bottom.
#
proc draw_stack {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt

  set sep $::VSEP
  set btm 0
  foreach term $lx {
    set m [draw_diagram $term]
    foreach {t exx exy} $m break
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
    if {$btm==0} {
      set btm $ty1
................................................................................
  return [list $tag $exit_x $exit_y]
}

proc draw_loop {forward back} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::VSEP
  set vsep [expr {$sep/2}]

  foreach {ft fexx fexy} [draw_diagram $forward] break
  foreach {fx0 fy0 fx1 fy1} [.c bbox $ft] break
  set fw [expr {$fx1-$fx0}]
  foreach {bt bexx bexy} [draw_backwards_line $back] break
  foreach {bx0 by0 bx1 by1} [.c bbox $bt] break
................................................................................
  return [list $tag $x1 $fexy]
}

proc draw_toploop {forward back} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::VSEP
  set vsep [expr {$sep/2}]

  foreach {ft fexx fexy} [draw_diagram $forward] break
  foreach {fx0 fy0 fx1 fy1} [.c bbox $ft] break
  set fw [expr {$fx1-$fx0}]
  foreach {bt bexx bexy} [draw_backwards_line $back] break
  foreach {bx0 by0 bx1 by1} [.c bbox $bt] break
................................................................................
  return [list $tag $x1 $fexy]
}

proc draw_or {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::VSEP
  set vsep [expr {$sep/2}]
  set n [llength $lx]
  set i 0
  set mxw 0
  foreach term $lx {
    set m($i) [set mx [draw_diagram $term]]
    set tx [lindex $mx 0]
    foreach {x0 y0 x1 y1} [.c bbox $tx] break
    set w [expr {$x1-$x0}]
    if {$i>0} {set w [expr {$w+20}]}  ;# extra space for arrowheads
    if {$w>$mxw} {set mxw $w}
    incr i
  }


  set x0 0                        ;# entry x
  set x1 $sep                     ;# decender 
  set x2 [expr {$sep*2}]          ;# start of choice
  set xc [expr {$mxw/2}]          ;# center point
  set x3 [expr {$mxw+$x2}]        ;# end of choice
  set x4 [expr {$x3+$sep}]        ;# accender
  set x5 [expr {$x4+$sep}]        ;# exit x

  for {set i 0} {$i<$n} {incr i} {
    foreach {t texx texy} $m($i) break
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
................................................................................
    set w [expr {$tx1-$tx0}]
    set dx [expr {($mxw-$w)/2 + $x2}]
    .c move $t $dx 0
    set texx [expr {$texx+$dx}]
    set m($i) [list $t $texx $texy]
    foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
    if {$i==0} {
      if {$dx>$x2} {set ax last} {set ax none}
      .c create line 0 0 $dx 0 -width 2 -tags $tag -arrow $ax
      .c create line $texx $texy [expr {$x5+1}] $texy -width 2 -tags $tag
      set exy $texy
      .c create arc -$sep 0 $sep [expr {$sep*2}] \
         -width 2 -start 90 -extent -90 -tags $tag -style arc
      .c create arc $x4 0 [expr {$x4+2*$sep}] [expr {2*$sep}] \
         -width 2 -start 180 -extent -90 -tags $tag -style arc
      set btm $ty1
................................................................................
    } else {
      set dy [expr {$btm - $ty0 + $vsep}]
      if {$dy<2*$sep} {set dy [expr {2*$sep}]}
      .c move $t 0 $dy
      set texy [expr {$texy+$dy}]
      if {$dx>$x2} {
        .c create line $x2 $dy $dx $dy -width 2 -tags $tag -arrow last
        if {$dx<$xc-2} {set ax last} {set ax none}
        .c create line $texx $texy $x3 $texy -width 2 -tags $tag -arrow $ax
      }
      set y1 [expr {$dy-2*$sep}]
      .c create arc $x1 $y1 [expr {$x1+2*$sep}] $dy \
          -width 2 -start 180 -extent 90 -style arc -tags $tag
      set y2 [expr {$texy-2*$sep}]
      .c create arc [expr {$x3-$sep}] $y2 $x4 $texy \
          -width 2 -start 270 -extent 90 -style arc -tags $tag
................................................................................
  return [list $tag $x5 $exy]   
}

proc draw_tail_branch {lx} {
  global tagcnt
  incr tagcnt
  set tag x$tagcnt
  set sep $::VSEP
  set vsep [expr {$sep/2}]
  set n [llength $lx]
  set i 0
  foreach term $lx {
    set m($i) [set mx [draw_diagram $term]]
    incr i
  }
................................................................................

proc draw_diagram {spec} {
  set n [llength $spec]
  if {$n==1} {
    return [draw_bubble $spec]
  }
  if {$n==0} {
    return [draw_bubble nil]
  }
  set cmd [lindex $spec 0]
  if {$cmd=="line"} {
    return [draw_line [lrange $spec 1 end]]
  }
  if {$cmd=="stack"} {
    return [draw_stack [lrange $spec 1 end]]
................................................................................
  if {$cmd=="toploop"} {
    return [draw_toploop [lindex $spec 1] [lindex $spec 2]]
  }
  if {$cmd=="or"} {
    return [draw_or [lrange $spec 1 end]]
  }
  if {$cmd=="opt"} {
    set args [lrange $spec 1 end]
    if {[llength $args]==1} {
      return [draw_or [list nil [lindex $args 0]]]
    } else {
      return [draw_or [list nil "line $args"]]
    }
  }
  if {$cmd=="optx"} {
    set args [lrange $spec 1 end]
    if {[llength $args]==1} {
      return [draw_or [list [lindex $args 0] nil]]
    } else {
      return [draw_or [list "line $args" nil]]
    }
  }
  if {$cmd=="tailbranch"} {
    return [draw_tail_branch [lrange $spec 1 end]]
  }
  error "unknown operator: $cmd"
}