1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays ascii combinators
5 combinators.short-circuit compiler.units definitions help
6 help.markup help.topics kernel lexer math math.order namespaces
7 parser sequences splitting strings strings.parser vocabs.parser
16 :: parse-help-token ( end -- str/obj/f literal? )
18 [ "{" = [ \ HELP{ ] [ f ] if ]
19 [ "syntax" lookup-word ]
20 [ { [ "$" head? ] [ "help.markup" lookup-word ] } 1&& ]
21 [ dup ?last ":{[(/\"" member-eq? [ search ] [ drop f ] if ]
23 { [ dup not ] [ drop f ] }
24 { [ dup end eq? ] [ 2drop f f ] }
25 { [ dup parsing-word? ] [
27 nip V{ } clone swap execute-parsing first
28 dup wrapper? [ wrapped>> \ $link swap 2array ] when
33 : push-help-text ( accum sbuf obj -- accum sbuf' )
34 [ dup empty? [ >string suffix! SBUF" " clone ] unless ]
35 [ [ suffix! ] curry dip ] bi* ;
38 : help-block? ( word -- ? )
40 $description $heading $subheading $syntax
41 $class-description $error-description $var-description
42 $contract $notes $curious $deprecated $errors
43 $side-effects $content $warning $subsections $nl
44 $list $table $example $unchecked-example $code
47 : ?push-help-space ( accum sbuf obj -- accum sbuf' obj )
52 [ dup array? [ ?first ] when help-block? not ]
56 over last " (" member? not
58 over string? [ over ?first " .,;:)" member? not and ] when
59 [ [ CHAR: \s suffix! ] dip ] when ;
61 :: parse-help-text ( end -- seq )
62 V{ } clone SBUF" " clone [
64 end parse-help-token :> ( obj literal? )
67 obj string? n m - 1 > and [
68 { [ dup empty? not ] [ over ?last string? ] } 0||
69 [ \ $nl push-help-text ] when
74 literal? [ ?push-help-space ] unless
75 dup string? not literal? or
76 [ push-help-text ] [ append! ] if
79 ] loop [ >string suffix! ] unless-empty >array ; inline
81 : parse-help-values ( -- seq )
82 [ scan-token dup "}" = not ] [
84 drop \ } parse-until >array
86 ":" ?tail drop scan-object 2array
90 : whitespace ( seq -- n )
91 [ [ blank? ] all? ] reject [ 0 ] [
92 [ [ blank? not ] find drop ] [ min ] map-reduce
95 : trim-whitespace ( seq -- seq' )
96 dup rest-slice dup whitespace
97 [ '[ _ index-or-length tail ] map! ] unless-zero drop
98 0 over [ [ blank? ] trim-head ] change-nth ;
100 : code-lines ( str -- seq )
101 split-lines trim-whitespace [ [ blank? ] all? ] trim ;
103 : make-example ( str -- seq )
104 code-lines dup { [ array? ] [ length 1 > ] } 1&& [
105 dup length 1 - over [ unescape-string ] change-nth
109 : parse-help-examples ( -- seq )
110 \ } parse-until dup [ string? ] all?
111 [ [ make-example ] { } map-as ] [ >array ] if ;
113 : parse-help-code ( -- seq )
114 \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
115 [ first code-lines ] [ >array ] if ;
117 : help-text? ( word -- ? )
119 $description $snippet $emphasis $strong $heading
120 $subheading $syntax $class-description
121 $error-description $var-description $contract $notes
122 $curious $deprecated $errors $side-effects $content
123 $slot $image $warning
126 : help-code? ( word -- ? )
127 { $example $unchecked-example $code } member-eq? ;
129 : help-values? ( word -- ? )
130 { $values $inputs $outputs } member-eq? ;
132 : help-examples? ( word -- ? )
133 { $examples } member-eq? ;
138 scan-object dup \ } eq? [ drop { } ] [
140 { [ dup help-text? ] [ \ } parse-help-text ] }
141 { [ dup help-code? ] [ parse-help-code ] }
142 { [ dup help-values? ] [ parse-help-values ] }
143 { [ dup help-examples? ] [ parse-help-examples ] }
144 [ \ } parse-until >array ]
149 H{ { "{" POSTPONE: HELP{ } } [
150 scan-word bootstrap-word
151 [ >link save-location ]
152 [ [ parse-array-def ] dip set-word-help ]
158 scan-object scan-object
159 \ ; parse-help-text <article>
160 over add-article >link
161 ] dip remember-definition ;
164 current-vocab scan-object >>help changed-definition ;