1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators compiler.units
4 definitions.icons effects hashtables help.stylesheet help.topics
5 io io.styles kernel make math namespaces present prettyprint
6 prettyprint.stylesheet quotations see sequences
7 sequences.private sets sorting splitting strings urls vocabs
9 FROM: prettyprint.sections => with-pprint ;
12 PREDICATE: simple-element < array
13 [ t ] [ first word? not ] if-empty ;
20 : last-span? ( -- ? ) last-element get span eq? ;
21 : last-block? ( -- ? ) last-element get block eq? ;
22 : last-blank-line? ( -- ? ) last-element get blank-line eq? ;
29 : ($blank-line) ( -- )
30 nl nl blank-line last-element namespaces:set ;
33 last-block? [ nl ] when
34 span last-element namespaces:set
37 GENERIC: print-element ( element -- )
39 M: simple-element print-element [ print-element ] each ;
40 M: string print-element [ write ] ($span) ;
41 M: array print-element unclip execute( arg -- ) ;
42 M: word print-element { } swap execute( arg -- ) ;
43 M: effect print-element effect>string print-element ;
44 M: f print-element drop ;
46 : print-element* ( element style -- )
47 [ print-element ] with-style ;
49 : with-default-style ( quot -- )
50 default-style get swap with-nesting ; inline
52 : print-content ( element -- )
53 [ print-element ] with-default-style ;
55 : ($block) ( quot -- )
57 span last-element namespaces:set
59 block last-element namespaces:set ; inline
63 : $snippet ( children -- )
64 [ snippet-style get print-element* ] ($span) ;
66 : $emphasis ( children -- )
67 [ emphasis-style get print-element* ] ($span) ;
69 : $strong ( children -- )
70 [ strong-style get print-element* ] ($span) ;
72 : $url ( children -- )
73 [ ?second ] [ first ] bi [ or ] keep >url [
74 dup present href associate url-style get assoc-union
75 [ write-object ] with-style
79 drop nl last-element get [ nl ] when
80 blank-line last-element namespaces:set ;
83 : ($heading) ( children quot -- )
86 : $heading ( element -- )
87 [ heading-style get print-element* ] ($heading) ;
89 : $subheading ( element -- )
90 [ strong-style get print-element* ] ($heading) ;
92 : ($code-style) ( presentation -- hash )
93 presented associate code-style get assoc-union ;
95 : ($code) ( presentation quot -- )
98 [ ($code-style) ] dip with-nesting
101 : $code ( element -- )
102 unlines dup <input> [ write ] ($code) ;
104 : $syntax ( element -- ) "Syntax" $heading $code ;
106 : $description ( element -- )
107 "Word description" $heading print-element ;
109 : $class-description ( element -- )
110 "Class description" $heading print-element ;
112 : $error-description ( element -- )
113 "Error description" $heading print-element ;
115 : $var-description ( element -- )
116 "Variable description" $heading print-element ;
118 : $contract ( element -- )
119 "Generic word contract" $heading print-element ;
121 : $examples ( element -- )
122 "Examples" $heading print-element ;
124 : $example ( element -- )
125 unclip-last [ unlines ] dip over <input> [
126 [ print ] [ output-style get format ] bi*
129 : $unchecked-example ( element -- )
130 ! help-lint ignores these.
133 : $markup-example ( element -- )
134 first dup unparse " print-element" append 1array $code
137 : $warning ( element -- )
141 "Warning" $heading print-element
145 : $deprecated ( element -- )
147 deprecated-style get [
149 "This word is deprecated" $heading print-element
154 : $image ( element -- )
155 [ first write-image ] ($span) ;
157 : <$image> ( path -- element )
158 1array \ $image prefix ;
164 : write-link ( string object -- )
165 link-style get [ write-object ] with-style ;
167 : link-icon ( topic -- )
168 definition-icon 1array $image ;
170 : link-text ( topic -- )
171 [ article-name ] keep write-link ;
173 GENERIC: link-long-text ( topic -- )
175 M: topic link-long-text
176 [ article-title ] keep write-link ;
178 GENERIC: link-effect? ( word -- ? )
180 M: parsing-word link-effect? drop f ;
181 M: symbol link-effect? drop f ;
182 M: word link-effect? drop t ;
184 : $effect ( effect -- )
185 effect>string base-effect-style get format ;
187 M: word link-long-text
188 dup presented associate [
189 [ article-name link-style get format ]
192 bl stack-effect $effect
197 : >topic ( obj -- topic ) dup topic? [ >link ] unless ;
199 : topic-span ( topic quot -- ) [ >topic ] dip ($span) ; inline
201 ERROR: number-of-arguments found required ;
203 : check-first ( seq -- first )
204 dup length 1 = [ length 1 number-of-arguments ] unless
207 : check-first2 ( seq -- first second )
208 dup length 2 = [ length 2 number-of-arguments ] unless
213 : ($link) ( topic -- ) [ link-text ] topic-span ;
215 : $link ( element -- ) check-first ($link) ;
217 : ($long-link) ( topic -- ) [ link-long-text ] topic-span ;
219 : $long-link ( element -- ) check-first ($long-link) ;
221 : ($pretty-link) ( topic -- )
222 [ [ link-icon ] [ drop bl ] [ link-text ] tri ] topic-span ;
224 : $pretty-link ( element -- ) check-first ($pretty-link) ;
226 : ($long-pretty-link) ( topic -- )
227 [ [ link-icon ] [ drop bl ] [ link-long-text ] tri ] topic-span ;
229 : <$pretty-link> ( definition -- element )
230 1array \ $pretty-link prefix ;
232 : ($subsection) ( element quot -- )
234 subsection-style get [ call ] with-style
237 : $subsection* ( topic -- )
239 [ ($long-pretty-link) ] with-scope
242 : $subsections ( children -- )
243 [ $subsection* ] each ($blank-line) ;
245 : $subsection ( element -- )
246 check-first $subsection* ;
248 : ($vocab-link) ( text vocab -- )
249 >vocab-link write-link ;
251 : $vocab-subsection ( element -- )
253 check-first2 dup vocab-help
254 [ 2nip ($long-pretty-link) ]
255 [ [ >vocab-link link-icon bl ] [ ($vocab-link) ] bi ]
259 : $vocab-link ( element -- )
260 check-first [ vocab-name ] keep ($vocab-link) ;
262 : $vocabulary ( element -- )
263 check-first vocabulary>> [
264 "Vocabulary" $heading nl dup ($vocab-link)
267 : (textual-list) ( seq quot sep -- )
268 '[ _ print-element ] swap interleave ; inline
270 : textual-list ( seq quot -- )
271 ", " (textual-list) ; inline
273 : $links ( topics -- )
274 [ [ ($link) ] textual-list ] ($span) ;
276 : $vocab-links ( vocabs -- )
277 [ lookup-vocab ] map $links ;
279 : $breadcrumbs ( topics -- )
280 [ [ ($link) ] " » " (textual-list) ] ($span) ;
282 : $see-also ( topics -- )
283 "See also" $heading $links ;
286 :: update-related-words ( words -- affected-words )
287 words words [| affected word |
288 word "related" [ affected union words ] change-word-prop
291 :: clear-unrelated-words ( words affected-words -- )
292 affected-words words diff
293 [ "related" [ words diff ] change-word-prop ] each ;
295 : notify-related-words ( affected-words -- )
296 fast-set notify-definition-observers ;
300 : related-words ( seq -- )
301 dup update-related-words
302 [ clear-unrelated-words ] [ notify-related-words ] bi ;
304 : $related ( element -- )
305 check-first dup "related" word-prop remove
306 [ $see-also ] unless-empty ;
308 : ($grid) ( style content-style quot -- )
310 _ [ last-element off _ tabular-output ] with-style
313 : $list ( element -- )
314 list-style get list-content-style get [
317 bullet get write-cell
318 [ print-element ] with-cell
323 : $table ( element -- )
324 table-style get table-content-style get [
327 [ [ print-element ] with-cell ] each
333 ALIAS: $slot $snippet
335 : $slots ( children -- )
336 [ unclip \ $slot swap 2array prefix ] map $table ;
338 : a/an ( str -- str )
339 [ first ] [ length ] bi 1 =
340 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
342 GENERIC: ($instance) ( element -- )
344 M: word ($instance) dup name>> a/an write bl ($link) ;
346 M: string ($instance) write ;
348 M: array ($instance) print-element ;
350 M: f ($instance) ($link) ;
352 : $instance ( element -- ) first ($instance) ;
356 { 1 [ first ($instance) ] }
357 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
361 [ [ ($instance) ", " print-element ] each ]
362 [ "or " print-element ($instance) ]
367 : $maybe ( element -- )
370 : $quotation ( element -- )
372 { "a " { $link quotation } " with stack effect " }
373 print-element $snippet ;
375 : ($instances) ( element -- )
376 dup word? [ ($link) "s" print-element ] [ print-element ] if ;
378 : $sequence ( element -- )
379 { "a " { $link sequence } " of " } print-element
381 { 1 [ first ($instances) ] }
382 { 2 [ first2 [ ($instances) " or " print-element ] [ ($instances) ] bi* ] }
386 [ [ ($instances) ", " print-element ] each ]
387 [ "or " print-element ($instances) ]
392 : values-row ( seq -- seq )
393 unclip \ $snippet swap present 2array
394 swap dup first word? [ \ $instance prefix ] when 2array ;
396 : ($values) ( element -- )
397 [ [ "None" write ] ($block) ]
398 [ [ values-row ] map $table ] if-empty ;
400 : $inputs ( element -- )
401 "Inputs" $heading ($values) ;
403 : $outputs ( element -- )
404 "Outputs" $heading ($values) ;
406 : $values ( element -- )
407 "Inputs and outputs" $heading ($values) ;
409 : $side-effects ( element -- )
410 "Side effects" $heading "Modifies " print-element
411 [ $snippet ] textual-list ;
413 : $errors ( element -- )
414 "Errors" $heading print-element ;
416 : $notes ( element -- )
417 "Notes" $heading print-element ;
419 : ($see) ( word quot -- )
420 [ code-style get swap with-nesting ] ($block) ; inline
422 : $see ( element -- ) check-first [ see* ] ($see) ;
424 : $synopsis ( element -- ) check-first [ synopsis write ] ($see) ;
426 : $definition ( element -- )
427 "Definition" $heading $see ;
429 : $methods ( element -- )
430 check-first methods [
435 : $value ( object -- )
436 "Variable value" $heading
437 "Current value in global namespace:" print-element
438 check-first dup [ pprint-short ] ($code) ;
440 : $curious ( element -- )
441 "For the curious..." $heading print-element ;
443 : $references ( element -- )
444 "References" $heading
445 unclip print-element [ \ $link swap ] { } map>assoc $list ;
447 : $shuffle ( element -- )
449 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description ;
451 : $complex-shuffle ( element -- )
453 { "The data flow represented by this shuffle word can be more clearly expressed using " { $link "locals" } "." } $deprecated ;
455 : $low-level-note ( children -- )
457 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
459 : $values-x/y ( children -- )
460 drop { { "x" number } { "y" number } } $values ;
462 : $parsing-note ( children -- )
464 "This word should only be called from parsing words."
467 : $io-error ( children -- )
469 "Throws an error if the I/O operation fails." $errors ;
471 : $prettyprinting-note ( children -- )
473 "This word should only be called from inside the "
474 { $link with-pprint } " combinator."
477 : $content ( element -- )
478 first article-content print-content nl ;
480 GENERIC: elements* ( elt-type element -- )
482 M: simple-element elements*
483 [ elements* ] with each ;
485 M: object elements* 2drop ;
488 [ dup first \ $markup-example eq? [ 2drop ] [ [ elements* ] with each ] if ]
489 [ [ first eq? ] keep swap [ , ] [ drop ] if ] 2bi ;
491 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
493 : collect-elements ( element seq -- elements )
494 swap '[ [ _ elements* ] each ] { } make [ rest ] map concat ;
496 : <$link> ( topic -- element )
497 1array \ $link prefix ;
499 : <$snippet> ( str -- element )
500 1array \ $snippet prefix ;
502 : $definition-icons ( element -- )
505 [ [ <$link> ] [ definition-icon-path <$image> ] bi* swap ] assoc-map
506 { f { $strong "Definition class" } } prefix