banterpixra
Check-in [59e11df421]
Login

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

Overview
Comment:0.1 release of Banterpixra migrated from svn
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 59e11df421d9e0e092a48ecdf7a86d9571b4c87b
User & Date: alaric 2012-04-09 21:04:18
Context
2012-04-09
21:05
Added the SVG documentation check-in: 028b679731 user: alaric tags: trunk
21:04
0.1 release of Banterpixra migrated from svn check-in: 59e11df421 user: alaric tags: trunk
21:02
initial empty check-in check-in: ccad260bfd user: alaric tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added LICENCE.txt.



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
Copyright (c) 2003-2006, Warhead.org.uk Ltd

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the names of Warhead.org.uk Ltd, Snell Systems, nor Kitten Technologies, nor the names of their contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Added README.wiki.















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
banterpixra is a tool to generate syntax diagrams from BNF-esque
grammars. It's written in Chicken Scheme, and outputs the diagrams
in SVG.

As it is distributed as a Chicken egg, just type `chicken-install`
within the source directory to install it, or (without a source directory)
type `chicken-install banterpixra` to install the latest stable release.

The grammar language is simple, as is the command-line syntax.

See
[the banterpixra grammar](http://www.kitten-technologies.co.uk/projects/banterpixra/banterpixra.ruleset)
for a sample of the grammar (that also happens to document the grammar
format itself, and the banterpixra command-line arguments).

The resulting diagram is:

<embed src="http://latest.www.kitten-technologies.co.uk/projects/banterpixra/banterpixra.svg" type="image/svg+xml"/>

The name "banterpixra" is [Lojban](http://www.lojban.org/) for "language artist", and is
pronounce something like "ban-tare-pihra". The name was chosen
due to my intention to use banterpixra to produce syntax diagrams
for the Lojban langauge. By Lojban contention, banterpixra should never be written with a capital first letter.

Added banterpixra.meta.















>
>
>
>
>
>
>
1
2
3
4
5
6
7
((egg "banterpixra.egg")
 (files "banterpixra.scm" "sexpr.ruleset" "LICENCE.txt" "lojban.ruleset" "README.txt" "VERSION.txt" "banterpixra.ruleset" "banterpixra.meta" "banterpixra.svg" "single.rule" "banterpixra.release-info" "banterpixra.setup")
 (license "BSD")
 (category graphics)
 (needs sxpath matchable)
 (author "Alaric Snell-Pym")
 (synopsis "A tool to generate syntax diagrams (in SVG format) from BNF-esque grammars"))

Added banterpixra.ruleset.

























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
(banterpixra-command-line
 . (seq
    "banterpixra" " "
    (optional (comment "Debug mode:\nAdds pink boxes\nshowing the layout" "-d"))
    (choice
     (seq "ruleset" " " "<" " " ruleset-filename)
     (seq "rule" " " "<" " " rule-filename))
    " " ">" " " output-svg-filename))

(ruleset
 . (one-or-more
    (seq "(" rule-label " " "." " " rule ")")))

(rule
 . (choice
    rule-label
    literal
    (seq "(elidable" " " literal ")")
    (seq "(optional" " " rule ")")
    (seq "(zero-or-more" " " rule ")")
    (seq "(one-or-more" " " rule ")")
    (seq "(seq" (one-or-more (seq " " rule)) ")")
    (seq "(choice" (one-or-more (seq " " rule)) ")")
    (seq "(optional-choice" (one-or-more (seq " " rule)) ")")
    (seq "(comment" " " literal " " rule ")")))

(rule-label
 . (choice
    (seq "|" (one-or-more "[^|]") "|")
    (seq label-start-character (zero-or-more label-subsequent-character))
    "+|-|..."))

(literal
 . (seq "\""
	 (zero-or-more
	  (choice
	   "[^\"]"
	   (seq
	    "\\" "[\"\\n]")))
	 "\""))

(label-start-character . "[a-zA-Z!$%&*/:<=>^?_]")

(label-subsequent-character . (choice label-start-character "[0-9+-.@]"))

Added banterpixra.scm.















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
;; Language diagram maker

;; Input: BNF-esque grammar description for a single rule, of the form:

;; "...literal string..."
;; (elidable "...literal string...")
;; (seq <rule>...)
;; (choice <rule>...)
;; <rule name>
;; (optional <rule>)
;; (zero-or-more <rule>)
;; (one-or-more <rule>)
;; (comment "...text..." <rule>)

;; Output: A diagram object.

;; All Diagram objects have properties x, y, width, and height, inheight, and outheight.

;; (x,y) is the coordinates of the top left hand corner, from a top-left
;; origin.

;; Width and height are the proportions of the bounding box.

;; THe diagram can be considered to have an input port on the left
;; hand side, inheight from the top, and an output port on the
;; of the right hand side, outheight from the top.

;; Diagram objects are either:

;; * Literal boxes, which fill their width and height, and have a
;; literal content string and an elidable flag

;; * Rule name boxes, which fill their width and height, and have a
;; rule name content symbol

;; * Optional boxes, which have an embedded diagram object.

;; * one-or-more boxes, which have an embedded diagram object.

;; * zero-or-more boxes, which have an embedded diagram object.

;; * sequence boxes, which have a list of embedded diagram objects, in order, and render them left to right with arrows.

;; * alternate boxes, which have a set of embedded diagram objects, and render them in a vertical stack with fanning arrows.

(use sxpath-lolevel)
(use matchable)
(use extras)
(use data-structures)
(use srfi-1)

;; GRAPHICS FORMATTING STUFF

(define *debug-mode* #f)

(define *text-cell-height* 20)
(define *text-cell-width* 10)

(define (text-width string)
  (+ *text-cell-width* (* *text-cell-width* (fold
	       (lambda (a b)
		 (max (string-length a) b))
	       0
	       (string-split string "\n" #t))))) ;; FIXME: Learn how to work out real text widths

(define (text-height string)
  (+ (/ *text-cell-height* 2) (* *text-cell-height* (length (string-split string "\n" #t)))))

(define *literal-width* 10)
(define *literal-height* 10)

(define *optional-dw* 20)
(define *optional-dh* 10)
(define *optional-dx* (/ *optional-dw* 2))
(define *optional-dy* *optional-dh*)

(define *one-or-more-dw* 40)
(define *one-or-more-dh* 10)
(define *one-or-more-dx* (/ *one-or-more-dw* 2))
(define *one-or-more-dy* 0)

(define *zero-or-more-dw* 40)
(define *zero-or-more-dh* 20)
(define *zero-or-more-dx* (/ *zero-or-more-dw* 2))
(define *zero-or-more-dy* (/ *zero-or-more-dh* 2))

(define *sequence-left-margin* 10)
(define *sequence-sep* 10)
(define *sequence-right-margin* 10)

(define *choice-left-margin* 20)
(define *choice-right-margin* 20)
(define *choice-top-margin* 0) ; *choice-sep* gets added to this before use
(define *choice-sep* 10)
(define *choice-bottom-margin* 10)
(define *choice-bypass-height* 10)

(define *comment-dw* 10)
(define *comment-dh* 10)
(define *comment-dx* (/ *comment-dw* 2))
(define *comment-dy* (/ *comment-dh* 2))
(define *comment-sep* 5)
(define *comment-margin* 0)

(define *label-width* 200)
(define *label-indent* (/ *label-width* 2))
(define *label-arrow* 10)
(define *label-sep* 10)

;; DIAGRAM OBJECTS

;; These record the structure of the diagram by allocating regions
;; of 2D space to bits of it.
;; inheight and outheight are the heights (relative to diagram-y) of the input and output
;; lines from that piece of diagram, to allow other bits of diagram to line up correctly.

(define-record diagram
  x y width height
  inheight outheight

  type
  content
  elidable?

  children)

;; Virtual accessors give (x,y) of the input and output points

(define (diagram-inx diagram)
  (diagram-x diagram))

(define (diagram-iny diagram)
  (+ (diagram-y diagram) (diagram-inheight diagram)))

(define (diagram-outx diagram)
  (+ (diagram-width diagram) (diagram-x diagram)))

(define (diagram-outy diagram)
  (+ (diagram-y diagram) (diagram-outheight diagram)))

(define (diagram->list diagram)
  (list 'x: (diagram-x diagram)
	'y: (diagram-y diagram)
	'width: (diagram-width diagram)
	'height: (diagram-height diagram)
	'inheight: (diagram-inheight diagram)
	'outheight: (diagram-outheight diagram)
	'type: (diagram-type diagram)
	'content: (diagram-content diagram)
	'elidable?: (diagram-elidable? diagram)
	'children: (map diagram->list (diagram-children diagram))))

;; Move a diagram (and its subdiagrams) by the indicated vector
(define (relocate-diagram diagram dx dy)
  (make-diagram
   (+ (diagram-x diagram) dx)
   (+ (diagram-y diagram) dy)
   (diagram-width diagram)
   (diagram-height diagram)
   (diagram-inheight diagram)
   (diagram-outheight diagram)
   (diagram-type diagram)
   (diagram-content diagram)
   (diagram-elidable? diagram)
   (map (lambda (subdiagram)
	  (relocate-diagram subdiagram dx dy))
	(diagram-children diagram))))

;; Type-specific constructors.
;; Each should construct its diagram with x and y equal to 0
;; as parent container constructors will relocate them into the correct
;; positions.

(define (make-terminal type content elidable?)
  (let ((width (+ *literal-width* (text-width content)))
	(height (+ *literal-height*  (text-height content))))
    (make-diagram 0 0 width height
		  (/ height 2) (/ height 2)
		  type
		  content
		  elidable?
		  '())))

(define (make-literal content elidable?)
  (make-terminal 'literal content elidable?))

(define (make-rule content)
  (make-terminal 'rule (symbol->string content) #f))

(define (make-single-embed type diagram dx dy dw dh)
  (let ((width (+ dw (diagram-width diagram)))
	(height (+ dh (diagram-height diagram))))
    (make-diagram
     0 0
     width height
     (+ dy (diagram-inheight diagram)) (+ dy (diagram-outheight diagram))
     type
     #f
     #f
     (list (relocate-diagram diagram dx dy)))))

(define (make-optional diagram)
  (make-single-embed 'optional diagram *optional-dx* *optional-dy* *optional-dw* *optional-dh*))

(define (make-one-or-more diagram)
  (make-single-embed 'one-or-more diagram *one-or-more-dx* *one-or-more-dy* *one-or-more-dw* *one-or-more-dh*))

(define (make-zero-or-more diagram)
  (make-single-embed 'zero-or-more diagram *zero-or-more-dx* *zero-or-more-dy* *zero-or-more-dw* *zero-or-more-dh*))

(define (make-comment text diagram)
  (let ((tw (text-width text))
	(th (text-height text)))
    (make-diagram
     0 0
     (+ *comment-dw* (max (diagram-width diagram) tw))
     (+ (diagram-height diagram) th *comment-dh* *comment-sep*)
     (+ (diagram-inheight diagram) *comment-dy*) (+ (diagram-outheight diagram) *comment-dy*)
     'comment
     text
     #f
     (list (relocate-diagram
	    diagram
	    (+ *comment-dx* ;; Horizontally center the subdiagram if needed
	       (if (> tw (diagram-width diagram))
		   (/ (- tw (diagram-width diagram)) 2)
		   0))
	    *comment-dy*)))))

(define (make-sequence diagrams)
  (cond
   ((= (length diagrams) 0) (error "Zero-length sequences make no sense!"))
   ((= (length diagrams) 1) (car diagrams))
   (else
    (let loop
	((x-offset *sequence-left-margin*)
	 (subdiagrams-so-far '())
	 (diagrams-to-do diagrams)
	 (min-y 0)
	 (max-y 0)
	 (inheight #f)
	 (last-outheight #f)
	 (vertical-alignment-offset 0))

      (if (null? diagrams-to-do)
	  (make-diagram
	   0 0
	   (+ *sequence-right-margin* x-offset) (- max-y min-y)
	   (- min-y) (- vertical-alignment-offset min-y)
	    'sequence
	    #f
	    #f
	    (reverse (map (cut relocate-diagram <> 0 (- min-y)) subdiagrams-so-far)))
	  (let* ((diagram (car diagrams-to-do))
		 (vertical-relocation (- vertical-alignment-offset (diagram-inheight diagram)))
		 (diagram*
		  (relocate-diagram diagram
				    (+ x-offset *sequence-sep*)
				    vertical-relocation)))

	    (loop (+ x-offset *sequence-sep* (diagram-width diagram))
		  (cons diagram* subdiagrams-so-far)
		  (cdr diagrams-to-do)
		  (min min-y vertical-relocation)
		  (max max-y (+ (diagram-height diagram) vertical-relocation))
		  (if inheight inheight (diagram-inheight diagram))
		  (diagram-outheight diagram)
		  (+ (diagram-outheight diagram) vertical-relocation))))))))

(define (make-choice diagrams elidable?)
  (cond
   ((= (length diagrams) 0) (error "Zero-length choices make no sense!"))
   ((= (length diagrams) 1) (car diagrams))
   (else
    (let loop
	((y-offset (if elidable? (+ *choice-top-margin* *choice-bypass-height*) *choice-top-margin*))
	 (subdiagrams-so-far '())
	 (diagrams-to-do diagrams)
	 (width 0))

      (if (null? diagrams-to-do)
	  (make-diagram
	   0 0
	   (+ *choice-left-margin* *choice-right-margin* width)
	   (+ *choice-bottom-margin* y-offset)
	   (/ (+ *choice-bottom-margin* y-offset) 2) (/ (+ *choice-bottom-margin* y-offset) 2)
	   'choice
	   #f
	   elidable?
	   (reverse subdiagrams-so-far))
	  (let* ((diagram (car diagrams-to-do))
		 (diagram*
		  (relocate-diagram diagram
				    *choice-left-margin*
				    (+ y-offset *choice-sep*))))

	    (loop (+ y-offset *choice-sep* (diagram-height diagram))
		  (cons diagram* subdiagrams-so-far)
		  (cdr diagrams-to-do)
		  (max width (diagram-width diagram)))))))))

;; Rule -> diagram
;; Dispatch on the syntax of rules to create the correct diagram object

(define (rule->diagram rule)
  (cond
   ((string? rule)
    (make-literal rule #f))
   ((symbol? rule)
    (make-rule rule))
   ((pair? rule)
    (cond
     ((eq? 'elidable (car rule))
      (if (not (null? (cddr rule))) (error "Too many arguments to (elidable \"literal\"): ~S" rule))
      (make-literal (cadr rule) #t))
     ((eq? 'optional (car rule))
      (if (not (null? (cddr rule))) (error "Too many arguments to (optional <rule>): ~S" rule))
      (make-optional (rule->diagram (cadr rule))))
     ((eq? 'zero-or-more (car rule))
      (if (not (null? (cddr rule))) (error "Too many arguments to (zero-or-more <rule>): ~S" rule))
      (make-zero-or-more (rule->diagram (cadr rule))))
     ((eq? 'one-or-more (car rule))
      (if (not (null? (cddr rule))) (error "Too many arguments to (one-or-more <rule>): ~S" rule))
      (make-one-or-more (rule->diagram (cadr rule))))
     ((eq? 'seq (car rule))
      (make-sequence (map rule->diagram (cdr rule))))
     ((eq? 'choice (car rule))
      (make-choice (map rule->diagram (cdr rule)) #f))
     ((eq? 'optional-choice (car rule))
      (make-choice (map rule->diagram (cdr rule)) #t))
     ((eq? 'comment (car rule))
      (if (not (= (length rule) 3)) (error "Wrong arguments to (comment \"text\" <rule>): ~S" rule))
      (make-comment (cadr rule) (rule->diagram (caddr rule))))
     (else (error "Invalid rule: " rule))))
   (else (error "Invalid rule: " rule))))

;; Rule simplifications:
;;   (s (optional (optional X))) -> (s (optional X))
;;   (s (optional (one-or-more X))) -> (s (zero-or-more X))
;;   (s (optional (choice ...))) -> (s (optional-choice ...))
;;   (s (seq X)) -> (s X)
;;   (s (choice X)) -> (s X)
;;   (s (optional-choice X)) -> (s X)
;;   (s (seq ..(seq X)..)) -> (s (seq ..X..))
;;   (s (choice ..(choice X)..)) -> (s (choice ..X..))
;;   (s (optional-choice ..(optional-choice X)..)) -> (s (optional-choice ..X..))
;;   (s (optional-choice ..(choice X)..)) -> (s (optional-choice ..X..))
;;   (s (choice ..(optional-choice X)..)) -> (s (optional-choice ..X..))
;;   (s (choice ..(optional X)..)) -> (s (optional-choice ..X..))
;;   (s (optional-choice ..(optional X)..)) -> (s (optional-choice ..X..))
;;   else (s X) -> X

(define (simplify-rule rule)
  (match rule
	 (('optional ('optional BODY))
	  (simplify-rule `(optional ,BODY)))
	 (('optional ('one-or-more BODY))
	  (simplify-rule `(zero-or-more ,BODY)))
	 (('zero-or-more ('optional BODY))
	  (simplify-rule `(zero-or-more ,BODY)))
	 (('one-or-more ('optional BODY))
	  (simplify-rule `(zero-or-more ,BODY)))
	 (('optional ('choice . BODY))
	  (simplify-rule `(optional-choice ,@BODY)))
	 (('seq . BODY)
	  (simplify-seq BODY))
	 (('choice . BODY)
	  (simplify-choice BODY #f))
	 (('optional-choice . BODY)
	  (simplify-choice BODY #t))
	 (else rule)))

(define (simplify-seq rules)
  (cond
   ((= (length rules) 1) ; (seq X) -> (simplify X)
    (simplify-rule (car rules)))
   (else
    (let loop ((rules-to-do rules) ; (seq ...(seq X)...) -> (seq (map simplify ...X...))
	       (result '()))
      (if (null? rules-to-do)
	  (cons 'seq (reverse result))
	  (let ((rule (simplify-rule (car rules-to-do))))
	    (if (pair? rule)
		(if (eq? (car rule) 'seq)
		    (loop (append (cdr rule) (cdr rules-to-do)) ; Nested seq
			  result)
		    (loop (cdr rules-to-do) ; Non-seq nested rule
			  (cons rule result)))
		(loop (cdr rules-to-do) ; Non-pair rule
		      (cons rule result)))))))))

(define (simplify-choice rules elidable?)
  (cond
   ((= (length rules) 1) ; (choice X) -> (simplify X)
    (if elidable?
	(simplify-rule (cons 'optional rules))
	(simplify-rule (car rules))))
   (else
    (let loop ((rules-to-do rules) ; (choice ...(choice X)...) -> (choice (map simplify ...X...))
	       (result '())
	       (final-elidable? elidable?))
      (if (null? rules-to-do)
	  (cons (if final-elidable? 'optional-choice 'choice) (reverse result))
	  (let ((rule (simplify-rule (car rules-to-do))))
	    (if (pair? rule)
		(cond
		 ((eq? (car rule) 'choice) ; Nested choice
		  (loop (append (cdr rule) (cdr rules-to-do))
			result final-elidable?))
		 ((eq? (car rule) 'optional) ; Nested optional
		  (loop (cons (cadr rule) (cdr rules-to-do))
			result #t)) ; Result is always elidable
		 ((eq? (car rule) 'optional-choice) ; Nested optional-choice
		  (loop (append (cdr rule) (cdr rules-to-do))
			result #t)) ; Result is always elidable
		 (else
		  (loop (cdr rules-to-do) ; Non-choice nested rule
			(cons rule result) final-elidable?)))
		(loop (cdr rules-to-do) ; Non-pair rule
		      (cons rule result) final-elidable?))))))))

;; diagram -> svg fragment rendering logic
;; This is done once the diagram tree has been constructed
;; So the final positions of things are known

(define (make-rect x y w h style)
  `(rect (@ (x ,(number->string x))
	    (y ,(number->string y))
	    (fill "none")
	    (stroke-width "1")
	    ,@style
	    (width ,(number->string w))
	    (height ,(number->string h)))))

(define (make-rounded-blob x y w h style)
  `(rect (@ (x ,(number->string x))
	    (y ,(number->string y))
	    (rx ,(number->string (/ h 2)))
	    (ry ,(number->string (/ h 2)))
	    (fill "none")
	    (stroke-width "1")
	    ,@style
	    (width ,(number->string w))
	    (height ,(number->string h)))))

;; (x,y) is midpoint of the text
(define (make-text x y* text style)
  (let ((all-lines (string-split text "\n" #t)))
    (let loop ((lines all-lines)
	       (y (- y* (- (/ *text-cell-height* 2)) (* *text-cell-height* (/ (length all-lines) 2)))) ;; Vertical centering (hacky)
	       (result '()))
      (if (null? lines)
	  (cons 'g result) ; Wrap result in an SVG group for neatness
	  (loop
	   (cdr lines)
	   (+ y *text-cell-height*)
	   (cons
	    `(text (@ (x ,(number->string x))
		      (y ,(number->string y))
		      (text-anchor "middle") ,@style) ,(car lines))
	    result))))))

;; coords is a list of things to turn into strings, and append with spaces between
(define (coords-to-path coords)
  (string-join (map ->string coords) " "))

(define (make-line . coords)
  `(path (@ (d ,(coords-to-path coords))
	    (stroke-width "1")
	    (stroke "black")
	    (fill "none"))))

;; (bx,by) is the base of the arrowhead
;; (dx,dy) is the vector from there to the tip of the arrowhead
;; w is the ratio of one half the width of the arrowhead to the length; eg, w = 0.5 for
;; a square bounding box, and smaller for a sharper arrow.
(define (make-arrow bx by dx dy w)
  (let* ((pdx (* w (- dy)))
	 (pdy (* w dx))
	 (qdx (* w dy))
	 (qdy (* w (- dx)))
	 (px (+ bx pdx))
	 (py (+ by pdy))
	 (qx (+ bx qdx))
	 (qy (+ by qdy))
	 (tx (+ bx dx))
	 (ty (+ by dy)))
  `(path (@ (d ,(coords-to-path
		 (list
		  'M tx ty
		  'L px py
		  'L qx qy
		  'z)))
	    (fill "black")))))

(define (make-group . paths)
  (list (cons 'g (append paths))))

(define (literal->svg* diagram)
  (make-group
   (make-rect (diagram-x diagram) (diagram-y diagram)
	      (diagram-width diagram) (diagram-height diagram)
	      (if (diagram-elidable? diagram)
		  '((stroke "grey") (stroke-dasharray "5,5"))
		  '((stroke "black"))))
   (make-text (+ (diagram-x diagram) (/ (diagram-width diagram) 2))
	      (+ (diagram-y diagram) (/ (diagram-height diagram) 2))
	      (diagram-content diagram) '((font-weight "bold") (font-family "monospace") (font-size "16")))))

(define (rule->svg* diagram)
  (make-group
   (make-rounded-blob (diagram-x diagram) (diagram-y diagram)
	      (diagram-width diagram) (diagram-height diagram)
	      '((stroke "black")))
   (make-text (+ (diagram-x diagram) (/ (diagram-width diagram) 2))
	      (+ (diagram-y diagram) (/ (diagram-height diagram) 2))
	      (diagram-content diagram) '((font-style "italic")))))

;; This function is highly useful in laying out graphics...
(define (mid a b) (/ (+ a b) 2))

;; Standard template for diagrams that embed a single diagram
;; with some kind of bordering
(define (single->svg* diagram extra-paths)
  (let* ((child (car (diagram-children diagram)))

	 ;; Calculate useful points around the border between the
	 ;; child and the parent.
	 ;; All these points are halfway between child and parent, in
	 ;; different places.

	 ; Midpoint of in connector line
	 (ix (mid (diagram-inx diagram) (diagram-inx child)))
	 (iy (mid (diagram-iny diagram) (diagram-iny child)))

	 ; Point to left of top left of child
	 (p1x ix)
	 (p1y (diagram-y child))

	 ; Point above top left of child
	 (p2x (diagram-x child))
	 (p2y (mid (diagram-y diagram) (diagram-y child)))

	 ; Point above top right of child
	 (p3x (diagram-outx child))
	 (p3y p2y)

	 ; Point to right of top right of child
	 (p4x (mid (diagram-outx child) (diagram-outx diagram)))
	 (p4y (diagram-y child))

	 ; Midpoint of out connector line
	 (ox p4x)
	 (oy (mid (diagram-outy child) (diagram-outy diagram)))

	 ; Point to right of bottom right of child
	 (p5x p4x)
	 (p5y (+ (diagram-y child) (diagram-height child)))

	 (bottom-y (+ (diagram-y diagram) (diagram-height diagram)))

	 ; Point below bottom right of child
	 (p6x p3x)
	 (p6y (mid p5y bottom-y))

	 ; Point below bottom left of child
	 (p7x (diagram-inx child))
	 (p7y p6y)

	 ; Point to left of bottom left of child
	 (p8x ix)
	 (p8y p5y))
    (make-group
     (list
      ; In connector line
      (make-line 'M (diagram-inx diagram) (diagram-iny diagram)
		 'L (diagram-inx child) (diagram-iny child))

      ; Out connector line
      (make-line 'M (diagram-outx child) (diagram-outy child)
		 'L (diagram-outx diagram) (diagram-outy diagram)))

      ; Type-specific paths
     (extra-paths ix iy p1x p1y p2x p2y p3x p3y p4x p4y ox oy p5x p5y p6x p6y p7x p7y p8x p8y)

     (diagram->svg* child))))

(define (optional->svg* diagram)
  (single->svg*
   diagram
   (lambda (ix iy p1x p1y p2x p2y p3x p3y p4x p4y ox oy p5x p5y p6x p6y p7x p7y p8x p8y)
     (list
      ; Bypass path
      (make-line 'M (diagram-inx diagram) (diagram-iny diagram)
		 'Q ix iy p1x p1y
		 'Q p1x p2y p2x p2y
		 'L p3x p3y
		 'Q p4x p3y p4x p4y
		 'Q ox oy (diagram-outx diagram) (diagram-outy diagram))

      ;; Bypass arrow
      (make-arrow (mid p2x p3x)
		  p3y
		  (+ (/ *optional-dy* 2))
		  0
		  0.5)))))

(define (one-or-more->svg* diagram)
  (let* ((child (car (diagram-children diagram))))
    (single->svg*
     diagram
     (lambda (ix iy p1x p1y p2x p2y p3x p3y p4x p4y ox oy p5x p5y p6x p6y p7x p7y p8x p8y)
       (list
	; Loopback path
	(make-line 'M (diagram-inx child) (diagram-iny child)
		   'Q ix iy p8x p8y
		   'Q p8x p7y p7x p7y
		   'L p6x p6y
		   'Q p5x p6y p5x p5y
		   'Q ox oy (diagram-outx child) (diagram-outy child))

	; Loopback arrow
	(make-arrow (mid p7x p6x)
		    p7y
		    (- (/ *zero-or-more-dy* 2))
		    0
		    0.5))))))


(define (zero-or-more->svg* diagram)
  (let* ((child (car (diagram-children diagram))))
    (single->svg*
     diagram
     (lambda (ix iy p1x p1y p2x p2y p3x p3y p4x p4y ox oy p5x p5y p6x p6y p7x p7y p8x p8y)
       (list
        ; Bypass path
	(make-line 'M (diagram-inx diagram) (diagram-iny diagram)
		   'Q ix iy p1x p1y
		   'Q p1x p2y p2x p2y
		   'L p3x p3y
		   'Q p4x p3y p4x p4y
		   'Q ox oy (diagram-outx diagram) (diagram-outy diagram))
        ; Bypass arrow
	(make-arrow (mid p2x p3x)
		    p3y
		    (+ (/ *optional-dy* 2))
		    0
		    0.5)
	; Loopback path
	(make-line 'M (diagram-inx child) (diagram-iny child)
		   'Q ix iy p8x p8y
		   'Q p8x p7y p7x p7y
		   'L p6x p6y
		   'Q p5x p6y p5x p5y
		   'Q ox oy (diagram-outx child) (diagram-outy child))

	; Loopback arrow
	(make-arrow (mid p7x p6x)
		    p7y
		    (- (/ *zero-or-more-dy* 2))
		    0
		    0.5))))))

(define (sequence->svg* diagram)
  (let loop ((lx (diagram-inx diagram))
	     (ly (diagram-iny diagram))
	     (children (diagram-children diagram))
	     (paths-so-far '()))
    (if (null? children)
	(make-group (cons
		     (make-line 'M lx ly
				'L (diagram-outx diagram) (diagram-outy diagram))
		     paths-so-far))
	(let* ((child (car children)))
	  (loop (diagram-outx child) (diagram-outy child)
		(cdr children)
		(append (cons (make-line
			       'M lx ly
			       'L (diagram-inx child) (diagram-iny child))
			      (diagram->svg* child))
			paths-so-far))))))

(define (choice->svg* diagram)
  (let loop ((children (diagram-children diagram))
	     (paths-so-far
	      (if (diagram-elidable? diagram)
		   (let* ((bypass-inx (+ (diagram-x diagram) *choice-left-margin*))
			  (bypass-iny (+ (diagram-y diagram) *choice-top-margin* (/ *choice-bypass-height* 2)))
			  (bypass-outx (+ (diagram-x diagram) (diagram-width diagram) (- *choice-right-margin*)))
			  (bypass-outy bypass-iny)
			  (midix (mid (diagram-inx diagram) bypass-inx))
			  (midiy (mid (diagram-iny diagram) bypass-iny))
			  (midox (mid bypass-outx (diagram-outx diagram)))
			  (midoy (mid bypass-outy (diagram-outy diagram))))
		     (make-group
   		      ; Bypass line
		      (make-line 'M (diagram-inx diagram) (diagram-iny diagram)
				 'Q midix (diagram-iny diagram) midix midiy
				 'Q midix bypass-iny bypass-inx bypass-iny
				 'L bypass-outx bypass-outy
				 'Q midox bypass-outy midox midoy
				 'Q midox (diagram-outy diagram) (diagram-outx diagram) (diagram-outy diagram))
		      (make-arrow (mid bypass-inx bypass-outx)
				  bypass-iny
				  (/ *choice-bypass-height* 2)
				  0
				  0.5)))
		  '())))
    (if (null? children)
	(make-group paths-so-far)
	(let* ((child (car children))
	       (midix (mid (diagram-inx diagram) (diagram-inx child)))
	       (midiy (mid (diagram-iny diagram) (diagram-iny child)))
	       (joinox (+ (diagram-x diagram) (diagram-width diagram) (- *choice-right-margin*)))
	       (joinoy (diagram-outy child))
	       (midox (mid joinox (diagram-outx diagram)))
	       (midoy (mid joinoy (diagram-outy diagram))))
	  (loop (cdr children)
		(append
		 (list
		  ; Input line
		  (make-line 'M (diagram-inx diagram) (diagram-iny diagram)
			     'Q midix (diagram-iny diagram) midix midiy
			     'Q midix (diagram-iny child) (diagram-inx child) (diagram-iny child))

		  ; Output line
		  (make-line 'M (diagram-outx child) (diagram-outy child)
			     'L joinox joinoy
			     'Q midox joinoy midox midoy
			     'Q midox (diagram-outy diagram) (diagram-outx diagram) (diagram-outy diagram)))
		 (diagram->svg* child)
		 paths-so-far))))))

(define (comment->svg* diagram)
  (single->svg*
   diagram
   (lambda (ix iy p1x p1y p2x p2y p3x p3y p4x p4y ox oy p5x p5y p6x p6y p7x p7y p8x p8y)
     (list
      ; Dotted box
      (make-rect (+ (diagram-x diagram) *comment-margin*) (+ (diagram-y diagram) *comment-margin*)
		 (- (diagram-width diagram)(* *comment-margin* 2) ) (- (diagram-height diagram) (* *comment-margin* 2))
		 '((stroke-dasharray "1,3") (stroke "black")))
      ; Text
      (make-text (+ (diagram-x diagram) (/ (diagram-width diagram) 2))
		 (+ p8y *comment-sep* (/ (text-height (diagram-content diagram)) 2))
		 (diagram-content diagram) '())))))

(define (diagram->svg* diagram)
  (append
   (list
    (if *debug-mode*
	(make-rect (diagram-x diagram) (diagram-y diagram)
		   (diagram-width diagram) (diagram-height diagram)
		   '((stroke "pink")))
	'()))
   (case (diagram-type diagram) ;; Eargh what was I THINKING? Too much C programming.
     ((literal) (literal->svg* diagram)) ;; Dude, seriously, just put a ->svg* closure in the diagram record...
     ((rule) (rule->svg* diagram))
     ((optional) (optional->svg* diagram))
     ((one-or-more) (one-or-more->svg* diagram))
     ((zero-or-more) (zero-or-more->svg* diagram))
     ((sequence) (sequence->svg* diagram))
     ((choice) (choice->svg* diagram))
     ((comment) (comment->svg* diagram))
     (else (error "Unknown diagram node type ~S in ~S" (diagram-type diagram) (diagram->list diagram))))))

;; Wrap an SVG fragment into a complete document

(define (wrap-svg width height paths)
  `(svg (@ (xmlns "http://www.w3.org/2000/svg")
	   (version "1.2")
	   (baseProfile "tiny")
	   (viewBox ,(sprintf "0 0 ~S ~S" width height)))
	,@paths))

;; Render a ruleset
(define (diagrams->svg diagrams)
  ;; diagrams is an alist mapping rule names to diagrams
  ;; We render them in a vertical stack, with labels to the left
  (let loop ((y-offset 0)
	     (diagrams-to-do diagrams)
	     (paths-so-far '())
	     (max-width 0))
    (if (null? diagrams-to-do)
	(wrap-svg (+ *label-width* max-width *label-arrow*) (- y-offset *label-sep*) paths-so-far)
	(let* ((diagram-pair (car diagrams-to-do))
	       (diagram-name (car diagram-pair))
	       (diagram (cdr diagram-pair)))
	  (loop
	   (+ y-offset (diagram-height diagram) *label-sep*)
	   (cdr diagrams-to-do)
	   (append
	    (make-group
	     (list
					; Label
	      (make-text *label-indent* (+ y-offset (/ (diagram-height diagram) 2))
			 (symbol->string diagram-name) '((font-style "italic")))
					; Input
	      (make-arrow
	       (- *label-width* *label-arrow*) (+ y-offset (diagram-iny diagram))
	       *label-arrow* 0 0.5)
					; Output
	      (make-arrow
	       (+ *label-width* (diagram-outx diagram)) (+ y-offset (diagram-outy diagram))
	       *label-arrow* 0 0.5))
	     (diagram->svg* (relocate-diagram diagram *label-width* y-offset)))
	    paths-so-far)
	   (max max-width (diagram-width diagram)))))))

;; Render a single rule
(define (diagram->svg diagram)
  (wrap-svg (diagram-width diagram) (diagram-height diagram) (diagram->svg* diagram)))

;; Test the rule simplifier

(define simplification-examples
  '((rule . rule)
    ("literal" . "literal")
    ((elidable "literal") . (elidable "literal"))
    ((optional "literal") . (optional "literal"))
    ((optional (optional "literal")) . (optional "literal"))
    ((optional (optional (optional "literal"))) . (optional "literal"))
    ((optional (one-or-more "literal")) . (zero-or-more "literal"))
    ((choice a (choice b c (choice (optional d))) (choice e)) . (optional-choice a b c d e))
))

(define (test-simplifier)
  (map (lambda (example)
	 (let* ((input (car example))
		(expected-output (cdr example))
		(output (simplify-rule input)))
	   (if (not (equal? output expected-output))
	       (error (printf "Simplifer failure: ~S -> ~S (wanted ~S)\n" input output expected-output)))))
       simplification-examples))

;; Rendering Driver

(define (render-single-rule rule)
  (let* ((simplified-rule (simplify-rule rule))
	 (diagram (rule->diagram simplified-rule))
	 (svg (diagram->svg diagram)))
    (map display (flatten (sxml:sxml->xml svg)))))

(define (render-ruleset rules)
  (let* ((diagrams
	  (map
	   (lambda (rule-binding)
	     (let ((rule-name (car rule-binding))
		   (rule-body (cdr rule-binding)))
	       (cons rule-name (rule->diagram (simplify-rule rule-body))))) rules))
	 (svg (diagrams->svg diagrams)))
    (map display (flatten (sxml:sxml->xml svg)))))

;; Command line interface

(define (main args)
  (match args
	 (("-d" . rest)
	  (set! *debug-mode* #t)
	  (main rest))
	 (("rule")
	  (render-single-rule (read)))
	 (("ruleset")
	  (render-ruleset (read-file)))
	 (else (fprintf (current-error-port)
			"Usage: banterpixra [-d] {rule|ruleset} < input > output.svg\n
\t-d: Enable debug mode (adds pink boxes showing the layout model)\n
\trule: Render a single rule\n
\truleset: Render a set of rules\n
\n
Rule syntax:\n
\t\"literal\"
\tsymbol
\t(elidable \"literal\")
\t(optional <rule>)
\t(one-or-more <rule>)
\t(zero-or-more <rule>)
\t(seq <rule> <rule>...)
\t(choice <rule> <rule>...)
\t(optional-choice <rule> <rule>...)
\n
banterpixra will do some basic simplifications of the input rule.\n
\n
Ruleset syntax:
\t(label . <rule>)...\n"))))

(main (command-line-arguments))

Added banterpixra.setup.









>
>
>
>
1
2
3
4
(compile banterpixra.scm)
(install-program 'banterpixra "banterpixra"
  '((version 0.1)
    (documentation "banterpixra.svg")))

Added lojban.ruleset.



















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
(bridi
 . (seq
    (elidable ".i")
    (optional
     (choice
      (seq modal "bo")
      (seq (optional JA)
	   (optional (seq
		      (one-or-more time-tense)
		      (one-or-more space-tense)))
	   "bo")))
    (zero-or-more (choice (seq tense "ku") place))
    (elidable "cu")
    (seq
     (choice
      (seq
       tense
       (optional "ki"))
      "nau"
      "ki"
      (comment "{ca'a} = actually happens\n{ka'e} = is possible\n{nu'o} = can, but hasn't\n{pu'i} = can, and has" "ca'a|ka'e|nu'o|pu'i"))
     (choice
      gismu
      lujvo
      |fi'uvla|
      tanru))
    (zero-or-more (choice (seq tense "ku") place))
    (elidable "vau")))

(tense
 . (seq
    (seq
     (one-or-more time-tense)
     (one-or-more space-tense))
    (optional (comment "Motion in the indicated direction" (seq "mo'i" space-direction)))
    (comment "{di'i} = regularly\n{na'o} = typically\n{ru'i} = continuously\n{ta'e} = habitually\n<N> {roi}[{nai}] = happens [not] N times\n<N> {re'u} = for the Nth time"
	     (optional-choice
	      (seq "di'i|na'o|ru'i|ta'e" (optional "nai"))
	      (seq number "roi")
	      (seq number "roinai")
	      (seq number "re'u")))
    (optional (comment
	       "{pu'o} = will start in future\n{co'a} = is starting\n{xa'o} = started early\n{ca'o} = is happening\n{de'a} = about to pause\n{di'a} = has just resumed\n{za'o} = should have stopped\n{co'u} = is stopping\n{ba'o} = has stopped\n{co'i} = whole event as a single point in time"
	       "pu'o|co'a|xa'o|ca'o|de'a|di'a|za'o|co'u|ba'o|co'i"))
    (optional (comment "Spatial repetition"
		       (seq "fe'e"
			    (choice
			     (seq "di'i|na'o|ru'i|ta'e" (optional "nai"))
			     (seq number "roi")
			     (seq number "roinai")
			     (seq number "re'u")))))))

(space-tense
 . (seq
    (optional space-direction)
    (optional (comment "{vi} = near\n{va} = medium\n{vu} = far" "vi|va|vu"))
    (optional (comment
	       "The area of space covered"
	       (seq
		(comment
		 "{ve'i} = small\n{ve'a} = medium\n{ve'u} = large\n{ve'e} = everywhere"
		 "ve'i|ve'a|ve'u|ve'e")
		(optional space-direction)
		(optional (comment
			   "{vi'i} = along a line\n{vi'a} = in an area\n{vi'u} = through a volume\n{vi'e} = through space and time"
			   "vi'i|vi'a|vi'u|vi'e")))))))

(space-direction
 . (comment
    "{be'a} (north) {du'a} (east) {ne'u} (south) {vu'a} (west)
{ca'u} (forward) {ti'a} (backward) {zu'a} (left) {ri'u} (right) {ga'u} (above) {ni'a} (below) {ru'u} (surrounding/orbiting)
{bu'u} (at same place as) {ne'a} (next to) {ne'i} (within) {pa'o} (transfixing/passing through) {re'i} (adjacent to) {te'e} (bordering)
{fa'a} (towards) {to'o} (away from) {ze'o} (outward from speaker)  {zo'i} (inward to speaker) {zo'a} (tangential to / passing)"
    "be'a|du'a|ne'u|vu'a|ca'u|ti'a|zu'a|ri'u|ga'u|ni'a|ru'u|bu'u|ne'a|ne'i|pa'o|re'i|te'e|fa'a|to'o|ze'o|zo'i|zo'a"))

(time-tense
 . (seq
    (comment "{pu} = past\n{ca} = present\n{ba} = future" (optional "pu|ca|ba"))
    (comment "{zi} = near\n{za} = medium\nn{zu} = far" (optional "zi|za|zu"))
    (optional (seq
	       (comment "{ze'i} = briefly\n{ze'a} = medium\n{ze'u} = prolonged\n{ze'e} = forever" "ze'i|ze'a|ze'u|ze'e")
	       (comment "{pu} = interval extends to past\n{ca} = interval surrounds point\n{ba} = interval extends to future"(optional "pu|ca|ba"))))))

 (place
  . (seq
     (optional-choice modal FA)
     (choice
      pro-sumti
      sumti)))

Added sexpr.ruleset.































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
(s-expression
 . (choice
    comment

    (comment "Expression is read in case-sensitive mode"
	     (seq "#cs" s-expression))
    (comment "Expression is read in case-insensitive mode"
	     (seq "#ci" s-expression))

    (comment "SRFI-10 constructor" (seq "#,(" symbol (zero-or-more expression) ")"))

    (comment "Improper list" (seq "(" (one-or-more s-expression) "." s-expression ")"))
    (comment "Proper list" (seq "(" (zero-or-more s-expression) ")"))
    (comment "Vector" (seq "#(" (zero-or-more s-expression) ")"))

    string

    shorthand

    keyword

    symbol

    (comment "Special EOF object" "#!eof")

    number
))

(comment
 . (choice
    (comment "Script marker comment"
	     (choice
	      (seq "#!/" (zero-or-more character) newline)
	      (seq "#!" whitespace (zero-or-more character) newline)))
    (comment "Multi-line block comment" (seq "#|" (zero-or-more character) "|#"))
    (comment "Expression comment" (seq "#;" s-expression))
    (comment "Traditional comment" (seq ";" (zero-or-more character) newline))

))

(string
 . (choice
    (comment "String" (seq "\"" (zero-or-more string-character) "\""))
    (comment "Multiline tagged string" (seq "#<<" symbol newline (zero-or-more character) symbol newline))
    (comment "Multiline tagged string\nAllows embedded #<expr> or #{<expr>}" (seq "#<#" symbol newline (zero-or-more character) symbol newline))))

(shorthand
 . (choice
    (comment "(cond-expand (<symbol> <s-expression>) (else))"
	     (seq "#+" symbol s-expression))
    (comment "(location <sexpr>)" (seq "#$" s-expression))
    (comment "(foreign-declare \"...\")"  (seq "#>" (zero-or-more string-character) "<#"))
    (comment "(quote <s-expression>" (seq "'" s-expression))
    (comment "(quasiquote <s-expression>" (seq "`" s-expression))
    (comment "(unquote <s-expression>" (seq "," s-expression))
    (comment "(unquote-splicing <s-expression>" (seq ",@" s-expression))
))

(keyword
 . (choice
    (seq ":" symbol)
    (seq "#:" symbol)
    (seq symbol ":")))

(symbol
 . (choice
    (seq "|" (one-or-more character) "|")
    (seq "#%" (one-or-more symbol-subsequent-character))
    (seq symbol-first-character (zero-or-more symbol-subsequent-character))
    "+"
    "-"
    "..."
    "#!optional"
    "#!rest"
    "#!key"))

(symbol-first-character . "[a-zA-Z!$%&*/:<=>^?_]")

(symbol-subsequent-character . (choice symbol-first-character "[0-9+-.@]"))

Added single.rule.



>
1
(seq "literal" symbol (optional optional-symbol))