-
Notifications
You must be signed in to change notification settings - Fork 0
/
shendoc.shen
157 lines (131 loc) · 4.97 KB
/
shendoc.shen
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
\\ Copyright (c) 2020 Bruno Deferrari. All rights reserved.
\\ BSD 3-Clause License: http://opensource.org/licenses/BSD-3-Clause
\\: = Shen documentation generator
\\:
\\: Shendoc is a tool to generate documentation from special comments
\\: embedded in Shen source code files.
\\:
\\: == Documentation comment syntax
\\:
\\: TODO
\\:
\\: == Usage
\\:
\\: TODO
(package shendoc [void]
\\ Needed so that package-macro doesn't alter annotations produced
\\ from comments inside package declarations.
(systemf standalone)
(systemf associate)
\\ Import these from Shen's kernel
(defcc <whitespaces> shen.<whitespaces> := shen.<whitespaces>;)
(defcc <comment> shen.<comment> := shen.<comment>;)
(defcc <atom> shen.<atom> := shen.<atom>;)
(defcc <digit> shen.<digit> := shen.<digit>;)
(defcc <backslash> shen.<backslash> := shen.<backslash>;)
(defcc <lcurly> shen.<lcurly> := shen.<lcurly>;)
(defcc <rcurly> shen.<rcurly> := shen.<rcurly>;)
(defcc <lsb> shen.<lsb> := shen.<lsb>;)
(defcc <rsb> shen.<rsb> := shen.<rsb>;)
(defcc <lrb> shen.<lrb> := shen.<lrb>;)
(defcc <rrb> shen.<rrb> := shen.<rrb>;)
(defcc <colon> shen.<colon> := shen.<colon>;)
(defcc <shen-code> shen.<st_input> := shen.<st_input>;)
(defcc <doc-comment-line>
<backslash> <backslash> <colon> <space?> <line-remaining> := <line-remaining>;)
(defcc <doc-comment-block>
<doc-comment-line> <newline> <doc-comment-block> := [<doc-comment-line> | <doc-comment-block>];
<doc-comment-line> := [<doc-comment-line>];)
(defcc <doc-comment>
<doc-comment-block> := <doc-comment-block>;)
(defcc <line-remaining>
<non-newline> <line-remaining> := (@s <non-newline> <line-remaining>);
<e> := "";)
(defcc <non-newline>
C := (n->string C) where (not (or (= C 10) (= C 13)));)
(defcc <whitespace*>
<whitespaces> := skip;
<e> := skip;)
(defcc <space?>
<space> := skip;
<e> := skip;)
(defcc <space>
32 := skip;
9 := skip;)
(defcc <newline>
13 10 := skip; \\ CRLF
10 := skip;) \\ LF
(defcc <st_input-withdocs>
<doc-comment> <newline> <newline> <st_input-withdocs>
:= [[standalone | <doc-comment>] | <st_input-withdocs>];
<doc-comment> <newline> <st_input-withdocs>
:= [[associate | <doc-comment>] | <st_input-withdocs>];
<lsb> <st_input-withdocs1> <rsb> <st_input-withdocs2>
:= [(macroexpand (shen.cons_form <st_input-withdocs1>)) | <st_input-withdocs2>];
<lrb> <st_input-withdocs1> <rrb> <st_input-withdocs2>
:= (shen.package-macro (macroexpand <st_input-withdocs1>) <st_input-withdocs2>);
<comment> <st_input-withdocs>
:= <st_input-withdocs>;
<atom> <st_input-withdocs>
:= [(macroexpand <atom>) | <st_input-withdocs>];
<whitespaces> <st_input-withdocs>
:= <st_input-withdocs>;
<shen-code> := <shen-code>;
<e> := [];)
(defcc <st_input-withdocs1>
<st_input-withdocs> := <st_input-withdocs>;)
(defcc <st_input-withdocs2>
<st_input-withdocs> := <st_input-withdocs>;)
(define make-docs
[] -> []
[[standalone | Lines] | Rest]
-> [[standalone | Lines] | (make-docs Rest)]
[[associate | Lines] [define Name | DefRest] | Rest]
-> [[func Name (extract-type-signature DefRest) | Lines]
| (make-docs Rest)]
[[associate | Lines] [defcc Name | DefRest] | Rest]
-> [[func Name untyped | Lines]
| (make-docs Rest)]
[[associate | Lines] _ | Rest]
-> (make-docs [[standalone | Lines] | Rest])
[_ | Rest] -> (do _ (make-docs Rest)))
(define extract-type-signature
[{ | Rest] -> (extract-type-signature-h Rest)
_ -> untyped)
(define extract-type-signature-h
[} | Rest] -> []
[X | Rest] -> [X | (extract-type-signature-h Rest)])
(define type-signature-string
[T] -> (make-string "~R" T)
[T | Rest] -> (@s (make-string "~R" T) " " (type-signature-string Rest)))
(define render-doc
[[standalone | Lines] | Rest]
-> (do (for-each (/. Line (output "~A~%" Line)) Lines)
(nl)
(render-doc Rest))
[[func Name Type | Lines] | Rest]
-> (do (if (= Type untyped)
(output "==== `~A`~%~%" Name)
(output "==== `~A` : `~A`~%~%" Name (type-signature-string Type)))
(for-each (/. Line (output "~A~%" Line)) Lines)
(nl)
(render-doc Rest))
[] -> void)
(define for-each
F [] -> void
F [X | Rest] -> (do (F X) (for-each F Rest)))
(define without-macros
Expr -> (let Macros (value *macros*)
_ (set *macros* [])
Result (thaw Expr)
_ (set *macros* Macros)
Result))
(define main
[Exe Input Output] -> (main [Exe Input]) \\ TODO
[Exe Input] -> (let Bytes (read-file-as-bytelist Input)
Parsed (without-macros (freeze (compile (function <st_input-withdocs>) Bytes)))
Docs (make-docs Parsed)
(render-doc Docs))
[Exe | Other] -> (print-usage Exe))
)
(shendoc.main (value *argv*))