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