aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/documentation.lux
blob: 4768a19754a242cef7295839daac80c96fcb49d6 (plain)
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
(.module:
  [library
   [lux (#- Definition Module example type)
    ["." meta]
    ["." type ("#\." equivalence)]
    [abstract
     [monad (#+ do)]]
    [control
     ["." maybe ("#\." functor)]
     ["." exception (#+ exception:)]
     ["<>" parser
      ["<.>" code (#+ Parser)]]]
    [data
     ["." product]
     ["." text (#+ \n) ("#\." order)
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." monad mix monoid)]
      ["." set (#+ Set)]]
     [format
      ["md" markdown (#+ Markdown Block)]]]
    ["." macro
     [syntax (#+ syntax:)]
     ["." code]
     ["." template]]
    [math
     [number
      ["n" nat]]]
    [tool
     [compiler
      [language
       [lux
        ["." syntax]]]]]]])

(type: Fragment
  (#Comment Text)
  (#Code Code))

(def: fragment
  (Parser Fragment)
  (<>.or <code>.text
         <code>.any))

(def: (reference_column code)
  (-> Code Nat)
  (case code
    (^template [<tag>]
      [[[_ _ column] (<tag> _)]
       column])
    ([#.Bit]
     [#.Nat]
     [#.Int]
     [#.Rev]
     [#.Frac]
     [#.Text]
     [#.Identifier]
     [#.Tag])

    (^template [<tag>]
      [[[_ _ column] (<tag> members)]
       (|> members
           (list\map reference_column)
           (list\mix n.min column))])
    ([#.Form]
     [#.Tuple])

    [[_ _ column] (#.Record pairs)]
    (|> (list\compose (list\map (|>> product.left reference_column) pairs)
                      (list\map (|>> product.right reference_column) pairs))
        (list\mix n.min column))
    ))

(def: (padding reference_column [_ old_line old_column] [_ new_line new_column])
  (-> Nat Location Location Text)
  (if (n.= old_line new_line)
    (if (n.< old_column new_column)
      ""
      (text.together (list.repeated (n.- old_column new_column) " ")))
    (format (if (n.< old_line new_line)
              ""
              (text.together (list.repeated (n.- old_line new_line) \n)))
            (if (n.< reference_column new_column)
              ""
              (text.together (list.repeated (n.- reference_column new_column) " "))))))

(def: un_paired
  (All [a] (-> (List [a a]) (List a)))
  (let [melded (: (All [a] (-> [a a] (List a) (List a)))
                  (function (_ [left right] tail)
                    (list& left right tail)))]
    (|>> list.reversed
         (list\mix melded #.End))))

(def: (code_documentation old_location reference_column example)
  (-> Location Nat Code [Location Text])
  (case example
    (^template [<tag> <format>]
      [[new_location (<tag> value)]
       (let [documentation (`` (|> value (~~ (template.spliced <format>))))]
         [(revised@ #.column (n.+ (text.size documentation)) new_location)
          (format (padding reference_column old_location new_location)
                  documentation)])])
    ([#.Bit [%.bit]]
     [#.Nat [%.nat]]
     [#.Int [%.int]]
     [#.Rev [%.rev]]
     [#.Frac [%.frac]]
     [#.Text [%.text]]
     [#.Identifier [%.name]]
     [#.Tag [%.name (text.prefix syntax.sigil)]])

    (^template [|<| |>| <tag> <prep>]
      [[group_location (<tag> members)]
       (let [[group_location' members_documentation] (list\mix (function (_ part [last_location text_accum])
                                                                 (let [[member_location member_documentation] (code_documentation last_location reference_column part)]
                                                                   [member_location (format text_accum member_documentation)]))
                                                               [(revised@ #.column ++ group_location) ""]
                                                               (<prep> members))]
         [(revised@ #.column ++ group_location')
          (format (padding reference_column old_location group_location)
                  |<| members_documentation |>|)])])
    ([syntax.open_form   syntax.close_form   #.Form   |>]
     [syntax.open_tuple  syntax.close_tuple  #.Tuple  |>]
     [syntax.open_record syntax.close_record #.Record ..un_paired])
    ))

(def: blank_line
  Text
  (format \n \n))

(def: single_line_comment
  (-> Text Text)
  (text.prefix "... "))

(def: (fragment_documentation fragment)
  (-> Fragment Text)
  (case fragment
    (#Comment comment)
    (..single_line_comment comment)

    (#Code example)
    (let [reference_column (..reference_column example)
          [location _] example]
      (|> example
          (..code_documentation (with@ #.column reference_column location) reference_column)
          product.right))))

(def: type
  (-> Type Text)
  %.type)

(def: description
  (Parser (Maybe Code))
  (<>.or (<code>.text! "")
         <code>.any))

(exception: .public (unqualified_identifier {name Name})
  (exception.report
   ["Name" (%.name name)]))

(def: qualified_identifier
  (Parser Name)
  (do <>.monad
    [name <code>.identifier]
    (case name
      ["" _]
      (<>.failure (exception.error ..unqualified_identifier [name]))
      
      _
      (in name))))

(def: example_separator
  Code
  (let [c/01 "...."
        c/04 (format c/01 c/01 c/01 c/01)
        c/16 (format c/04 c/04 c/04 c/04)]
    (code.text (format blank_line
                       c/16 \n c/16
                       blank_line))))

(type: Example
  (List Fragment))

(def: example
  (Parser Example)
  (<code>.tuple (<>.many ..fragment)))

(def: example_documentation
  (-> Example Code)
  (|>> (list\map ..fragment_documentation)
       (list.interposed ..blank_line)
       (text.interposed "")
       code.text))

(syntax: (minimal_definition_documentation
          [name ..qualified_identifier])
  (with_expansions [<\n> (~! text.\n)]
    (macro.with_identifiers [g!type]
      (in (list (` ($_ ((~! md.then))
                       ... Name
                       (<| ((~! md.heading/3))
                           (~ (code.text (|> name product.right [""] %.name))))
                       ... Type
                       (let [(~ g!type) ("lux in-module"
                                         (~ (code.text (product.left name)))
                                         (.:of (~ (code.identifier name))))]
                         ((~! md.code)
                          (if ((~! type\=) .Type (~ g!type))
                            (|> (~ (code.identifier name))
                                (:as .Type)
                                ((~! type.anonymous))
                                ((~! ..type))
                                ((~! %.format)
                                 ((~! ..single_line_comment) ((~! ..type) (~ g!type)))
                                 <\n>))
                            ((~! ..type) (~ g!type))))))
                   ))))))

(syntax: (definition_documentation
          [name ..qualified_identifier
           description ..description
           examples (<>.some ..example)])
  (with_expansions [<\n> (~! text.\n)]
    (in (list (` ($_ ((~! md.then))
                     ((~! ..minimal_definition_documentation)
                      (~ (code.identifier name)))
                     ... Description
                     (~+ (case description
                           (#.Some description)
                           (list (` (<| ((~! md.paragraph))
                                        ((~! md.text))
                                        (~ description))))
                           
                           #.None
                           (list)))
                     ... Examples
                     (~+ (case examples
                           #.End
                           (list)
                           
                           _
                           (list (` (<| ((~! md.code))
                                        ((~! %.format)
                                         (~+ (|> examples
                                                 (list\map ..example_documentation)
                                                 (list.interposed ..example_separator))))))))))
                 )))))

(type: .public Definition
  {#definition Text
   #documentation (Markdown Block)})

(type: .public #rec Module
  {#module Text
   #description Text
   #expected (Set Text)
   #definitions (List Definition)})

(syntax: .public (default [name ..qualified_identifier])
  (let [[_ short] name]
    (in (list (` (: (.List ..Definition)
                    (list {#..definition (~ (code.text short))
                           #..documentation ((~! ..minimal_definition_documentation)
                                             (~ (code.identifier name)))})))))))

(syntax: .public (documentation: [name ..qualified_identifier
                                  extra (<>.some <code>.any)])
  (let [[_ short] name]
    (in (list (` (.def: .public (~ (code.local_identifier short))
                   (.List ..Definition)
                   (.list {#..definition (~ (code.text short))
                           #..documentation ((~! ..definition_documentation)
                                             (~ (code.identifier name))
                                             (~+ extra))})))))))

(def: definitions_documentation
  (-> (List Definition) (Markdown Block))
  (|>> (list.sorted (function (_ left right)
                      (text\< (value@ #definition right)
                              (value@ #definition left))))
       (list\map (value@ #documentation))
       (list\mix md.then md.empty)))

(def: expected_separator
  Text
  (text.of_char 31))

(def: expected_format
  (-> (List Text) Text)
  (list\mix (function (_ short aggregate)
              (case aggregate
                "" short
                _ (format aggregate ..expected_separator short)))
            ""))

(def: expected
  (-> Text (Set Text))
  (|>> (text.all_split_by ..expected_separator)
       (set.of_list text.hash)))

(def: (module' name description expected definitions)
  (-> Text Text Text (List Definition) Module)
  {#module name
   #description description
   #expected (..expected expected)
   #definitions definitions})

(syntax: .public (module [[name _] ..qualified_identifier
                          description <code>.any
                          definitions (<code>.tuple (<>.some <code>.any))
                          subs (<code>.tuple (<>.some <code>.any))])
  (do meta.monad
    [expected (meta.exports name)]
    (in (list (` (: (List Module)
                    (list& ((~! ..module')
                            (~ (code.text name))
                            (~ description)
                            (~ (code.text (|> expected
                                              (list\map product.left)
                                              ..expected_format)))
                            ((~! list.together) (list (~+ definitions))))
                           ($_ (\ (~! list.monoid) (~' compose))
                               (: (List Module)
                                  (\ (~! list.monoid) (~' identity)))
                               (~+ subs)))))))))

(def: listing
  (-> (List Text) (Markdown Block))
  (|>> (list.sorted text\<)
       (list\map (function (_ definition)
                   [(md.snippet definition)
                    #.None]))
       md.numbered_list))

(def: (module_documentation module)
  (-> Module (Markdown Block))
  (let [(^slots [#expected]) module]
    ($_ md.then
        ... Name
        (md.heading/1 (value@ #module module))
        ... Description
        (case (value@ #description module)
          "" md.empty
          description (<| md.paragraph
                          md.text
                          description))
        ... Definitions
        (md.heading/2 "Definitions")
        (|> module
            (value@ #definitions)
            (list.only (|>> (value@ #definition)
                            (set.member? expected)))
            ..definitions_documentation)
        ... Missing documentation
        (case (|> module
                  (value@ #definitions)
                  (list\mix (function (_ definition missing)
                              (set.lacks (value@ #definition definition) missing))
                            expected)
                  set.list)
          #.End
          md.empty

          missing
          ($_ md.then
              (md.heading/2 "Missing documentation")
              (..listing missing)))
        ... Un-expected documentation
        (case (|> module
                  (value@ #definitions)
                  (list.only (|>> (value@ #definition) (set.member? expected) not))
                  (list\map (value@ #definition)))
          #.End
          md.empty
          
          un_expected
          ($_ md.then
              (md.heading/2 "Un-expected documentation")
              (..listing un_expected)))
        )))

(def: .public documentation
  (-> (List Module) Text)
  (|>> (list.sorted (function (_ left right)
                      (text\< (value@ #module right) (value@ #module left))))
       (list\map ..module_documentation)
       (list.interposed md.horizontal_rule)
       (list\mix md.then (: (Markdown Block) md.empty))
       md.markdown))