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