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