]> gitweb.factorcode.org Git - factor.git/blob - basis/help/syntax/syntax.factor
help.syntax: some fixes for $examples blocks with text.
[factor.git] / basis / help / syntax / syntax.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii combinators
4 combinators.short-circuit compiler.units definitions help
5 help.markup help.topics kernel lexer math namespaces parser
6 sequences splitting strings strings.parser vocabs.parser words ;
7 IN: help.syntax
8
9 <PRIVATE
10
11 :: parse-help-token ( end -- str/obj/f )
12     ?scan-token dup search {
13         { [ dup end eq? ] [ 2drop f ] }
14         { [ dup parsing-word? ] [
15             nip V{ } clone swap execute-parsing first
16             dup wrapper? [ wrapped>> \ $link swap 2array ] when ] }
17         [ drop ]
18     } cond ;
19
20 : push-help-text ( accum sbuf obj -- accum sbuf' )
21     [ dup empty? [ >string suffix! SBUF" " clone ] unless ]
22     [ [ suffix! ] curry dip ] bi* ;
23
24 DEFER: help-block?
25
26 : push-help-space ( accum sbuf -- accum sbuf )
27     dup empty? [
28         over empty? not
29         pick ?last dup array? [ ?first ] when
30         help-block? not and
31     ] [
32         dup last CHAR: \s eq? not
33     ] if [ CHAR: \s suffix! ] when ;
34
35 :: parse-help-text ( end -- seq )
36     V{ } clone SBUF" " clone [
37         lexer get line>> :> m
38         end parse-help-token :> obj
39         lexer get line>> :> n
40
41         obj string? n m - 1 > and [
42             { [ dup empty? not ] [ over ?last string? ] } 0||
43             [ \ $nl push-help-text ] when
44         ] when
45
46         obj [
47             [
48                 dup string? [
49                     dup ?first " .,;:" member? [
50                         [ push-help-space ] dip
51                     ] unless append!
52                 ] [
53                     [ push-help-space ]
54                     [ push-help-text ] bi*
55                 ] if
56             ] when*
57         ] keep
58     ] loop [ >string suffix! ] unless-empty >array ; inline
59
60 : parse-help-values ( -- seq )
61     [ scan-token dup "}" = not ] [
62         dup "{" = [
63             drop \ } parse-until >array
64         ] [
65             ":" ?tail drop scan-object 2array
66         ] if
67     ] produce nip ;
68
69 : code-lines ( seq -- seq' )
70     dup { [ string? ] [ [ blank? not ] member? ] } 1&& [
71         string-lines [ [ blank? ] trim ] map harvest
72         dup length 1 = [ first ] when
73     ] when ;
74
75 : make-example ( seq -- seq )
76     dup string? [
77         code-lines
78         dup { [ array? ] [ length 1 > ] } 1&& [
79             dup length 1 - over [ unescape-string ] change-nth
80             \ $example prefix
81         ] when
82     ] when ;
83
84 : parse-help-examples ( -- seq )
85     \ } parse-until [ make-example ] { } map-as ;
86
87 : parse-help-code ( -- seq )
88     \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
89     [ first code-lines ] [ >array ] if ;
90
91 : help-text? ( word -- ? )
92     {
93         $description $snippet $emphasis $strong $url $heading
94         $subheading $syntax $class-description
95         $error-description $var-description $contract $notes
96         $curious $deprecated $errors $side-effects $content
97         $slot $image $warning
98     } member-eq? ;
99
100 : help-code? ( word -- ? )
101     { $example $unchecked-example $code } member-eq? ;
102
103 : help-block? ( word -- ? )
104     {
105         $description $heading $subheading $syntax
106         $class-description $error-description $var-description
107         $contract $notes $curious $deprecated $errors
108         $side-effects $content $warning $subsections $nl
109     } member-eq? ;
110
111 : help-values? ( word -- ? )
112     { $values $inputs $outputs } member-eq? ;
113
114 : help-examples? ( word -- ? )
115     { $examples } member-eq? ;
116
117 PRIVATE>
118
119 SYNTAX: HELP{
120     scan-object dup \ } eq? [ drop { } ] [
121         {
122             { [ dup help-text? ] [ \ } parse-help-text ] }
123             { [ dup help-code? ] [ parse-help-code ] }
124             { [ dup help-values? ] [ parse-help-values ] }
125             { [ dup help-examples? ] [ parse-help-examples ] }
126             [ \ } parse-until >array ]
127         } cond swap prefix
128     ] if suffix! ;
129
130 SYNTAX: HELP:
131     H{ { "{" POSTPONE: HELP{ } } [
132         scan-word bootstrap-word
133         [ >link save-location ]
134         [ [ parse-array-def ] dip set-word-help ]
135         bi
136     ] with-words ;
137
138 SYNTAX: ARTICLE:
139     location [
140         scan-object scan-object
141         \ ; parse-help-text <article>
142         over add-article >link
143     ] dip remember-definition ;
144
145 SYNTAX: ABOUT:
146     current-vocab scan-object >>help changed-definition ;