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