-
Notifications
You must be signed in to change notification settings - Fork 0
/
ginfo.scm
1422 lines (1258 loc) · 52 KB
/
ginfo.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
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
;;;
;;; ginfo
;;;
;;; MIT License
;;; Copyright 2012 aharisu
;;; All rights reserved.
;;;
;;; Permission is hereby granted, free of charge, to any person obtaining a copy
;;; of this software and associated documentation files (the "Software"), to deal
;;; in the Software without restriction, including without limitation the rights
;;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;;; copies of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be included in all
;;; copies or substantial portions of the Software.
;;;
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;;; SOFTWARE.
;;;
;;;
;;; aharisu
;;;
(define-module ginfo
(use srfi-1) ;;filter
(use srfi-11) ;;let-values
(use srfi-13) ;;string util
(use util.list)
(use util.match)
(use file.util)
(use text.parse)
(use gauche.parameter)
#;(export <doc> <geninfo-warning> <convert-context>
<unit-top> <unit-proc> <unit-var> <unit-class>
unit-bottom-initializer-add! slot-update!
output geninfo)
(export-all)
)
(select-module ginfo)
(define ignore-geninfo-warning? (make-parameter #f))
(define (guarded-read :optional (port (current-input-port)))
(guard (exc [(or (<read-error> exc) (<error> exc)) (guarded-read)])
(read port)))
;-------***************-----------
;data structure
;-------***************-----------
;;解析中に遭遇した例外を表すコンディションタイプ
(define-condition-type <geninfo-warning> <message-condition> #f)
;;;;;
;;documentのunitを変換するためのトップクラス
(define-class <convert-context> ()
(
(port :init-keyword :port)
))
;;;;;
;;一つのドキュメントを表すクラス
(define-class <doc> ()
(
(units :init-keyword :units :init-value '())
(name :init-keyword :name)
(filepath :init-keyword :filepath :init-value "")
(extend :init-keyword :extend :init-value '())
))
(define (add-unit doc unit)
(when unit
(slot-set! doc 'units (cons unit (slot-ref doc 'units))))
unit)
(define (add-extend! doc module)
(slot-set! doc 'extend (cons module (slot-ref doc 'extend))))
(define (commit-doc doc)
(slot-set! doc 'units (reverse (slot-ref doc 'units)))
(slot-set! doc 'extend (reverse (slot-ref doc 'extend)))
doc)
(define (keyword-replace keyword-list keyword replacer
:optional add-value)
(guard (e [else (if (undefined? add-value)
keyword-list
(append keyword-list (list keyword add-value)))])
(let1 value (get-keyword keyword keyword-list)
(if replacer
(let loop ([l keyword-list]
[acc '()])
(if (eq? (car l) keyword)
(append acc
(list (car l) (replacer value))
(cddr l))
(loop (cddr l) (append acc
(list (car l) (cadr l))))))
keyword-list))))
(define (description-constract description)
(if (string-null? description)
'()
(map
escape-special-character
(string-split description "\n"))))
;;;;;
;;全てのドキュメントユニットの上位クラス
;; @slot name slot name
;; @slot type type name
(define-class <unit-top> ()
(
;; Do you need to have <unit-top>?
(cur-tag :init-value 'description)
(name :init-keyword :name :init-value #f)
(type :init-keyword :type :init-value #f)
(line :init-keyword :line :init-value 0)
(description :init-keyword :description :init-value '())
))
(define-method initialize ((c <unit-top>) initargs)
(let* ([initargs (keyword-replace initargs :name escape-special-character)]
[initargs (keyword-replace initargs :description description-constract)])
(next-method c initargs)))
(define (set-unit-name! unit name)
(slot-set! unit 'name (escape-special-character name)))
(define (set-unit-description! unit description)
(slot-set! unit 'description (description-constract description)))
(define (reverse-and-escape-character l)
(let loop ([t l]
[acc '()])
(if (null? t)
acc
(loop (cdr t) (cons (escape-special-character (car t))
acc)))))
(define-macro (define-special-initialize class type . body)
`(define-method initialize ((c ,class) initargs)
(let ([initargs (keyword-replace initargs :type #f ,type)])
,@body
(next-method c initargs))))
;;<unit-bottom>の各スロットから値をコピーする
;;これ以降unitを編集することはない
(define-method commit ((unit <unit-top>) original)
(slot-set! unit 'name (escape-special-character (slot-ref original 'name)))
(slot-set! unit 'type (escape-special-character (slot-ref original 'type)))
(slot-set! unit 'line (slot-ref original 'line))
(slot-set! unit 'description (reverse-and-escape-character (slot-ref original 'description)))
unit)
(define-method show ((unit <unit-top>))
(format #t "type: ~S\n" (slot-ref unit 'type))
(format #t "name: ~S\n" (slot-ref unit 'name))
(format #t "line: ~S\n" (slot-ref unit 'line))
(format #t "description: ~S\n" (slot-ref unit 'description)))
;;unit-bottomクラスのためのメタクラス
;;全てのユニットクラスの下位クラスとして扱えるようにする
(define-class <unit-bottom-meta> (<class>) ())
;;スロットが見つからなかった場合はハッシュテーブル内からget or setする
(define-method slot-missing ((class <unit-bottom-meta>) obj slot . value)
(if (null? value)
(cond ;get
[(hash-table-exists? (slot-ref obj '%slots) slot)
(hash-table-get (slot-ref obj '%slots) slot)]
[else (next-method)])
(begin ;set
(hash-table-put! (slot-ref obj '%slots) slot (car value))
(slot-set! obj 'initial-state? #f)
(undefined))))
;;;;;
;;スロットに対する更新関数
;;ユニットオブジェクトのスロットに対する更新はこの関数を利用する
(define (slot-update! obj slot f)
(cond
[(find (lambda (s) (eq? (car s) slot)) (class-slots (class-of obj)))
(slot-set! obj slot (f (slot-ref obj slot)))]
[else (hash-table-update! (slot-ref obj '%slots) slot f)
(slot-set! obj 'initial-state? #f)]))
;;全てのドキュメントユニットの下位クラス
;;存在しないスロットもハッシュテーブルとして持つことで疑似的に下位クラスのように扱う
;;method not support
(define-class <unit-bottom> (<unit-top>)
(
(%slots :init-value (make-hash-table))
(initial-state?)
(tag-first-line :init-value #t)
(tag-text-indent :init-value #f)
)
:metaclass <unit-bottom-meta>)
;;;;;
;;unit-bottom初期化用関数
;;unit-bottomの疑似スロットに対してmake時に初期化する必要があるものはこのタイミングで行う
(define unit-bottom-initializer '())
(define (unit-bottom-initializer-add! proc)
(set! unit-bottom-initializer (cons proc unit-bottom-initializer)))
(define-method initialize ((class <unit-bottom>) initargs)
(let1 ret (next-method)
(for-each
(lambda (proc) (proc ret initargs))
unit-bottom-initializer)
(slot-set! ret 'initial-state? #t)
ret))
(define (initial-state? unit)
(if (is-a? unit <unit-bottom>)
(not (or (slot-ref unit 'name)
(slot-ref unit 'type)
(not (zero? (length (slot-ref unit 'description))))
(not (slot-ref unit 'initial-state?))))
#f))
(define-method show ((unit <unit-bottom>))
(next-method)
(hash-table-for-each
(slot-ref unit '%slots)
(lambda (key value)
(print key ":" value))))
;;;;;
;;functionやmethodタイプ用のunit
(define-class <unit-proc> (<unit-top>)
(
(param :init-value '())
(return :init-value '())
))
(define-special-initialize <unit-proc> type-fn)
(define-method add-unit-param! ((unit <unit-bottom>)
name :key (acceptable '())
(description ""))
(slot-set! unit 'param
(append
(slot-ref unit 'param)
(cons
(list
(escape-special-character name)
(escape-special-character name)
(map escape-special-character acceptable)
(description-constract description))
'()))))
(define-method add-unit-param! ((unit <unit-proc>)
name :key (acceptable '())
(description ""))
(slot-set! unit 'param
(append
(slot-ref unit 'param)
(cons
(list
(escape-special-character name)
(map escape-special-character acceptable)
(description-constract description))
'()))))
(define (set-unit-return! unit description)
(slot-set! unit 'return (description-constract description)))
(define (param-name param)
(car param))
(define (param-acceptable param)
(cadr param))
(define (param-description param)
(caddr param))
(define-method commit ((unit <unit-proc>) original)
(next-method)
;(slot-set! unit 'description (reverse (slot-ref original 'description)))
(slot-set! unit 'return (reverse-and-escape-character (slot-ref original 'return)))
;;この時点で(hidden new (accept1 accept2 ...) (text1 text2 ...))のリスト構造から
;;(new (accept1 accept2 ...) (text1 text2 ...))のリスト構造に修正する
;;hiddenとnewは自動解析結果の引数名を手動で修正するためにある
(slot-set! unit 'param (map
(lambda (p)(list (escape-special-character (cadr p))
(reverse-and-escape-character (caddr p))
(reverse-and-escape-character (cadddr p))))
(reverse (slot-ref original 'param))))
unit)
(unit-bottom-initializer-add!
(lambda (unit initargs)
(slot-set! unit 'param '())
(slot-set! unit 'return '())))
(define-method show ((unit <unit-proc>))
(next-method)
(format #t "param: ~S\n" (slot-ref unit 'param))
(format #t "return: ~S\n" (slot-ref unit 'return)))
;;;;;
;;var、constant、parameterタイプ用のunit
(define-class <unit-var> (<unit-top>) () )
(define-special-initialize <unit-var> type-var)
;;;;;
;;classタイプ用のunit
(define-class <unit-class> (<unit-top>)
(
(supers :init-value '())
(slots :init-value '())
)
)
(define-special-initialize <unit-class> type-class)
(define-method add-unit-slot! ((unit <unit-bottom>)
name :key (acceptable '())
(description ""))
(slot-set! unit 'slots
(append
(slot-ref unit 'slots)
(cons
(list
(escape-special-character name)
(escape-special-character name)
(map escape-special-character acceptable)
(description-constract description))
'()))))
(define-method add-unit-slot! ((unit <unit-class>)
name :key (acceptable '())
(description ""))
(slot-set! unit 'slots
(append
(slot-ref unit 'slots)
(cons
(list
(escape-special-character name)
(map escape-special-character acceptable)
(description-constract description))
'()))))
(define (set-unit-supers! unit supers)
(slot-set! unit 'supers (map escape-special-character supers)))
(define-method commit ((unit <unit-class>) original)
(next-method)
(slot-set! unit 'supers (reverse-and-escape-character (slot-ref original 'supers)))
(slot-set! unit 'slots (map
(lambda (s) (list (escape-special-character (car s))
(reverse-and-escape-character (caddr s))
(reverse-and-escape-character (cadddr s))))
(reverse (slot-ref original 'slots))))
unit)
(unit-bottom-initializer-add!
(lambda (unit initargs)
(slot-set! unit 'supers '())
(slot-set! unit 'slots '())))
(define-method show ((unit <unit-class>))
(next-method)
(format #t "supers: ~S\n" (slot-ref unit 'supers))
(format #t "slots: ~S\n" (slot-ref unit 'slots)))
(define-macro (or-equal? x . any)
`(or ,@(map (lambda (y) `(equal? ,x ,y)) any)))
;;unit-bottomからtypeにあったunitクラスに変換する
;;TODO この関数を外側からも拡張可能にする
(define (spcify-unit type unit)
(commit (cond
[(or-equal? type type-fn type-method type-macro) (make <unit-proc>)]
[(or-equal? type type-var type-const type-parameter) (make <unit-var>)]
[(or-equal? type type-class) (make <unit-class>)]
[else (raise (condition (<geninfo-warning> (message "unkown document unit type"))))]) ;TODO warning
unit))
;;unitのプロパティへのアクセサ関数群
(define (get-tag unit) (slot-ref unit 'cur-tag))
(define (set-tag tag unit) (slot-set! unit 'cur-tag tag))
(define (set-unit-name name config unit)
(if ((get-allow-multiple "name") config unit)
((get-init 'name) name config unit))
unit)
(define (set-unit-type type config unit)
(if ((get-allow-multiple "type") config unit)
((get-init 'type) type config unit))
unit)
(define (append-text text config unit)
((get-appender (get-tag unit)) text config unit)
unit)
(define (valid-unit? unit)
(and (slot-ref unit 'name) (slot-ref unit 'type)))
(define (get-invalid-unit-reason unit)
(string-append "invalid reason"
(if (slot-ref unit 'name) "" " [no specification of a name]")
(if (slot-ref unit 'type) "" " [no specification of a type]")))
(define (commit-unit config unit)
(cond
[(and (slot-ref unit 'type) (equal? (slot-ref unit 'type) type-cmd)) #f]
[(valid-unit? unit) (spcify-unit (slot-ref unit 'type) unit)]
[else (raise (condition
(<geninfo-warning> (message (get-invalid-unit-reason unit)))))]))
;-------***************-----------
;;parse document
;-------***************-----------
(define (x->writable-string x)
(if (and (pair? x) (not (list? x)))
(x->string (cell->list x)) ; dotted pair
(match x
[(? string? x) (string-append "\"" x "\"")]
;[(symbol? x) (string-append "'" (symbol->string x))]
[(? keyword? x) (string-append ":" (keyword->string x))]
[('quote x) (string-append "'" (x->writable-string x))]
[(? list? x) (string-append "("
(string-trim-right
(fold-right
(lambda (x acc) (string-append (x->writable-string x) " " acc))
""
x))
")")]
[_ (x->string x)])))
(define tags '())
(define-macro (define-tag name allow-multiple? init appender)
`(set! tags (acons (symbol->string (quote ,name))
(cons ,allow-multiple? (cons ,init ,appender))
tags)))
(define (get-allow-multiple tag)
(cond
[(assoc-ref tags (x->string tag)) => car]
[else #f]))
(define (get-init tag)
(cond
[(assoc-ref tags (x->string tag)) => cadr]
[else #f]))
(define (get-appender tag)
(cond
[(assoc-ref tags (x->string tag)) => cddr]
[else #f]))
(define (tag-allow-multiple-ret-true config unit) #t)
(define (tag-init first-line config unit) first-line)
(define (tag-append-text tag)
(lambda (text config unit)
(slot-update! unit tag (lambda (value) (cons text value)))))
(define (split-first-token line)
(let1 m (rxmatch #/^\s*\.(?!\.)(.*)$/ line)
(if m
;;param name is "."
(list "." "." (string-trim (m 1)))
(let ([port (open-input-string line)])
(let ([first (guard (e
[(<read-error> e)
(raise (condition
(<geninfo-warning> (message (format #f "name syntax error. [~s]" line)))))])
(read port))])
(if (eof-object? first)
#f
(append (match first
[(hidden '>> new) (list (x->writable-string hidden) (x->writable-string new))]
[sym (list (x->writable-string sym) (x->writable-string sym))])
(cons (string-trim (port->string port)) '()))))))))
;;define @description tag
(define-tag description
tag-allow-multiple-ret-true
tag-init
(tag-append-text 'description))
(define (process-acceptable-input text slot config unit)
(case (slot-ref unit 'state)
[(0)
(let1 text (string-trim text)
(if (string-null? text)
#f
(if (and (<= 2 (string-length text))
(string=? "{@" (substring text 0 2)))
(begin
(slot-set! unit 'state 1)
(process-acceptable-input (substring text 2 (string-length text)) slot config unit))
(begin
(slot-set! unit 'state 2)
(process-acceptable-input text slot config unit)))))]
[(1)
(let1 texts (string-split (string-trim-both text) #[\s])
(let loop ([texts texts])
(let* ([token (car texts)]
[found (string-scan token #\})])
(if found
;;finish
(let1 before (substring token 0 found)
(unless (string-null? before)
(slot-update! unit slot
(lambda (value)
(set-car! (cddr (car value))
(cons before (caddr (car value))))
value)))
(slot-set! unit 'state 2)
(let1 text (string-trim (string-scan text #\} 'after))
(process-acceptable-input (and (not (string-null? text)) text)
slot config unit)))
;;add acceptable
(let1 token (string-trim-both token)
(unless (string-null? token)
(slot-update! unit slot
(lambda (value)
(set-car! (cddr (car value))
(cons token (caddr (car value))))
value)))
(if (null? (cdr texts))
#f
(loop (cdr texts))))))))]
[(2) text]))
;;define @param tag
(define-tag param
tag-allow-multiple-ret-true
(lambda (first-line config unit)
(slot-set! unit 'state 0)
(cond
[(split-first-token first-line)
=> (lambda (tokens)
(slot-update! unit 'param
(lambda (v)
(cons
(list (car tokens) (cadr tokens) '() '())
v)))
(caddr tokens))]
[else (raise (condition (<geninfo-warning> (message "param name is required"))))]))
(lambda (text config unit)
(cond
[(process-acceptable-input text 'param config unit)
=> (lambda (text)
(slot-update! unit 'param
(lambda (value)
(set-car! (cdddr (car value)) (cons text (cadddr (car value))))
value)))])))
;;define @return tag
(define-tag return
(lambda (config unit) (null? (slot-ref unit 'return)))
tag-init
(tag-append-text 'return))
;;define @slot tag
(define-tag slot
tag-allow-multiple-ret-true
(lambda (first-line config unit)
(slot-set! unit 'state 0)
(cond
[(split-first-token first-line)
=> (lambda (tokens)
(slot-update! unit 'slots (lambda (v) (cons (list (car tokens) #f '() '()) v)))
(caddr tokens))]
[else (raise (condition (<geninfo-warning> (message "slot name is required"))))]))
(lambda (text config unit)
(cond
[(process-acceptable-input text 'slots config unit)
=> (lambda (text)
(slot-update! unit 'slots
(lambda (v)
(set-car! (cdddr (car v)) (cons text (cadddr (car v))))
v)))])))
;;define @supers tag
(define-tag supers
(lambda (config unit) (null? (slot-ref unit 'return)))
tag-init
(lambda (text config unit)
(slot-update! unit 'supers
(lambda (value)
(fold
(lambda (t acc)
(if (string-null? t)
acc
(cons t acc)))
value
(string-split text #[\s]))))))
;;define @name tag
(define-tag name
(lambda (config unit) (not (slot-ref unit 'name)))
(lambda (first-line config unit)
(let ([name (guard (e [(<read-error> e)
(raise (condition
(<geninfo-warning> (message #`"could not parse name [,first-line]"))))])
(read (open-input-string first-line)))])
(if (eof-object? name)
(raise (condition (<geninfo-warning> (message "@name tag required document unit name"))))
(slot-set! unit 'name (x->string name)))
""))
(lambda (text config unit)
(unless (string-null? text)
(slot-set! unit 'name text))))
;;define @type tag
(define-constant type-fn "Function")
(define-constant type-var "var")
(define-constant type-method "Method")
(define-constant type-macro "Macro")
(define-constant type-const "Constant")
;Parameterは自動解析無理なので指定したいのなら自分で書いてね
(define-constant type-parameter "Parameter")
(define-constant type-class "Class")
;;cmdをtypeに手動設定するとそのユニットはドキュメントに追加されない
;;@@で始まるタグのみのユニットなどに設定する
(define-constant type-cmd "cmd")
(define-constant allow-types
`(
,type-fn
,type-var
,type-method
,type-macro
,type-const
,type-parameter
,type-class
,type-cmd
))
(define-tag type
(lambda (config unit) (not (slot-ref unit 'type)))
(lambda (first-line config unit)
(let ([type (guard (e [(<read-error> e)
(raise (condition
(<geninfo-warning> (message #`"could not parse type name [,first-line]"))))])
(read (open-input-string first-line)))])
(if (eof-object? type)
(raise (condition (<geninfo-warning> (message "@type tag required type name"))))
(let ([type (x->string type)])
(if (find (lambda (x) (string=? type x)) allow-types)
(slot-set! unit 'type type))))
""))
(lambda (text config unit) (undefined)))
;;------------
;;@@で始まるタグはドキュメント解析動作にかかわるプロパティ設定を行うためのタグ
;;------------
;;define @@parse-relative tag
;;@@parse-relative #fに設定すると関連定義式の自動解析が行われない
;;default: #t
(define-tag @parse-relative
tag-allow-multiple-ret-true
(lambda (first-line config unit)
(let ([parse? (guard (e [(<read-error> e) #t])
(read (open-input-string first-line)))])
(if (or (eof-object? parse?) (not (boolean? parse?)))
(raise (condition (<geninfo-warning> (message "@parse-relative tag value selection is #t or #f"))))
(set-config config 'skip-relative (not parse?)))
""))
(lambda (text config unit) (undefined)))
;;define @@class-c->scm
;;@@class-c->scm stubファイル用.Cレベルのクラス名とGaucheレベルのクラス名を関連付ける
;;format: c-class-name scm-class-name
(define-tag @class-c->scm
tag-allow-multiple-ret-true
(lambda (first-line config unit)
(let ([c-scm (string-split first-line #[\s])])
(unless (eq? (length c-scm) 2)
(raise (condition (<geninfo-warning>
(message "@@class-c->scm tag format is ''c-class-name scm-class-name''.")))))
(put-class-c->scm config (car c-scm) (cadr c-scm)))
"")
(lambda (text config unit) (undefined)))
;;次の有効なドキュメントテキストを取得する
;;有効なドキュメントテキストがなければ#fを返す
(define (next-doc-text config)
(if (not (zero? (string-length (string-trim
(string-append
(next-token-of '(#\space #\tab))
(next-token-of '(#\;)))))))
(read-line-inc config)
#f))
;;ドキュメントタグと本文を分解する
;;example: "@return hoge piyo" -> {"return" "hoge piyo"}
(define (split-tag-and-text token)
(cond [(string-scan token #\space)
=> (lambda (index)
(values
(substring token 1 index)
(substring token (+ index 1) (string-length token))))]
[else (values
(substring token 1 (string-length token))
"")]))
;;次のタグかドキュメントの終わりまで本文のテキストをスキップする
(define (skip-current-tag config)
(let ([org-fp (port-seek (current-input-port) 0 SEEK_CUR)])
(unless (zero? (string-length (string-trim (next-token-of '(#\space #\tab #\;)))))
(let ([line (read-line-inc config)])
(if (zero? (string-length line))
(skip-current-tag config) ; read next line
(if (eq? #\@ (string-ref line 0))
;;return to origin point file pointer
(port-seek (current-input-port)
(- org-fp (port-seek (current-input-port) 0 SEEK_CUR))
SEEK_CUR)
(skip-current-tag config))))))) ; read next line
(define original-string-ref string-ref)
(define (string-ref str idx)
(if (<= (string-length str) idx)
#f
(original-string-ref str idx)))
;;テキスト内にタグがあれば処理を行う
(define (process-tag text config unit)
(if (eq? #\@ (string-ref (string-trim text) 0))
(let-values ([(tag text) (split-tag-and-text (string-trim text))])
(cond
[(get-allow-multiple tag)
=> (lambda (pred)
(if (pred config unit)
(begin
(slot-set! unit 'tag-first-line #t)
(slot-set! unit 'tag-text-indent #f)
(set-tag (string->symbol tag) unit)
((get-init tag) text config unit))
#f))]
[else (raise (condition
(<geninfo-warning> (message #`"unkwon tag name [,tag]."))))]))
text))
(define (escape-special-character text)
(regexp-replace-all #/"/ text "\\\\\""))
(define (trim-indent text unit)
(let1 indent (slot-ref unit 'tag-text-indent)
(if (and indent (string-prefix? indent text))
(substring (string-length indent) (string-length text))
text)))
;;テキストを現在のタグ内に追加する
(define (process-text text config unit)
(if (slot-ref unit 'tag-first-line)
(slot-set! unit 'tag-first-line #f)
(unless (slot-ref unit 'tag-text-indent)
(slot-set! unit 'tag-next-indent
(next-token-of '(#\space #\tab) (open-input-string text)))))
(append-text text config unit)
unit)
;;一つのドキュメントを最後までパースする
(define (parse-doc config unit)
(cond
[(next-doc-text config)
=> (lambda (text)
(parse-doc config (cond
[(process-tag text config unit)
=> (lambda (text) (process-text text config unit))]
;;TODO Warning
[else (skip-current-tag config) unit])))];skip tag text
[else unit]))
;;------***************-----------
;;analyze define expression
;-------***************-----------
(define (cell->list args)
(define (c->l args c)
(cond
[(pair? (cdr args))
(c->l (cdr args) (cons (car args) c))]
[(null? (cdr args))
(reverse (cons (car args) c))]
[else (reverse (append
(list (cdr args) (string->symbol ".") (car args))
c))]))
(cond
[(null? args) args]
[(symbol? args) (list '|.| args)]
[else (c->l args '())]))
;;仮引数部をマッチングしながら再帰的に解析する
(define (parse-each-arg args func-get-var config)
(let ([unit (make <unit-bottom>)]
[init (get-init 'param)])
(let loop ([args (cell->list args)])
(match args
[(:optional spec ...)
(init ":optional" config unit)
(loop spec)]
[(:key spec ...)
(init ":key" config unit)
(loop spec)]
[(:allow-other-keys spec ...)
(init ":allow-other-keys" config unit)
(loop spec)]
[(:rest var spec ...)
(init ":rest" config unit)
(init (symbol->string (func-get-var var)) config unit)
(loop spec)]
[(((keyword var) init-exp) spec ...)
(init
#`"((,(x->writable-string keyword) ,(x->writable-string (func-get-var var))) ,(x->writable-string init-exp))"
config unit)
(loop spec)]
[((var init-exp) spec ...)
(init #`"(,(symbol->string (func-get-var var)) ,(x->writable-string init-exp))" config unit)
(loop spec)]
[(var args ...)
(init (symbol->string (func-get-var var)) config unit)
(loop args)]
[() (slot-ref unit 'param)]))))
;;lambda式の仮引数部を解析する
;;さらに手書きparamのドキュメントとマージする
(define (analyze-args args func-get-var config unit)
(let ([org-param (slot-ref unit 'param)]
[gen-param (parse-each-arg args func-get-var config)])
(for-each
(lambda (p)
(cond
[(assoc (car p) gen-param)
=> (lambda (param) (set-cdr! param (cdr p)))]
[else (print "analyze-args warning. " (car p))])) ;TODO warging
org-param)
(slot-set! unit 'param gen-param)))
;;define, define-constantの解析を行う
(define (analyze-normal-define l config unit doc)
(let ([constant? (eq? (car l) 'define-constant)]
[type (if (eq? (car l) 'define-macro) type-macro type-fn)])
(match l
[(_ (symbol args ...) _ ...) ;; lambda -> function
(set-unit-name (symbol->string symbol) config unit)
(set-unit-type type config unit)
(analyze-args args identity config unit)]
[(_ symbol first-exp exp ...)
(if (pair? symbol)
(begin
(set-unit-name (symbol->string (car symbol)) config unit)
(set-unit-type type config unit)
(analyze-args (cdr symbol) identity config unit))
(begin
(set-unit-name (symbol->string symbol) config unit)
(match first-exp
[(or ('lambda (args ...) _ ...) ;; lambda -> function
('^ (args ...) _ ...))
(set-unit-type type config unit)
(analyze-args args identity config unit)]
[(or ('lambda arg _ ...) ;; lambda -> function
('^ arg _ ...))
(set-unit-type type config unit)
(analyze-args (list :rest arg) identity config unit)]
[_ (set-unit-type (cond
[(eq? type type-macro) type-macro]
[constant? type-const]
[else type-var])
config unit)])))];; other -> var or constant
[(_) #f])))
;;stub用 parseing for define-enum
(define (analyze-stub-define-enum l config unit doc)
(when (null? (cdr l))
#f) ;TODO warning
(set-unit-name (symbol->string (cadr l)) config unit)
(set-unit-type type-const config unit))
;;define-methodの解析を行う
;;TODO エラー処理
(define (analyze-method-define l config unit doc)
(if (null? (cdr l))
#f);TODO warning
(set-unit-name (symbol->string (cadr l)) config unit)
(set-unit-type type-method config unit)
(if (null? (caddr l))
#f);TODO warning
(analyze-args (caddr l) identity config unit))
;;parse define-syntax
(define (analyze-syntax-define l config unit doc)
(set-unit-name (symbol->string (cadr l)) config unit)
(set-unit-type type-macro config unit)
(when (eq? (car (caddr l)) 'syntax-rules)
(let* ([init (get-init 'param)]
[params-list (let loop ([section (cddr (caddr l))]
[c '()])
(if (null? section)
(reverse c)
(let1 u (make <unit-bottom>)
(for-each
(lambda (arg)
(init (x->string arg) config u))
(cell->list (cdaar section)))
(loop (cdr section)
(cons (slot-ref u 'param) c)))))])
(unless (null? params-list)
(slot-set! unit 'param (car params-list))
(for-each
(lambda (params)
((get-appender 'description)
(string-append
"other pattern: ("
(slot-ref unit 'name)
(let1 args (string-join
(reverse (map (.$ x->string cadr) params))
" ")
(if (string-null? args)
args
(string-append " " args)))
")")
config unit))
(cdr params-list))))))
(define (convert-type type)
(let ([conv '(
(<void> . "#<undefined>")
)])
(cond
[(assq-ref conv type #f) => identity]
[else (symbol->string type)])))
;;stub用 define-cprocの解析を行う
;;TODO エラー処理
(define (analyze-stub-proc-define l config unit doc)
(set-unit-name (symbol->string (cadr l)) config unit)
(set-unit-type type-fn config unit)
(analyze-args (caddr l)
(lambda (var)(string->symbol (car (string-split (symbol->string var) "::"))))
config unit)
(let ([ret (if (string=? (x->string (cadddr l)) ":")
(car (cddddr l))
(string->symbol (string-drop (x->string (cadddr l)) 1)))])
(slot-update! unit 'return
(lambda (v)
(append v (cons (if (list? ret)
(string-append (fold
(lambda (type acc)
(string-append acc
" "
(convert-type type)))
"(values"
ret)
")")
(convert-type ret))
'()))))))
;;クラスのslot定義部分を解析
;;さらに手書きのslotドキュメントとマージする
(define (analyze-slots slots unit)
(let ([org-slots (slot-ref unit 'slots)]
[gen-slots (map
(lambda (s)
(list (symbol->string (car s))
#f
'()
'()))
slots)])
(for-each
(lambda (s)