]> gitweb.factorcode.org Git - factor.git/blob - basis/help/syntax/syntax.factor
help.syntax: start to make ARTICLE: work.
[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 : push-help-space ( accum sbuf -- accum sbuf )
25     dup empty? [
26         over empty? not pick ?last \ $nl eq? not and
27     ] [
28         dup last CHAR: \s eq? not
29     ] if [ CHAR: \s suffix! ] when ;
30
31 :: parse-help-text ( end -- seq )
32     V{ } clone SBUF" " clone [
33         lexer get line>> :> m
34         end parse-help-token :> obj
35         lexer get line>> :> n
36
37         obj string? n m - 1 > and [
38             { [ dup empty? not ] [ over ?last string? ] } 0||
39             [ \ $nl push-help-text ] when
40         ] when
41
42         obj [
43             [
44                 dup string? [
45                     dup ?first " .,;:" member? [
46                         [ push-help-space ] dip
47                     ] unless append!
48                 ] [
49                     [ push-help-space ]
50                     [ push-help-text ] bi*
51                 ] if
52             ] when*
53         ] keep
54     ] loop [ >string suffix! ] unless-empty >array ; inline
55
56 : parse-help-values ( -- seq )
57     [ scan-token dup "}" = not ] [
58         dup "{" = [
59             drop \ } parse-until >array
60         ] [
61             ":" ?tail drop scan-object 2array
62         ] if
63     ] produce nip ;
64
65 : example-lines ( seq -- seq' )
66     dup string? [
67         string-lines [ [ blank? ] trim ] map harvest
68         dup length 1 - over [ unescape-string ] change-nth
69     ] when ;
70
71 : make-example ( seq -- seq )
72     dup string? [
73         example-lines dup length 1 > [ \ $example prefix ] when
74     ] when ;
75
76 : parse-help-examples ( -- seq )
77     \ } parse-until [ make-example ] { } map-as ;
78
79 : parse-help-example ( -- seq )
80     \ } parse-until dup { [ length 1 = ] [ first string? ] } 1&&
81     [ first example-lines ] [ >array ] if ;
82
83 : help-text? ( word -- ? )
84     {
85         $description $snippet $emphasis $strong $url $heading
86         $subheading $syntax $class-description
87         $error-description $var-description $contract $notes
88         $curious $deprecated $errors $side-effects $content
89         $slot $image
90     } member-eq? ;
91
92 : help-values? ( word -- ? )
93     { $values $inputs $outputs } member-eq? ;
94
95 : help-examples? ( word -- ? )
96     { $examples } member-eq? ;
97
98 : help-example? ( word -- ? )
99     { $example $unchecked-example $code } member-eq? ;
100
101 PRIVATE>
102
103 SYNTAX: HELP{
104     scan-object dup \ } eq? [ drop { } ] [
105         {
106             { [ dup help-text? ] [ \ } parse-help-text ] }
107             { [ dup help-values? ] [ parse-help-values ] }
108             { [ dup help-example? ] [ parse-help-example ] }
109             { [ dup help-examples? ] [ parse-help-examples ] }
110             [ \ } parse-until >array ]
111         } cond swap prefix
112     ] if suffix! ;
113
114 SYNTAX: HELP:
115     H{ { "{" POSTPONE: HELP{ } } [
116         scan-word bootstrap-word
117         [ >link save-location ]
118         [ [ parse-array-def ] dip set-word-help ]
119         bi
120     ] with-words ;
121
122 SYNTAX: ARTICLE:
123     location [
124         scan-object scan-object
125         \ ; parse-help-text <article>
126         over add-article >link
127     ] dip remember-definition ;
128
129 SYNTAX: ABOUT:
130     current-vocab scan-object >>help changed-definition ;