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