-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamiga_pathname_diagrams.tcl
1394 lines (1278 loc) · 39.3 KB
/
amiga_pathname_diagrams.tcl
1
2
3
4
5
6
7
8
9
10
11
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/wish
#
# POSIX pathname syntax diagram generator for M2C, M2Sharp and M2J.
#
# This script is derived from the SQLite project's bubble-generator script.
# It is quite possibly the only such tool that can wrap-around diagrams so
# that they do not go off-page when inserting them into an ordinary A4 or
# US letter document. Thanks to the folks at the SQLite project for making
# their script available to the public.
#
# The present version of the script was cleaned up, enhanced, documented
# and modified by B.Kowarsch to become accessible to those unfamiliar with
# TCL/TK, and in particular to generate syntax diagrams for Modula-2 (R10).
#
# Ideally the design would have been changed such that the script can read
# grammars from a text file in EBNF notation. Ideally, this script would
# have been rewritten in a more readable language, Modula-2 for instance.
# Due to time constraints, these tasks had to be left for some other time
# or for somebody else to do. In the meantime the documentation embedded
# herein should suffice even for those unfamiliar with TCL to modify the
# script to generate diagrams for their own grammars.
#
# THIS SOFTWARE COMES WITHOUT ANY WARRANTY OF ANY KIND. USE IS STRICTLY AT
# THE RISK OF THE USER. PERSONS WHO HAVE A LEGAL RIGHT TO SUE AUTHORS OF
# NO-WARRANTY-FREE-OF-CHARGE OPEN SOURCE SOFTWARE ARE SPECIFICALLY *NOT*
# GIVEN ANY PERMISSION TO USE THIS SOFTWARE. THE BOTTOM LINE IS : YOU MAY
# USE THIS SOFTWARE WITHOUT PERMISSION ANYWAY, BUT YOU *CANNOT* SUE THE
# AUTHORS FOR DAMAGES BECAUSE IF YOUR GOVERNMENT GRANTS YOU SUCH RIGHTS
# THEN YOU DID *NOT* HAVE PERMISSION TO USE THIS SOFTWARE TO BEGIN WITH.
# THUS, NO MATTER WHAT THE CIRCUMSTANCES, THE RISK IS ALWAYS YOURS ALONE.
#
# Top-level displays
#
toplevel .bb
canvas .c -bg white
pack .c -side top -fill both -expand 1
wm withdraw .
#
# ===========================================================================
# D O C U M E N T A T I O N
# ===========================================================================
#
# The grammar is encoded as a nested TCL list structure of the general form:
#
# { production1 { ... } production2 { ... } ... }
#
# Production rules can be translated from (ANTLR) EBNF to TCL list items as
# follows:
#
# Simple term
#
# production : term ;
# => production { line term }
#
# Sequence and group
#
# production : term1 term2 ;
# => production { line term1 term2 }
#
# Alternative
#
# production : term1 | term2 ;
# => production { or term1 term2 }
#
# Optional term
#
# production : term? ;
# => production { opt term }
# => production { optx term }
#
# opt renders the bypass line in the main path
# optx renders the term in the main path
#
# Terms that occur one or more times
#
# production : term+ ;
# => production { loop { line term } {} }
#
# Terms that occur zero or more times
#
# production : term* ;
# => production { loop {} { nil term } }
#
# Causing diagrams to wrap-around
#
# production : term1 /* wrap here */ term2 /* wrap here */ term3 ;
# => production { stack {line term1} {line term2} {line term3} }
#
# Rendering of terminals, non-terminals and tokens
#
# Symbols are rendered according to their category:
# (1) reserved words, names in all uppercase letters
# (2) reserved identifiers, names in all uppercase letters preceded by /
# (3) other terminals, mixed case names with a leading uppercase letter
# (4) non-terminals, mixed case names with a leading lowercase letter
# (5) single letter tokens, a single letter or a range eg. a..z / A..Z
# (6) special symbol tokens, any other characters or character sequences
#
# Special names for tokens that TCL cannot handle verbatim
#
# BACKSLASH is rendered as \
# SINGLE_QUOTE is rendered as '
# DOUBLE_QUOTE is rendered as "
# LEFT_BRACE is rendered as {
# RIGHT_BRACE is rendered as }
#
# Rendering parameters
#
# RES_WORD_FONT - font/size/style used to render reserved words
# RES_IDENT_FONT - font/size/style used to render reserved identifiers
# TERM_FONT - font/size/style used to render any other terminals
# NON_TERM_FONT - font/size/style used to render non-terminals
# TOKEN_FONT - font/size/style used to render tokens
# RADIUS - turn radius for arcs
# HSEP - horizontal separation
# VSEP - vertical separation
# LWIDTH -line width
#
# Pre-requisites
#
# TCL and TK need to be installed
#
# Running the script
#
# the most fool-proof method to run this script is to call the wish shell
# with the name of the script as an argument:
#
# $ wish posix_pathname_diagrams.tcl
#
# in the window that appears, click on the top button "Draw All Diagrams"
#
# Diagrams will be written into postscript files in the working directory
#
# ===========================================================================
#
# ===========================================================================
# Amiga Pathname Grammar for M2C/M2J/M2Sharp
# ===========================================================================
# To reuse this diagram generator for other languages, replace the following
# section with a definition of the grammar of the target language.
#
# Do NOT add comments or blank lines within a production rule's definition!
#
# ---------------------------------------------------------------------------
# Non-Terminal Symbols
# ---------------------------------------------------------------------------
#
set non_terminals {}
# (1) Pathname
#
# pathname :=
# rootPath | parentPath | relativePath | filenameOnly
# ;
#
lappend non_terminals amiga_pathname {
or
rootpath
parentPath
relativePath
fileNameOnly
}
# (2) Root Path
#
# rootPath :=
# devname? ':' relativePath
# ;
lappend non_terminals amiga_rootPath {
line {optx devname} : relativePath
}
# (3) Device Name
#
# devname :=
# Letter ComponentChar* ( ' ' ComponentChar+ )*
# ;
lappend non_terminals amiga_device {
line Letter {loop {} {ComponentChar}}
{loop {} {Space {loop {ComponentChar}}}}
}
# (4) Parent Path
#
# parentPath :=
# '/'+ relativePath
# ;
lappend non_terminals amiga_parentPath {
line {loop {BACKSLASH}} relativePath
}
# (5) Relative Path
#
# relativePath :=
# ( pathComponent '/' )* pathComponent?
# ;
lappend non_terminals amiga_relativePath {
line {loop {} {pathComponent BACKSLASH}} {optx pathComponent}
}
# (6) Path Component
#
# pathComponent :=
# '.'? pathSubComponent ( '.' pathSubComponent )*
# ;
lappend non_terminals amiga_pathComponent {
line {optx .} pathSubComponent {loop {} {nil . pathSubComponent}}
}
# (7) Path Sub-Component
#
# pathSubComponent :=
# ComponentLeadChar ComponentChar* ( ' ' ComponentChar+ )*
# ;
lappend non_terminals amiga_pathSubComponent {
line ComponentLeadChar {loop {} {nil ComponentChar}}
{loop {} {nil Space {loop {ComponentChar} } } }
}
# (8) Filename Only
#
# alias filenameOnly = pathComponent ;
#
lappend non_terminals amiga_filenameOnly {
line pathComponent
}
# ---------------------------------------------------------------------------
# Terminal Symbols
# ---------------------------------------------------------------------------
#
set terminals {}
# (1) Path Component Character
#
# ComponentChar :=
# ComponentLeadChar | '-'
# ;
lappend terminals amiga_ComponentChar {
or
ComponentLeadChar
-
}
# (2) Alternative Path Component Character #1
#
# AltComponentChar1 :=
# ComponentLeadChar | '-' | '~'
# ;
lappend terminals amiga_AltComponentChar1 {
or
ComponentLeadChar
-
~
}
# (3) Alternative Path Component Character #2
#
# AltComponentChar2 :=
# ComponentLeadChar | '-' | '^'
# ;
lappend terminals amiga_AltComponentChar2 {
or
ComponentLeadChar
-
^
}
# (4) Alternative Path Component Character #3
#
# AltComponentChar3 :=
# ComponentLeadChar | '-' | '~' | '^'
# ;
lappend terminals amiga_AltComponentChar3 {
or
ComponentLeadChar
-
~
^
}
# (5) Path Component Lead Character
#
# ComponentLeadChar :=
# Letter | Digit | '_'
# ;
lappend terminals amiga_ComponentLeadChar {
or
Letter
Digit
_
}
# (6) Digit
#
# Digit :=
# '0' .. '9'
# ;
lappend terminals amiga_Digit {
line 0..9
}
# (7) Letter
#
# Letter :=
# 'a' .. 'z' | 'A' .. 'Z'
# ;
lappend terminals amiga_Letter {
or
a..z
A..Z
}
# ---------------------------------------------------------------------------
# Ignore Symbols
# ---------------------------------------------------------------------------
#
set ignore_symbols {}
# NONE
# ---------------------------------------------------------------------------
# Pragmas
# ---------------------------------------------------------------------------
#
set pragmas {}
# NONE
# ---------------------------------------------------------------------------
# Alias Diagrams
# ---------------------------------------------------------------------------
#
set aliases {}
# NONE
# ---------------------------------------------------------------------------
# Legend Diagrams
# ---------------------------------------------------------------------------
#
set legend {}
# EBNF -- Terminal #
lappend legend EBNF_terminal {
line Terminal
}
# EBNF -- Non-Terminal #
lappend legend EBNF_non_terminal {
line nonTerminal
}
# EBNF -- Literal #
lappend legend EBNF_literal_hash {
line #
}
# EBNF -- Literal BAZ
lappend legend EBNF_literal_BAZ {
line BAZ
}
# EBNF -- Sequence #
lappend legend EBNF_sequence {
line bar baz
}
# EBNF -- Alternative #
lappend legend EBNF_alternative {
or bar baz
}
# EBNF -- Grouping #
lappend legend EBNF_grouping {
line bar {or baz bam}
}
# EBNF -- Option #
lappend legend EBNF_option {
optx bar
}
# EBNF -- Kleene Star #
lappend legend EBNF_kleene_star {
loop nil bar
}
# EBNF -- One Or More #
lappend legend EBNF_one_or_more {
loop bar nil
}
# EBNF -- (bar baz)+ #
lappend legend EBNF_bar_baz_plus {
loop {line bar baz} nil
}
# EBNF -- (bar baz) | (bar bam) #
lappend legend EBNF_bar_baz_or_bar_bam {
line {or {line bar baz} {line bar bam}}
}
# EBNF -- bar (baz| bam) #
lappend legend EBNF_bar_or_baz_bam {
line bar {or baz bam}
}
# EBNF -- bar (baz| bam) #
lappend legend EBNF_code_option {
line {optx bar} baz
}
# EBNF -- bar (baz| bam) #
lappend legend EBNF_code_kleene_star {
line {loop nil bar} baz
}
# EBNF -- bar (baz| bam) #
lappend legend EBNF_code_plus {
line {loop bar nil} baz
}
# Legend -- Reserved Word
lappend legend legendReservedWord {
line RESERVED
}
# Legend -- Terminal Symbol, Example 1
lappend legend legendTerminal1 {
line #
}
# Legend -- Terminal Symbol, Example 2
lappend legend legendTerminal2 {
line Terminal
}
# Legend -- Identifier
lappend legend legendIdentifier {
line /IDENTIFIER
}
# Legend -- Non-Terminal Symbol
lappend legend legendNonTerminal {
line nonTerminal
}
# end posix pathname grammar
#
# ===========================================================================
# C O D E S T A R T S H E R E
# ===========================================================================
#
# ---------------------------------------------------------------------------
# Draw the button box
# ---------------------------------------------------------------------------
#
set bn 0
set b .bb.b$bn
wm title .bb "M2"
# Menu: All Diagrams
#
button $b -text "Draw All Diagrams" -command {draw_all_graphs}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Non-Terminals
#
incr bn
set b .bb.b$bn
button $b -text "Draw Non-Terminals" -command {draw_graphs $non_terminals}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Terminals
#
incr bn
set b .bb.b$bn
button $b -text "Draw Terminals" -command {draw_graphs $terminals}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Reserved Words
#
incr bn
set b .bb.b$bn
button $b -text "Draw Reserved Words" -command {draw_graphs $res_words}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Special Symbols
#
incr bn
set b .bb.b$bn
button $b -text "Draw Special Symbols" -command {draw_graphs $res_symbols}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Pragmas
#
incr bn
set b .bb.b$bn
button $b -text "Draw Pragmas" -command {draw_graphs $pragmas}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Ignore Symbols
#
incr bn
set b .bb.b$bn
button $b -text "Draw Ignore Symbols" -command {draw_graphs $ignore_symbols}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Aliases
#
incr bn
set b .bb.b$bn
button $b -text "Draw Aliases" -command {draw_graphs $aliases}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Legend
#
incr bn
set b .bb.b$bn
button $b -text "Draw Legend" -command {draw_graphs $legend}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: posix_pathnames
#
incr bn
set b .bb.b$bn
button $b -text "Draw Posix Pathnames" -command {draw_graphs $posix_pathnames}
pack $b -side top -fill x -expand 0 -pady 0
# Menu: Quit
#
incr bn
set b .bb.b$bn
button $b -text "Quit" -command {exit}
pack $b -side top -fill x -expand 0 -pady {0 14}
# ---------------------------------------------------------------------------
# L a y o u t - P a r a m e t e r s
# ---------------------------------------------------------------------------
#
set RES_WORD_FONT {Helvetica 12 bold}; # reserved word font
set RES_IDENT_FONT {Helvetica 12 bold italic}; # reserved identifier font
set TERM_FONT {Helvetica 12 bold}; # font for other terminals
set NON_TERM_FONT {Helvetica 12}; # non-terminal font
set TOKEN_FONT {Monaco 12 bold}; # special symbol token font
set STRING_FONT {Courier 12 bold}; # quoted string font
set RADIUS 9; # default turn radius
set HSEP 15; # horizontal separation
set VSEP 7; # vertical separation
set LWIDTH 3; # line width
set DPI 96; # dots per inch for GIFs
set tagcnt 0; # tag counter - don't modify this
# ---------------------------------------------------------------------------
# Draw a right-hand turn around. Approximately a ")"
# ---------------------------------------------------------------------------
#
proc draw_right_turnback {tag x y0 y1} {
global RADIUS
global LWIDTH
if {$y0 + 2*$RADIUS < $y1} {
set xr0 [expr {$x-$RADIUS}]
set xr1 [expr {$x+$RADIUS}]
.c create arc $xr0 $y0 $xr1 [expr {$y0+2*$RADIUS}] \
-width $LWIDTH -start 90 -extent -90 -tags $tag -style arc
set yr0 [expr {$y0+$RADIUS}]
set yr1 [expr {$y1-$RADIUS}]
if {abs($yr1-$yr0)>$RADIUS*2} {
set half_y [expr {($yr1+$yr0)/2}]
.c create line $xr1 $yr0 $xr1 $half_y -width $LWIDTH -tags $tag -arrow last
.c create line $xr1 $half_y $xr1 $yr1 -width $LWIDTH -tags $tag
} else {
.c create line $xr1 $yr0 $xr1 $yr1 -width $LWIDTH -tags $tag
}
.c create arc $xr0 [expr {$y1-2*$RADIUS}] $xr1 $y1 \
-width $LWIDTH -start 0 -extent -90 -tags $tag -style arc
} else {
set r [expr {($y1-$y0)/2.0}]
set x0 [expr {$x-$r}]
set x1 [expr {$x+$r}]
.c create arc $x0 $y0 $x1 $y1 \
-width $LWIDTH -start 90 -extent -180 -tags $tag -style arc
}
} ;# end draw_right_turnback
# ---------------------------------------------------------------------------
# Draw a left-hand turn around. Approximatley a "("
# ---------------------------------------------------------------------------
#
proc draw_left_turnback {tag x y0 y1 dir} {
global RADIUS
global LWIDTH
if {$y0 + 2*$RADIUS < $y1} {
set xr0 [expr {$x-$RADIUS}]
set xr1 [expr {$x+$RADIUS}]
.c create arc $xr0 $y0 $xr1 [expr {$y0+2*$RADIUS}] \
-width $LWIDTH -start 90 -extent 90 -tags $tag -style arc
set yr0 [expr {$y0+$RADIUS}]
set yr1 [expr {$y1-$RADIUS}]
if {abs($yr1-$yr0)>$RADIUS*3} {
set half_y [expr {($yr1+$yr0)/2}]
if {$dir=="down"} {
.c create line $xr0 $yr0 $xr0 $half_y -width $LWIDTH -tags $tag -arrow last
.c create line $xr0 $half_y $xr0 $yr1 -width $LWIDTH -tags $tag
} else {
.c create line $xr0 $yr1 $xr0 $half_y -width $LWIDTH -tags $tag -arrow last
.c create line $xr0 $half_y $xr0 $yr0 -width $LWIDTH -tags $tag
}
} else {
.c create line $xr0 $yr0 $xr0 $yr1 -width $LWIDTH -tags $tag
}
# .c create line $xr0 $yr0 $xr0 $yr1 -width $LWIDTH -tags $tag
.c create arc $xr0 [expr {$y1-2*$RADIUS}] $xr1 $y1 \
-width $LWIDTH -start 180 -extent 90 -tags $tag -style arc
} else {
set r [expr {($y1-$y0)/2.0}]
set x0 [expr {$x-$r}]
set x1 [expr {$x+$r}]
.c create arc $x0 $y0 $x1 $y1 \
-width $LWIDTH -start 90 -extent 180 -tags $tag -style arc
}
} ;# end draw_left_turnback
# ---------------------------------------------------------------------------
# Draw a bubble containing $txt.
# ---------------------------------------------------------------------------
#
proc draw_bubble {txt} {
global LWIDTH
global tagcnt
incr tagcnt
set tag x$tagcnt
if {$txt=="nil"} {
.c create line 0 0 1 0 -width $LWIDTH -tags $tag
return [list $tag 1 0]
} elseif {$txt=="bullet"} {
.c create oval 0 -3 6 3 -width $LWIDTH -tags $tag
return [list $tag 6 0]
}
# special name replacements
set isQuotedString 0
if {$txt=="SPACE"} {
set label "' '"
} elseif {$txt=="BACKSLASH"} {
set label "\\"
} elseif {$txt=="SINGLE_QUOTE"} {
set label "\'"
} elseif {$txt=="DOUBLE_QUOTE"} {
set label "\""
} elseif {$txt=="LBRACE" || $txt=="LEFT_BRACE"} {
set label "\{"
} elseif {$txt=="RBRACE" || $txt=="RIGHT_BRACE"} {
set label "\}"
} else {
set label $txt
}
# initialise symbol flags
set isReservedIdent 0
set isNonTerminal 0
set isTerminal 0
set isToken 0
# determine type of symbol
if {[regexp {^[A-Z][A-Z]+$} $label]} {
# reserved word
set isTerminal 1
set isReservedWord 1
set font $::RES_WORD_FONT
set label " $label "
set baseline [expr {$LWIDTH/2}]
} elseif {[regexp {^/[A-Z][A-Z]+$} $label]} {
set label [string range $label 1 end]
# reserved identifier
set isTerminal 1
set isReservedIdent 1
set font $::RES_IDENT_FONT
set label " $label "
set baseline [expr {$LWIDTH/2}]
} elseif {[regexp {^[A-Z][a-z0-9][a-zA-Z0-9]*$} $label]} {
# other terminal
set isTerminal 1
set font $::TERM_FONT
set label " $label "
set baseline [expr {$LWIDTH/2}]
} elseif {[regexp {^[a-z][a-zA-Z0-9]+$} $label]} {
# non-terminal
set isNonTerminal 1
set font $::NON_TERM_FONT
set label " $label "
set baseline 0
} elseif {[regexp {^`[a-zA-Z0-9]+$} $label]} {
# quoted string literal
set label [string range $label 1 end]
set isToken 1
set font $::STRING_FONT
set label " \"$label\" "
set baseline [expr {$LWIDTH/2}]
} elseif {[regexp {^[a-zA-Z]$} $label]} {
# single letter token
set isToken 1
set font $::TOKEN_FONT
set baseline [expr {$LWIDTH/2}]
} elseif {[regexp {^[a-zA-Z0-9]\.\.[a-zA-Z0-9]$} $label]} {
# letter or digit range
set isToken 1
set font $::TOKEN_FONT
set baseline [expr {$LWIDTH/2}]
} else {
# anything else is a token
set isToken 1
set font $::TOKEN_FONT
set baseline [expr {$LWIDTH/2}]
}
set id1 [.c create text 0 $baseline -anchor c -text $label -font $font -tags $tag]
# lassign [.c bbox $id1] x0 y0 x1 y1 # to do: replace all foreach with lassign
foreach {x0 y0 x1 y1} [.c bbox $id1] break
# move parentheses, brackets, braces and underscore up by one pixel
if {$label in {( ) [ ] \{ \} _ }} { .c move $id1 0 -1 }
# move the asterisk down by one pixel
if {$label=="*"} { .c move $id1 0 1 }
# move label left by one pixel if font is italic
set slantAttr [font actual $font -slant]
if {$slantAttr eq "italic"} { .c move $id1 -1 0 }
set h [expr {$y1-$y0+$LWIDTH}]
set rad [expr {($h+1)/2}]
if {$isNonTerminal} {
set top [expr {$y0-$LWIDTH}]
set btm [expr {$y1+$LWIDTH}]
set left [expr {$x0-$LWIDTH}]
set right [expr {$x1+$LWIDTH}]
.c create rect $left $top $right $btm -width $LWIDTH -tags $tag
} else {
set top [expr {$y0-$LWIDTH}]
set btm [expr {$y1+1}]
set left [expr {$x0+$LWIDTH}]
set right [expr {$x1-$LWIDTH}]
if {$left>$right} {
set left [expr {($x0+$x1)/2}]
set right $left
}
.c create arc [expr {$left-$rad}] $top [expr {$left+$rad}] $btm \
-width $LWIDTH -start 90 -extent 180 -style arc -tags $tag
.c create arc [expr {$right-$rad}] $top [expr {$right+$rad}] $btm \
-width $LWIDTH -start -90 -extent 180 -style arc -tags $tag
if {$left<$right} {
.c create line $left $top $right $top -width $LWIDTH -tags $tag
.c create line $left $btm $right $btm -width $LWIDTH -tags $tag
}
}
foreach {x0 y0 x1 y1} [.c bbox $tag] break
set width [expr {$x1-$x0}]
.c move $tag [expr {-$x0}] 0
# Entry is always 0 0
# Return: TAG EXIT_X EXIT_Y
#
return [list $tag $width 0]
} ;# end draw_bubble
# ---------------------------------------------------------------------------
# Draw a sequence of terms from left to write.
# ---------------------------------------------------------------------------
# Each element of $lx describes a single term.
#
proc draw_line {lx} {
global LWIDTH
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}]
.c move $t $xn $exy
.c create line [expr {$exx-1}] $exy $xn $exy \
-tags $tag -width $LWIDTH -arrow last
set exx [expr {$xn+$texx}]
} else {
set exx $texx
}
set exy $texy
.c addtag $tag withtag $t
.c dtag $t $t
}
if {$exx==0} {
set exx [expr {$sep*2}]
.c create line 0 0 $sep 0 -width $LWIDTH -tags $tag -arrow last
.c create line $sep 0 $exx 0 -width $LWIDTH -tags $tag
set exx $sep
}
return [list $tag $exx $exy]
} ;# end draw_line
# ---------------------------------------------------------------------------
# Draw a sequence of terms from right to left.
# ---------------------------------------------------------------------------
#
proc draw_backwards_line {lx} {
global LWIDTH
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]
}
foreach term $lb {
set m [draw_diagram $term]
foreach {t texx texy} $m break
foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
set w [expr {$tx1-$tx0}]
if {$exx>0} {
set xn [expr {$exx+$sep}]
.c move $t $xn 0
.c create line $exx $exy $xn $exy -tags $tag -width $LWIDTH -arrow first
set exx [expr {$xn+$texx}]
} else {
set exx $texx
}
set exy $texy
.c addtag $tag withtag $t
.c dtag $t $t
}
if {$exx==0} {
.c create line 0 0 $sep 0 -width $LWIDTH -tags $tag
set exx $sep
}
return [list $tag $exx $exy]
} ;# end draw_backwards_line
# ---------------------------------------------------------------------------
# Draw a sequence of terms from top to bottom.
# ---------------------------------------------------------------------------
#
proc draw_stack {indent lx} {
global tagcnt RADIUS VSEP LWIDTH
incr tagcnt
set tag x$tagcnt
set sep [expr {$VSEP*2}]
set btm 0
set n [llength $lx]
set i 0
set next_bypass_y 0
foreach term $lx {
set bypass_y $next_bypass_y
if {$i>0 && $i<$n && [llength $term]>1 &&
([lindex $term 0]=="opt" || [lindex $term 0]=="optx")} {
set bypass 1
set term "line [lrange $term 1 end]"
} else {
set bypass 0
set next_bypass_y 0
}
set m [draw_diagram $term]
foreach {t exx exy} $m break
foreach {tx0 ty0 tx1 ty1} [.c bbox $t] break
if {$i==0} {
set btm $ty1
set exit_y $exy
set exit_x $exx
} else {
set enter_y [expr {$btm - $ty0 + $sep*2 + 2}]
if {$bypass} {set next_bypass_y [expr {$enter_y - $RADIUS}]}
set enter_x [expr {$sep + $indent}]
set back_y [expr {$btm + $sep + 1}]
if {$bypass_y>0} {
set mid_y [expr {($bypass_y+$RADIUS+$back_y)/2}]
.c create line $bypass_x $bypass_y $bypass_x $mid_y \
-width $LWIDTH -tags $tag -arrow last
.c create line $bypass_x $mid_y $bypass_x [expr {$back_y+$RADIUS}] \
-tags $tag -width $LWIDTH
}
.c move $t $enter_x $enter_y
set e2 [expr {$exit_x + $sep}]
.c create line $exit_x $exit_y $e2 $exit_y \
-width $LWIDTH -tags $tag
draw_right_turnback $tag $e2 $exit_y $back_y
set e3 [expr {$enter_x-$sep}]
set bypass_x [expr {$e3-$RADIUS}]
set emid [expr {($e2+$e3)/2}]
.c create line $e2 $back_y $emid $back_y \
-width $LWIDTH -tags $tag -arrow last
.c create line $emid $back_y $e3 $back_y \
-width $LWIDTH -tags $tag
set r2 [expr {($enter_y - $back_y)/2.0}]
draw_left_turnback $tag $e3 $back_y $enter_y down
.c create line $e3 $enter_y $enter_x $enter_y \
-arrow last -width $LWIDTH -tags $tag
set exit_x [expr {$enter_x + $exx}]
set exit_y [expr {$enter_y + $exy}]
}
.c addtag $tag withtag $t
.c dtag $t $t
set btm [lindex [.c bbox $tag] 3]
incr i
}
if {$bypass} {
set fwd_y [expr {$btm + $sep + 1}]
set mid_y [expr {($next_bypass_y+$RADIUS+$fwd_y)/2}]
set descender_x [expr {$exit_x+$RADIUS}]
.c create line $bypass_x $next_bypass_y $bypass_x $mid_y \
-width $LWIDTH -tags $tag -arrow last
.c create line $bypass_x $mid_y $bypass_x [expr {$fwd_y-$RADIUS}] \
-tags $tag -width $LWIDTH
.c create arc $bypass_x [expr {$fwd_y-2*$RADIUS}] \
[expr {$bypass_x+2*$RADIUS}] $fwd_y \
-width $LWIDTH -start 180 -extent 90 -tags $tag -style arc
.c create arc [expr {$exit_x-$RADIUS}] $exit_y \
$descender_x [expr {$exit_y+2*$RADIUS}] \
-width $LWIDTH -start 90 -extent -90 -tags $tag -style arc
.c create arc $descender_x [expr {$fwd_y-2*$RADIUS}] \
[expr {$descender_x+2*$RADIUS}] $fwd_y \
-width $LWIDTH -start 180 -extent 90 -tags $tag -style arc
set exit_x [expr {$exit_x+2*$RADIUS}]
set half_x [expr {($exit_x+$indent)/2}]
.c create line [expr {$bypass_x+$RADIUS}] $fwd_y $half_x $fwd_y \
-width $LWIDTH -tags $tag -arrow last
.c create line $half_x $fwd_y $exit_x $fwd_y \
-width $LWIDTH -tags $tag
.c create line $descender_x [expr {$exit_y+$RADIUS}] \
$descender_x [expr {$fwd_y-$RADIUS}] \
-width $LWIDTH -tags $tag -arrow last
set exit_y $fwd_y
}
set width [lindex [.c bbox $tag] 2]
return [list $tag $exit_x $exit_y]
} ;# end draw_stack
# ---------------------------------------------------------------------------
# Draw a loop
# ---------------------------------------------------------------------------
#
proc draw_loop {forward back} {
global LWIDTH
global tagcnt
incr tagcnt
set tag x$tagcnt
set sep $::HSEP
set vsep $::VSEP
if {$back in {. , ; |}} {
set vsep 0
} elseif {$back=="SINGLE_QUOTE"} {
set vsep 0
} elseif {$back=="nil"} {
set vsep [expr {$vsep/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
set bw [expr {$bx1-$bx0}]
set dy [expr {$fy1 - $by0 + $vsep}]
.c move $bt 0 $dy
set biny $dy
set bexy [expr {$dy+$bexy}]
set by0 [expr {$dy+$by0}]
set by1 [expr {$dy+$by1}]
if {$fw>$bw} {
if {$fexx<$fw && $fexx>=$bw} {
set dx [expr {($fexx-$bw)/2}]
.c move $bt $dx 0
set bexx [expr {$dx+$bexx}]
.c create line 0 $biny $dx $biny -width $LWIDTH -tags $bt