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