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