1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://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 )
18 [ "{" = [ \ HELP{ ] [ f ] if ]
19 [ "syntax" lookup-word ]
20 [ "help.markup" lookup-word ]
21 [ dup ?last ":{[(/\"" member-eq? [ search ] [ drop f ] if ]
23 { [ dup not ] [ drop ] }
24 { [ dup end eq? ] [ 2drop f ] }
25 { [ dup parsing-word? ] [
26 nip V{ } clone swap execute-parsing first
27 dup wrapper? [ wrapped>> \ $link swap 2array ] when ] }
31 : push-help-text ( accum sbuf obj -- accum sbuf' )
32 [ dup empty? [ >string suffix! SBUF" " clone ] unless ]
33 [ [ suffix! ] curry dip ] bi* ;
35 : help-block? ( word -- ? )
37 $description $heading $subheading $syntax
38 $class-description $error-description $var-description
39 $contract $notes $curious $deprecated $errors
40 $side-effects $content $warning $subsections $nl
41 $list $table $example $unchecked-example $code
44 : push-help-space ( accum sbuf -- accum sbuf )
47 pick ?last dup array? [ ?first ] when
50 dup last CHAR: \s eq? not
51 ] if [ CHAR: \s suffix! ] when ;
53 :: parse-help-text ( end -- seq )
54 V{ } clone SBUF" " clone [
56 end parse-help-token :> obj
59 obj string? n m - 1 > and [
60 { [ dup empty? not ] [ over ?last string? ] } 0||
61 [ \ $nl push-help-text ] when
67 dup ?first " .,;:" member? [
68 [ push-help-space ] dip
72 [ push-help-text ] bi*
76 ] loop [ >string suffix! ] unless-empty >array ; inline
78 : parse-help-values ( -- seq )
79 [ scan-token dup "}" = not ] [
81 drop \ } parse-until >array
83 ":" ?tail drop scan-object 2array
87 : whitespace ( seq -- n )
88 [ [ blank? ] all? ] reject [ 0 ] [
89 [ [ blank? not ] find drop ] [ min ] map-reduce
92 : trim-whitespace ( seq -- seq' )
93 dup rest-slice dup whitespace
94 [ '[ _ bound tail ] map! ] unless-zero drop
95 0 over [ [ blank? ] trim-head ] change-nth ;
97 : code-lines ( str -- seq )
98 split-lines trim-whitespace [ [ blank? ] all? ] trim ;
100 : make-example ( str -- seq )
101 code-lines dup { [ array? ] [ length 1 > ] } 1&& [
102 dup length 1 - over [ unescape-string ] change-nth
106 : parse-help-examples ( -- seq )
107 \ } parse-until dup [ string? ] all?
108 [ [ make-example ] { } map-as ] [ >array ] if ;
110 : parse-help-code ( -- seq )
111 \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
112 [ first code-lines ] [ >array ] if ;
114 : help-text? ( word -- ? )
116 $description $snippet $emphasis $strong $url $heading
117 $subheading $syntax $class-description
118 $error-description $var-description $contract $notes
119 $curious $deprecated $errors $side-effects $content
120 $slot $image $warning
123 : help-code? ( word -- ? )
124 { $example $unchecked-example $code } member-eq? ;
126 : help-values? ( word -- ? )
127 { $values $inputs $outputs } member-eq? ;
129 : help-examples? ( word -- ? )
130 { $examples } member-eq? ;
135 scan-object dup \ } eq? [ drop { } ] [
137 { [ dup help-text? ] [ \ } parse-help-text ] }
138 { [ dup help-code? ] [ parse-help-code ] }
139 { [ dup help-values? ] [ parse-help-values ] }
140 { [ dup help-examples? ] [ parse-help-examples ] }
141 [ \ } parse-until >array ]
146 H{ { "{" POSTPONE: HELP{ } } [
147 scan-word bootstrap-word
148 [ >link save-location ]
149 [ [ parse-array-def ] dip set-word-help ]
155 scan-object scan-object
156 \ ; parse-help-text <article>
157 over add-article >link
158 ] dip remember-definition ;
161 current-vocab scan-object >>help changed-definition ;